reading the value of a document property from within a behavior

守給你的承諾、 提交于 2019-12-13 05:16:32

问题


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>


回答1:


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);
      


来源:https://stackoverflow.com/questions/19136946/reading-the-value-of-a-document-property-from-within-a-behavior

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!