Package org.wso2.carbon.registry.jcr

Source Code of org.wso2.carbon.registry.jcr.RegistryNode

/*
* Copyright (c) 2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.wso2.carbon.registry.jcr;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.registry.core.CollectionImpl;
import org.wso2.carbon.registry.core.Resource;
import org.wso2.carbon.registry.core.ResourceImpl;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.jcr.lock.RegistryLockManager;
import org.wso2.carbon.registry.jcr.nodetype.RegistryNodeDefinition;
import org.wso2.carbon.registry.jcr.nodetype.RegistryNodeType;

import javax.jcr.*;
import javax.jcr.lock.Lock;
import javax.jcr.lock.LockException;
import javax.jcr.nodetype.ConstraintViolationException;
import javax.jcr.nodetype.NoSuchNodeTypeException;
import javax.jcr.nodetype.NodeDefinition;
import javax.jcr.nodetype.NodeType;
import javax.jcr.version.ActivityViolationException;
import javax.jcr.version.Version;
import javax.jcr.version.VersionException;
import javax.jcr.version.VersionHistory;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Pattern;

public class RegistryNode implements Node {

    public String nodePath = "";
    public CollectionImpl collection;
    private RegistrySession registrySession;
    private RegistryProperty property;
    private RegistryItem item;

    public RegistryNodeType nodeType = null;
    private static Log log = LogFactory.getLog(RegistryNode.class);

    public RegistryNode(String s, RegistrySession registrySession) {

        this.nodePath = s;
        this.registrySession = registrySession;
        initColl(s);
        item = new RegistryItem(this);

    }


    public NodeType getNodetype() {  //mine

        return nodeType;
    }


    public void initColl(String np) {

        try {

            collection = (CollectionImpl) registrySession.getUserRegistry().newCollection();
            collection.setPath(np);

        } catch (RegistryException e) {
            e.printStackTrace();
        }

    }


    public void setCollection(String s) throws RegistryException { //mine //s-abs path:assume

        try {

            collection = (CollectionImpl) registrySession.getUserRegistry().newCollection();

            if ((s != null) && (s.equals("/"))) {

                collection.setDescription("nt:base");
                if (!registrySession.getUserRegistry().resourceExists("/")) {
                    registrySession.getUserRegistry().put("/", collection);
                }
            }

        } catch (RegistryException e) {
            String msg = "Exception occurred in registry collection creation " + this;
            log.debug(msg);
            throw new RegistryException(msg, e);
        }
    }


    public Node addNode(String s) throws ItemExistsException, PathNotFoundException, VersionException, ConstraintViolationException, LockException, RepositoryException {

        String absPath = "";
        String ntName = "";
        List<String> versionList = new ArrayList<String>();
        versionList.add("A");

        if ((nodePath != null) && (nodePath.equals("/"))) {

            absPath = nodePath + s;
        } else {

            absPath = nodePath + "/" + s;
        }

        RegistryNode subNode = new RegistryNode(absPath, registrySession);

        if ((!nodePath.equals("/")) && (((RegistryNode) this.getParent()).getNodetype() != null)) {
            ntName = ((RegistryNode) this.getParent()).getNodetype().getName();
        }

        CollectionImpl subCollection = null;

        try {
            subCollection = (CollectionImpl) registrySession.getUserRegistry().newCollection();
            subCollection.setDescription(ntName);
            subCollection.setProperty("jcr:uuid", absPath)//Here we use node's path as its identifier
            subCollection.setProperty("wso2.registry.jcr.versions", versionList);
            registrySession.getUserRegistry().put(absPath, subCollection);

        } catch (RegistryException e) {
            String msg = "failed to resolve the path of the given node " + this;
            log.debug(msg);
            throw new PathNotFoundException(msg, e);
        }


        if (!nodePath.equals("/")) {
            subNode.nodeType = (RegistryNodeType) ((RegistryNode) this.getParent()).getNodetype();
        }

        subNode.setPrimaryType(ntName);
        if (subNode.nodeType != null) {
            subNode.nodeType.setNode(subNode);

        }


        return subNode;
    }

    public Node addNode(String s, String s1) throws ItemExistsException, PathNotFoundException, NoSuchNodeTypeException, LockException, VersionException, ConstraintViolationException, RepositoryException {

        String absPath = "";
        String ntName = "";
        List<String> versionList = new ArrayList<String>();
        versionList.add("A");

        if ((nodePath != null) && (nodePath.equals("/"))) {

            absPath = nodePath + s;

        } else {

            absPath = nodePath + "/" + s;
        }

        RegistryNode subNode = new RegistryNode(absPath, registrySession);

        ntName = s1;

        CollectionImpl subCollection = null;

        try {

            subCollection = (CollectionImpl) registrySession.getUserRegistry().newCollection();
            subCollection.setDescription(ntName);
            subCollection.setProperty("jcr:uuid", absPath)//Here we use node's path as its identifier
            subCollection.setProperty("wso2.registry.jcr.versions", versionList);

            if (ntName.equals("mix:simpleVersionable")) {

                subCollection.setProperty("jcr:checkedOut", "true");
                subCollection.setProperty("jcr:isCheckedOut", "true");
            }

            if (ntName.startsWith("mix")) {

                addMixin(s1);
            }

            registrySession.getUserRegistry().put(absPath, subCollection);


        } catch (RegistryException e) {

            String msg = "failed to resolve the path of the given node " + this;
            log.debug(msg);
            throw new PathNotFoundException(msg, e);
        }


        subNode.setPrimaryType(s1);
        subNode.nodeType = (RegistryNodeType) (registrySession.getWorkspace().getNodeTypeManager().getNodeType(s1));

        if (subNode.nodeType != null) {

            subNode.nodeType.setNode(subNode);
        }

        return subNode;
    }

    public void orderBefore(String s, String s1) throws UnsupportedRepositoryOperationException, VersionException, ConstraintViolationException, ItemNotFoundException, LockException, RepositoryException {


    }

    public Property setProperty(String s, Value value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {

        if ((collection != null) && (value != null)) {

            if (value.getType() == 1) {
                Resource res = null;
                try {
                    res = registrySession.getUserRegistry().newResource();
                    res.setContent(value.getString());
                    res.setProperty("registry.jcr.property.type", "value_type");
                    registrySession.getUserRegistry().put(nodePath + "/" + s, res);

                } catch (RegistryException e) {
                    String msg = "failed to resolve the path of the given node " + this;
                    log.debug(msg);
                    throw new RepositoryException(msg, e);
                }

            }

            property = new RegistryProperty(this.collection, registrySession, s);

            property.setValue(value);

            item = new RegistryItem(property);

            return property;

        } else if (value == null) {

            try {

                CollectionImpl collec = (CollectionImpl) registrySession.getUserRegistry().get(nodePath);
                collec.removeProperty(s);
                registrySession.getUserRegistry().put(nodePath, collec);

            } catch (RegistryException e) {
                String msg = "failed to resolve the path of the given node or violation of repository syntax " + this;
                log.debug(msg);
                throw new RepositoryException(msg, e);
            }

            return null;
        } else {
            return null;
        }
    }

    public Property setProperty(String s, Value value, int i) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {

        return setProperty(s, value);
    }

    public Property setProperty(String s, Value[] values) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {

        List<String> properties = new ArrayList<String>();   //Here we can set the Values which has String   content only.

        if (values != null) {

            for (Value val : values) {
                if (val != null) {
                    properties.add(val.getString());
                }
            }
            Resource res = null;
            try {
                res = registrySession.getUserRegistry().newResource();
                res.setProperty(s, properties);
                res.setProperty("registry.jcr.property.type", "values_type");
                registrySession.getUserRegistry().put(nodePath + "/" + s, res);

            } catch (RegistryException e) {
                String msg = "failed to resolve the path of the given node or violation of repository syntax " + this;
                log.debug(msg);
                throw new RepositoryException(msg, e);
            }


            property = new RegistryProperty(this.collection, registrySession, s);
            property.setValue(values);
            item = new RegistryItem(property);


            return property;

        } else {
            return null;
        }
    }


    public Property setProperty(String s, Value[] values, int i) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {

        return setProperty(s, values);
    }

    public Property setProperty(String s, String[] strings) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {

        List<String> properties = new ArrayList<String>();
        CollectionImpl coll = null;
        try {
            if (strings != null) {
                for (String val : strings) {

                    if (val != null) {

                        properties.add(val);
                    }

                }

                coll = (CollectionImpl) registrySession.getUserRegistry().get(nodePath);
                coll.setProperty(s, properties);
                registrySession.getUserRegistry().put(nodePath, coll);
            } else {

                coll = (CollectionImpl) registrySession.getUserRegistry().get(nodePath);
                coll.removeProperty(s);
                registrySession.getUserRegistry().put(nodePath, coll);


            }

        } catch (RegistryException e) {
            String msg = "failed to resolve the path of the given node or violation of repository syntax " + this;
            log.debug(msg);
            throw new RepositoryException(msg, e);
        }

        property = new RegistryProperty(coll, registrySession, s);
        property.setValue(strings);

        item = new RegistryItem(property);

        return property;

    }

    public Property setProperty(String s, String[] strings, int i) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {

        return setProperty(s, strings);
    }

    public Property setProperty(String s, String s1) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {

        CollectionImpl collc = null;

        try {
            if (s1 != null) {
                collc = (CollectionImpl) registrySession.getUserRegistry().get(nodePath);
                List lis = new ArrayList();
                lis.add(s1);
                collc.setProperty(s, lis);
                registrySession.getUserRegistry().put(nodePath, collc);

            } else {

                collc = (CollectionImpl) registrySession.getUserRegistry().get(nodePath);
                collc.removeProperty(s);
                registrySession.getUserRegistry().put(nodePath, collc);

            }

        } catch (RegistryException e) {
            String msg = "failed to resolve the path of the given node or violation of repository syntax " + this;
            log.debug(msg);
            throw new RepositoryException(msg, e);
        }

        property = new RegistryProperty(collc, registrySession, s);
        if (property != null) {
            property.setValue(s1);

            item = new RegistryItem(property);

        }

        return property;

    }

    public Property setProperty(String s, String s1, int i) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
        return setProperty(s, s1);
    }

    public Property setProperty(String s, InputStream inputStream) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
        Resource res = null;
        try {
            res = registrySession.getUserRegistry().newResource();
            if (inputStream != null) {
                res.setContentStream(inputStream);
                res.setProperty("registry.jcr.property.type", "input_stream");
                registrySession.getUserRegistry().put(nodePath + "/" + s, res);
            }
        } catch (RegistryException e) {
            String msg = "failed to resolve the path of the given node or violation of repository syntax " + this;
            log.debug(msg);
            throw new RepositoryException(msg, e);
        }

        property = new RegistryProperty(this.collection, registrySession, s);
        property.setValue(inputStream);

        item = new RegistryItem(property);

        return property;
    }

    public Property setProperty(String s, Binary binary) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
        //still we can return a property.But we actually have only string to set

        property = new RegistryProperty(this.collection, registrySession, s);
        property.setValue(binary);

        item = new RegistryItem(property);

        return property;
    }

    public Property setProperty(String s, boolean b) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {

        Resource res = null;
        try {
            res = registrySession.getUserRegistry().newResource();
            res.setContent(String.valueOf(b));
            res.setProperty("registry.jcr.property.type", "boolean");
            registrySession.getUserRegistry().put(nodePath + "/" + s, res);

        } catch (RegistryException e) {
            String msg = "failed to resolve the path of the given node or violation of repository syntax " + this;
            log.debug(msg);
            throw new RepositoryException(msg, e);
        }


        property = new RegistryProperty(this.collection, registrySession, s);
        property.setValue(b);

        item = new RegistryItem(property);

        return property;
    }

    public Property setProperty(String s, double v) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {

        Resource res = null;
        try {
            res = registrySession.getUserRegistry().newResource();
            res.setContent(String.valueOf(v));
            res.setProperty("registry.jcr.property.type", "double");
            registrySession.getUserRegistry().put(nodePath + "/" + s, res);

        } catch (RegistryException e) {
            String msg = "failed to resolve the path of the given node or violation of repository syntax " + this;
            log.debug(msg);
            throw new RepositoryException(msg, e);
        }


        property = new RegistryProperty(this.collection, registrySession, s);
        property.setValue(v);

        item = new RegistryItem(property);

        return property;
    }

    public Property setProperty(String s, BigDecimal bigDecimal) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
        if (bigDecimal != null) {

            Resource res = null;
            try {
                res = registrySession.getUserRegistry().newResource();
                res.setContent(bigDecimal.toString());
                res.setProperty("registry.jcr.property.type", "big_decimal");
                registrySession.getUserRegistry().put(nodePath + "/" + s, res);

            } catch (RegistryException e) {
                String msg = "failed to resolve the path of the given node or violation of repository syntax " + this;
                log.debug(msg);
                throw new RepositoryException(msg, e);
            }


            property = new RegistryProperty(this.collection, registrySession, s);
            property.setValue(bigDecimal);

            item = new RegistryItem(property);
            return property;

        } else
            return null;
    }


    public Property setProperty(String s, long l) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {


        Resource res = null;
        try {
            res = registrySession.getUserRegistry().newResource();
            res.setContent(String.valueOf(l));
            res.setProperty("registry.jcr.property.type", "long");
            registrySession.getUserRegistry().put(nodePath + "/" + s, res);

        } catch (RegistryException e) {
            String msg = "failed to resolve the path of the given node or violation of repository syntax " + this;
            log.debug(msg);
            throw new RepositoryException(msg, e);
        }


        property = new RegistryProperty(this.collection, registrySession, s);
        property.setValue(l);

        item = new RegistryItem(property);

        return property;
    }


    public Property setProperty(String s, Calendar calendar) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {

        if (calendar != null) {

            Resource res = null;

            try {
                res = registrySession.getUserRegistry().newResource();
                res.setContent(String.valueOf(calendar.getTimeInMillis()));
                res.setProperty("registry.jcr.property.type", "calendar");

                registrySession.getUserRegistry().put(nodePath + "/" + s, res);

            } catch (RegistryException e) {
                String msg = "failed to resolve the path of the given node or violation of repository syntax " + this;
                log.debug(msg);
                throw new RepositoryException(msg, e);
            }


            property = new RegistryProperty(res, registrySession, s);
            property.setValue(calendar);


            item = new RegistryItem(property);
            return property;

        } else {
            return null;
        }
    }

    public Property setProperty(String s, Node node) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {

        if (node != null) {

            property = new RegistryProperty(this.collection, registrySession, s);
            property.setValue(node);

            item = new RegistryItem(property);
        }
        return property;

    }

    public Node getNode(String s) throws PathNotFoundException, RepositoryException {
        //s-rel path
        String abs = "";

        if (!(nodePath.endsWith("/")) && (!(s.startsWith("/")))) {

            abs = nodePath + "/" + s;

        } else {

            abs = nodePath + s;

        }

        RegistryNode subNode = null;

        try {


            if (registrySession.getUserRegistry().resourceExists(abs)) {

                subNode = new RegistryNode(abs, registrySession);
                subNode.collection = (CollectionImpl) registrySession.getUserRegistry().get(abs);

            } else {

                throw new PathNotFoundException();
            }
        } catch (RegistryException e) {
            String msg = "failed to resolve the path of the given node or violation of repository syntax " + this;
            log.debug(msg);
            throw new RepositoryException(msg, e);
        }

        return subNode;
    }

    public NodeIterator getNodes() throws RepositoryException {

        HashSet nodes = new HashSet();
        CollectionImpl tempCollection = null;
        try {

            if (registrySession.getUserRegistry().get(nodePath) instanceof CollectionImpl) {
                tempCollection = (CollectionImpl) registrySession.getUserRegistry().get(nodePath);

                String[] children = tempCollection.getChildren();

                for (int i = 0; i < children.length; i++) {

                    nodes.add(registrySession.getNode(children[i]));

                }
            }
        } catch (RegistryException e) {
            String msg = "failed to resolve the path of the given node or violation of repository syntax " + this;
            log.debug(msg);
            throw new RepositoryException(msg, e);
        }

        RegistryNodeIterator nodeIterator = new RegistryNodeIterator(nodes);


        return (NodeIterator) nodeIterator;
    }

    public NodeIterator getNodes(String s) throws RepositoryException {

        Set nodes = new HashSet();

        try {

            CollectionImpl coll = (CollectionImpl) registrySession.getUserRegistry().get(nodePath);
            String[] childpaths = coll.getChildren();

            for (int i = 0; i < childpaths.length; i++) {
                Node node = new RegistryNode(childpaths[i], registrySession);
                nodes.add(node);
            }

        } catch (RegistryException e) {
            String msg = "failed to resolve the path of the given node or violation of repository syntax " + this;
            log.debug(msg);
            throw new RepositoryException(msg, e);
        }
        return new RegistryNodeIterator((HashSet) nodes);
    }


    public NodeIterator getNodes(String[] strings) throws RepositoryException {


        return null;
    }

    public Property getProperty(String s) throws PathNotFoundException, RepositoryException {

        String absPath = nodePath + "/" + s;
        String[] tempArr;
        String tempPath = absPath;
        tempArr = tempPath.split("/");
        tempPath = tempPath.substring(0, (tempPath.length()) - (tempArr[tempArr.length - 1].length()) - 1);

        RegistryProperty regProp = null;
        String prop = "";
        List<String> propList = null;
        String propName = tempArr[tempArr.length - 1];
        ResourceImpl res = null;
        boolean resFlag = false;

        try {

            regProp = new RegistryProperty((CollectionImpl) registrySession.getUserRegistry().get(tempPath), registrySession, tempArr[tempArr.length - 1]);

            if ((registrySession.getUserRegistry().resourceExists(absPath)) && (registrySession.getUserRegistry().get(absPath) != null)) {
                res = (ResourceImpl) registrySession.getUserRegistry().get(absPath);
                resFlag = true;

            }

            if ((!resFlag) && (registrySession.getUserRegistry().resourceExists(tempPath)) && (registrySession.getUserRegistry().get(tempPath) != null)) {


                propList = registrySession.getUserRegistry().get(tempPath).getPropertyValues(propName);

                if ((propList != null) && (propList.size() == 1)) {

                    prop = propList.get(0);
                    regProp.setValue(prop);
                } else {

                    if (propList != null) {

                        String[] arr = new String[propList.size()];
                        int i = 0;

                        for (Object ob : propList) {

                            arr[i] = ob.toString();
                            i++;
                        }
                        regProp.setValue(arr);
                    }

                }


            } else if (res != null) {

                if ((res.getProperty("registry.jcr.property.type") != null) && (res.getProperty("registry.jcr.property.type").equals("input_stream"))) {
                    regProp.setValue(res.getContentStream());

                } else if ((res.getProperty("registry.jcr.property.type") != null) && (res.getProperty("registry.jcr.property.type").equals("values_type"))) {
                    List<String> valuesProp = res.getPropertyValues(propName);
                    if (valuesProp != null) {
                        int i = 0;
                        Value[] values = new RegistryValue[valuesProp.size()];

                        for (int j = 0; j < values.length; j++) {

                            values[j] = new RegistryValue(valuesProp.get(j));

                        }

                        regProp.setValue(values);
                    }
                } else if (res.getContent() instanceof String) {
                    prop = res.getContent().toString();

                } else if (res.getContent() instanceof byte[]) {
                    prop = new String((byte[]) res.getContent());

                } else if (prop.equals("")) {

                    throw new PathNotFoundException();
                }


                regProp = RegistrySession.getRegistryProperty(res.getProperty("registry.jcr.property.type"), prop, regProp);

                resFlag = false;


            }

        } catch (RegistryException e) {
            String msg = "failed to resolve the path of the given node or violation of repository syntax " + this;
            log.debug(msg);
            throw new RepositoryException(msg, e);
        }

        return regProp;
    }


    public PropertyIterator getProperties() throws RepositoryException {

        Set<String> propNamesList = new HashSet<String>();
        Set<Property> properties = new HashSet<Property>();

        //
        CollectionImpl coll = null;
        try {
            if (registrySession.getUserRegistry().get(nodePath) instanceof CollectionImpl) {

                coll = (CollectionImpl) registrySession.getUserRegistry().get(nodePath);

                String[] childPaths = coll.getChildren();

                for (int i = 0; i < childPaths.length; i++) {
                    Resource res = registrySession.getUserRegistry().get(childPaths[i]);

                    if ((res instanceof ResourceImpl) && (res.getProperty("registry.jcr.property.type") != null)) {
                        String[] temp = childPaths[i].split("/");
                        propNamesList.add(temp[temp.length - 1]);

                    }
                }

                Properties propyList = coll.getProperties();
                Enumeration en = propyList.propertyNames();
                while (en.hasMoreElements()) {
                    String pName = en.nextElement().toString();
                    if ((pName != null) && (!isImplicitProperty(pName))) {
                        propNamesList.add(pName);

                    }
                }

                Iterator it = propNamesList.iterator();

                while (it.hasNext()) {

                    properties.add(getProperty(it.next().toString()));

                }
            }
        } catch (Exception e) {
            String msg = "failed to resolve the path of the given node or violation of repository syntax " + this;
            log.debug(msg);
            throw new RepositoryException(msg, e);
        }


        RegistryPropertyIterator propertyIterator = new RegistryPropertyIterator(properties, this);


        return propertyIterator;

    }

    public PropertyIterator getProperties(String s) throws RepositoryException {

        boolean isMatch = false;

        Properties propyList = collection.getProperties();

        Iterator nameIt = getCollectionProperties(propyList).iterator();
        Set propValList = new HashSet();

        String regex = s + "*";
        String input = "";

        while (nameIt.hasNext()) {

            input = nameIt.next().toString();

            isMatch = Pattern.matches(regex, input);


            propValList.add(collection.getProperty(input));

        }


        RegistryPropertyIterator propertyIterator = new RegistryPropertyIterator(propValList, this);

        return propertyIterator;


    }

    public PropertyIterator getProperties(String[] strings) throws RepositoryException {

        boolean isMatch = false;

        Properties propyList = collection.getProperties();
        Set<String> propNames = getCollectionProperties(propyList);
        Iterator nameIt = propNames.iterator();
        Set propValList = new HashSet();

        String[] regex = new String[strings.length];

        String input = "";
        String reg = "";

        while (nameIt.hasNext()) {

            input = nameIt.next().toString();

            for (int i = 0; i < regex.length; i++) {

                reg = regex[i] + "*";
                isMatch = Pattern.matches(reg, input);
                if (isMatch) break;

            }

            propValList.add(collection.getProperty(input));
        }

        RegistryPropertyIterator propertyIterator = new RegistryPropertyIterator(propValList, this);

        return propertyIterator;
    }

    public Item getPrimaryItem() throws ItemNotFoundException, RepositoryException {
        if (nodeType != null) {
            String name = nodeType.getPrimaryItemName();
            return this.getNode(name);
        } else
            return null;
    }

    public String getUUID() throws UnsupportedRepositoryOperationException, RepositoryException {
        return null;
    }

    public String getIdentifier() throws RepositoryException {
        return nodePath;
    }

    public int getIndex() throws RepositoryException // TODO
        return 0;
    }

    public PropertyIterator getReferences() throws RepositoryException {

        return getProperties();

    }

    public PropertyIterator getReferences(String s) throws RepositoryException {
        return getProperties(s);
    }

    public PropertyIterator getWeakReferences() throws RepositoryException {
        return getProperties();
    }

    public PropertyIterator getWeakReferences(String s) throws RepositoryException {
        return getProperties(s);
    }

    public boolean hasNode(String s) throws RepositoryException {    //s-relative path
        String subnodepath = nodePath + s;
        boolean hasNode = false;
        if (nodePath.equals("/")) {
            subnodepath = nodePath + s;
        } else {
            subnodepath = nodePath + "/" + s;
        }

        try {

            if (registrySession.getUserRegistry().resourceExists(subnodepath)) {

                if ((registrySession.getUserRegistry().get(subnodepath)) instanceof CollectionImpl) {

                    hasNode = true;

                }

            }

        } catch (RegistryException e) {
            String msg = "failed to resolve the path of the given node or violation of repository syntax " + this;
            log.debug(msg);
            throw new RepositoryException(msg, e);
        }
        return hasNode;
    }

    public boolean hasProperty(String s) throws RepositoryException {

        boolean hasProperty = false;

        String absPath = nodePath + "/" + s;

        String[] tempArr;
        String tempPath = absPath;
        tempArr = tempPath.split("/");
        tempPath = tempPath.substring(0, (tempPath.length()) - (tempArr[tempArr.length - 1].length()) - 1);
        String propName = tempArr[tempArr.length - 1];

        try {

            if (registrySession.getUserRegistry().resourceExists(tempPath)) {
                CollectionImpl c = (CollectionImpl) registrySession.getUserRegistry().get(tempPath);

                if (c.getProperty(propName) != null) {
                    hasProperty = true;

                }

            } else if (registrySession.getUserRegistry().resourceExists(absPath)) {
                if (!((registrySession.getUserRegistry().get(absPath)) instanceof Collection)) {

                    hasProperty = true;
                }
            }

        } catch (RegistryException e) {
            String msg = "failed to resolve the path of the given node or violation of repository syntax " + this;
            log.debug(msg);
            throw new RepositoryException(msg, e);
        }
        return hasProperty;
    }

    public boolean hasNodes() throws RepositoryException {

        boolean hasNodes = true;
        CollectionImpl collec = null;
        try {

            if ((registrySession.getUserRegistry().resourceExists(nodePath)) && ((registrySession.getUserRegistry().get(nodePath) instanceof CollectionImpl))) {

                collec = (CollectionImpl) registrySession.getUserRegistry().get(nodePath);

            }

            if (collec != null) {
                String[] children = collec.getChildren();

                if (children != null) {
                    if (children.length == 0) hasNodes = false;
                }
            }

        } catch (RegistryException e) {
            e.printStackTrace();
        }

        return hasNodes;
    }

    public boolean hasProperties() throws RepositoryException {

        boolean hasProperties = true;
        CollectionImpl coll = null;
        try {
            coll = (CollectionImpl) registrySession.getUserRegistry().get(nodePath);

        } catch (RegistryException e) {

            String msg = "failed to resolve the path of the given node or violation of repository syntax " + this;
            log.debug(msg);
            throw new RepositoryException(msg, e);

        }

        Properties propyList = coll.getProperties();
        Set<String> propNames = getCollectionProperties(propyList);

        if (propNames.size() == 0) {

            hasProperties = false;
        }

        return hasProperties;
    }

    public NodeType getPrimaryNodeType() throws RepositoryException {

        NodeType nt = null;
        CollectionImpl collec = null;
        String priType = null;
        try {
            if (registrySession.getUserRegistry().get(nodePath) instanceof CollectionImpl) {
                collec = (CollectionImpl) registrySession.getUserRegistry().get(nodePath);
                priType = collec.getProperty("jcr:primaryType");
            }

            Iterator it = registrySession.getRepository().getNodeTypeList().iterator();

            while (it.hasNext()) {

                nt = (NodeType) it.next();

                if ((nt != null) && (nt.getName().equals(priType))) {
                    break;
                }

            }

        } catch (RegistryException e) {
            String msg = "failed to resolve the path of the given node or violation of repository syntax " + this;
            log.debug(msg);
            throw new RepositoryException(msg, e);
        }

        return nt;
    }

    public NodeType[] getMixinNodeTypes() throws RepositoryException {
        return new NodeType[0];
    }

    public boolean isNodeType(String s) throws RepositoryException {
        boolean isNodeType = false;

        try {

            CollectionImpl collec = (CollectionImpl) registrySession.getUserRegistry().get(nodePath);

            if ((collec.getPropertyValues("jcr:mixinTypes") != null) && (collec.getPropertyValues("jcr:mixinTypes").contains(s))) {

                isNodeType = true;

            } else if ((getPrimaryNodeType() != null) && (getPrimaryNodeType().getName().equals(s))) {

                isNodeType = true;
            }


        } catch (RegistryException e) {
            String msg = "failed to resolve the path of the given node or violation of repository syntax " + this;
            log.debug(msg);
            throw new RepositoryException(msg, e);
        }

        return isNodeType;
    }

    public void setPrimaryType(String s) throws NoSuchNodeTypeException, VersionException, ConstraintViolationException, LockException, RepositoryException {

        try {

            CollectionImpl collec = (CollectionImpl) registrySession.getUserRegistry().get(nodePath);
            collec.setProperty("jcr:primaryType", s);
            registrySession.getUserRegistry().put(nodePath, collec);

        } catch (RegistryException e) {
            String msg = "failed to resolve the path of the given node or violation of repository syntax " + this;
            log.debug(msg);
            throw new RepositoryException(msg, e);
        }


    }

    public void addMixin(String s) throws NoSuchNodeTypeException, VersionException, ConstraintViolationException, LockException, RepositoryException {

        try {

            CollectionImpl collec = (CollectionImpl) registrySession.getUserRegistry().get(nodePath);

            if (collec.getPropertyValues("jcr:mixinTypes") == null) {

                List list = new ArrayList();
                list.add(s);
                collec.setProperty("jcr:mixinTypes", list);

            } else {

                collec.getPropertyValues("jcr:mixinTypes").add(s);

            }
            registrySession.getUserRegistry().put(nodePath, collec);

        } catch (RegistryException e) {
            String msg = "failed to resolve the path of the given node or violation of repository syntax " + this;
            log.debug(msg);
            throw new RepositoryException(msg, e);
        }


    }

    public void removeMixin(String s) throws NoSuchNodeTypeException, VersionException, ConstraintViolationException, LockException, RepositoryException {

        try {
            CollectionImpl collec = (CollectionImpl) registrySession.getUserRegistry().get(nodePath);
            collec.getPropertyValues("jcr:mixinTypes").remove(s);
            registrySession.getUserRegistry().put(nodePath, collec);

        } catch (RegistryException e) {
            String msg = "failed to resolve the path of the given node or violation of repository syntax " + this;
            log.debug(msg);
            throw new RepositoryException(msg, e);
        }


    }

    public boolean canAddMixin(String s) throws NoSuchNodeTypeException, RepositoryException {

        return true; //for testing versioning
    }

    public NodeDefinition getDefinition() throws RepositoryException {

        return new RegistryNodeDefinition();
    }

    public Version checkin() throws VersionException, UnsupportedRepositoryOperationException, InvalidItemStateException, LockException, RepositoryException {

        return registrySession.getWorkspace().getVersionManager().checkin(nodePath);
    }

    public void checkout() throws UnsupportedRepositoryOperationException, LockException, ActivityViolationException, RepositoryException {

        registrySession.getWorkspace().getVersionManager().checkout(nodePath);

    }

    public void doneMerge(Version version) throws VersionException, InvalidItemStateException, UnsupportedRepositoryOperationException, RepositoryException {

    }

    public void cancelMerge(Version version) throws VersionException, InvalidItemStateException, UnsupportedRepositoryOperationException, RepositoryException {

    }

    public void update(String s) throws NoSuchWorkspaceException, AccessDeniedException, LockException, InvalidItemStateException, RepositoryException {


    }

    public NodeIterator merge(String s, boolean b) throws NoSuchWorkspaceException, AccessDeniedException, MergeException, LockException, InvalidItemStateException, RepositoryException {

        return null;
    }

    public String getCorrespondingNodePath(String s) throws ItemNotFoundException, NoSuchWorkspaceException, AccessDeniedException, RepositoryException {

        String npath = null;
        Set a = registrySession.getRepository().getWorkspaces();

        try {

            RegistrySession sess;

            for (Object aa : a) {

                sess = (RegistrySession) aa;

                if (sess.getWorkspaceName() != null) {
                    if (sess.getWorkspaceName().equals(s)) {

                        CollectionImpl collec = (CollectionImpl) sess.getUserRegistry().get(nodePath);
                        npath = collec.getPath();
                    }
                } else {

                    throw new NoSuchWorkspaceException();
                }

            }

        }

        catch (RegistryException e) {
            e.printStackTrace();
        }


        return npath;
    }

    public NodeIterator getSharedSet() throws RepositoryException {
        return null;
    }

    public void removeSharedSet() throws VersionException, LockException, ConstraintViolationException, RepositoryException {

    }

    public void removeShare() throws VersionException, LockException, ConstraintViolationException, RepositoryException {

    }

    public boolean isCheckedOut() throws RepositoryException {

        return registrySession.getWorkspace().getVersionManager().isCheckedOut(nodePath);
    }

    public void restore(String s, boolean b) throws VersionException, ItemExistsException, UnsupportedRepositoryOperationException, LockException, InvalidItemStateException, RepositoryException {
        try {
//
//
            if ((!(registrySession.getUserRegistry().get(nodePath).getPropertyValues("wso2.registry.jcr.versions").contains(s)))) {
                throw new VersionException();
            } else {
                setProperty("jcr:isCheckedOut", "false");
            }
        } catch (RegistryException e) {

        }
//
    }

    public void restore(Version version, boolean b) throws VersionException, ItemExistsException, InvalidItemStateException, UnsupportedRepositoryOperationException, LockException, RepositoryException {

        try {
            if ((version != null) && ((registrySession.getUserRegistry().get(nodePath).getPropertyValues("wso2.registry.jcr.versions").contains(version.getName())))) {
                registrySession.getWorkspace().getVersionManager().restore(nodePath, version, true);

            } else {
                throw new VersionException();

            }
        } catch (RegistryException e) {

        }
    }

    public void restore(Version version, String s, boolean b) throws PathNotFoundException, ItemExistsException, VersionException, ConstraintViolationException, UnsupportedRepositoryOperationException, LockException, InvalidItemStateException, RepositoryException {

        setProperty("jcr:isCheckedOut", "false");
    }

    public void restoreByLabel(String s, boolean b) throws VersionException, ItemExistsException, UnsupportedRepositoryOperationException, LockException, InvalidItemStateException, RepositoryException {

    }

    public VersionHistory getVersionHistory() throws UnsupportedRepositoryOperationException, RepositoryException {

        return registrySession.getWorkspace().getVersionManager().getVersionHistory(nodePath);
    }

    public Version getBaseVersion() throws UnsupportedRepositoryOperationException, RepositoryException {

        return registrySession.getWorkspace().getVersionManager().getBaseVersion(nodePath);
    }

    public Lock lock(boolean b, boolean b1) throws UnsupportedRepositoryOperationException, LockException, AccessDeniedException, InvalidItemStateException, RepositoryException {

        return registrySession.getWorkspace().getLockManager().lock(nodePath, b, b1, 10, this.getName());

    }

    public Lock getLock() throws UnsupportedRepositoryOperationException, LockException, AccessDeniedException, RepositoryException {

        return registrySession.getWorkspace().getLockManager().getLock(nodePath);
    }

    public void unlock() throws UnsupportedRepositoryOperationException, LockException, AccessDeniedException, InvalidItemStateException, RepositoryException {

        new RegistryLockManager(registrySession).unlock(nodePath);
    }

    public boolean holdsLock() throws RepositoryException {

        return registrySession.getWorkspace().getLockManager().holdsLock(nodePath);
    }

    public boolean isLocked() throws RepositoryException {

        return registrySession.getWorkspace().getLockManager().isLocked(nodePath);
    }

    public void followLifecycleTransition(String s) throws UnsupportedRepositoryOperationException, InvalidLifecycleTransitionException, RepositoryException {


    }

    public String[] getAllowedLifecycleTransistions() throws UnsupportedRepositoryOperationException, RepositoryException {

        return new String[0];
    }

    public String getPath() throws RepositoryException {

        return nodePath;
    }

    public String getName() throws RepositoryException {

        String[] nodeName = nodePath.split("/")//assume that we give the path including its name when creating a node

        int nameIndex = nodeName.length - 1;


        return nodeName[nameIndex];
    }

    public Item getAncestor(int i) throws ItemNotFoundException, AccessDeniedException, RepositoryException {

        return item.getAncestor(i);
    }

    public Node getParent() throws ItemNotFoundException, AccessDeniedException, RepositoryException {
        Node par = null;
        String parent = "";
        if (nodePath.equals("/")) {

            throw new ItemNotFoundException();
        }

        try {

            if (!registrySession.getUserRegistry().resourceExists(nodePath)) {
                throw new InvalidItemStateException();
            }

            CollectionImpl col = (CollectionImpl) registrySession.getUserRegistry().get(nodePath);

            if (col != null) {

                if (nodePath.split("/").length == 2) {
                    parent = "/";

                } else {
                    parent = col.getParentPath();
                }
                par = new RegistryNode(parent, registrySession);
            }

        } catch (RegistryException e) {
            e.printStackTrace();

        } catch (InvalidItemStateException e) {
            throw new InvalidItemStateException();
        }

        return par;
    }

    public int getDepth() throws RepositoryException {
        String[] len = nodePath.split("/");


        return len.length - 2;
    }

    public RegistrySession getSession() throws RepositoryException {


        return registrySession;
    }

    public boolean isNode() {


        return item.isNode();
    }

    public boolean isNew() {

        boolean isNew = false;

        try {
            if ((collection != null) && (collection.getChildren() != null) && (collection.getChildren().length == 0)) {

                isNew = true;

            }
        } catch (Exception e) {
            isNew = true;

        }

        return isNew;

    }

    public boolean isModified() {


        return item.isModified();

    }

    public boolean isSame(Item item) throws RepositoryException {

        /*
        The ability to address the same piece of data via more than one path is a
        common feature of many content storage systems. In JCR this feature is
        supported through shareable nodes.
        */

        return this.item.isSame(item);

    }

    public void accept(ItemVisitor itemVisitor) throws RepositoryException {

    }

    public void save() throws AccessDeniedException, ItemExistsException, ConstraintViolationException, InvalidItemStateException, ReferentialIntegrityException, VersionException, LockException, NoSuchNodeTypeException, RepositoryException {


    }

    public void refresh(boolean b) throws InvalidItemStateException, RepositoryException {


        if (!b) {
            try {
                if (!(registrySession.getUserRegistry().resourceExists(nodePath))) {

                    throw new InvalidItemStateException();
                }
            } catch (RegistryException e) {
                e.printStackTrace()//To change body of catch statement use File | Settings | File Templates.
            }

        }

    }

    public void remove() throws VersionException, LockException, ConstraintViolationException, AccessDeniedException, RepositoryException {

        registrySession.removeItem(nodePath);

    }

    private boolean isImplicitProperty(String s) {

        if ((s != null) && (RegistryRepository.implicitPropertiyNames.contains(s))) {

            return true;
        } else {

            return false;
        }
    }

    private Set getCollectionProperties(Properties propyList) {

          Set propNamesList = new HashSet();

          Enumeration en = propyList.propertyNames();
                while (en.hasMoreElements()) {
                    String pName = en.nextElement().toString();
                    if ((pName != null) && (!isImplicitProperty(pName))) {
                        propNamesList.add(pName);

                    }
                }

          return propNamesList;
    }



}
TOP

Related Classes of org.wso2.carbon.registry.jcr.RegistryNode

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.