daily pastebin goal
12%
SHARE
TWEET

Untitled

a guest Dec 8th, 2017 20 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.tls.powergrid;
  2.  
  3.  
  4. import java.io.Serializable;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8.  
  9.  
  10. import org.activiti.engine.delegate.DelegateExecution;
  11. import org.activiti.engine.delegate.DelegateTask;
  12. import org.activiti.engine.delegate.ExecutionListener;
  13. import org.activiti.engine.delegate.TaskListener;
  14. import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
  15. import org.activiti.engine.impl.context.Context;
  16. import org.alfresco.error.AlfrescoRuntimeException;
  17. import org.alfresco.model.ContentModel;
  18. import org.alfresco.repo.workflow.WorkflowNotificationUtils;
  19. import org.alfresco.repo.workflow.activiti.ActivitiConstants;
  20. import org.alfresco.repo.workflow.activiti.ActivitiScriptNode;
  21. import org.alfresco.service.ServiceRegistry;
  22. import org.alfresco.service.cmr.repository.ChildAssociationRef;
  23. import org.alfresco.service.cmr.repository.NodeRef;
  24. import org.alfresco.service.cmr.repository.NodeService;
  25. import org.alfresco.service.namespace.QName;
  26. import org.apache.commons.httpclient.HttpClient;
  27. import org.apache.commons.httpclient.HttpStatus;
  28. import org.apache.commons.httpclient.methods.GetMethod;
  29. import org.apache.commons.httpclient.methods.PostMethod;
  30. import org.apache.commons.httpclient.methods.StringRequestEntity;
  31. import org.json.JSONObject;
  32.  
  33.  
  34. public class StamponDocument implements ExecutionListener, TaskListener{
  35.    
  36.  
  37.  
  38.     @Override
  39.     public void notify(DelegateTask task) {
  40.         // TODO Auto-generated method stub
  41.         System.out.println("HI Delagate task");
  42.    
  43.         System.out.println(task.getVariable("vendordocumentreview_approvalcategory"));
  44.     }
  45.  
  46.     @Override
  47.     public void notify(DelegateExecution execution) throws Exception {
  48.        
  49.        
  50.     ServiceRegistry serviceRegistry=getServiceRegistry();
  51.    
  52.     NodeService nodeService=serviceRegistry.getNodeService();
  53.        
  54.         System.out.println("HI Delagate Execution");
  55.         System.out.println(execution.getVariable("vendordocumentreview_approvalcategory"));
  56.          ActivitiScriptNode scriptNode = (ActivitiScriptNode)execution.getVariable("bpm_package");
  57.          
  58.          
  59.          
  60.          
  61.          ActivitiScriptNode scriptNode1 = (ActivitiScriptNode)execution.getVariable(WorkflowNotificationUtils.PROP_PACKAGE);              
  62.          NodeRef packagenode = scriptNode1.getNodeRef();
  63.          
  64.          
  65.          NodeRef docRef= nodeService.getChildAssocs(packagenode).get(0).getChildRef();
  66.          
  67.          String nodename=docRef.getId();
  68.  
  69.          
  70.         System.out.println("Node Name:"+nodename);
  71.  
  72.          final NodeRef workflowPackageNodeRef = scriptNode.getNodeRef();
  73.                  
  74.          final List<ChildAssociationRef> resource = nodeService.getChildAssocs(workflowPackageNodeRef);
  75.          
  76.          for(int i=0;i<resource.size();i++){
  77.              
  78.              ChildAssociationRef childNodeRef=resource.get(i);
  79.              
  80.              NodeRef  nodeRefs=childNodeRef.getChildRef();
  81.              
  82.            
  83.              
  84.             // String nodeRef=node.getId();
  85.              
  86.              
  87.             // NodeRef node_details=new NodeRef(nodeRef);
  88.              System.out.println("List Node Ref:"+nodeRefs);
  89.              
  90.              if (! nodeService.exists(nodeRefs)) {
  91.                    throw new AlfrescoRuntimeException("Sorry, " + nodeRefs + " doesn't exist");
  92.                }
  93.  
  94.                // Fetch all the properties
  95.                Map<QName, Serializable> props = nodeService.getProperties(nodeRefs);
  96.  
  97.                // Fetch a few common properties
  98.                String nodeName = (String)props.get(ContentModel.PROP_NAME);
  99.                
  100.                System.out.println("File Name"+nodeName);
  101.                
  102.                String title = (String)props.get(ContentModel.PROP_TITLE);
  103.                System.out.println("Title Name"+title);
  104.                
  105.              /*  String nodeRef=(String) props.get(ContentModel.PROP_NODE_REF);*/
  106.                
  107.                System.out.println("NodeRef"+nodeRefs.toString());
  108.              
  109.              
  110.              //Frame the Url     
  111.                
  112.                String ticket=getLoginTicket();
  113.                
  114.     String resp=getUrlResponse(nodeRefs.toString(),nodeName,ticket);
  115.              
  116.      System.out.println("Api Response:"+resp);
  117.              
  118.          }
  119.  
  120.  
  121.     }
  122.  
  123.     protected ServiceRegistry getServiceRegistry() {
  124.        
  125.         System.out.println("NodeService Object");
  126.         ProcessEngineConfigurationImpl config = Context.getProcessEngineConfiguration();
  127.         if (config != null) {
  128.             // Fetch the registry that is injected in the activiti spring-configuration
  129.             ServiceRegistry registry = (ServiceRegistry) config.getBeans().get(ActivitiConstants.SERVICE_REGISTRY_BEAN_KEY);
  130.  
  131.             if (registry == null) {
  132.                 throw new RuntimeException("Service-registry not present in ProcessEngineConfiguration beans, expected ServiceRegistry with key" + ActivitiConstants.SERVICE_REGISTRY_BEAN_KEY);
  133.             }
  134.             System.out.println("Registry Object"+registry.SERVICE_REGISTRY);
  135.             return registry;
  136.         }
  137.         throw new IllegalStateException("No ProcessEngineCOnfiguration found in active context");
  138.     }
  139.    
  140.    
  141.     //Call the api to add the empty pdf page
  142.    
  143.     public String getUrlResponse(String nodeRef,String nodeName,String ticket)
  144.     {
  145.        
  146.         String urlLink="http://localhost:8080/alfresco/service/pdftoolkit/powergrid/InsertPdfPage?alf_ticket="+ticket;
  147.    
  148.        
  149.          System.out.println("Url Is:"+urlLink);
  150.         // {"nodeRef":"workspace://SpacesStore/f6468074-0919-4b3c-9cdd-35077236fe80","paramPage":"1","x":"200","y":"200","font":"80.f","paramPosition":"center","paramWaterMarkerText":"Approved,Karma"}
  151.          String response = null;
  152.          HttpClient client1 = new HttpClient();
  153.        
  154.          PostMethod createSitePost1 = new PostMethod(urlLink);
  155.  
  156.          //Pass the Request Body Data as json payload
  157.         JSONObject siteObject = new JSONObject();
  158.          try {
  159.              
  160.             // {"page":"1","inplace":"true","insert-content":"workspace://SpacesStore/aaef66c0-7130-4ce8-8113-a87b458d64fe","nodeRef":"workspace://SpacesStore/609b75b0-ee39-4e2b-9500-ef6e6b6a22ee","destination-folder":"workspace://SpacesStore/609b75b0-ee39-4e2b-9500-ef6e6b6a22ee","destination-name":"474054-400kv 4TTT(T1)(SH.1 OF 1).pdf"}
  161.  
  162.              
  163.          siteObject.put("nodeRef",nodeRef);
  164.          siteObject.put("paramPage", "1");
  165.          siteObject.put("x", "100");
  166.          siteObject.put("y","100");
  167.          siteObject.put("font","80.f");
  168.          siteObject.put("paramPosition","center");
  169.          siteObject.put("paramWaterMarkerText","Approved,Karma");
  170.        
  171.          createSitePost1.setRequestHeader("Content-Type", "application/json");
  172.          createSitePost1.setRequestHeader("Accept", "application/json");
  173.              createSitePost1.setRequestEntity(new StringRequestEntity(siteObject.toString(), "application/json", "UTF-8"));
  174.  
  175.          int status = client1.executeMethod(createSitePost1);
  176.          System.out.println("create a site script status :: " + status);
  177.  
  178.          if (status == HttpStatus.SC_OK) {
  179.  
  180.          System.out.println("Site created OK");
  181.          response=createSitePost1.getResponseBodyAsString();
  182.  
  183.          } else {
  184.  
  185.  
  186.  
  187.          System.out.println("There is a problem of while Inserting Page");
  188.          }
  189.          } catch (Exception err) {
  190.          err.printStackTrace();
  191.          }
  192.                
  193.                
  194.          
  195.        
  196.         return response;
  197.        
  198.     }  
  199.    
  200.    
  201.     /*public String getLink(String ticket) {
  202.            StringBuffer sb = new StringBuffer();
  203.            sb.append("\r\n");
  204.            sb.append("http://localhost:8080/alfresco/service/pdftoolkit/powergrid/InsertPdfPage?alf_ticket="+ticket);
  205.            sb.append("&guest=true");
  206.            sb.append("\r\n\r\n");
  207.            return sb.toString();
  208.        }
  209. */
  210.    
  211.    
  212.     public String getLoginTicket(){
  213.         String body = null;
  214.       String username="admin";
  215.       String password="admin";
  216.         HttpClient client = new HttpClient();
  217.         String url="http://localhost:8080/alfresco/service/api/login?u="+username+"&pw="+password;
  218.         System.out.println(url);
  219.         GetMethod createSitePost = new GetMethod(url);
  220.         try {
  221.            
  222.             createSitePost.setRequestHeader("Accept", "application/json");
  223.             int status = client.executeMethod(createSitePost);
  224.            
  225.             System.out.println("create a site script status :: " + status);
  226.             if (status == HttpStatus.SC_OK) {
  227.                 System.out.println("logged in");
  228.              body=createSitePost.getResponseBodyAsString();
  229.                 body=body.substring(body.indexOf("<ticket>")+8,body.indexOf("</"));
  230.                 System.out.println("Site created OK  "+body);
  231.             }
  232.         }catch(Exception e){
  233.            
  234.             e.printStackTrace();
  235.         }
  236.         return body;
  237.     }
  238.  
  239. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top