/webapp/src/main/java/io/github/microcks/repository/ServiceRepositoryImpl.java

https://github.com/microcks/microcks · Java · 110 lines · 74 code · 12 blank · 24 comment · 3 complexity · b71c86b1cc667c0c5ba9282170649493 MD5 · raw file

  1. /*
  2. * Licensed to Laurent Broudoux (the "Author") under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. Author licenses this
  6. * file to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package io.github.microcks.repository;
  20. import io.github.microcks.domain.Service;
  21. import org.bson.types.ObjectId;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.data.mongodb.core.MongoTemplate;
  26. import org.springframework.data.mongodb.core.aggregation.Aggregation;
  27. import org.springframework.data.mongodb.core.aggregation.AggregationResults;
  28. import org.springframework.data.mongodb.core.aggregation.ObjectOperators;
  29. import org.springframework.data.mongodb.core.query.Criteria;
  30. import org.springframework.data.mongodb.core.query.Query;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. import java.util.Map;
  34. import static org.springframework.data.domain.Sort.Direction.DESC;
  35. import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
  36. /**
  37. * Implementation for CustomServiceRepository.
  38. * @author laurent
  39. */
  40. public class ServiceRepositoryImpl implements CustomServiceRepository {
  41. /** A simple logger for diagnostic messages. */
  42. private static Logger log = LoggerFactory.getLogger(ServiceRepositoryImpl.class);
  43. @Autowired
  44. private MongoTemplate template;
  45. @Override
  46. public List<Service> findByIdIn(List<String> ids) {
  47. // Convert ids into BSON ObjectId.
  48. List<ObjectId> objIds = new ArrayList<ObjectId>();
  49. for (String id : ids){
  50. objIds.add(new ObjectId(id));
  51. }
  52. List<Service> results = template.find(
  53. new Query(Criteria.where("_id").in(objIds)),
  54. Service.class);
  55. return results;
  56. }
  57. @Override
  58. public List<Service> findByLabels(Map<String, String> labels) {
  59. Query query = new Query();
  60. for (String labelKey : labels.keySet()) {
  61. query.addCriteria(Criteria.where("metadata.labels." + labelKey).is(labels.get(labelKey)));
  62. }
  63. List<Service> results = template.find(query, Service.class);
  64. return results;
  65. }
  66. @Override
  67. public List<Service> findByLabelsAndNameLike(Map<String, String> labels, String name) {
  68. Query query = new Query(Criteria.where("name").regex(name, "i"));
  69. for (String labelKey : labels.keySet()) {
  70. query.addCriteria(Criteria.where("metadata.labels." + labelKey).is(labels.get(labelKey)));
  71. }
  72. List<Service> results = template.find(query, Service.class);
  73. return results;
  74. }
  75. public List<ServiceCount> countServicesByType() {
  76. Aggregation aggregation = newAggregation(
  77. project("type"),
  78. group("type").count().as("number"),
  79. project("number").and("type").previousOperation(),
  80. sort(DESC, "number")
  81. );
  82. AggregationResults<ServiceCount> results = template.aggregate(aggregation, Service.class, ServiceCount.class);
  83. return results.getMappedResults();
  84. }
  85. public List<LabelValues> listLabels() {
  86. ObjectOperators.ObjectToArray labels = ObjectOperators.ObjectToArray.valueOfToArray("metadata.labels");
  87. Aggregation aggregation = newAggregation(
  88. project().and(labels).as("labels"),
  89. unwind("labels"),
  90. sort(DESC, "labels.v"),
  91. group("labels.k")
  92. .addToSet("labels.v").as("values")
  93. .first("labels.k").as("key")
  94. );
  95. AggregationResults<LabelValues> results = template.aggregate(aggregation, Service.class, LabelValues.class);
  96. return results.getMappedResults();
  97. }
  98. }