Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.openl.rules.repository.jcr;
  
  import java.util.List;
  
  import javax.jcr.Node;
 
Implementation for JCR Folder.

Author(s):
Aleh Bykhavets
 
 public class JcrFolderAPI extends JcrEntityAPI implements FolderAPI {

    
Creates new folder.

Parameters:
parentNode parent node (files or other folder)
nodeName name of new node
Returns:
newly created folder
Throws:
javax.jcr.RepositoryException if fails
 
     protected static JcrFolderAPI createFolder(JcrFolderAPI parentString nodeNameArtefactPath paththrows RepositoryException {
         Node parentNode = parent.node();
         Node n = NodeUtil.createNode(parentNodenodeName.true);
         parentNode.save();
         n.save();
 
         return new JcrFolderAPI(nparent.getTransactionManager(), pathfalse);
     }
 
     public JcrFolderAPI(Node nodeRTransactionManager transactionManagerArtefactPath pathboolean oldVersionthrows RepositoryException {
         super(nodetransactionManagerpatholdVersion);
 
 //        NodeUtil.checkNodeType(node, JcrNT.NT_FOLDER);
     }
 
     public JcrFolderAPI(Node nodeRTransactionManager transactionManagerArtefactPath paththrows RepositoryException {
         this(nodetransactionManagerpathfalse);
     }
 
     public JcrFileAPI addResource(String nameInputStream contentthrows ProjectException {
         try {
             JcrFileAPI file = JcrFileAPI.createFile(thisnamegetArtefactPath().withSegment(name));
             file.setContent(content);
             return file;
         } catch (RepositoryException e) {
             throw new ProjectException(String.format("Failed to Create File \"%s\". Reason: %s"namee.getMessage()), e);
         }
     }
 
     public JcrFolderAPI addFolder(String namethrows ProjectException {
         try {
             return JcrFolderAPI.createFolder(thisnamegetArtefactPath().withSegment(name));
         } catch (RepositoryException e) {
             throw new ProjectException(String.format("Failed to Create Sub Folder \"%s\". Reason: %s"namee.getMessage()), e);
         }
     }
 
     public List<JcrEntityAPIgetFiles() throws RRepositoryException {
         List<JcrEntityAPIresult = new LinkedList<JcrEntityAPI>();
         listNodes(resulttrue);
         return result;
     }

    
 
     public List<JcrFolderAPIgetFolders() throws RRepositoryException {
         List<JcrFolderAPIresult = new LinkedList<JcrFolderAPI>();
         listNodes(resultfalse);
         return result;
     }
 
     // ------ private methods ------
 
    
Lists nodes.

Parameters:
list2add list to which nodes should be added
isFiles whether return only files or only folders
Throws:
org.openl.rules.repository.exceptions.RRepositoryException if failed
 
     private void listNodes(List list2addboolean isFilesthrows RRepositoryException {
         try {
             //FIXME for old
             NodeIterator ni = node().getNodes();
             while (ni.hasNext()) {
                Node n = ni.nextNode();
                String name = n.getName();
                // TODO: use search? But looking through direct child nodes
                // seems faster
                boolean isFolder = false;
                if(isOldVersion()){
                    Node frozenNode = NodeUtil.normalizeOldNode(ngetVersion());
                    isFolder = frozenNode.getProperty("jcr:frozenPrimaryType").getString().equals(.);
                    n = frozenNode;
                }else{
                    isFolder = n.isNodeType(.);
                }
                if (isFolder) {
                    if (!isFiles) {
                        list2add.add(new JcrFolderAPI(ngetTransactionManager(), getArtefactPath().withSegment(name), isOldVersion()));
                    }
                } else if(!n.isNodeType(.)){
                    //FIXME
                    if (isFiles) {
                        list2add.add(new JcrFileAPI(ngetTransactionManager(), getArtefactPath().withSegment(name), isOldVersion()));
                    }
                }
            }
        } catch (RepositoryException e) {
            e.printStackTrace();
            throw new RRepositoryException("Failed to list nodes."e);
        }
    }
    public JcrEntityAPI getArtefact(String namethrows ProjectException {
        try {
            Node n = node().getNode(name);
            boolean isFolder = false;
            if(isOldVersion()){
                Node frozenNode = NodeUtil.normalizeOldNode(ngetVersion());
                isFolder = frozenNode.getProperty("jcr:frozenPrimaryType").getString().equals(.);
                n = frozenNode;
            }else{
                isFolder = n.isNodeType(.);
            }
            if (isFolder) {
                return new JcrFolderAPI(ngetTransactionManager(), getArtefactPath().withSegment(name), isOldVersion());
            } else {
                return new JcrFileAPI(ngetTransactionManager(), getArtefactPath().withSegment(name), isOldVersion());
            }
        } catch (RepositoryException e) {
            throw new ProjectException("Failed to list nodes."e);
        }
    }
    public boolean hasArtefact(String name) {
        try {
            return node().hasNode(name);
        } catch (RepositoryException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }
    public Collection<? extends JcrEntityAPIgetArtefacts() {
        List<JcrEntityAPIartefacts = new ArrayList<JcrEntityAPI>();
        try {
            artefacts.addAll(getFolders());
            artefacts.addAll(getFiles());
        } catch (RRepositoryException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return artefacts;
    }
    
    @Override
    public boolean isFolder() {
        return true;
    }
    
    @Override
    public JcrFolderAPI getVersion(CommonVersion versionthrows RRepositoryException{
        try {
            Node frozenNode = NodeUtil.getNode4Version(node(), version);
            return new JcrFolderAPI(frozenNodegetTransactionManager(), getArtefactPath(), true);
        } catch (RepositoryException e) {
            throw new RRepositoryException("Failed to get version for node."e);
        }
    }
New to GrepCode? Check out our FAQ X