Añadir archivos al repositorio Alfresco con java

cancel
Showing results for 
Search instead for 
Did you mean: 
ditmgl
Member II

Añadir archivos al repositorio Alfresco con java

He creado un bean que hace una transformaciones a un archivo pdf y que como resultado me genera un archivo temporal (en c:\temp.dat por ejemplo) que luego quiero meter en otro espacio.

La pregunta es ¿como "muevo" un archivo de c:\ a por ejemplo guest_home?. (via bean de java, sin jsp o a traves de formularios).

Hasta ahora he probadocon la API de jcr:

//////////////
   ApplicationContext context = new ClassPathXmlApplicationContext("classpath:alfresco/application-context.xml");
   Repository repository = (Repository)context.getBean("JCR.Repository");
         
   SimpleCredentials credentials = new SimpleCredentials("admin",   "admin".toCharArray());
   Session session = repository.login(credentials);

   // now we're logged in, display some information about the root node
   Node rootNode = session.getRootNode();
         
   Node docFirmado = rootNode.getNode("app:company_home");
   Node contentNode = docFirmado.addNode("cm:content", "cm:content");
   contentNode.setProperty("cm:title", "Doc firmado");
   ClassPathResource resource = new ClassPathResource("c:\\temp.dat");
   contentNode.setProperty("cm:content", resource.getInputStream());
   session.save();

   /////////////

Pero me da el siguiente error (Cache is not alive ¿¿¿??):

12:53:55,584 ERROR [org.hibernate.engine.ActionQueue] could not release a cache lock
org.hibernate.cache.CacheException: java.lang.IllegalStateException: The org.hibernate.cache.UpdateTimestampsCache Cache is not alive.
   at org.hibernate.cache.EhCache.put(EhCache.java:125)
   at org.hibernate.cache.UpdateTimestampsCache.invalidate(UpdateTimestampsCache.java:69)
   at org.hibernate.engine.ActionQueue.afterTransactionCompletion(ActionQueue.java:217)
   at org.hibernate.impl.SessionImpl.afterTransactionCompletion(SessionImpl.java:424)
   at org.hibernate.jdbc.JDBCContext.afterTransactionCompletion(JDBCContext.java:229)
   at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:174)
   at org.springframework.orm.hibernate3.HibernateTransactionManager.doRollback(HibernateTransactionManager.java:594)
   at org.springframework.transaction.support.AbstractPlatformTransactionManager.processRollback(AbstractPlatformTransactionManager.java:753)
   at org.springframework.transaction.support.AbstractPlatformTransactionManager.rollback(AbstractPlatformTransactionManager.java:730)
   at org.springframework.transaction.interceptor.TransactionAspectSupport.completeTransactionAfterThrowing(TransactionAspectSupport.java:332)
   at org.alfresco.util.transaction.SpringAwareUserTransaction.completeTransactionAfterThrowing(SpringAwareUserTransaction.java:530)
   at org.alfresco.util.transaction.SpringAwareUserTransaction.rollback(SpringAwareUserTransaction.java:498)
   at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:362)
   at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:209)

Un saludo.
4 Replies
pjcaracuel_2349
Active Member II

Re: Añadir archivos al repositorio Alfresco con java

Buenas,

Y usando webservices no te resultaria mas facil??

Respecto al error que te sale, no se si lo habras visto ya pero te lo pongo de todas formas
http://forums.alfresco.com/en/viewtopic.php?f=4&t=14239

http://forums.alfresco.com/en/viewtopic.php?f=8&t=14848

Saludos
ditmgl
Member II

Re: Añadir archivos al repositorio Alfresco con java

Gracias, esos post ya los habia leido, llevo toda la mañana investigando el tema pero estoy totalmente atascado.

En cuanto a los webServices…no se si podre hacerlo usandolos, pero mirare de todas formas.

Un saludo.
pjcaracuel_2349
Active Member II

Re: Añadir archivos al repositorio Alfresco con java

Buenas,

Entiendo que hacerlo si se podra,te pongo un ejemplo proporcionado por el SDK de Alfresco


/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception.  You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.sample;

import org.alfresco.webservice.content.Content;
import org.alfresco.webservice.content.ContentServiceSoapBindingStub;
import org.alfresco.webservice.repository.UpdateResult;
import org.alfresco.webservice.types.CML;
import org.alfresco.webservice.types.CMLAddAspect;
import org.alfresco.webservice.types.CMLCreate;
import org.alfresco.webservice.types.ContentFormat;
import org.alfresco.webservice.types.NamedValue;
import org.alfresco.webservice.types.ParentReference;
import org.alfresco.webservice.types.Reference;
import org.alfresco.webservice.types.Store;
import org.alfresco.webservice.util.AuthenticationUtils;
import org.alfresco.webservice.util.Constants;
import org.alfresco.webservice.util.Utils;
import org.alfresco.webservice.util.WebServiceFactory;


/**
* Simple client example demonstrating the use of the Alfresco Web Service API.
*
* Note: An existing Alfresco Server must be started in order for the client to
*       connect to it.
*
* The client creates a content node in the "Company Home" folder.  The content
* may be viewed and operated on within the Alfresco Web Client. 
*
* This client demonstrates the "Alfresco Server" deployment option as described
* in the Alfresco Respotiory Architecture docucment -
* http://wiki.alfresco.com/wiki/Alfresco_Repository_Architecture
*/
public class FirstWebServiceClient
{
   
    public static void main(String[] args) throws Exception
    {
        // Start the session
        AuthenticationUtils.startSession("admin", "admin");
       
        try
        {
           // Create a reference to the parent where we want to create content
            Store storeRef = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
            ParentReference companyHomeParent = new ParentReference(storeRef, null, "/app:company_home", Constants.ASSOC_CONTAINS, null);

            // Assign name
            String name = "Web Services sample (" + System.currentTimeMillis() + ")";
            companyHomeParent.setChildName("cm:" + name);
           
            // Construct CML statement to create content node
            // Note: Assign "1" as a local id, so we can refer to it in subsequent
            //       CML statements within the same CML block
            NamedValue[] contentProps = new NamedValue[1];
            contentProps[0] = Utils.createNamedValue(Constants.PROP_NAME, name);
            CMLCreate create = new CMLCreate("1", companyHomeParent, null, null, null, Constants.TYPE_CONTENT, contentProps);
           
            // Construct CML statement to add titled aspect
            NamedValue[] titledProps = new NamedValue[2];
            titledProps[0] = Utils.createNamedValue(Constants.PROP_TITLE, name);
            titledProps[1] = Utils.createNamedValue(Constants.PROP_DESCRIPTION, name);
            CMLAddAspect addAspect = new CMLAddAspect(Constants.ASPECT_TITLED, titledProps, null, "1");
           
            // Construct CML Block
            CML cml = new CML();
            cml.setCreate(new CMLCreate[] {create});
            cml.setAddAspect(new CMLAddAspect[] {addAspect});

            // Issue CML statement via Repository Web Service and retrieve result
            // Note: Batching of multiple statements into a single web call
            UpdateResult[] result = WebServiceFactory.getRepositoryService().update(cml);    
            Reference content = result[0].getDestination();

            //
            // Write some content
            //
           
            ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService();
            String text = "The quick brown fox jumps over the lazy dog";
            ContentFormat contentFormat = new ContentFormat("text/plain", "UTF-8");
            Content contentRef = contentService.write(content, Constants.PROP_CONTENT, text.getBytes(), contentFormat);
            System.out.println("Content Length: " + contentRef.getLength());
        }
        catch(Throwable e)
        {
            System.out.println(e.toString());
        }
        finally
        {
            // End the session
            AuthenticationUtils.endSession();
            System.exit(0);
        }
    }
       
}

ditmgl
Member II

Re: Añadir archivos al repositorio Alfresco con java

Efectivamente, más tarde despues de postear vi ese código y probe ha hacerlo de esa manera y funcionó. Al menos al principio, no se que he tocado que ahora me crea archivos vacios  Smiley Tongue .

Moraleja: investigar más antes de postear y pensar que no se puede hacer.

Gracias.