/corelib-dereference/src/main/java/eu/europeana/corelib/dereference/impl/VocabularyMongoServerImpl.java

https://gitlab.com/Aaeinstein54/corelib · Java · 157 lines · 93 code · 21 blank · 43 comment · 7 complexity · 8463f961f59d58d126e91784f9a9b6b0 MD5 · raw file

  1. /*
  2. * Copyright 2007-2012 The Europeana Foundation
  3. *
  4. * Licenced under the EUPL, Version 1.1 (the "Licence") and subsequent versions as approved
  5. * by the European Commission;
  6. * You may not use this work except in compliance with the Licence.
  7. *
  8. * You may obtain a copy of the Licence at:
  9. * http://joinup.ec.europa.eu/software/page/eupl
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed under
  12. * the Licence is distributed on an "AS IS" basis, without warranties or conditions of
  13. * any kind, either express or implied.
  14. * See the Licence for the specific language governing permissions and limitations under
  15. * the Licence.
  16. */
  17. package eu.europeana.corelib.dereference.impl;
  18. import java.util.List;
  19. import java.util.logging.Logger;
  20. import org.apache.commons.lang.StringUtils;
  21. import com.google.code.morphia.Datastore;
  22. import com.google.code.morphia.Morphia;
  23. import com.mongodb.Mongo;
  24. import eu.europeana.corelib.dereference.VocabularyMongoServer;
  25. /**
  26. * A mongo server instance for use with the controlled vocabularies
  27. *
  28. * @author Yorgos.Mamakis@ kb.nl
  29. *
  30. */
  31. public class VocabularyMongoServerImpl implements VocabularyMongoServer {
  32. private final Logger log = Logger.getLogger(getClass().getName());
  33. private Mongo mongoServer;
  34. private String databaseName;
  35. private Datastore datastore;
  36. private String username;
  37. private String password;
  38. /* (non-Javadoc)
  39. * @see eu.europeana.corelib.dereference.impl.VocabularyMongoServer#getDatastore()
  40. */
  41. @Override
  42. /**
  43. * Retrieve the created datastore
  44. */
  45. public Datastore getDatastore() {
  46. return this.datastore;
  47. }
  48. /* (non-Javadoc)
  49. * @see eu.europeana.corelib.dereference.impl.VocabularyMongoServer#close()
  50. */
  51. @Override
  52. public void close() {
  53. mongoServer.close();
  54. }
  55. public VocabularyMongoServerImpl(Mongo mongoServer, String databaseName) {
  56. log.info("VocabularyMongoServer is instantiated");
  57. this.mongoServer = mongoServer;
  58. this.databaseName = databaseName;
  59. createDatastore();
  60. }
  61. public VocabularyMongoServerImpl(Mongo mongoServer, String databaseName,String username,String password) {
  62. log.info("VocabularyMongoServer is instantiated");
  63. this.mongoServer = mongoServer;
  64. this.databaseName = databaseName;
  65. this.username = username;
  66. this.password = password;
  67. createDatastoreWithCredentials();
  68. }
  69. public VocabularyMongoServerImpl() {
  70. }
  71. /**
  72. * Create a datastore and map the required morphia entities
  73. */
  74. private void createDatastore() {
  75. Morphia morphia = new Morphia();
  76. morphia.map(ControlledVocabularyImpl.class).map(EntityImpl.class);
  77. datastore = morphia.createDatastore(mongoServer, databaseName);
  78. datastore.ensureIndexes();
  79. log.info("VocabularyMongoServer datastore is created");
  80. }
  81. private void createDatastoreWithCredentials() {
  82. Morphia morphia = new Morphia();
  83. morphia.map(ControlledVocabularyImpl.class).map(EntityImpl.class);
  84. datastore = morphia.createDatastore(mongoServer, databaseName,username, password.toCharArray());
  85. datastore.ensureIndexes();
  86. log.info("VocabularyMongoServer datastore is created");
  87. }
  88. /* (non-Javadoc)
  89. * @see eu.europeana.corelib.dereference.impl.VocabularyMongoServer#getControlledVocabulary(java.lang.String, java.lang.String)
  90. */
  91. @Override
  92. public ControlledVocabularyImpl getControlledVocabulary(String field,
  93. String filter) {
  94. String[] splitName = filter.split("/");
  95. String vocabularyName = splitName[0] + "/" + splitName[1] + "/"
  96. + splitName[2] + "/";
  97. List<ControlledVocabularyImpl> vocabularies = getDatastore()
  98. .find(ControlledVocabularyImpl.class)
  99. .filter(field, vocabularyName).asList();
  100. for (ControlledVocabularyImpl vocabulary : vocabularies) {
  101. boolean ruleController = true;
  102. for (String rule : vocabulary.getRules()) {
  103. ruleController = ruleController
  104. && (filter.contains(rule) || StringUtils.equals(rule,
  105. "*"));
  106. }
  107. if (ruleController) {
  108. return vocabulary;
  109. }
  110. }
  111. return null;
  112. }
  113. /* (non-Javadoc)
  114. * @see eu.europeana.corelib.dereference.impl.VocabularyMongoServer#getControlledVocabularyByUri(java.lang.String, java.lang.String)
  115. */
  116. @Override
  117. public ControlledVocabularyImpl getControlledVocabularyByUri(String uri,
  118. String name) {
  119. List<ControlledVocabularyImpl> vocabularies = getDatastore()
  120. .find(ControlledVocabularyImpl.class)
  121. .asList();
  122. for (ControlledVocabularyImpl vocabulary : vocabularies) {
  123. if (StringUtils.equals(name, vocabulary.getName())) {
  124. return vocabulary;
  125. }
  126. }
  127. return null;
  128. }
  129. /* (non-Javadoc)
  130. * @see eu.europeana.corelib.dereference.impl.VocabularyMongoServer#getControlledVocabularyByName(java.lang.String)
  131. */
  132. @Override
  133. public ControlledVocabularyImpl getControlledVocabularyByName(String name) {
  134. return getDatastore().find(ControlledVocabularyImpl.class).filter("name", name).get();
  135. }
  136. }