Advertisement
Guest User

FindNearby

a guest
Jul 23rd, 2014
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 22.07 KB | None | 0 0
  1. public class LocalizationMap {
  2.    
  3.     public Boolean searchLeads { get; set; }
  4.     public Boolean searchAccounts { get; set; }
  5.     public Boolean searchContacts { get; set; }
  6.     public Boolean OnlyMyRecords{ get; set; }
  7.     public Boolean pAccounts = false;  
  8.    
  9.     public Boolean ListAccounts = false;
  10.     public Boolean ListLeads = false;  
  11.     public Boolean ListContacts = false;  
  12.    
  13.     public String distance;
  14.     public Boolean needFilters{ get; set;}
  15.    
  16.     public List<MapItem> locs = new List<MapItem>();
  17.     public List<MapItem> destinations = new List<MapItem>();
  18.     public MapItem startingPoint { get; set;}
  19.     public String StartingAddress { get; set;}
  20.    
  21.     public Map<String, MapItem> locationMap = new Map<String, MapItem>{};
  22.     public List<SelectOption> AccountTypeOptions = new List<SelectOption>();
  23.     public List<SelectOption> AccountIndustryOptions = new List<SelectOption>();
  24.    
  25.     public List<MapItem> getDestinations(){
  26.         return destinations;
  27.     }
  28.    
  29.     public String getDistance(){
  30.         return distance + '';
  31.     }
  32.    
  33.     public String[] AccountTypes = new String[]{'All'};
  34.     public String[] AccountIndustries = new String[]{'All'};
  35.     public String[] LeadStatuses = new String[]{'All'};
  36.    
  37.     public void setAccountTypes(String[] s){ AccountTypes = s;}
  38.     public String[] getAccountTypes(){return AccountTypes;}
  39.     public void setAccountIndustries(String[] s){ AccountIndustries = s;}
  40.     public String[] getAccountIndustries(){return AccountIndustries;}
  41.     public void setLeadStatuses(String[] s){ LeadStatuses = s;}
  42.     public String[] getLeadStatuses(){return LeadStatuses;}
  43.     public boolean LeadsAvailable, ContactsAvailable, AccountsAvailable;  
  44.    
  45.     /////////////////////////////////
  46.     public final List<Account> leads;
  47.     public string testLead { get; set; }
  48.     public string testAccount { get; set; }
  49.     public string testContact { get; set; }
  50.    
  51.     public LocalizationMap() {
  52.         leads = [select Id, Geolocation__c from Account WHERE DISTANCE(Geolocation__c, GEOLOCATION(37.775,-122.418), 'mi') < 20 Limit 50];
  53.     }
  54.  
  55.     public List<Account> getLeads() {
  56.         return leads;
  57.     }
  58.     //////////////////////////////
  59.    
  60.     public boolean getLeadsAvailable(){        
  61.         /*try{
  62.             //FindNearby__c FNA = FindNearby__c.getOrgDefaults();
  63.             LeadsAvailable = FNA.Leads__c;
  64.         }
  65.         catch(Exception e){
  66.             LeadsAvailable=false;
  67.         }*/
  68.         LeadsAvailable = true;
  69.         return LeadsAvailable;
  70.     }
  71.    
  72.     public boolean getContactsAvailable(){
  73.         /*try{
  74.             //FindNearby__c FNA = FindNearby__c.getOrgDefaults();
  75.             ContactsAvailable = FNA.Contacts__c;
  76.         }
  77.         catch(Exception e){ContactsAvailable=false;}
  78.         */
  79.         ContactsAvailable = true;
  80.         return ContactsAvailable;
  81.     }
  82.    
  83.     public boolean getAccountsAvailable(){
  84.         /*try{
  85.             //FindNearby__c FNA = FindNearby__c.getOrgDefaults();
  86.             AccountsAvailable = FNA.Accounts__c;
  87.         }
  88.         catch(Exception e){
  89.             AccountsAvailable=false;
  90.         }*/
  91.         AccountsAvailable = true;
  92.         return AccountsAvailable;
  93.     }
  94.    
  95.     public PageReference init(){
  96.        
  97.         //testLead = [select Name from Lead WHERE Id = '00Q7000000XnxY1'];
  98.         //testAccount = [select Name from Account WHERE Id = '0017000000TgyYc'];
  99.         //testContact = [select Name from Contact WHERE Id = '005700000015Q77'];
  100.        
  101.         //reset variables to default.
  102.         needFilters = true;
  103.         distance = '25';
  104.        
  105.         searchLeads = getLeadsAvailable();
  106.        
  107.         onlyMyRecords = true;
  108.        
  109.         searchAccounts = getAccountsAvailable();
  110.        
  111.         searchContacts = getContactsAvailable();
  112.        
  113.         HandleListViews();
  114.        
  115.         setAccountTypeOptions();
  116.         //hasPersonAccounts();
  117.         setAccountIndustryOptions();
  118.        
  119.         return setStartingPoint();
  120.        
  121.     }
  122.    
  123.     public void HandleListViews(){
  124.         handleAccountListView();
  125.         handleLeadListView();
  126.         handleContactListView();
  127.     }
  128.    
  129.    
  130.     public void handleContactListView(){
  131.         String clids = ApexPages.currentPage().getParameters().get('clids');
  132.         List<String> cids = new List<String>();
  133.         if(clids !='null' && clids !=null){
  134.                 cids.addAll(clids.split(','));          
  135.                 needFilters = false;
  136.                 try{
  137.                     List<Contact> ContactList= [Select Title, Phone, OwnerId, OtherStreet, OtherState, OtherPostalCode, OtherPhone, OtherCountry, OtherCity, Name, Owner.Name, Account.Type,
  138.                                                      MailingStreet, MailingState, Account.Industry,
  139.                                                      MailingPostalCode, MailingCountry, MailingCity, Id, Email
  140.                                                      From Contact
  141.                                                     where id in :cids];
  142.                
  143.                     for(Contact a: ContactList){
  144.                         locs.add(new MapItem(a));
  145.                     }
  146.                     listContacts = true;
  147.                 }
  148.                 catch(Exception e){
  149.                
  150.                 }
  151.         }
  152.     }
  153.    
  154.     public void handleLeadListView(){
  155.         String llids = ApexPages.currentPage().getParameters().get('llids');
  156.         List<String> lids = new List<String>();
  157.         if(llids !='null' && llids !=null){
  158.                 lids.addAll(llids.split(','));          
  159.                 needFilters = false;
  160.                 try{
  161.                     List<Lead> leadList= [Select OwnerId, Phone,Status,Industry, PostalCode, State, Street, City, Country, Name,Owner.Name
  162.                                           from Lead
  163.                                           where id in :lids];
  164.                
  165.                     for(Lead a: leadList){
  166.                         locs.add(new MapItem(a));
  167.                     }
  168.                     ListLeads = true;
  169.                 }
  170.                 catch(Exception e){
  171.                
  172.                 }
  173.         }
  174.     }
  175.    
  176.     public void handleAccountListView(){
  177.             String Alids = ApexPages.currentPage().getParameters().get('alids');
  178.             List<String> ids = new List<String>();
  179.             if(Alids !='null' && Alids !=null){
  180.                 ids.addAll(Alids.split(','));          
  181.                 needFilters = false;
  182.                
  183.                 try{
  184.                     List<Account> asd = [Select  OwnerId, Id,  Name, Phone,
  185.                         BillingStreet, BillingCity,BillingState, BillingPostalCode, BillingCountry,
  186.                         ShippingStreet,ShippingCity,ShippingState,Type, Industry,
  187.                         ShippingPostalCode,ShippingCountry, Owner.Name from Account
  188.                         where id in :ids ];
  189.                
  190.                     for(Account a: asd){
  191.                         locs.add(new MapItem(a));
  192.                     }
  193.                     ListAccounts = true;
  194.                 }
  195.                 catch(Exception e){
  196.                
  197.                 }
  198.             }
  199.     }
  200.    
  201.    
  202.     public void setDistance(String d){
  203.         try{
  204.             Decimal a = Decimal.valueOf(d);
  205.             if(a > 200){
  206.                 distance = '25';
  207.                 ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Distance must be less than 200 miles');
  208.                 ApexPages.addMessage(myMsg);
  209.                
  210.             }
  211.             else{
  212.                 distance = d;
  213.             }
  214.         }
  215.         catch(Exception e){
  216.             //If it's not a decimal.
  217.             distance = '25';
  218.         }
  219.    
  220.        
  221.     }
  222.    
  223.    
  224.     public PageReference setStartingPoint(){
  225.         /*if(locs.size() == 0 && !needFilters){
  226.             return Page.MapError_BadListView;
  227.         }
  228.         else if(!needFilters){*/          
  229.             startingPoint = locs[0];
  230.             startingPoint.rName= 'List View';
  231.         //}
  232.        
  233.         if(needFilters){
  234.             String sID = ApexPages.currentPage().getParameters().get('lid');
  235.            
  236.             /*if(sID =='null'||sID ==null){
  237.                 sID = ApexPages.currentPage().getParameters().get('aid');
  238.                
  239.                 if(sID !='null' && sID !=null){
  240.                     Account a = MapUtility.getSingleAccountToPlot(sID);
  241.                     startingPoint = new MapItem(a);
  242.                 }
  243.                 else{
  244.                     sID = ApexPages.currentPage().getParameters().get('cid');
  245.                     if(sID !='null' && sID !=null){
  246.                         Contact c = MapUtility.getSingleContactToPlot(sID,pAccounts);
  247.                         startingPoint = new MapItem(c);
  248.                     }
  249.                 }
  250.             }
  251.             else{
  252.                 Lead l = MapUtility.getSingleLeadToPlot(sID);
  253.                 startingPoint = new MapItem(l);
  254.             }*/
  255.         }
  256.    
  257.         /*
  258.         if(startingPoint == null)
  259.         {
  260.             return Page.MapError_NoStartingPoint;
  261.         }*/
  262.          
  263.         return null;
  264.     }
  265.    
  266.     public List<MapItem> getLocations(){
  267.         locs = new List<MapItem>();
  268.        
  269.         locs.addAll(getNearbyThings(startingPoint, Decimal.valueOf(distance), 'lead'));
  270.         /*try{
  271.             if(needFilters){
  272.                 if(searchAccounts){
  273.                     locs.addAll(MapUtility.getNearbyAccounts(startingPoint, Decimal.valueOf(distance)));
  274.                 }
  275.                 if(searchLeads){
  276.                     locs.addAll(MapUtility.getNearbyLeads(startingPoint, Decimal.valueOf(distance)));
  277.                 }
  278.                 if(searchContacts){
  279.                     locs.addAll(MapUtility.getNearbyContacts(startingPoint, Decimal.valueOf(distance)));
  280.                 }
  281.                 filterLocs();
  282.                 sortLocs();
  283.             }
  284.             else{
  285.                 handleListViews();
  286.             }
  287.            
  288.             makeMap();
  289.         }
  290.         catch(Exception e){
  291.             sortLocs();
  292.             makeMap();
  293.         }*/
  294.         return locs;
  295.     }
  296.        
  297.     /*
  298.     Instead of Customizing the Query... which can get cumbersome.
  299.     Query more - Remove After the fact.
  300.    
  301.     */
  302.     public void filterLocs(){
  303.         filterMine();
  304.         filterIndustry();
  305.         filterAccountType();
  306.         filterLeadStatuses();
  307.     }
  308.        
  309.     public void filterIndustry(){
  310.         //Check if ALL has been selected.
  311.         Boolean all=false;
  312.         for(String i: AccountIndustries){
  313.             if(i == 'All'){
  314.                 all = true;
  315.             }
  316.         }
  317.        
  318.         if(!all){
  319.             List<MapItem> ls = new List<MapItem>();
  320.             for(Integer k=0; k<locs.size(); k++){
  321.            
  322.                 for(String ind : AccountIndustries){
  323.                     if(ind.equals(locs[k].rIndustry)){
  324.                         ls.add(locs[k]);
  325.                        
  326.                     }//Are they equal?
  327.                 }//check for EACH indsutry value.
  328.             }//the list of locations
  329.             locs = ls;  
  330.         }//If we are filtering
  331.     }
  332.    
  333.     public void filterAccountType(){
  334.         //Check if ALL has been selected.
  335.         Boolean all=false;
  336.         for(String i: AccountTypes){
  337.             if(i == 'All'){
  338.                 all = true;
  339.             }
  340.         }
  341.        
  342.         if(!all){
  343.             List<MapItem> ls = new List<MapItem>();
  344.             for(Integer k=0; k<locs.size(); k++){
  345.            
  346.                 for(String ind : AccountTypes){
  347.                     if(locs[k].rType=='Account' && ind.equals(locs[k].rOne)){
  348.                         ls.add(locs[k]);
  349.                        
  350.                     }//Are they equal?
  351.                    if(locs[k].rType != 'Account'){
  352.                         ls.add(locs[k]);
  353.                    }
  354.                 }//check for EACH indsutry value.
  355.             }//the list of locations
  356.             locs = ls;  
  357.         }//If we are filtering
  358.     }
  359.    
  360.     public void filterLeadStatuses(){
  361.         //Check if ALL has been selected.
  362.         Boolean all=false;
  363.         for(String i: LeadStatuses){
  364.             if(i == 'All'){
  365.                 all = true;
  366.             }
  367.         }
  368.        
  369.         if(!all){
  370.             List<MapItem> ls = new List<MapItem>();
  371.             for(Integer k=0; k<locs.size(); k++){
  372.            
  373.                 for(String ind : LeadStatuses){
  374.                     if(locs[k].rType=='Lead' && ind.equals(locs[k].rOne)){
  375.                         ls.add(locs[k]);
  376.                        
  377.                     }//Are they equal?
  378.                    if(locs[k].rType != 'Lead'){
  379.                         ls.add(locs[k]);
  380.                    }
  381.                 }//check for EACH indsutry value.
  382.             }//the list of locations
  383.             locs = ls;  
  384.         }//If we are filtering
  385.     }
  386.    
  387.     public void makeMap(){
  388.         for(MapItem a: locs){
  389.             locationMap.put(a.rId,a);  
  390.         }
  391.     }
  392.    
  393.     /*
  394.     Is the record owned by me?
  395.     */
  396.     public void filterMine(){
  397.         if(onlyMyRecords){
  398.             for(Integer k=0; k<locs.size(); k++)
  399.             {  
  400.                 //If you find the main account.
  401.                if(locs[k].rOwnerId != UserInfo.getUserId())
  402.                {
  403.                    Locs.remove(k);
  404.                }
  405.             }
  406.         }
  407.     }
  408.     /*
  409.     This function goes through the final Filtered list, and makes sure the Starting Point is #1
  410.     */
  411.     public void sortLocs(){
  412.          for(Integer k=0; k<locs.size(); k++)
  413.          {  
  414.            //If you find the main account.
  415.            if(locs[k].rId == startingPoint.rId)
  416.            {
  417.                Locs.remove(k);
  418.                break;
  419.            }
  420.         }
  421.         //If that was the only account found... then you have to add it with different syntax.    
  422.         if(Locs.isEmpty()){
  423.             //startingPoint.rName = 'e:None Returned';
  424.             Locs.add(startingPoint);
  425.         }
  426.         else{Locs.add(0,startingPoint);}
  427.          
  428.     }
  429.    
  430.     public pageReference Search(){
  431.         getLocations();
  432.         return null;
  433.     }
  434.    
  435.    
  436.    
  437.    
  438.     public PageReference goBackToRecord(){
  439.         PageReference p;
  440.         if(needFilters){
  441.             p = new PageReference('/'+startingPoint.rId);
  442.         }
  443.         else if (ListAccounts){
  444.             p = new PageReference('/001/o');
  445.         }
  446.         else if (ListContacts){
  447.             p = new PageReference('/003/o');
  448.         }
  449.         else if (ListLeads){
  450.             p = new PageReference('/00Q/o');
  451.         }
  452.         return p;
  453.     }
  454.     /*
  455.     public Boolean hasPersonAccounts(){
  456.         pAccounts =  MapUtility.hasPersonAccounts();
  457.         return pAccounts;
  458.     }*/
  459.    
  460.     /*
  461.         Set the Account Type Options from the UI
  462.     */
  463.     public void setAccountTypeOptions(){
  464.         Schema.DescribeFieldResult F = Account.Type.getDescribe();
  465.         List<Schema.PicklistEntry> P = F.getPicklistValues();
  466.    
  467.         AccountTypeOptions.add(new SelectOption('All','All'));
  468.    
  469.         for(integer k =0; k< P.size(); k++)
  470.         {
  471.             if(P[k].isActive())
  472.             {
  473.                AccountTypeOptions.add(new SelectOption(P[k].getLabel(),P[k].getLabel()));
  474.             }
  475.         }  
  476.     }
  477.    
  478.     /*
  479.         get the list of all Account Types
  480.     */
  481.     public List<SelectOption> getAccountTypeOptions(){
  482.        if(AccountTypeOptions == null){
  483.             setAccountTypeOptions();
  484.        }
  485.          
  486.        return AccountTypeOptions;
  487.     }
  488.    
  489.    
  490.     /*
  491.         Get the list of all Account Industries.
  492.     */
  493.     public List<SelectOption> getAccountIndustryOptions(){
  494.        if(AccountIndustryOptions == null){
  495.             setAccountIndustryOptions();
  496.        }
  497.        return AccountIndustryOptions;
  498.     }
  499.    
  500.     /*
  501.         Creates the list of all Account Industries.
  502.     */
  503.     public void setAccountIndustryOptions(){
  504.         Schema.DescribeFieldResult F = Account.Industry.getDescribe();
  505.         List<Schema.PicklistEntry> P = F.getPicklistValues();
  506.    
  507.         AccountIndustryOptions.add(new SelectOption('All','All'));
  508.    
  509.         for(integer k =0; k< P.size(); k++)
  510.         {
  511.             if(P[k].isActive())
  512.             {
  513.                AccountIndustryOptions.add(new SelectOption(P[k].getLabel(),P[k].getLabel()));
  514.             }
  515.         }
  516.     }
  517.    
  518.    
  519.     /*
  520.         Creates drop down of Distances
  521.     */  
  522.     public List<SelectOption> getDistances(){
  523.         List<SelectOption> distances = new List<SelectOption>{new SelectOption('1','1'),
  524.                 new SelectOption('5','5'),new SelectOption('10','10'), new SelectOption('15','15'),
  525.                 new SelectOption('20','20'), new SelectOption('25','25'), new SelectOption('50','50'), new SelectOption('75','75'),
  526.                 new SelectOption('100','100'),new SelectOption('150','150')
  527.         };
  528.       return distances;
  529.     }
  530.    
  531.     /*
  532.         Create the Lead Status Options List
  533.     */
  534.     public List<SelectOption> getLeadStatusOptions(){
  535.             List<SelectOption> items = new List<SelectOption>();
  536.             Schema.DescribeFieldResult F = Lead.Status.getDescribe();
  537.             List<Schema.PicklistEntry> P = F.getPicklistValues();
  538.        
  539.             items.add(new SelectOption('All','All'));
  540.        
  541.             for(integer k =0; k< P.size(); k++)
  542.             {
  543.                 if(P[k].isActive())
  544.                 {
  545.                    items.add(new SelectOption(P[k].getLabel(),P[k].getLabel()));
  546.                 }
  547.             }
  548.              return items;
  549.     }
  550.    
  551.     /*
  552.         returns the number of locations on the map
  553.     */
  554.     public Integer getCount(){
  555.         if(locs == null)
  556.         {
  557.             getLocations();
  558.         }
  559.         return locs.size();
  560.     }
  561.     public static List<MapItem> getNearbyThings(MapItem startingPoint, Decimal d, String type){
  562.                          
  563.         List<Account> Accounts = new List<Account>();
  564.         List<Lead> Leads = new List<Lead>();
  565.         List<Contact> Contacts = new List<Contact>();
  566.         List<MapItem> items = new List<MapItem>();
  567.         //Integer k =0;
  568.        
  569.         try{
  570.             //List<Decimal> latLons = getBoundingBox(startingPoint, d);
  571.            
  572.             if(type == 'account'){
  573.                 String q = 'Select OwnerId, Id, Name, FN__Lat__c, FN__Lon__c, Phone, BillingStreet, BillingCity, BillingState, BillingPostalCode,' +
  574.                         'BillingCountry, ShippingStreet, ShippingCity, ShippingState,Type, Industry, ShippingPostalCode,ShippingCountry, Owner.Name' +
  575.                             'FROM Account' +
  576.                             'WHERE DISTANCE(' + String.valueOf(startingPoint) + ', GEOLOCATION(37.775,-122.418/*FN__Lat__c, FN__Lon__c*/), \'km\') < ' + String.valueOf(d);
  577.                 Accounts = Database.query(q);
  578.                
  579.                     /*Accounts = [Select  OwnerId, Id, Name, FN__Lat__c, FN__Lon__c, Phone, BillingStreet, BillingCity, BillingState, BillingPostalCode,
  580.                                 BillingCountry, ShippingStreet, ShippingCity, ShippingState,Type, Industry, ShippingPostalCode,ShippingCountry, Owner.Name
  581.                             FROM Account
  582.                             WHERE DISTANCE(startingPoint, GEOLOCATION(FN__Lat__c, FN__Lon__c), 'km') < 20
  583.                         limit :getObjectLimit()
  584.                      ];*/
  585.             }
  586.             else if(type == 'lead'){
  587.                 String q = 'Select  OwnerId, /*Mapping_Status__c,*/ Id, Name, FN__Lat__c, FN__Lon__c, Phone, ' +
  588.                            'Address, Owner.Name ' +
  589.                            'FROM Lead WHERE DISTANCE(' + String.valueOf(startingPoint) +', GEOLOCATION(37.775, -122.417), \'km\') < ' + String.valueOf(d);
  590.                 /*Leads = [Select  OwnerId, /*Mapping_Status__c, Id, Name, FN__Lat__c, FN__Lon__c, Phone, /*BillingStreet, BillingCity, BillingState, BillingPostalCode,
  591.                                 BillingCountry, Address, /*Which_Address__c, ShippingStreet, ShippingCity, ShippingState, Type, Industry, ShippingPostalCode, ShippingCountry, Owner.Name
  592.                         from Lead
  593.                         where DISTANCE(startingPoint, GEOLOCATION(37.775, -122.417/*FN__Lat__c, FN__Lon__c), 'km') < 20
  594.                          ((lat__c >= :latLons[0] and lon__c >= :latLons[1]
  595.                              and lat__c <= :latLons[2]  and lon__c <= :latLons[3])
  596.                              or Id = :EncodingUtil.urlEncode(startingPoint.rId,'UTF-8'))
  597.                         limit :getObjectLimit()
  598.                      ];*/
  599.             }
  600.             else if(type == 'contact'){
  601.                 Contacts = [Select  OwnerId, /*Mapping_Status__c,*/ Id, Name, /*Lat__c, Lon__c,*/ Phone, /*BillingStreet, BillingCity, BillingState, BillingPostalCode,*/
  602.                                 /*BillingCountry, Which_Address__c, ShippingStreet, ShippingCity, ShippingState,*/ OtherAddress, /*Type, Industry, ShippingPostalCode, ShippingCountry,*/ Owner.Name
  603.                         from Contact
  604.                         where DISTANCE(startingPoint, GEOLOCATION(37.775, -122.416/*FN__Lat__c, FN__Lon__c*/), 'km') < 20
  605.                         //limit :getObjectLimit()
  606.                      ];
  607.             }
  608.             //Get all leads nearby
  609.    
  610.             //Convert the leads to MapItems
  611.             if(type == 'account'){
  612.                 for(Account a: Accounts){
  613.                     items.add(new MapItem(a));
  614.                 }
  615.             }
  616.             else if(type == 'lead'){
  617.                 for(Lead l: Leads){
  618.                     items.add(new MapItem(l));
  619.                 }
  620.             }
  621.             else if(type == 'contact'){
  622.                 for(Contact c: Contacts){
  623.                     items.add(new MapItem(c));
  624.                 }
  625.             }
  626.         }
  627.         //If Something went wrong, just return the Starting Point
  628.         catch(Exception e){
  629.             items.add(startingPoint);
  630.         }
  631.         return items;
  632.     }
  633. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement