Search in sources :

Example 1 with AccountAwareResourceRepository

use of com.sequenceiq.cloudbreak.common.dal.repository.AccountAwareResourceRepository in project cloudbreak by hortonworks.

the class RepositoryBasedDataCollector method fetchDataFromDbIfNeed.

/**
 * Attaches {@code RESOURCE_ID}, {@code RESOURCE_CRN}, or {@code RESOURCE_NAME} to the provided {@code params} if they're missing.
 *
 * @param params should include exactly one of {@code RESOURCE_CRN} or {@code RESOURCE_NAME}
 */
public void fetchDataFromDbIfNeed(Map<String, String> params) {
    String resourceCrn = params.get(RESOURCE_CRN);
    String resourceName = params.get(RESOURCE_NAME);
    // check each repository for a matching CRN or Name.
    for (Map.Entry<String, AccountAwareResourceRepository<?, ?>> pathRepositoryEntry : pathRepositoryMap.entrySet()) {
        AccountAwareResourceRepository<?, ?> resourceRepository = pathRepositoryEntry.getValue();
        if (resourceCrn == null && resourceName != null) {
            String accountId = ThreadBasedUserCrnProvider.getAccountId();
            Optional<ResourceBasicView> entity = resourceRepository.findResourceBasicViewByNameAndAccountId(resourceName, accountId);
            if (entity.isPresent()) {
                ResourceBasicView resource = entity.get();
                params.put(RESOURCE_ID, Long.toString(resource.getId()));
                params.put(RESOURCE_CRN, resource.getResourceCrn());
                break;
            }
        } else if (resourceName == null) {
            Optional<ResourceBasicView> entity = resourceRepository.findResourceBasicViewByResourceCrn(resourceCrn);
            if (entity.isPresent()) {
                ResourceBasicView resource = entity.get();
                params.put(RESOURCE_ID, Long.toString(resource.getId()));
                params.put(RESOURCE_NAME, resource.getName());
                break;
            }
        }
    }
}
Also used : ResourceBasicView(com.sequenceiq.cloudbreak.common.dal.ResourceBasicView) Optional(java.util.Optional) HashMap(java.util.HashMap) Map(java.util.Map) AccountAwareResourceRepository(com.sequenceiq.cloudbreak.common.dal.repository.AccountAwareResourceRepository)

Aggregations

ResourceBasicView (com.sequenceiq.cloudbreak.common.dal.ResourceBasicView)1 AccountAwareResourceRepository (com.sequenceiq.cloudbreak.common.dal.repository.AccountAwareResourceRepository)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1