Guest User

Untitled

a guest
Jan 18th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. //a controller class to get incidents for display on VF page and then
  2. //anonymise the incidents and related contacts
  3. public class incidentAnonController {
  4.  
  5. public Incident__c firstDate{get; set;}
  6. public Incident__c lastDate{get; set;}
  7.  
  8. public incidentAnonController(ApexPages.StandardController controller){
  9. Integer dy = System.today().day();
  10. Integer mo = System.today().month();
  11. Integer yr = System.today().year();
  12. firstDate = new Incident__c(Date_Of_Incident__c = Date.newInstance(yr, mo, dy).addMonths(-6));
  13. lastDate = new Incident__c(Date_Of_Incident__c = Date.newInstance(yr, mo, dy).addMonths(-12));
  14. }
  15.  
  16. //method to anonymise relevant records
  17. public void anonymise() {
  18. //list of incidents older than 6 months. These will be anonymised.
  19. List<Incident__c> results = [SELECT Id, Incident_name__c, Date_of_incident__c, Name, Garda_Name__c
  20. FROM Incident__c WHERE Date_of_Incident__c <= :firstDate.Date_Of_Incident__c AND
  21. Date_of_Incident__c >= :lastDate.Date_Of_Incident__c];
  22.  
  23.  
  24. //anonymise incidents that are older than 6 months
  25. for (Incident__c inc : results) {
  26. inc.Incident_name__c = inc.Name;
  27. inc.Garda_Name__c = '';
  28. inc.Pulse_ID__c = '';
  29. }
  30. update results;
  31.  
  32. @isTest
  33. public class incidentAnonTest {
  34.  
  35. public static testMethod void incidentAnon() {
  36. Integer dy = System.today().day();
  37. Integer mo = System.today().month();
  38. Integer yr = System.today().year();
  39.  
  40. //create an incident
  41. Incident__c inc1 = new Incident__c();
  42. inc1.Date_Of_Incident__c = Date.newInstance(yr, mo, dy).addMonths(-7);
  43. inc1.Incident_name__c = 'Incident 1';
  44. inc1.Garda_Name__c = 'Test Garda';
  45. insert inc1;
  46.  
  47. PageReference pageRef = Page.AnonymiseIncidents;
  48. Test.setCurrentPage(pageRef);
  49.  
  50. ApexPages.StandardController sc = new ApexPages.StandardController(inc1);
  51. incidentAnonController testInc = new incidentAnonController(sc);
  52.  
  53. inc1 = [SELECT Id, Garda_Name__c, Date_of_Incident__c, FROM Incident__c WHERE Id = :inc1.Id];
  54.  
  55. testInc.anonymise();
  56.  
  57. System.debug(inc1.Date_Of_Incident__c);
  58. System.debug(inc1.Garda_Name__c);
  59.  
  60. System.assertEquals(TRUE, String.isBlank(inc1.Garda_Name__c));
  61.  
  62. }
  63.  
  64. <apex:page standardController="Incident__c" extensions="incidentAnonController" docType="html-5.0">
  65. <apex:form >
  66.  
  67. <apex:pageBlock title="Notes on Usage">
  68. <apex:pageBlockSection >
  69. //some content
  70. </apex:pageBlockSection>
  71. </apex:pageBlock>
  72.  
  73. <apex:pageBlock title="Date Ranges for Anonymisation">
  74. <apex:pageBlockSection >
  75. <apex:inputField value="{!firstDate.Date_Of_Incident__c}" label="From (Default 6 Months ago)"/>
  76. <apex:inputField value="{!lastDate.Date_Of_Incident__c}" label="To (Default 1 year ago)"/>
  77. </apex:pageBlockSection>
  78. </apex:pageBlock>
  79.  
  80. <apex:pageBlock title="List of Incidents">
  81. <apex:pageBlockTable value="{!incidents}" var="inc">
  82. <apex:column headerValue="Incident Number">
  83. <apex:outputLink value="/{!inc.id}">{!inc.Name}</apex:outputLink>
  84. </apex:column>
  85. <apex:column value="{!inc.Incident_Name__c}"/>
  86. <apex:column value="{!inc.Date_Of_Incident__c}"/>
  87. </apex:pageBlockTable>
  88.  
  89. <apex:pageBlockButtons >
  90. <apex:commandButton action="{!anonymise}" value="Anonymise"/>
  91. <apex:commandButton onclick="location.reload();" value="Update List"/>
  92. </apex:pageBlockButtons>
  93.  
  94. </apex:pageBlock>
  95. </apex:form>
Add Comment
Please, Sign In to add comment