Advertisement
Guest User

Untitled

a guest
Dec 5th, 2011
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.67 KB | None | 0 0
  1. public class IAMSCUController
  2. {  
  3.     // Public Properties
  4.     public String nameSelected { get; set; }
  5.     public String awardLevelSelected { get; set; }
  6.     public String stateSelected { get; set; }
  7.     public String countrySelected { get; set; }
  8.     public String statesEnabled { get; set; }
  9.     public String output { get; set; }
  10.     public List<String> outputs { get; set; }
  11.     private Integer count = 0;
  12.    
  13.     // Private Properties
  14.     private List<Account> schools { get; set; }
  15.     private String result = null;
  16.     private Id schoolRecordTypeId = null;
  17.     private Id addressRecordTypeId = null;
  18.     private Filter filter = new Filter();
  19.    
  20.     // Public Methods
  21.     public List<Account> getIAMSCUSChools()
  22.     {
  23.         count++;
  24.         return conn.getRecords();
  25.     }
  26.  
  27.     // instantiate the StandardSetController from a query locator
  28.     public ApexPages.StandardSetController conn
  29.     {
  30.         get
  31.         {
  32.             if(outputs == null)
  33.             {
  34.                 outputs = new List<String>();
  35.             }
  36.            
  37.             filter.loadFilters(nameSelected, awardLevelSelected, stateSelected, countrySelected);
  38.             output = 'conn is null: ' + (conn == null) + ' changed: ' + filter.changed + ' count: ' + count + ' filters: ' + filter.filterValues;
  39.             outputs.add(output);
  40.            
  41.             if(conn == null || filter.changed)
  42.             {
  43.                 String awardFilter = '';
  44.                 String countryStateFilter = '';
  45.                 String countryFilter = '';
  46.                 String stateFilter = '';
  47.                 String queryFilter = '{0}{1}';
  48.                 String query = '';
  49.        
  50.                 // Award Level
  51.                 if(awardLevelSelected != null && awardLevelSelected != 'Any')
  52.                 {
  53.                     awardFilter = ' AND account.Institution_Type__c = ||' + awardLevelSelected + '||';
  54.                 }
  55.                
  56.                 // - Country & State -
  57.                 countryStateFilter = ' AND account.Id IN (SELECT address.Organization_Name__c FROM Address__c address WHERE address.RecordTypeId = ||' + getOrgLocationRecordTypeId() + '|| {0}{1})';
  58.                
  59.                 // Country
  60.                 if(countrySelected != null && countrySelected != 'Any')
  61.                 {
  62.                     countryFilter = ' AND address.Country__c = ||' + countrySelected + '|| ';
  63.                     countryStateFilter = String.format(countryStateFilter, new String[] { countryFilter });
  64.                 }
  65.                
  66.                 // State
  67.                 if(stateSelected != null && stateSelected != 'Any')
  68.                 {
  69.                     stateFilter = ' AND address.State__c = ||' + stateSelected + '|| ';
  70.                     countryStateFilter = String.format(countryStateFilter, new String[] { stateFilter });
  71.                 }
  72.                
  73.                 if(countryFilter.length() == 0 && stateFilter.length() == 0)
  74.                 {
  75.                     countryStateFilter = '';
  76.                 }
  77.                 else
  78.                 {
  79.                     countryStateFilter = String.format(countryStateFilter, new String[] { countryFilter, stateFilter });
  80.                 }
  81.                
  82.                 queryFilter = String.format(queryFilter, new String[] { awardFilter, countryStateFilter });
  83.                 queryFilter = queryFilter.replace('||', '\'');
  84.    
  85.                 // Complete Query
  86.                 query = 'SELECT account.Id, account.Name FROM Account account WHERE account.RecordTypeId = \'' + getSchoolRecordTypeId() + '\' AND account.Name LIKE \'%' + nameSelected + '%\'' + queryFilter;
  87.                
  88.                 // Query Result
  89.                 conn = new ApexPages.StandardSetController(Database.getQueryLocator(query));
  90.                 conn.setPageSize(2);
  91.             }
  92.            
  93.             return conn;
  94.         }
  95.         set;
  96.     }
  97.    
  98.     public List<SelectOption> getAwardsLevels()
  99.     {
  100.         List<SelectOption> options = new List<SelectOption>();
  101.        
  102.         options.add(new SelectOption('Any','Any'));
  103.         options.add(new SelectOption('Colleges/Universities','Colleges/Universities'));
  104.         options.add(new SelectOption('Pre-Baccalaureate','Pre-Baccalaureate'));
  105.         options.add(new SelectOption('Pre-Collegiate','Pre-Collegiate'));
  106.         options.add(new SelectOption('Professional Schools','Professional Schools'));
  107.         options.add(new SelectOption('Seminary/Theological','Seminary/Theological'));
  108.         options.add(new SelectOption('Two-Year Institutions','Two-Year Institutions'));
  109.        
  110.         return options;
  111.     }
  112.    
  113.     public List<SelectOption> getCountries()
  114.     {
  115.         List<SelectOption> options = new List<SelectOption>();
  116.        
  117.         // Used to created a distinct list of countries
  118.         Set<String> countries = new Set<String>();
  119.        
  120.         // Select all countries that are actually being used.
  121.         for(Address__c[] addresses : [SELECT address.Country__c FROM Address__c address WHERE address.Country__c != '' AND address.RecordTypeId = :getOrgLocationRecordTypeId() AND Organization_Name__c IN (SELECT account.Id FROM Account WHERE account.RecordTypeId = :getSchoolRecordTypeId() AND account.Is_Active__c = 'Yes')])
  122.         {
  123.             for(Address__c address : addresses)
  124.             {    
  125.                 countries.add(address.Country__c);
  126.             }
  127.         }
  128.        
  129.         // Create the options
  130.         options.add(new SelectOption('Any','Any'));
  131.         for(String country : countries)
  132.         {
  133.             options.add(new SelectOption(country, country));
  134.         }
  135.        
  136.         return options;
  137.     }
  138.    
  139.     public List<SelectOption> getStates()
  140.     {
  141.         List<SelectOption> options = new List<SelectOption>();
  142.        
  143.         // Used to created a distinct list of countries
  144.         Set<String> states = new Set<String>();
  145.        
  146.         // Select all countries that are actually being used.
  147.         for(Address__c[] addresses : [SELECT address.State__c FROM Address__c address WHERE address.State__c != '' AND address.RecordTypeId = :getOrgLocationRecordTypeId() AND Organization_Name__c IN (SELECT account.Id FROM Account WHERE account.RecordTypeId = :getSchoolRecordTypeId() AND account.Is_Active__c = 'Yes')])
  148.         {
  149.             for(Address__c address : addresses)
  150.             {    
  151.                 states.add(address.State__c );
  152.             }
  153.         }
  154.  
  155.         // Create the options
  156.         options.add(new SelectOption('Any','Any'));
  157.  
  158.         for(String state : states)
  159.         {
  160.             options.add(new SelectOption(state, state));
  161.         }
  162.        
  163.         return options;
  164.     }
  165.  
  166.     public PageReference invokeService()
  167.     {
  168.         Id id = System.currentPageReference().getParameters().get('id2');
  169.         result = [SELECT Name FROM Account WHERE Id = :id].Name;
  170.        
  171.         return null;
  172.     }
  173.    
  174.     public PageReference invokeService2()
  175.     {
  176.         // Make global configuration chanegs here
  177.         statesEnabled = (countrySelected == 'Any' || countrySelected == 'UNITED STATES OF AMERICA') ? 'true' : 'false';
  178.        
  179.         return null;
  180.     }
  181.    
  182.     // indicates whether there are more records after the current page set.
  183.     public Boolean hasNext {
  184.         get {
  185.             return conn.getHasNext();
  186.         }
  187.         set;
  188.     }
  189.  
  190.     // indicates whether there are more records before the current page set.
  191.     public Boolean hasPrevious {
  192.         get {
  193.             return conn.getHasPrevious();
  194.         }
  195.         set;
  196.     }
  197.  
  198.     // returns the page number of the current page set
  199.     public Integer pageNumber {
  200.         get {
  201.             return conn.getPageNumber();
  202.         }
  203.         set;
  204.     }
  205.  
  206.     // returns the first page of records
  207.     public void first() {
  208.         conn.first();
  209.     }
  210.  
  211.     // returns the last page of records
  212.     public void last() {
  213.         conn.last();
  214.     }
  215.  
  216.     // returns the previous page of records
  217.     public void previous() {
  218.         conn.previous();
  219.     }
  220.  
  221.     // returns the next page of records
  222.     public void next() {
  223.         conn.next();
  224.     }
  225.  
  226.     // returns the PageReference of the original page, if known, or the home page.
  227.     public void cancel() {
  228.         conn.cancel();
  229.     }
  230.    
  231.     // Private Methods
  232.     private String getSchoolRecordTypeId()
  233.     {
  234.          // School Record Type
  235.         if(schoolRecordTypeId == null)
  236.         {
  237.             return String.valueOf([SELECT Id FROM RecordType WHERE Name = 'School' AND SObjectType = 'Account'].Id);
  238.         }
  239.        
  240.         return String.valueOf(schoolRecordTypeId);
  241.     }
  242.    
  243.     private String getOrgLocationRecordTypeId()
  244.     {
  245.         if(addressRecordTypeId == null)
  246.         {
  247.             return String.valueOf([SELECT Id FROM RecordType WHERE Name = 'Organization Location' AND SObjectType = 'Address__c'].Id);
  248.         }
  249.        
  250.         return String.valueOf(addressRecordTypeId);
  251.     }
  252.    
  253.     private class Filter
  254.     {
  255.         public String filterValues { get; private set; }
  256.         public Boolean changed { get; private set; }
  257.        
  258.         public void loadFilters(String name, String awardLevel, String state, String country)
  259.         {
  260.             setFilters(name, awardLevel, state, country);
  261.         }
  262.        
  263.         private void setFilters(String name, String awardLevel, String state, String country)
  264.         {
  265.             String filterFmt = '{0}{1}{2}{3}';
  266.             String newFilterValues = String.format(filterFmt, new String[] { name, awardLevel, state, country });
  267.            
  268.             changed = (filterValues != newFilterValues);
  269.             filterValues = newFilterValues;
  270.         }
  271.     }
  272. }
  273.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement