AnsweredAssumed Answered

Récupérer un NodeRef à partir d'un nom de fichier

Question asked by francky31 on Mar 24, 2009
Latest reply on Mar 24, 2009 by francky31
Bonjour, je dois écrire un composant alfresco chargé de généré un document dans un espace quelconque à partir d'un template (lui aussi stocké dans un espace Alfresco. Ce composant doit etre appelable par Web Service .
J'ai donc écrit la custom action suivante qui prend 3 paramètres :
- une string contenant le nom du fichier à générer: (ex: docgenere.zip)
- une string contenant le répertoire (l'espace) de destination:
- une 3eme  parametre sans importance pour la questionservant uniquement à la génération

Ma question: comment récupérer à partir du nom du fichier et du nom du répertoire un NodeRef ? le but étant de récupérer un outputStream pour générer mon fichier ?

Merci pour vos réponses



public class GenDocActionExecuter extends ActionExecuterAbstractBase {
    /** The logger */
    private static Log logger = LogFactory.getLog("xxx.yyy");

    /** The name of the action */
    public static final String NAME = "gen_doc_eds-action";

    /** The parameter names */
    public static final String PARAM_XML_INPUT = "param-xml-input";
    public static final String PARAM_DESTINATION_FILE = "param_destination-file";
    public static final String PARAM_DESTINATION_FOLDER = "param_destination-folder";

    /** Standard Alfresco manager allowing manipulating Alfresco object */
    private ContentService contentService = null;
    private NodeService nodeService = null;

    /**
     *
     * @see org.alfresco.repo.action.executer.ActionExecuterAbstractBase#executeImpl(org.alfresco.service.cmr.action.Action,
     *      org.alfresco.service.cmr.repository.NodeRef)
     */
    @Override
    protected void executeImpl(Action action, NodeRef actionedUponNodeRef) {
        logger.debug("GenDocActionExecuter entering …");
       
        if (!nodeService.exists(actionedUponNodeRef)) {
            logger.error("GenDocActionExecuter template doesn't exist: " + actionedUponNodeRef.toString());
            return;
        }

        /* get the parameter of the action */
        String xmlInput = (String) action.getParameterValue(PARAM_XML_INPUT);
        logger.debug("GenDocActionExecuter —> xmlInput " + xmlInput);

        String fileName = (String) nodeService.getProperty(actionedUponNodeRef,
                ContentModel.PROP_NAME);
        logger.debug("GenDocActionExecuter —> fileName " + fileName);

        // Get a reader, then ZipInputStream on the template
        // ————————————————–
        ContentReader reader = contentService.getReader(actionedUponNodeRef,
                ContentModel.PROP_CONTENT);
        InputStream in = reader.getContentInputStream();
        ZipInputStream zin = new ZipInputStream(in);

        // Get a writer, then a ZipOutputStrem on the generated document
        // ————————————————————
        String sdestinationFolder = (String) action
                .getParameterValue(PARAM_DESTINATION_FOLDER);
        String destinationFile = (String) action
                .getParameterValue(PARAM_DESTINATION_FILE);

       
        NodeRef nodeRef=     ???? getNodeRef à partir de sdestinationFolder et destinationFile        ?????
       

        
        ContentWriter writer = contentService.getWriter(nodeRef,
                ContentModel.PROP_CONTENT, true);
        OutputStream outStream = writer.getContentOutputStream();
        ZipOutputStream out = new ZipOutputStream(outStream);

        byte[] buf = new byte[1024];

        try {
            ZipEntry entry = zin.getNextEntry();
            while (entry != null) {
                String name = entry.getName();
                logger.debug("Extracting: " + name);
                out.putNextEntry(new ZipEntry(entry.getName()));

                OutputStream tempOut = null;

                if (name.equals("customXml/item1.xml")) {
                    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
                    tempOut = byteOut;
                } else {
                    tempOut = out;
                }
                int len;
                while ((len = zin.read(buf)) > 0) {
                    tempOut.write(buf, 0, len);
                }
                if (name.equals("customXml/item1.xml")) {
                    tempOut.write(xmlInput.getBytes("utf-8"));
                }
                entry = zin.getNextEntry();
            }
            zin.close();
            out.close();
        } catch (UnsupportedEncodingException e) {
            logger.error("Error generating document", e);
        } catch (IOException e) {
            logger.error("Error generating document", e);
        }

        logger.debug("GenDocActionExecuter ending …");

    }

Outcomes