AnsweredAssumed Answered

Custom WCM Action - Corrupt Browse-Website.jsp

Question asked by tommahy on Nov 2, 2009
Hi,

I have been playing around with custom actions for WCM.
One of my scripts is an extra button that creates files on demand from the content of other files.
When the script is run it all runs fine, however when you try to continue browsing the jsp seems to have corrupted and the page will always display the same content.
ECM is still fine, but to fix this i need to logout and log back in.
I would like to know if its possible to force the Browse-website.jsp to refresh.

In web-client-config-wcm-actions.xml

<!– A createAggregate –>
         <action id="createAggregate">
            <permissions>
              <permission allow="true">Write</permission>
            </permissions>
            <evaluator>org.alfresco.web.action.evaluator.WCMWorkflowEvaluator</evaluator>
            <label>Create Aggregates</label>
            <image>/images/icons/add.gif</image>
            <action-listener>#{AVMBrowseBean.createAggregates}</action-listener>
         <params>
             <param name="id">#{actionContext.id}</param>
           </params>    
</action>

and the AVMBrowserBean


public Object[] getFiles(String newPath, String store, ArrayList<String> allStores, String pathOfFile){
   
   SortedMap<String, AVMNodeDescriptor> pathsBuffer = avmService.getDirectoryListing(-1, newPath);
    
   Object[] allPaths = pathsBuffer.toString().split(",");
    
   for (int z=0; z < allPaths.length; z++){

        String stringPaths = allPaths[z].toString();
        Object[] objectPaths = stringPaths.split("=");
       
        try{
       
           if (objectPaths[1].toString().startsWith("[LD")){
             
              //this is the folder
              String BufferPaths = objectPaths[1].toString().replace("}", "");
              BufferPaths = BufferPaths.replace("]", "");
              String[] BufferPathObject = BufferPaths.split(":");

              String Buffer = store+":"+BufferPathObject[3];
              String Buffer2 = BufferPathObject[3];
              getFiles(Buffer,store,allStores,Buffer2);
          
           }else if (objectPaths[1].toString().startsWith("[PF")){
            
              //this is the file
              allPaths[z] = objectPaths[0].toString().replace("{", "");
             
              if (allPaths[z].toString().startsWith(" ")){
                 allStores.add(pathOfFile+"/"+allPaths[z].toString().substring(1, allPaths[z].toString().length()));

              }else{
                 allStores.add(pathOfFile+"/"+allPaths[z].toString());
              }
           }
        }catch(Exception e1){
          logger.warn("The path did not contain a file");
        }
     }

   return allStores.toArray();
}

public String convertStreamToString(InputStream is){
   
   BufferedReader reader = new BufferedReader(new InputStreamReader(is));
   StringBuilder sb = new StringBuilder();
   
   String line = null;
   try{
      while ((line = reader.readLine()) != null){
         sb.append(line + "\n");
      }
   }catch (IOException e) {
      e.printStackTrace();
   } finally{
      try{
         is.close();
      }catch (IOException e) {
         e.printStackTrace();
      }
   }
   
   return sb.toString();
}


public void createAggregates(ActionEvent event)
   {
     
      UIActionLink link = (UIActionLink)event.getComponent();
      Map<String, String> params = link.getParameterMap();
      String path = params.get("id");
     
     
      removeLock(event);
     
     
      String[] storeBuffer = path.split(":");
      String store = storeBuffer[0];
     
      String[] staticPath = new String[3];
      staticPath[0] = path+"/WEB-INF/raw/hs-news";
      staticPath[1] = path+"/WEB-INF/raw/hs-doclib";
      staticPath[2] = path+"/WEB-INF/raw/hs-page";
           
      for (int i=0; i<staticPath.length; i++) {
       
         String newPath = staticPath[i];
         String[] bufferPathOfFile = newPath.split(":");
         String pathOfFile = bufferPathOfFile[1];
         ArrayList<String> allStores  = new ArrayList<String>();
         Object[] allPaths = getFiles(newPath,store,allStores,pathOfFile);
        
         // first create or load the file
        String file = newPath.replace(path+"/WEB-INF/raw/", "");
        try{
           avmService.createFile(newPath+"-agg", file+".xml");
        }catch (AVMExistsException e1){
           logger.warn("The file already exists, will just be creating new content");
        }
       

        //adding all the content to a String
       
        String content = "<content><type>"+ file +"</type>";
       
         for (int z=0; z < allPaths.length; z++){

            String pathTest = store+":"+allPaths[z].toString();
            
            AVMBrowseBean sts = new AVMBrowseBean();
            InputStream dataInFileFirst = avmService.getFileInputStream(-1, pathTest);
           
            String BufferString = sts.convertStreamToString(dataInFileFirst);
            String[] itemSplit = BufferString.split("<item>");
           

            for (int s=0; s < itemSplit.length; s++){
              
              
               int startLanguague = itemSplit[s].indexOf("<language>");
               int stopLanguague = itemSplit[s].indexOf("</language>");
              
               String languague;
              
               if (startLanguague == -1 || stopLanguague == -1){
                  languague = "<language>empty";
               }else{
                  languague = itemSplit[s].substring(startLanguague, stopLanguague);
               }

              
               int startUrl = itemSplit[s].indexOf("<url>");
               int stopUrl = itemSplit[s].indexOf("</url>");
              
               String url;
              
               if (startUrl == -1 || stopUrl == -1){
                  url = "<url>empty";
               }else{
                  url = itemSplit[s].substring(startUrl, stopUrl);
               }
              
               int startfile = itemSplit[s].indexOf("<file>");
               int stopfile = itemSplit[s].indexOf("</file>");
               String file2;
              
               if (startfile == -1 || stopfile == -1){
                  file2 = "<file>empty";
               }else{
                  file2 = itemSplit[s].substring(startfile, stopfile);
               }

               int startunit = itemSplit[s].indexOf("<unit>");
               int stopunit = itemSplit[s].indexOf("</unit>");
              
               String unit2;
              
               if (startunit == -1 || stopunit == -1){
                  unit2 = "<unit>empty";
               }else{
                  unit2 = itemSplit[s].substring(startunit, stopunit);
               }

               if (url != "<url>empty"){
                  content = content+"<item>" + languague +"</language>" + url +"</url>" + file2+"</file>" + unit2 +"</unit></item>";
               }
            }
            
         }

         logger.info("Writing the content");
        
         content = content+"</content>";

         byte[] test = content.getBytes();
         OutputStream dataInFile = avmService.getFileOutputStream(newPath+"-agg/"+file+".xml");
        
         try {
         dataInFile.write(test, 0, content.length());
         dataInFile.flush();
         dataInFile.close();
         } catch (IOException e) {
         e.printStackTrace();
         }
        
         logger.info("Finished Writing the content");
      }
   }
 

Outcomes