All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.capitalone.dashboard.repository.CustomRepositoryQueryImpl Maven / Gradle / Ivy

package com.capitalone.dashboard.repository;


import com.capitalone.dashboard.model.Collector;
import com.capitalone.dashboard.model.CollectorItem;
import com.capitalone.dashboard.model.CollectorType;
import com.capitalone.dashboard.model.Metadata;
import com.capitalone.dashboard.util.GitHubParsedUrl;
import org.apache.commons.lang3.StringUtils;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

//TODO: Need to migrate during Component Migration
@Component
public class CustomRepositoryQueryImpl implements CustomRepositoryQuery {

    private final MongoTemplate template;
    private static final String REGEX_ANY_STRING_INCLUDING_EMPTY = "^$|^.*";

    @Autowired
    public CustomRepositoryQueryImpl(MongoTemplate template) {
        this.template = template;
    }


    @Override
    public List findCollectorItemsBySubsetOptions(ObjectId id, Map allOptions, Map uniqueOptions,Map uniqueOptionsFromCollector) {
        Criteria c = Criteria.where("collectorId").is(id);
        uniqueOptions.values().removeIf(d-> d.equals(null) || ((d instanceof String) && StringUtils.isEmpty((String) d)));
        for (Map.Entry e : allOptions.entrySet()) {
            if (uniqueOptionsFromCollector.containsKey(e.getKey())) {
                c = getCriteria(uniqueOptions, c, e);
            }
        }
        List items =  template.find(new Query(c), CollectorItem.class);
        return items;
    }

    @Override
    public List findComponents(Collector collector) {
        Criteria c = Criteria.where("collectorItems." + collector.getCollectorType() + ".collectorId").is(collector.getId());
        return template.find(new Query(c), com.capitalone.dashboard.model.Component.class);
    }

    @Override
    public List findComponents(CollectorType collectorType) {
        Criteria c = Criteria.where("collectorItems." + collectorType).exists(true);
        return template.find(new Query(c), com.capitalone.dashboard.model.Component.class);
    }


    @Override
    public List findComponents(Collector collector, CollectorItem collectorItem) {
        return findComponents(collector.getId(), collector.getCollectorType(), collectorItem.getId());
    }

    @Override
    public List findComponents(ObjectId collectorId, CollectorType collectorType, CollectorItem collectorItem) {
        return findComponents(collectorId, collectorType, collectorItem.getId());
    }

    @Override
    public List findComponents(ObjectId collectorId, CollectorType collectorType, ObjectId collectorItemId) {
        Criteria c = Criteria.where("collectorItems." + collectorType + "._id").is(collectorItemId);
        return template.find(new Query(c), com.capitalone.dashboard.model.Component.class);
    }

    @Override
    public List findAllMetaDataBySearchQuery(String searchKey, String value) {
        Criteria c = Criteria.where(searchKey).is(value);
        return template.find(new Query(c), Metadata.class);
    }

    private String getGitHubParsedString(Map options, Map.Entry e) {
        String url = (String)options.get(e.getKey());
        GitHubParsedUrl gitHubParsedUrl = new GitHubParsedUrl(url);
        return gitHubParsedUrl.getUrl();
    }

    private Criteria getCriteria(Map options, Criteria c, Map.Entry e) {
        Criteria criteria = c;
        if("url".equalsIgnoreCase(e.getKey())){
            String url = getGitHubParsedString(options, e);
            criteria = criteria.and("options." + e.getKey()).regex(Pattern.compile(url,Pattern.CASE_INSENSITIVE));
        }
        else if("branch".equalsIgnoreCase(e.getKey())){
            String branch = (String)options.get(e.getKey());
            criteria = criteria.and("options." + e.getKey()).regex(Pattern.compile(branch,Pattern.CASE_INSENSITIVE));
        }
        else {
            criteria = criteria.and("options." + e.getKey()).is(options.get(e.getKey()));
        }
        return criteria;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy