0

question description and code was updated

Question 1: would be with what do I replace the dummy int attachmentid = 123; in the code below in order to read custom property sc:OpenERPattachmentID1 to get the id value stored in it? (Question 1 was Answered by alfrescian!)

package com.openerp.behavior;



import java.util.List;
import java.net.*;
import java.io.*;


import org.alfresco.repo.node.NodeServicePolicies;

import org.alfresco.repo.policy.Behaviour;

import org.alfresco.repo.policy.JavaBehaviour;

import org.alfresco.repo.policy.PolicyComponent;

import org.alfresco.repo.policy.Behaviour.NotificationFrequency;

import org.alfresco.repo.security.authentication.AuthenticationUtil;

import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;

import org.alfresco.service.cmr.repository.ChildAssociationRef;

import org.alfresco.service.cmr.repository.NodeRef;

import org.alfresco.service.cmr.repository.NodeService;

import org.alfresco.service.namespace.NamespaceService;

import org.alfresco.service.namespace.QName;

import org.alfresco.service.transaction.TransactionService;

import org.apache.log4j.Logger;



//import com.openerp.model.scOpenERPModel;

public class DeleteAsset implements NodeServicePolicies.BeforeDeleteNodePolicy  {



    private PolicyComponent policyComponent;

    private Behaviour beforeDeleteNode;
    private NodeService nodeService;



    public void init() {

        this.beforeDeleteNode = new JavaBehaviour(this,"beforeDeleteNode",NotificationFrequency.EVERY_EVENT);

        this.policyComponent.bindClassBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI,"beforeDeleteNode"), 

                QName.createQName(scOpenERPModel.NAMESPACE,scOpenERPModel.ASSET_CONTENT_TYPE), this.beforeDeleteNode);

    }
    public setNodeService(NodeService nodeService){
           this.nodeService = nodeService;  
        }




    @Override

    public void beforeDeleteNode(NodeRef node) {

        System.out.println("beforeDeleteNode!");

        try {
            QName attachmentID1= QName.createQName("http://www.someco.com/model/content/1.0", "OpenERPattachmentID1"); // this could/shoul be defined in your OpenERPModel-class
            int attachmentid = (Integer)nodeService.getProperty(node, attachmentID1);
            //int attachmentid = 123;
            URL oracle = new URL("http://0.0.0.0:1885/delete/%20?attachmentid=" + attachmentid);
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                        yc.getInputStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null) 
            //System.out.println(inputLine);
                in.close(); 

        } catch(Exception e) {

            e.printStackTrace();

        }



    }

}

Question 2: where do I put the DeleteAsset.class?

I'm a Java and Alfresco novice, I'd be great if someone could tell me if alfresco-4.2.c/tomcat/webapps/alfresco/WEB-INF/classes/com/openerp/behavior/ is the right folder to put the compiled DeleteAsset.class

Question 3: What should I put in NAMESPACE and ASSET_CONTENT_TYPE? I'd like to work without the model class as I haven't had a tutorial on that yet, what do I replace scOpenERPModel.NAMESPACE,scOpenERPModel.ASSET_CONTENT_TYPE with?

This is my full custom-web-context file:

<?xml version='1.0' encoding='UTF-8'?>
 <!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN'
 'http://www.springframework.org/dtd/spring-beans.dtd'>

 <beans>
<!-- Registration of new models -->
<bean id="smartsolution.dictionaryBootstrap" parent="dictionaryModelBootstrap"
 depends-on="dictionaryBootstrap">
    <property name="models">
        <list>
                <value>alfresco/extension/scOpenERPModel.xml</value>
        </list>
    </property>
</bean>

<!-- deletion of attachments within openERP when delete is initiated in Alfresco-->
<bean id="deletionBehavior" class="com.openerp.behavior.DeleteAsset" init-method="init">
    <property name="nodeService">
        <ref bean="nodeService" />
    </property>
    <property name="policyComponent">
        <ref bean="policyComponent" />
    </property>
</bean>

MrHappy
  • 81
  • 1
  • 1
  • 10
  • When should your behaviour be triggered? At the moment it's binded to MyModel.ASSET_CONTENT_TYPE is that what you like to achieve? – alfrescian Oct 04 '13 at 09:45
  • @alfrescian it should be triggered the moment before the document is delete from alfresco, so an ID number stored in one of it's properties can be read and passed via an url to a webservice on local host. – MrHappy Oct 04 '13 at 11:07
  • I mean which type of node are your interested in -> Which content type do your docs have? – alfrescian Oct 04 '13 at 12:45
  • @alfrescian ` OpenERP Document cm:content` taken over from jeff potts' tutorial on types and aspects – MrHappy Oct 06 '13 at 12:11

1 Answers1

3

Well, you have a long way to go...what do you like to achieve with your "oracle" connection?

To answer your main questions: How to read a property:

  1. Don't put the XML Model into com/openerp/model/scOpenERPModel - it should be a java class that defines constants to access your custom types, aspects & props (example: https://svn.alfresco.com/repos/alfresco-open-mirror/alfresco/HEAD/root/projects/data-model/source/java/org/alfresco/model/ContentModel.java) But that is not mandatory - it just helps you.

  2. To read the property

    1. inject NodeService:

      private NodeService nodeService;
      public setNodeService(NodeService nodeService){
         this.nodeService = nodeService;  
      }
      
    2. in your beforeDeleteNode

      QName attachmentID1= QName.createQName("your sc NS uri", "OpenERPattachmentID1"); // this could/shoul be defined in your OpenERPModel-class
      int attachmentid = (Integer) nodeService.getProperty(node, attachmentID1);
      
alfrescian
  • 4,079
  • 1
  • 18
  • 20
  • I'm visiting an url that's connected to a web.py webservice. This webservice uses that ID to delete the reference to the deleted doc in an external database. That part is working since a long time btw xD I couldn't find a good tutorial/documentation concerning how to do the above though – MrHappy Oct 04 '13 at 10:40
  • I'm cooking right now but I'll implement your suggestions today. Could you tell me if I need to delete `import com.openerp.model.scOpenERPModel` from my code then? Also I'm not sure what you mean with `your sc NS uri` =S – MrHappy Oct 04 '13 at 11:04
  • you have to define an model uri in your content scOpenERPModel.xml - that's the URI you need – alfrescian Oct 04 '13 at 12:47
  • ah namespace uri `` so in my case `QName attachmentID1= QName.createQName("http://www.someco.com/model/content/1.0", "OpenERPattachmentID1");` – MrHappy Oct 06 '13 at 12:15
  • ARGH IT WONT FIND THE DAMN CLASS `Error loading class [com.openerp.behavior.DeleteAsset] for bean with name 'deletionBehavior' defined in URL [file:/home/openerp/alfresco-4.2.c/tomcat/shared/classes/alfresco/extension/custom-web-context.xml]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: com/openerp/behavior/DeleteAsset (wrong name: DeleteAsset)` – MrHappy Oct 06 '13 at 15:21
  • is alfresco-4.2.c/tomcat/webapps/alfresco/WEB-INF/classes/com/openerp/behavior even correct? it's in there as compiled class damn it "DeleteAsset.class" is the name of the file, I took it from the bin folder of the eclipse workspace – MrHappy Oct 06 '13 at 15:23
  • I think that if you could tell me what to replace `QName.createQName(scOpenERPModel.NAMESPACE,scOpenERPModel.ASSET_CONTENT_TYPE)` with and where to put the compiled DeleteAsset.class I'd probably be done – MrHappy Oct 06 '13 at 15:54
  • I did place the `public setNodeService` correctly in the updated code above right? – MrHappy Oct 06 '13 at 16:55
  • are you familar with the concept of Dependy Injection? Did you updated your *-context.xml – alfrescian Oct 09 '13 at 07:27
  • The bean was already added to the question when you asked, I've now added the full context file instead just to be sure, please have a look at Question 2 and Question 3 above, I'm kinda running late with this :p – MrHappy Oct 11 '13 at 04:29
  • are alfresco uri and alfresco namespace the same by any chance? – MrHappy Oct 16 '13 at 08:15
  • The "Class" not the file was probably missing, being corrupted by my dumb self by not adding the dependencies. I think I also got the QName things right now so finally accepting the answer. – MrHappy Oct 25 '13 at 12:26