cancel
Showing results for 
Search instead for 
Did you mean: 

create folder structure

enkidu
Champ in-the-making
Champ in-the-making
hi mates,

i want to create a folder structure.. my code looks like this:


   @Override
   protected void executeImpl(Action uAction, NodeRef uNodeRef) {
      
      try {
      
         List<String> data = new ArrayList<String>();
         
         data.add("folderone");
         data.add("foldertwo");
                  
         checkFolder(data);
         
      } catch (Exception e) {
         // TODO Auto-generated catch block
         throw new AlfrescoRuntimeException("Error "+e.toString());   
      }       
   }

   private boolean checkFolder(List<String> folderName) {
      
      boolean folderCheck = false;
      StringBuilder strBuilder = new StringBuilder();
      
      StoreRef store = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
      String base = "//app:company_home/st:sites/cm:folders";
      
      for(String data : folderName) {
         
         strBuilder.append("/cm:"+search.ISO9075Encode(data));
         
         String query = "//app:company_home/st:sites/cm:folders"+strBuilder.toString();
         ResultSet rs = searchService.query(store, SearchService.LANGUAGE_XPATH, query);
         logger.error("ResultSet: "+rs.length()+" - Query: "+query+" Base: "+base);
         if(rs.length()==0) {
            rs = searchService.query(store, SearchService.LANGUAGE_XPATH, base);
            NodeRef ref = rs.getNodeRef(0);            
            fileFolderService.create(ref, search.ISO9075Decode(data), ContentModel.TYPE_FOLDER);
         }
         base = base+strBuilder.toString();
      }
      return folderCheck;
   }



when i pass just one folder to the List then it works.. but when i add two or more i get and indexoutofbounceexception. any ideas??
5 REPLIES 5

mrogers
Star Contributor
Star Contributor
You check the result set is zero length then get the node ref of the first [0] element.

enkidu
Champ in-the-making
Champ in-the-making
i check if the folder i wanna create already exists if not the result must be 0.. if so i create the folder. as i said it works if i only pass one argument within the list to the function.. but it fails when i pass 2 or more.

mrogers
Star Contributor
Star Contributor
I still suspect its your line of code NodeRef ref = rs.getNodeRef(0);   that's giving grief.   You have an exception in front of you so it should tell you which line number.

Studying your code a bit more my next suspicion is that your line of code base = base+strBuilder.toString(); is going to cause problems with duplicated entries.

enkidu
Champ in-the-making
Champ in-the-making
do u know a better way to create a folder structure?

enkidu
Champ in-the-making
Champ in-the-making
thnx mrogers for ur help.. for all who wanna now how to creating a folder structe at once: FileFolderUtil.makeFolders(…)