Advertisement
Guest User

Untitled

a guest
Jan 24th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.73 KB | None | 0 0
  1. public with sharing class SOM_DealerToMachineConnector {
  2.  
  3.     public static Boolean IS_RUNNING = false;
  4.    
  5.     private final SOM_AssetRepository assetRepository;
  6.     private final COM_AccountRepository accountRepository;
  7.  
  8.     private Map<Id, Set<Id>> topDealerConnections;
  9.     private Map<Id, Set<Id>> subdealerConnections;
  10.     private Map<Id, Set<Id>> dealerBranchConnections;
  11.  
  12.     private Map<Id, Asset> machines;
  13.     private Map<Id, Account> dealers;
  14.  
  15.     Map<String, Set<String>> LEVEL_CONNECTION_OPTIONS;
  16.  
  17.     static {
  18.         LEVEL_CONNECTION_OPTIONS = new Map<String, Set<String>> {
  19.             SOM_Constants.ACCOUNT_PARTY_LEVEL_L1 => new Set<String> {
  20.                 SOM_Constants.MACHINE_CONN_TOP_DEALER
  21.             },
  22.             SOM_Constants.ACCOUNT_PARTY_LEVEL_L2 => new Set<String> {
  23.                 SOM_Constants.MACHINE_CONN_TOP_DEALER,
  24.                 SOM_Constants.MACHINE_CONN_SUB_DEALER
  25.             },
  26.             SOM_Constants.ACCOUNT_PARTY_LEVEL_L3 => new Set<String> {
  27.                 SOM_Constants.MACHINE_CONN_TOP_DEALER,
  28.                 SOM_Constants.MACHINE_CONN_DEALER_BRANCH
  29.             }
  30.         };
  31.     }
  32.  
  33.     public SOM_DealerToMachineConnector() {
  34.         this.assetRepository = SOM_AssetRepository.getInstance();
  35.         this.accountRepository = COM_AccountRepository.getInstance();
  36.         this.topDealerConnections = new Map<Id, Set<Id>>();
  37.         this.subdealerConnections = new Map<Id, Set<Id>>();
  38.         this.dealerBranchConnections = new Map<Id, Set<Id>>();
  39.     }
  40.    
  41.     public void addConnection(Id accountId, Id machineId, String connectionType) {
  42.         if (connectionType == SOM_Constants.MACHINE_CONN_TOP_DEALER) {
  43.             addMachineToConnect(topDealerConnections, accountId, machineId);
  44.         } else if (connectionType == SOM_Constants.MACHINE_CONN_SUB_DEALER) {
  45.             addMachineToConnect(subdealerConnections, accountId, machineId);
  46.         } else if (connectionType == SOM_Constants.MACHINE_CONN_DEALER_BRANCH) {
  47.             addMachineToConnect(dealerBranchConnections, accountId, machineId);
  48.         }
  49.     }
  50.  
  51.     public void connect() {
  52.         dealers = new Map<Id, Account>(accountRepository.getByIds(getDealerIds()));
  53.         setMachinesFromConnections(SOM_Constants.MACHINE_CONN_TOP_DEALER, topDealerConnections);
  54.         setMachinesFromConnections(SOM_Constants.MACHINE_CONN_SUB_DEALER, subdealerConnections);
  55.         setMachinesFromConnections(SOM_Constants.MACHINE_CONN_DEALER_BRANCH, dealerBranchConnections);
  56.     }
  57.  
  58.     private void setMachinesFromConnections(String connectionType, Map<Id, Set<Id>> connections) {
  59.         for (Id dealerId : connections.keySet()) {
  60.             Account dealer = dealers.get(dealerId);
  61.             Set<Id> machineIds = connections.get(accountId);
  62.             for (Id machineId : machineIds) {
  63.                 Asset machine;
  64.                 if (machines.containsKey(machineId)) {
  65.                     machine = machines.get(machineId);
  66.                 } else {
  67.                     machine = new Asset(Id = machineId);
  68.                     machines.put(machineId, machine);
  69.                 }
  70.                 setAccountsOnMachine(connectionType, machine, dealer);
  71.             }
  72.         }
  73.     }
  74.  
  75.     private void setAccountsOnMachine(String connectionType, Asset machine, Account dealer) {
  76.         if (isValidConnectionTypeForLevel(dealer.PartyLevel__c, connectionType)) {
  77.             if (connectionType == SOM_Constants.MACHINE_CONN_TOP_DEALER) {
  78.                 setAccountsForTopDealerConnection(connectionType, machine, dealer);
  79.             } else if (connectionType == SOM_Constants.MACHINE_CONN_SUB_DEALER) {
  80.                 setAccountsForSubDealerConnection(machine, dealer);
  81.             } else if (connectionType == SOM_Constants.MACHINE_CONN_DEALER_BRANCH) {
  82.                 setAccountsForDealerBranchConnection(machine, dealer);
  83.             }
  84.         } else {
  85.             throw new COM_Exceptions.NotSupportedException('Invalid connection type for party level');
  86.         }
  87.     }
  88.  
  89.     private void setAccountsForTopDealerConnection(Asset machine, Account dealer) {
  90.         if (dealer.PartyLevel__c == SOM_Constants.ACCOUNT_PARTY_LEVEL_L1) {
  91.             machine.SOM_TopDealer__c = dealer.Id;
  92.             machine.SOM_SubDealer__c = null;
  93.             machine.SOM_DealerBranch__c = null;
  94.         } else if (dealer.PartyLevel__c == SOM_Constants.ACCOUNT_PARTY_LEVEL_L2) {
  95.             machine.SOM_TopDealer__c = getParentIdOnLevelOf(SOM_Constants.ACCOUNT_PARTY_LEVEL_L1, dealer);
  96.             machine.SOM_SubDealer__c = dealer.Id;
  97.             machine.SOM_DealerBranch__c = null;
  98.         } else if (dealer.PartyLevel__c == SOM_Constants.ACCOUNT_PARTY_LEVEL_L3) {
  99.             machine.SOM_TopDealer__c = getParentIdOnLevelOf(SOM_Constants.ACCOUNT_PARTY_LEVEL_L1, dealer);
  100.             machine.SOM_SubDealer__c = getParentIdOnLevelOf(SOM_Constants.ACCOUNT_PARTY_LEVEL_L2, dealer);
  101.             machine.SOM_DealerBranch__c = dealer.Id;
  102.         }
  103.     }
  104.  
  105.     private setAccountsForSubDealerConnection(Asset machine, Account dealer) {
  106.         if (isSubDealerValid(machine, dealer)) {
  107.             machine.SOM_SubDealer__c = dealer.Id;
  108.         }
  109.     }
  110.  
  111.     private setAccountsForDealerBranchConnection(Asset machine, Account dealer) {
  112.         if (isDealerBranchValid(machine, dealer)) {
  113.             machine.SOM_DealerBranch__c = dealer.Id;
  114.         }
  115.     }
  116.  
  117.     private void addMachineToConnect(Map<Id, Set<Id>> connectionsMap, Id accountId, Id machineId) {
  118.         if (!connectionsMap.containsKey(accountId)) {
  119.             connectionsMap.put(accountId, new Set<Id>());
  120.         }
  121.         connectionsMap.get(accountId).add(machineId);  
  122.     }
  123.  
  124.     private Set<Id> getMachineIds() {
  125.         Set<Id> machineIds = new Set<Id>();
  126.         machineIds.addAll(COM_CollectionUtils.flattenMapOfSets(topDealerConnections));
  127.         machineIds.addAll(COM_CollectionUtils.flattenMapOfSets(subdealerConnections));
  128.         machineIds.addAll(COM_CollectionUtils.flattenMapOfSets(dealerBranchConnections));
  129.         return machineIds;
  130.     }
  131.  
  132.     private Set<Id> getDealerIds() {
  133.         Set<Id> dealerIds = new Set<Id>();
  134.         dealerIds.addAll(topDealerConnections.keySet());
  135.         dealerIds.addAll(subdealerConnections.keySet());
  136.         dealerIds.addAll(dealerBranchConnections.keySet());
  137.         return dealerIds;
  138.     }
  139.  
  140.     private Boolean isValidConnectionTypeForLevel(String level, String connectionType) {
  141.         return LEVEL_CONNECTION_OPTIONS.containsKey(level)
  142.             && LEVEL_CONNECTION_OPTIONS.get(level).contains(connectionType);
  143.     }
  144.  
  145.     private Boolean isSubDealerValid(Asset machine, Account dealer) {
  146.         return isDealerInHierarchyOnLevel(machine.SOM_TopDealer__c, SOM_Constants.ACCOUNT_PARTY_LEVEL_L1, dealer);
  147.     }
  148.  
  149.     private Boolean isDealerBranch(Asset machine, Account dealer) {
  150.         return isDealerInHierarchyOnLevel(machine.SOM_TopDealer__c, SOM_Constants.ACCOUNT_PARTY_LEVEL_L1, dealer)
  151.             && isDealerInHierarchyOnLevelOf(machine.SOM_SubDealer__c, SOM_Constants.ACCOUNT_PARTY_LEVEL_L2, dealer);
  152.     }
  153.  
  154.     private Boolean isDealerInHierarchyOnLevelOf(Id dealerToCheck, String level, Account dealer) {
  155.         return dealerToCheck != null && dealerToCheck == getParentIdOnLevelOf(level, dealer);
  156.     }
  157.  
  158.     private Id getParentIdOnLevelOf(String level, Account account) {
  159.         Id parentId = null;
  160.         Account currentAccount = account;
  161.         while (currentAccount.Parent != null && currentAccount.Parent.PartyLevel__c == level) {
  162.             currentAccount = account.Parent;
  163.         }
  164.         return parentId;
  165.     }
  166.  
  167.     private void performUpdate() {
  168.         IS_RUNNING = true;
  169.         if (!machines.isEmpty()) {
  170.             update machines.values();
  171.         }
  172.         IS_RUNNING = false;
  173.     }
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement