AnsweredAssumed Answered

Lister et Télécharger les documents d'un espace

Question asked by tegel on Sep 10, 2008
Latest reply on Oct 13, 2008 by tegel
Bonjour,

J'ai commencé depuis 2 semaines à travailler sur les WS Java Backed de alfresco et j'ai quelques soucis.
Je souhaiterais récupérer les documents d'un espace les lister sous forme de liens pour pouvoir les télécharger par la suite.

J'ai tenté cela :


package org.alfresco.module.demoscripts;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

//import javax.transaction.Status;
import javax.transaction.UserTransaction;

import javax.servlet.http.HttpServletResponse;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.model.FileInfo;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.StoreRef;

import org.alfresco.repo.security.authentication.AuthenticationException;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.web.scripts.DeclarativeWebScript;
import org.alfresco.web.scripts.Status;
import org.alfresco.web.scripts.WebScriptException;
import org.alfresco.web.scripts.WebScriptRequest;

import org.alfresco.service.transaction.TransactionService;
import org.alfresco.repo.web.scripts.Repository;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.*;

/**
* Login and establish a ticket
*
* @author davidc
*/
public class JfolderWebScript extends DeclarativeWebScript
{
    // dependencies
   private static final Log logger = LogFactory.getLog(JfolderWebScript.class);
   
    private AuthenticationService authenticationService;
    /**
     * @param authenticationService
     */
    public void setAuthenticationService(AuthenticationService authenticationService)
    {
        this.authenticationService = authenticationService;
    }
   
   protected ServiceRegistry serviceRegistry ;
   
   public void setServiceRegistry(ServiceRegistry serviceRegistry) {
          this.serviceRegistry = serviceRegistry;
   }
   
   protected Repository repository;
   public void setRepository(Repository repository){
           this.repository = repository;
   }
   
   private TransactionService transactionService;
   public TransactionService getTransactionService() {
      return transactionService;
   }

   public void setTransactionService(TransactionService transactionService) {
      this.transactionService = transactionService;
   }
   
    protected FileFolderService getFileFolderService()
    {
       return serviceRegistry.getFileFolderService();
    }
    /* (non-Javadoc)
     * @see org.alfresco.web.scripts.DeclarativeWebScript#executeImpl(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.WebScriptResponse)
     */
    @Override
    protected Map<String, Object> executeImpl(WebScriptRequest req, Status status)
    {
       
      
       // define username and password
      String username = "admin";
        String password = "ldapadmin";
      
      //get path param
      String path = req.getParameter("path");
        if (path == null || path.length() == 0)
        {
            throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "Path not specified");
        }
      String nodeId = req.getParameter("node");
       
      UserTransaction txn = null ;
      
        try
        {

            // get ticket
            String currentUser = authenticationService.getCurrentUserName();
              
             if (currentUser.equals("guest")) {
                authenticationService.createAuthentication(username, password.toCharArray());
             }
                          
              txn = transactionService.getUserTransaction();
           
             
           txn.begin();
                  
           StoreRef storeRef = new StoreRef("workspace://SpacesStore");
            NodeRef companyHomeRef = this.repository.getCompanyHome();
            NodeRef nodeRef = companyHomeRef;
           
            if (nodeId != null) {
                nodeRef = new NodeRef(storeRef, nodeId);
            }
           
           FileFolderService fileFolderService = this.serviceRegistry.getFileFolderService();
         FileInfo fileInfo = fileFolderService.getFileInfo(nodeRef);
         
         List<FileInfo> children = getFileFolderService().listFiles(nodeRef);
         Object[] rows = new Object[children.size()];
           int i = 0;
           for (FileInfo info : children) {
            
               Map<String, Object> row = new HashMap<String, Object>();
               row.put("nodeId", info.getNodeRef().getId());
               row.put("name", info.getName());
               //row.put("title", getProperty(info, ContentModel.PROP_TITLE, ""));
            
             rows[i++] = row;
         }

            // add ticket to model for javascript and template access
            Map<String, Object> model = new HashMap<String, Object>(7, 1.0f);
            model.put("ticket",  authenticationService.getCurrentTicket());
         model.put("path", path.toString());
         model.put("node", nodeRef);
         model.put("folderName", fileInfo.getName().toString());
         model.put("folderId", nodeRef.getId());
            model.put("rows", rows);
         
         txn.commit();
         
            return model;
           
        }
        catch (Throwable ex)
      {   logger.error("",ex);
         try {   txn.rollback();}
         catch (Exception ex2) {   logger.error("Failed to rollback transaction", ex2);}
         if (ex instanceof RuntimeException) {   throw (RuntimeException) ex;}
         else {throw new RuntimeException("Login failed", ex);}
         
         return null;
      }
        finally
        {
            authenticationService.clearCurrentSecurityContext();
        }
      
    }

}


et j'ai ajouté le bean ci-dessus  dans le fichier module-context.xml:

<bean id="webscript.org.alfresco.demo.jfolder.get" class="org.alfresco.module.demoscripts.JfolderWebScript" parent="webscript"
       abstract="false" singleton="true" lazy-init="default"
       autowire="default" dependency-check="default">
     <property name="repository" ref="webscripts.repo" />
      <property name="serviceRegistry" ref="ServiceRegistry" />
     <property name="authenticationService" ref="authenticationService" />   
     <property name="transactionService">
      <ref bean="transactionService" />
      </property>
   </bean>

mais j'ai toujours l'erreur suivante:

<exception>java.lang.NullPointerException</exception>

<callstack>

      java.lang.NullPointerException
         org.alfresco.module.demoscripts.JfolderWebScript.executeImpl(JfolderWebScript.java:130)
         org.alfresco.web.scripts.DeclarativeWebScript.executeImpl(DeclarativeWebScript.java:251)
         org.alfresco.web.scripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:87)
         org.alfresco.repo.web.scripts.RepositoryContainer$1.execute(RepositoryContainer.java:311)
         org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:236)
         org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:166)
        
Quelqu'un aurait une idée?

Merci.

Outcomes