
io.github.microcks.repository.ServiceRepositoryImpl Maven / Gradle / Ivy
/*
* Licensed to Laurent Broudoux (the "Author") under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Author licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.github.microcks.repository;
import io.github.microcks.domain.Service;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.ObjectOperators;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static org.springframework.data.domain.Sort.Direction.DESC;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
/**
* Implementation for CustomServiceRepository.
* @author laurent
*/
public class ServiceRepositoryImpl implements CustomServiceRepository {
/** A simple logger for diagnostic messages. */
private static Logger log = LoggerFactory.getLogger(ServiceRepositoryImpl.class);
@Autowired
private MongoTemplate template;
@Override
public List findByIdIn(List ids) {
// Convert ids into BSON ObjectId.
List objIds = new ArrayList();
for (String id : ids){
objIds.add(new ObjectId(id));
}
List results = template.find(
new Query(Criteria.where("_id").in(objIds)),
Service.class);
return results;
}
@Override
public List findByLabels(Map labels) {
Query query = new Query();
for (String labelKey : labels.keySet()) {
query.addCriteria(Criteria.where("metadata.labels." + labelKey).is(labels.get(labelKey)));
}
List results = template.find(query, Service.class);
return results;
}
@Override
public List findByLabelsAndNameLike(Map labels, String name) {
Query query = new Query(Criteria.where("name").regex(name, "i"));
for (String labelKey : labels.keySet()) {
query.addCriteria(Criteria.where("metadata.labels." + labelKey).is(labels.get(labelKey)));
}
List results = template.find(query, Service.class);
return results;
}
public List countServicesByType() {
Aggregation aggregation = newAggregation(
project("type"),
group("type").count().as("number"),
project("number").and("type").previousOperation(),
sort(DESC, "number")
);
AggregationResults results = template.aggregate(aggregation, Service.class, ServiceCount.class);
return results.getMappedResults();
}
public List listLabels() {
ObjectOperators.ObjectToArray labels = ObjectOperators.ObjectToArray.valueOfToArray("metadata.labels");
Aggregation aggregation = newAggregation(
project().and(labels).as("labels"),
unwind("labels"),
sort(DESC, "labels.v"),
group("labels.k")
.addToSet("labels.v").as("values")
.first("labels.k").as("key")
);
AggregationResults results = template.aggregate(aggregation, Service.class, LabelValues.class);
return results.getMappedResults();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy