Advertisement
Guest User

Untitled

a guest
Feb 1st, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.21 KB | None | 0 0
  1. package com.millim.ged.behavior;
  2.  
  3. import java.io.Serializable;
  4. import java.text.ParseException;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import java.util.concurrent.ThreadPoolExecutor;
  8. import java.util.concurrent.Executors;
  9.  
  10. import org.alfresco.error.AlfrescoRuntimeException;
  11. import org.alfresco.model.ContentModel;
  12. import org.alfresco.repo.node.NodeServicePolicies;
  13. import org.alfresco.repo.policy.Behaviour;
  14. import org.alfresco.repo.policy.Behaviour.NotificationFrequency;
  15. import org.alfresco.repo.policy.JavaBehaviour;
  16. import org.alfresco.repo.policy.PolicyComponent;
  17. import org.alfresco.repo.security.authentication.AuthenticationUtil;
  18. import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
  19. import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
  20. import org.alfresco.repo.transaction.RetryingTransactionHelper;
  21. import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
  22. import org.alfresco.service.cmr.repository.ChildAssociationRef;
  23. import org.alfresco.service.cmr.repository.ContentService;
  24. import org.alfresco.service.cmr.repository.NodeRef;
  25. import org.alfresco.service.cmr.repository.NodeService;
  26. import org.alfresco.service.cmr.search.SearchService;
  27. import org.alfresco.service.namespace.NamespaceService;
  28. import org.alfresco.service.namespace.QName;
  29. import org.alfresco.service.transaction.TransactionService;
  30. import org.alfresco.util.transaction.TransactionListener;
  31. import org.alfresco.util.transaction.TransactionListenerAdapter;
  32. import org.apache.log4j.Logger;
  33.  
  34. import com.ibm.icu.text.SimpleDateFormat;
  35. import com.millim.ged.model.BillsModel;
  36. import com.millim.ged.model.smartfolder.BillSmartFolder;
  37.  
  38. public class BillFolderBehaviors implements NodeServicePolicies.BeforeCreateNodePolicy,
  39.         NodeServicePolicies.OnCreateNodePolicy, NodeServicePolicies.BeforeDeleteNodePolicy, NodeServicePolicies.OnUpdateNodePolicy {
  40.  
  41.     //private static final String UPDATED_NODE = BillFolderBehaviors.class.getName() + ".updatedNode";
  42.    
  43.     // Dependencies
  44.     private NodeService nodeService;
  45.     private PolicyComponent policyComponent;
  46.     private SearchService searchService;
  47.     private ContentService contentService;
  48.     private TransactionService transactionService;
  49.     private ThreadPoolExecutor threadPoolExecutor;
  50.    
  51.     private TransactionListener transactionListener;
  52.  
  53.     // Behaviours
  54.     private Behaviour beforeCreateNode;
  55.     private Behaviour onCreateNode;
  56.     private Behaviour beforeDeleteNode;
  57.     private Behaviour onUpdateNode;
  58.  
  59.     private Logger logger = Logger.getLogger(BillFolderBehaviors.class);
  60.  
  61.     private final QName A_BILLFOLDER = QName.createQName(BillsModel.NAMESPACE_MILLIM_BILLS_CONTENT_MODEL,
  62.             BillsModel.ASPECT_BIL_BILLFOLDER);
  63.  
  64.     private final QName T_BILL = QName.createQName(BillsModel.NAMESPACE_MILLIM_BILLS_CONTENT_MODEL,
  65.             BillsModel.TYPE_BIL_BILL);
  66.  
  67.     private final QName P_PROVIDER = QName.createQName(BillsModel.NAMESPACE_MILLIM_BILLS_CONTENT_MODEL,
  68.             BillsModel.PROP_PROVIDER);
  69.  
  70.     private final QName P_ID = QName.createQName(BillsModel.NAMESPACE_MILLIM_BILLS_CONTENT_MODEL, BillsModel.PROP_ID);
  71.  
  72.     private final QName P_DATE = QName.createQName(BillsModel.NAMESPACE_MILLIM_BILLS_CONTENT_MODEL,
  73.             BillsModel.PROP_DATE);
  74.  
  75.     public void init() {
  76.         if (logger.isDebugEnabled())
  77.             logger.debug("Initializing BillFolder behaviors");
  78.    
  79.  
  80.         this.transactionListener = new BillTransactionListener();
  81.        
  82.         this.threadPoolExecutor = (ThreadPoolExecutor)Executors.newCachedThreadPool();
  83.  
  84.         // Create behaviours
  85.         this.beforeCreateNode = new JavaBehaviour(this, "beforeCreateNode", NotificationFrequency.EVERY_EVENT);
  86.         this.onCreateNode = new JavaBehaviour(this, "onCreateNode", NotificationFrequency.EVERY_EVENT);
  87.         this.beforeDeleteNode = new JavaBehaviour(this, "beforeDeleteNode", NotificationFrequency.EVERY_EVENT);
  88.         this.onUpdateNode = new JavaBehaviour(this, "onUpdateNode", NotificationFrequency.TRANSACTION_COMMIT);
  89.  
  90.         // Bind behaviours to node policies
  91.         this.policyComponent.bindClassBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "beforeCreateNode"),
  92.                 ContentModel.TYPE_FOLDER, this.beforeCreateNode);
  93.  
  94.         this.policyComponent.bindClassBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "onCreateNode"),
  95.                 ContentModel.TYPE_CONTENT, this.onCreateNode);
  96.  
  97.         this.policyComponent.bindClassBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "beforeDeleteNode"),
  98.                 ContentModel.TYPE_CONTENT, this.beforeDeleteNode);
  99.  
  100.         this.policyComponent.bindClassBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "onUpdateNode"),
  101.                 BillsModel.TYPE_BIL_BILL, this.onUpdateNode);
  102.     }
  103.  
  104.     @Override
  105.     public void beforeCreateNode(NodeRef parentRef, QName assocTypeQName, QName assocQName, QName nodeTypeQName) {
  106.         if (nodeService.hasAspect(parentRef, A_BILLFOLDER)) {
  107.             throw new AlfrescoRuntimeException("Folders are not allowed in Bill Folders");
  108.         }
  109.     }
  110.  
  111.     @Override
  112.     public void onCreateNode(ChildAssociationRef association) {
  113.         NodeRef content = association.getChildRef();
  114.         if (nodeService.hasAspect(association.getParentRef(), A_BILLFOLDER)) {
  115.             AlfrescoTransactionSupport.bindListener(transactionListener);
  116.             this.assignProperties(content);
  117.         }
  118.     }
  119.    
  120.     @Override
  121.     public void beforeDeleteNode(NodeRef content) {
  122.         /*if (0 == nodeService.getType(content).compareTo(T_BILL)) {
  123.             AlfrescoTransactionSupport.bindListener(transactionListener);
  124.         }*/
  125.     }
  126.    
  127.     @Override
  128.     public void onUpdateNode(NodeRef content) {
  129.         if (nodeService.exists(content) && 0 == nodeService.getType(content).compareTo(T_BILL)) {
  130.             this.assignProperties(content);
  131.             AlfrescoTransactionSupport.bindListener(transactionListener);
  132.         }
  133.     }
  134.  
  135.     private class BillTransactionListener extends TransactionListenerAdapter implements TransactionListener {
  136.  
  137.         @Override
  138.         public void afterCommit() {
  139.             try {              
  140.                 Runnable runnable = new JSONUpdate();
  141.                 threadPoolExecutor.execute(runnable);
  142.             } catch (Exception e) {
  143.                 throw new AlfrescoRuntimeException(e.getMessage());
  144.             }
  145.         }
  146.     }
  147.    
  148.     private class JSONUpdate implements Runnable{
  149.  
  150.         @Override
  151.         public void run() {
  152.             AuthenticationUtil.runAsSystem(new RunAsWork<Void>(){
  153.  
  154.                 @Override
  155.                 public Void doWork() throws Exception {
  156.                     RetryingTransactionCallback<Void> callback = new RetryingTransactionCallback<Void>() {
  157.                         @Override
  158.                         public Void execute() throws Throwable {
  159.                             BillSmartFolder bsf = new BillSmartFolder("Dossiers virtuels", searchService, nodeService);
  160.                             bsf.updateJSONFile(contentService);
  161.                             return null;
  162.                         }
  163.                     };
  164.                    
  165.                     try {
  166.                         RetryingTransactionHelper txnHelper =
  167.                             transactionService.getRetryingTransactionHelper();
  168.                             txnHelper.doInTransaction(callback, false, true);
  169.                     } catch (Throwable e) {
  170.                         e.printStackTrace();
  171.                     }                    
  172.                    
  173.                     return null;
  174.                 }
  175.                
  176.             });
  177.         }
  178.        
  179.     }
  180.  
  181.     private void assignProperties(NodeRef node) {
  182.         Map<QName, Serializable> props = new HashMap<QName, Serializable>();
  183.         SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
  184.         String nodeName = (String) nodeService.getProperty(node, ContentModel.PROP_NAME);
  185.         String[] nameParts = nodeName.split("_");
  186.  
  187.         nodeService.setType(node, T_BILL);
  188.  
  189.         props.put(P_PROVIDER, nameParts[0]);
  190.         props.put(P_ID, nameParts[1]);
  191.  
  192.         try {
  193.             props.put(P_DATE, sdf.parse(nameParts[2]));
  194.         } catch (ParseException e) {
  195.             logger.error(e.getMessage());
  196.         }
  197.  
  198.         nodeService.addProperties(node, props);
  199.     }
  200.  
  201.     public void setNodeService(NodeService nodeService) {
  202.         this.nodeService = nodeService;
  203.     }
  204.  
  205.     public void setPolicyComponent(PolicyComponent policyComponent) {
  206.         this.policyComponent = policyComponent;
  207.     }
  208.  
  209.     public void setSearchService(SearchService searchService) {
  210.         this.searchService = searchService;
  211.     }
  212.  
  213.     public void setContentService(ContentService contentService) {
  214.         this.contentService = contentService;
  215.     }
  216.    
  217.     public void setTransactionService(TransactionService transactionService){
  218.         this.transactionService = transactionService;
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement