Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. import org.springframework.stereotype.Component;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import com.marklogic.client.query.QueryManager;
  4.  
  5. /**
  6. * A base class for services that interact with MarkLogic.
  7. */
  8. @Component
  9. public abstract class ServiceBase {
  10.  
  11. /**
  12. * Manage MarkLogic interaction via a single database client
  13. */
  14. @Autowired
  15. protected Client client;
  16.  
  17. /**
  18. * Gets a new MarkLogic QueryManager for a derived class
  19. * @return A QueryManager
  20. */
  21. protected QueryManager queryManager() {
  22. return client.getClient().newQueryManager();
  23. }
  24.  
  25.  
  26. }
  27.  
  28. import com.marklogic.client.io.ValuesHandle;
  29. import com.marklogic.client.query.QueryManager;
  30. import com.marklogic.client.query.ValuesDefinition;
  31. import com.marklogic.client.query.CountedDistinctValue;
  32.  
  33.  
  34. import org.springframework.stereotype.Component;
  35.  
  36. import uk.co.corbas.slides.model.Keyword;
  37.  
  38. import java.util.ArrayList;
  39. import java.util.HashSet;
  40.  
  41.  
  42. /**
  43. * KeywordService
  44. * Interface class to query MarkLogic for keywords
  45. */
  46. @Component
  47. public class KeywordService extends ServiceBase {
  48.  
  49. public HashSet<Keyword> listKeywords() {
  50.  
  51. HashSet keywords = new HashSet<Keyword>();
  52.  
  53. QueryManager qMgr = client.newQueryManager();
  54. ValuesDefinition defs = qMgr.newValuesDefinition("keyword-list", "keyword-list");
  55. ValuesHandle results = qMgr.values(defs, new ValuesHandle());
  56.  
  57. for (CountedDistinctValue result: results.getValues()) {
  58. keywords.add(new Keyword(result.get("xs:string", String.class)));
  59. }
  60.  
  61. return keywords;
  62. }
  63.  
  64. }
  65.  
  66. import org.springframework.beans.factory.annotation.Autowired;
  67. import org.springframework.context.annotation.Bean;
  68. import org.springframework.context.annotation.ComponentScan;
  69. import org.springframework.core.env.Environment;
  70. import org.springframework.stereotype.Component;
  71.  
  72. /**
  73. * Class to store IoC wiring for Spring. Currently only handles the
  74. * database client.
  75. */
  76. @Component
  77. @ComponentScan
  78. public class Wiring {
  79.  
  80. @Autowired
  81. private Environment env;
  82.  
  83. @Bean
  84. public Client client() {
  85. Client client = new Client(env);
  86. return client;
  87. }
  88.  
  89. }
  90.  
  91. import com.marklogic.client.query.QueryManager;
  92. import org.springframework.core.env.Environment;
  93.  
  94. import com.marklogic.client.DatabaseClient;
  95. import com.marklogic.client.DatabaseClientFactory;
  96.  
  97. /**
  98. * A simple wrapper for a connection that enables autowiring of the MarkLogic
  99. * database client in the service classes
  100. *
  101. */
  102. public class Client {
  103.  
  104. /**
  105. * Provided by Spring at startup, for accessing environment-specific variables.
  106. */
  107. private Environment env;
  108.  
  109. /**
  110. * Database client to be returned to services
  111. */
  112. private DatabaseClient client;
  113.  
  114. public Client(Environment env) {
  115. super();
  116. this.env = env;
  117. this.client = initializeDatabaseClient();
  118. }
  119.  
  120. /**
  121. * Get the client
  122. * @return client - a DatabaseClient object
  123. */
  124. public DatabaseClient getClient() {
  125. return client;
  126. }
  127.  
  128. /**
  129. * Constructs a Java Client API database Client
  130. * and stores the instance for use by all connections
  131. * @return A DatabaseClient for accessing MarkLogic
  132. */
  133. private DatabaseClient initializeDatabaseClient() {
  134.  
  135. String host = env.getProperty("marklogic.rest.host");
  136. Integer port = Integer.parseInt(env.getProperty("marklogic.rest.port"));
  137. String username = env.getProperty("marklogic.rest.user");
  138. String password = env.getProperty("marklogic.rest.password");
  139.  
  140. DatabaseClientFactory.DigestAuthContext context = new DatabaseClientFactory.DigestAuthContext(username, password);
  141. DatabaseClient client = DatabaseClientFactory.newClient(host, port, context);
  142.  
  143. return client;
  144.  
  145. }
  146.  
  147. /**
  148. * Get a new query manager
  149. * @return queryMgr - a QueryManager object.
  150. */
  151. public QueryManager newQueryManager() { return client.newQueryManager(); }
  152. }
  153.  
  154. @RequestMapping("/keywords")
  155. public HashSet<Keyword> list() {
  156.  
  157. KeywordService kws = new KeywordService();
  158. return kws.listKeywords();
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement