Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public with sharing class SOM_DealerToMachineConnector {
- public static Boolean IS_RUNNING = false;
- private final SOM_AssetRepository assetRepository;
- private final COM_AccountRepository accountRepository;
- private Map<Id, Set<Id>> topDealerConnections;
- private Map<Id, Set<Id>> subdealerConnections;
- private Map<Id, Set<Id>> dealerBranchConnections;
- private Map<Id, Asset> machines;
- private Map<Id, Account> dealers;
- Map<String, Set<String>> LEVEL_CONNECTION_OPTIONS;
- static {
- LEVEL_CONNECTION_OPTIONS = new Map<String, Set<String>> {
- SOM_Constants.ACCOUNT_PARTY_LEVEL_L1 => new Set<String> {
- SOM_Constants.MACHINE_CONN_TOP_DEALER
- },
- SOM_Constants.ACCOUNT_PARTY_LEVEL_L2 => new Set<String> {
- SOM_Constants.MACHINE_CONN_TOP_DEALER,
- SOM_Constants.MACHINE_CONN_SUB_DEALER
- },
- SOM_Constants.ACCOUNT_PARTY_LEVEL_L3 => new Set<String> {
- SOM_Constants.MACHINE_CONN_TOP_DEALER,
- SOM_Constants.MACHINE_CONN_DEALER_BRANCH
- }
- };
- }
- public SOM_DealerToMachineConnector() {
- this.assetRepository = SOM_AssetRepository.getInstance();
- this.accountRepository = COM_AccountRepository.getInstance();
- this.topDealerConnections = new Map<Id, Set<Id>>();
- this.subdealerConnections = new Map<Id, Set<Id>>();
- this.dealerBranchConnections = new Map<Id, Set<Id>>();
- }
- public void addConnection(Id accountId, Id machineId, String connectionType) {
- if (connectionType == SOM_Constants.MACHINE_CONN_TOP_DEALER) {
- addMachineToConnect(topDealerConnections, accountId, machineId);
- } else if (connectionType == SOM_Constants.MACHINE_CONN_SUB_DEALER) {
- addMachineToConnect(subdealerConnections, accountId, machineId);
- } else if (connectionType == SOM_Constants.MACHINE_CONN_DEALER_BRANCH) {
- addMachineToConnect(dealerBranchConnections, accountId, machineId);
- }
- }
- public void connect() {
- dealers = new Map<Id, Account>(accountRepository.getByIds(getDealerIds()));
- setMachinesFromConnections(SOM_Constants.MACHINE_CONN_TOP_DEALER, topDealerConnections);
- setMachinesFromConnections(SOM_Constants.MACHINE_CONN_SUB_DEALER, subdealerConnections);
- setMachinesFromConnections(SOM_Constants.MACHINE_CONN_DEALER_BRANCH, dealerBranchConnections);
- }
- private void setMachinesFromConnections(String connectionType, Map<Id, Set<Id>> connections) {
- for (Id dealerId : connections.keySet()) {
- Account dealer = dealers.get(dealerId);
- Set<Id> machineIds = connections.get(accountId);
- for (Id machineId : machineIds) {
- Asset machine;
- if (machines.containsKey(machineId)) {
- machine = machines.get(machineId);
- } else {
- machine = new Asset(Id = machineId);
- machines.put(machineId, machine);
- }
- setAccountsOnMachine(connectionType, machine, dealer);
- }
- }
- }
- private void setAccountsOnMachine(String connectionType, Asset machine, Account dealer) {
- if (isValidConnectionTypeForLevel(dealer.PartyLevel__c, connectionType)) {
- if (connectionType == SOM_Constants.MACHINE_CONN_TOP_DEALER) {
- setAccountsForTopDealerConnection(connectionType, machine, dealer);
- } else if (connectionType == SOM_Constants.MACHINE_CONN_SUB_DEALER) {
- setAccountsForSubDealerConnection(machine, dealer);
- } else if (connectionType == SOM_Constants.MACHINE_CONN_DEALER_BRANCH) {
- setAccountsForDealerBranchConnection(machine, dealer);
- }
- } else {
- throw new COM_Exceptions.NotSupportedException('Invalid connection type for party level');
- }
- }
- private void setAccountsForTopDealerConnection(Asset machine, Account dealer) {
- if (dealer.PartyLevel__c == SOM_Constants.ACCOUNT_PARTY_LEVEL_L1) {
- machine.SOM_TopDealer__c = dealer.Id;
- machine.SOM_SubDealer__c = null;
- machine.SOM_DealerBranch__c = null;
- } else if (dealer.PartyLevel__c == SOM_Constants.ACCOUNT_PARTY_LEVEL_L2) {
- machine.SOM_TopDealer__c = getParentIdOnLevelOf(SOM_Constants.ACCOUNT_PARTY_LEVEL_L1, dealer);
- machine.SOM_SubDealer__c = dealer.Id;
- machine.SOM_DealerBranch__c = null;
- } else if (dealer.PartyLevel__c == SOM_Constants.ACCOUNT_PARTY_LEVEL_L3) {
- machine.SOM_TopDealer__c = getParentIdOnLevelOf(SOM_Constants.ACCOUNT_PARTY_LEVEL_L1, dealer);
- machine.SOM_SubDealer__c = getParentIdOnLevelOf(SOM_Constants.ACCOUNT_PARTY_LEVEL_L2, dealer);
- machine.SOM_DealerBranch__c = dealer.Id;
- }
- }
- private setAccountsForSubDealerConnection(Asset machine, Account dealer) {
- if (isSubDealerValid(machine, dealer)) {
- machine.SOM_SubDealer__c = dealer.Id;
- }
- }
- private setAccountsForDealerBranchConnection(Asset machine, Account dealer) {
- if (isDealerBranchValid(machine, dealer)) {
- machine.SOM_DealerBranch__c = dealer.Id;
- }
- }
- private void addMachineToConnect(Map<Id, Set<Id>> connectionsMap, Id accountId, Id machineId) {
- if (!connectionsMap.containsKey(accountId)) {
- connectionsMap.put(accountId, new Set<Id>());
- }
- connectionsMap.get(accountId).add(machineId);
- }
- private Set<Id> getMachineIds() {
- Set<Id> machineIds = new Set<Id>();
- machineIds.addAll(COM_CollectionUtils.flattenMapOfSets(topDealerConnections));
- machineIds.addAll(COM_CollectionUtils.flattenMapOfSets(subdealerConnections));
- machineIds.addAll(COM_CollectionUtils.flattenMapOfSets(dealerBranchConnections));
- return machineIds;
- }
- private Set<Id> getDealerIds() {
- Set<Id> dealerIds = new Set<Id>();
- dealerIds.addAll(topDealerConnections.keySet());
- dealerIds.addAll(subdealerConnections.keySet());
- dealerIds.addAll(dealerBranchConnections.keySet());
- return dealerIds;
- }
- private Boolean isValidConnectionTypeForLevel(String level, String connectionType) {
- return LEVEL_CONNECTION_OPTIONS.containsKey(level)
- && LEVEL_CONNECTION_OPTIONS.get(level).contains(connectionType);
- }
- private Boolean isSubDealerValid(Asset machine, Account dealer) {
- return isDealerInHierarchyOnLevel(machine.SOM_TopDealer__c, SOM_Constants.ACCOUNT_PARTY_LEVEL_L1, dealer);
- }
- private Boolean isDealerBranch(Asset machine, Account dealer) {
- return isDealerInHierarchyOnLevel(machine.SOM_TopDealer__c, SOM_Constants.ACCOUNT_PARTY_LEVEL_L1, dealer)
- && isDealerInHierarchyOnLevelOf(machine.SOM_SubDealer__c, SOM_Constants.ACCOUNT_PARTY_LEVEL_L2, dealer);
- }
- private Boolean isDealerInHierarchyOnLevelOf(Id dealerToCheck, String level, Account dealer) {
- return dealerToCheck != null && dealerToCheck == getParentIdOnLevelOf(level, dealer);
- }
- private Id getParentIdOnLevelOf(String level, Account account) {
- Id parentId = null;
- Account currentAccount = account;
- while (currentAccount.Parent != null && currentAccount.Parent.PartyLevel__c == level) {
- currentAccount = account.Parent;
- }
- return parentId;
- }
- private void performUpdate() {
- IS_RUNNING = true;
- if (!machines.isEmpty()) {
- update machines.values();
- }
- IS_RUNNING = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement