Advertisement
Guest User

protectAttachmentclass

a guest
Sep 14th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.06 KB | None | 0 0
  1. public class Protect2 {
  2.    
  3.     //method to process regular attachments
  4.     //+will create separate constructors for chatter FeedItem attachments
  5.     //+will split this method into multiple methods to avoid duplicating code
  6.     public static void ProcessAttachments (List<Attachment> incomingList) {
  7.  
  8.         //create list of IDs to store all parentIDs for the incoming attachments
  9.         List<id> protectedIds = new List <Id>();
  10.        
  11.         FOR (Attachment ilatt : incomingList){
  12.             protectedIds.add(ilatt.ParentId);
  13.         }
  14.  
  15.         //bind query to list of parentIDs
  16.         //this will ensure that we will never hit the 50,000 limit on SOQL query results
  17.         List<ProcessInstance> approvedProcesses = new List<ProcessInstance> ([SELECT TargetObjectId
  18.                                          FROM ProcessInstance where Status = 'Approved' AND TargetObject.Type = 'Vendor_Invoice__c' AND TargetObjectId IN : protectedIds]);
  19.        
  20.         //create a set of parentIDs to evaluate against using set contains method
  21.         Set<Id> protectedRecords = new Set<Id>();
  22.             FOR(ProcessInstance pi : approvedProcesses){
  23.                 protectedRecords.add(pi.TargetObjectId);
  24.             }
  25.        
  26.         //iterate over the list of incoming attachments to see if the attachments ParentID is in the list of Protected Records
  27.         //still evaluating against the full query
  28.         List<Attachment> protectedAttachments = new List<Attachment>();
  29.         FOR (Attachment attach : incominglist){
  30.             IF(protectedRecords.contains(attach.ParentId)){
  31.                 protectedAttachments.add(attach);
  32.                 }
  33.         }
  34.      
  35.      //call killSwitch on protected attachments if size > 0
  36.         IF(protectedAttachments.size()>0){
  37.         attachmentKillSwitch(protectedAttachments);
  38.             }    
  39.     }
  40.    
  41.     public static void processFeedItems(List<FeedItem> incomingList){
  42.         //add code to process feed items
  43.            List<id> protectedIds = new List <Id>();
  44.        
  45.           FOR (FeedItem ilatt : incomingList){
  46.             protectedIds.add(ilatt.ParentId);
  47.         }
  48.        
  49.         //bind query to list of parentIDs
  50.         //this will ensure that we will never hit the 50,000 limit on SOQL query results
  51.         List<ProcessInstance> approvedProcesses = new List<ProcessInstance> ([SELECT TargetObjectId
  52.                                          FROM ProcessInstance where Status = 'Approved' AND TargetObject.Type = 'Vendor_Invoice__c' AND TargetObjectId IN : protectedIds]);
  53.        
  54.         //create a set of parentIDs to evaluate against using set contains method
  55.         Set<Id> protectedRecords = new Set<Id>();
  56.             FOR(ProcessInstance pi : approvedProcesses){
  57.                 protectedRecords.add(pi.TargetObjectId);
  58.             }
  59.        
  60.         //iterate over the list of incoming attachments to see if the attachments ParentID is in the list of Protected Records
  61.         //still evaluating against the full query
  62.         List<FeedItem> protectedAttachments = new List<FeedItem>();
  63.         FOR (FeedItem attach : incominglist){
  64.             IF(protectedRecords.contains(attach.ParentId)){
  65.                 protectedAttachments.add(attach);
  66.                 }
  67.         }
  68.      
  69.      //call killSwitch on protected attachments if size > 0
  70.         IF(protectedAttachments.size()>0){
  71.         attachmentKillSwitch(protectedAttachments);
  72.             }    
  73.     }  
  74.    
  75.     public static void processContentDocument (List<ContentDocument> incomingList)
  76.     {
  77.      /*
  78.  
  79.    //add code to process feed items
  80.            List<id> protectedIds = new List <Id>();
  81.        
  82.           FOR (ContentDocument ilatt : incomingList){
  83.             protectedIds.add(ilatt.ParentId);
  84.         }
  85.        
  86.         //bind query to list of parentIDs
  87.         //this will ensure that we will never hit the 50,000 limit on SOQL query results
  88.         List<ProcessInstance> approvedProcesses = new List<ProcessInstance> ([SELECT TargetObjectId
  89.                                          FROM ProcessInstance where Status = 'Approved' AND TargetObject.Type = 'Vendor_Invoice__c' AND TargetObjectId IN : protectedIds]);
  90.        
  91.         //create a set of parentIDs to evaluate against using set contains method
  92.         Set<Id> protectedRecords = new Set<Id>();
  93.             FOR(ProcessInstance pi : approvedProcesses){
  94.                 protectedRecords.add(pi.TargetObjectId);
  95.             }
  96.        
  97.         //iterate over the list of incoming contentDocuments to see if the ParentID is in the list of Protected Records
  98.         List<FeedItem> protectedAttachments = new List<FeedItem>();
  99.         FOR (FeedItem attach : incominglist){
  100.             IF(protectedRecords.contains(attach.ParentId)){
  101.                 protectedAttachments.add(attach);
  102.                 }
  103.         }
  104.      
  105.      //call killSwitch on protected attachments if size > 0
  106.         IF(protectedAttachments.size()>0){
  107.         attachmentKillSwitch(protectedAttachments);
  108.             }    
  109.     }  
  110.    
  111.  
  112.  
  113.      */
  114.        
  115.        
  116.     }
  117.    
  118.     //Kill switch method adds error for all attachments that come to it
  119.     //dont know if having this as a static method makes sense?
  120.     public static void attachmentKillSwitch (List<Attachment> violators) {
  121.        
  122.         User_Controls__c CS = User_Controls__c.getInstance();
  123.        
  124.         FOR (attachment vAtt : violators){
  125.             system.debug('attachment on approved object - Id: '+vatt.ParentId);
  126.             IF(!CS.Attachments_Allow_Delete__c){
  127.             vAtt.addError('bogus - deleting attachments on approved records is not allowed');
  128.             }
  129.         }
  130.     }  
  131.    
  132.     public static void attachmentKillSwitch (List<FeedItem> violators) {
  133.        
  134.         User_Controls__c CS = User_Controls__c.getInstance();
  135.        
  136.         FOR (FeedItem vAtt : violators){
  137.             system.debug('FeedItem attachment on approved object - Id: '+vatt.ParentId);
  138.             IF(!CS.Attachments_Allow_Delete__c){
  139.             vAtt.addError('bogus - deleting attachments on approved records is not allowed');
  140.             }
  141.         }
  142.     }  
  143.    
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement