cancel
Showing results for 
Search instead for 
Did you mean: 

add event handler on setProperty(cm:content)

denza
Champ on-the-rise
Champ on-the-rise
Hi,
is it possible to do something like that in custom java action? What I want do to is: I wanna take file from my file system and add it to alfresco, also add some properties etc. . But I want to be sure that alfresco wrote content to the node because I'm deleting the file after so I need something to say: OK i added the content you can delete that file.  Thanks in advance!
Denis
3 REPLIES 3

mrogers
Star Contributor
Star Contributor
From a remote context it's probably safe just to check that you get a successful upload.    To be paranoid you could also check that you can read the content from alfresco before deleting from your filesystem.

If you are running code from within the context of an action then you need to be careful of transactions,  it would be no good to write a new node, delete your content from your non-transactional filesystem and then rollback.    You probably want to start a separate transaction within your action and then delete from the non-transactional filesystem according to whether the transaction has committed.

Another option is to look at behaviours, which are very much "event handlers for setProperty(cm:content)"

denza
Champ on-the-rise
Champ on-the-rise
Thanks mrogers for your quick replay!
Thing is, in my action I'm adding about 50 000 files and on some nodes content was missing, I assume it happened because setProperty(cm:content) was still doing its work when I deleted the file and I got nodes that had size but content was missing also did not get any exceptions. I canceled the delete job hopping it would be OK now.
One more question: on transaction.commit() alfresco will do the work if exception happened rollback and if not everything is OK?

denza
Champ on-the-rise
Champ on-the-rise
I separated two actions but again when I'm adding large number of files to alfresco  (over > 500) some of them are missing content. Here is my code:

if (rsScan.next()) {         
      ubaciUAlfresko();
}   



            protected void ubaciUAlfresko() {
                UserTransaction trx = serviceRegistry.getTransactionService().getUserTransaction();
      try {
         trx.begin();
      }
      catch (NotSupportedException e1) {
         e1.printStackTrace();
      }
      catch (SystemException e1) {
         e1.printStackTrace();
      }
                     
      Session session = null;
      Node companyHome = null;
      
      try {
         session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
         Node rootNode = session.getRootNode();
         companyHome = rootNode.getNode("app:company_home");
      }
      catch (LoginException e) {
         e.printStackTrace();
      }
      catch (RepositoryException e) {
         e.printStackTrace();
      }

      /*try {
         is = new FileInputStream(tifSlika);
      }
      catch (FileNotFoundException e1) {
         e1.printStackTrace();
      }*/
      
      Node mjesec;
      try {
         mjesec = companyHome.getNode("ARHIVA/"+ISO9075.encode(godina_potpisivanja)+"/"+ISO9075.encode(mjesec_potpisivanja));
                        
         Node ugovor = mjesec.addNode(tifId,"telekom:ugovor");
         ugovor.setProperty("cm:content", new FileInputStream(tifSlika));
         ugovor.save();
         trx.commit();

         azurirajPutanju(mjesec_potpisivanja,godina_potpisivanja, tifId);
      
      }
      catch (Exception e) {
         try {
            System.out.println("Trying rollback:");
            trx.rollback();
         }
         catch (Exception e1) {
            System.out.println("Rollback failed!");
            e1.printStackTrace();
         }
         e.printStackTrace();
      }       
      
      try {
         session.save();
      }
      catch (Exception e) {
         e.printStackTrace();
      }
      /*finally {
         try {
            is.close();
         }
         catch (IOException e) {
            e.printStackTrace();
         }
      }*/
      session.logout();   
   }

Is there something wrong with my code? I really appreciate any help!!