protected void deleteOldAccountLinks(Account account, List<? extends Long> newAccountIds) { List<? extends AccountLink> allLinks = objectManager.find(AccountLink.class, ACCOUNT_LINK.ACCOUNT_ID, account.getId(), ACCOUNT_LINK.REMOVED, null); for (AccountLink link : allLinks) { if (!newAccountIds.contains(link.getLinkedAccountId())) { objectProcessManager.scheduleStandardProcessAsync(StandardProcess.REMOVE, link, null); } } }
@Override public void doWithLockNoResult() { List<? extends AccountLink> allLinks = objectManager.find(AccountLink.class, ACCOUNT_LINK.ACCOUNT_ID, account.getId(), ACCOUNT_LINK.REMOVED, null, ACCOUNT_LINK.STATE, new Condition(ConditionType.NE, CommonStatesConstants.REMOVING)); List<Long> newLinks = new ArrayList<>(); for (AccountLink aLink : allLinks) { newLinks.add(aLink.getLinkedAccountId()); } List<Long> existingLinks = DataAccessor.fieldLongList(account, AccountConstants.FIELD_ACCOUNT_LINKS); if (existingLinks.containsAll(newLinks) && newLinks.containsAll(existingLinks)) { return; } objectManager.setFields(account, AccountConstants.FIELD_ACCOUNT_LINKS, newLinks); ObjectUtils.publishChanged(eventService, objectManager, account); } });
account.getId(), ACCOUNT_LINK.REMOVED, null, ACCOUNT_LINK.LINKED_ACCOUNT_ID, new Condition(ConditionType.NOTNULL)); for (AccountLink accountLink : accountLinks) { Long accountId = accountLink.getLinkedAccountId(); accounts.put(accountId, allAccountsMap.get(accountId));
private void registerTargetServices(Service service) { List<Long> targetAccountIds = new ArrayList<>(); targetAccountIds.add(service.getAccountId()); // add all accounts that are linked to service's account List<? extends AccountLink> linkedAccounts = objectManager.find(AccountLink.class, ACCOUNT_LINK.ACCOUNT_ID, service.getAccountId(), ACCOUNT_LINK.REMOVED, null, ACCOUNT_LINK.LINKED_ACCOUNT_ID, new Condition(ConditionType.NOTNULL)); for (AccountLink linkedAccount : linkedAccounts) { targetAccountIds.add(linkedAccount.getLinkedAccountId()); } List<Service> targetServices = new ArrayList<>(); for (Long accountId : targetAccountIds) { targetServices.addAll(objectManager.find(Service.class, SERVICE.ACCOUNT_ID, accountId, SERVICE.REMOVED, null)); } for (Service targetService : targetServices) { // skip itself if (targetService.getId().equals(service.getId())) { continue; } if (isSelectorLinkMatch(service.getSelectorLink(), targetService)) { addServiceLink(service, targetService); } } }
@SuppressWarnings("unchecked") private void updateServices(AccountLink accountLink) { List<? extends ServiceConsumeMap> consumeMaps = objectManager.find(ServiceConsumeMap.class, SERVICE_CONSUME_MAP.ACCOUNT_ID, accountLink.getAccountId(), SERVICE_CONSUME_MAP.REMOVED, null); List<? extends Service> services = objectManager.find(Service.class, SERVICE.ACCOUNT_ID, accountLink.getLinkedAccountId(), SERVICE.REMOVED, null); List<Long> serviceIds = (List<Long>) CollectionUtils.collect(services, TransformerUtils.invokerTransformer("getId")); for (ServiceConsumeMap consumeMap : consumeMaps) { if (serviceIds.contains(consumeMap.getConsumedServiceId())) { objectProcessManager.scheduleStandardProcessAsync(StandardProcess.REMOVE, consumeMap, null); } } }
@Override public HandlerResult handle(ProcessState state, ProcessInstance process) { AccountLink accountLink = (AccountLink) state.getResource(); if (accountLink.getLinkedAccountId() == null) { regionSvc.deleteExternalAccountLink(accountLink); } else { updateServices(accountLink); Account account = objectManager.loadResource(Account.class, accountLink.getAccountId()); if (account == null) { return null; } regenerateAccountLinks(account); } return null; }
@Override public HandlerResult handle(ProcessState state, ProcessInstance process) { AccountLink accountLink = (AccountLink) state.getResource(); if (accountLink.getLinkedAccountId() == null) { regionSvc.createExternalAccountLink(accountLink); } else { List<? extends Service> services = objectManager.find(Service.class, SERVICE.ACCOUNT_ID, accountLink.getAccountId(), SERVICE.SELECTOR_LINK, new Condition(ConditionType.NOTNULL), SERVICE.REMOVED, null); for (Service service : services) { sdSvc.registerServiceLinks(service); } } return null; }
/** * {@inheritDoc} */ @Override public void from(io.cattle.platform.core.model.AccountLink from) { setId(from.getId()); setName(from.getName()); setAccountId(from.getAccountId()); setKind(from.getKind()); setUuid(from.getUuid()); setDescription(from.getDescription()); setState(from.getState()); setCreated(from.getCreated()); setRemoved(from.getRemoved()); setRemoveTime(from.getRemoveTime()); setData(from.getData()); setLinkedAccountId(from.getLinkedAccountId()); setLinkedAccount(from.getLinkedAccount()); setLinkedRegionId(from.getLinkedRegionId()); setLinkedRegion(from.getLinkedRegion()); setExternal(from.getExternal()); }