Deepak Talape
Deepak Talape

Reputation: 997

Update Document Template Dynamically in Alfresco from property page

I have one document template as like attached screen shot. now my requirement is i want to dynamically update some values of that document when i will upload it in alfresco site. i will either create that Document through node template or upload. I want to provide those values from document property page. can anyone help me how can i achieve this.

I tried it using java script, but i am getting some problem. so i am thinking for using Alfresco CMIS API. But i am quite new in alfresco cmis. so can any one tell the better way which i can use for this task.

Thanks in advance.enter image description here

Upvotes: 1

Views: 827

Answers (1)

Akah
Akah

Reputation: 1920

I think you should trigger an custom action by a Rule

When someone update a node (for example by editing properties), your action will be triggered and you will be able to :

  • get the content of your node
  • get the properties of your node
  • use the apache poi library provided by alfresco (my guess is that you use word documents) to find your parameters, and the replace them by the value you find in the property nodes.

Here is an excellent tutorial to learn how to create a custom action.

Note : You may have to keep the original templated document if you want to change a second time your properties (by using versionning for example).

Edit (see discussion below) :

I assume you know how to use rules in alfresco : http://docs.alfresco.com/5.0/tasks/library-folder-rules-define.html

  1. Declare a new action to assign to a rule. This action must be triggered when the excel is dropped or updated

    1. Create an action implementing your need :

      public class MyAction extends ActionExecuterAbstractBase {
      ...
      
          @Override
          protected void executeImpl(final Action arg0, final NodeRef arg1) {
              // your code here
          }
      ....
      }
      

      This action will :

      1. Take the nodeRef parameter (which is the excel file) and load the file

        ...
        Inputstream inputstream = getFileFolderService().getReader(arg1).getContentInputStream();
        ...
        NPOIFSFileSystem fs = null;
        HSSFWorkbook workbook = null;
        try {
        fs = new NPOIFSFileSystem(inputstream);
        workbook = new HSSFWorkbook(fs.getRoot(), true);
        //get your data with POI
        
      2. For each row of your excel :

        1. Make a copy of your template :

          • find it :

            getSearchService().query(searchParameters)
            
          • copy it :

            getFileFolderService().copy(sourceNodeRef, targetParentRef, newName)
            
        2. Do transformations of your new word content (Find occurrences and replace with poi library).
        3. Update the content of your new file :

          ContentWriter writerDoc = getContentService().getWriter(document.getNodeRef(), ContentModel.PROP_CONTENT,
                  true);
          writerDoc.putContent(file/inputStream);
          
    2. In your context file, declare you action :

      <bean id="my-action" class="x.y.z.MyAction" parent="action-executer">
          <property name="searchService">
              <ref bean="searchService" />
          </property>
          <property name="nodeService">
              <ref bean="nodeService" />
          </property>
          <property name="contentService">
              <ref bean="contentService" />
          </property>
          <property name="fileFolderService">
              <ref bean="FileFolderService" />
          </property>
          ....
      </bean>
      
  2. In Share, assign to a folder a rule with the action you have created.

Upvotes: 2

Related Questions