Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.springframework.stereotype.Component;
- import org.springframework.beans.factory.annotation.Autowired;
- import com.marklogic.client.query.QueryManager;
- /**
- * A base class for services that interact with MarkLogic.
- */
- @Component
- public abstract class ServiceBase {
- /**
- * Manage MarkLogic interaction via a single database client
- */
- @Autowired
- protected Client client;
- /**
- * Gets a new MarkLogic QueryManager for a derived class
- * @return A QueryManager
- */
- protected QueryManager queryManager() {
- return client.getClient().newQueryManager();
- }
- }
- import com.marklogic.client.io.ValuesHandle;
- import com.marklogic.client.query.QueryManager;
- import com.marklogic.client.query.ValuesDefinition;
- import com.marklogic.client.query.CountedDistinctValue;
- import org.springframework.stereotype.Component;
- import uk.co.corbas.slides.model.Keyword;
- import java.util.ArrayList;
- import java.util.HashSet;
- /**
- * KeywordService
- * Interface class to query MarkLogic for keywords
- */
- @Component
- public class KeywordService extends ServiceBase {
- public HashSet<Keyword> listKeywords() {
- HashSet keywords = new HashSet<Keyword>();
- QueryManager qMgr = client.newQueryManager();
- ValuesDefinition defs = qMgr.newValuesDefinition("keyword-list", "keyword-list");
- ValuesHandle results = qMgr.values(defs, new ValuesHandle());
- for (CountedDistinctValue result: results.getValues()) {
- keywords.add(new Keyword(result.get("xs:string", String.class)));
- }
- return keywords;
- }
- }
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.core.env.Environment;
- import org.springframework.stereotype.Component;
- /**
- * Class to store IoC wiring for Spring. Currently only handles the
- * database client.
- */
- @Component
- @ComponentScan
- public class Wiring {
- @Autowired
- private Environment env;
- @Bean
- public Client client() {
- Client client = new Client(env);
- return client;
- }
- }
- import com.marklogic.client.query.QueryManager;
- import org.springframework.core.env.Environment;
- import com.marklogic.client.DatabaseClient;
- import com.marklogic.client.DatabaseClientFactory;
- /**
- * A simple wrapper for a connection that enables autowiring of the MarkLogic
- * database client in the service classes
- *
- */
- public class Client {
- /**
- * Provided by Spring at startup, for accessing environment-specific variables.
- */
- private Environment env;
- /**
- * Database client to be returned to services
- */
- private DatabaseClient client;
- public Client(Environment env) {
- super();
- this.env = env;
- this.client = initializeDatabaseClient();
- }
- /**
- * Get the client
- * @return client - a DatabaseClient object
- */
- public DatabaseClient getClient() {
- return client;
- }
- /**
- * Constructs a Java Client API database Client
- * and stores the instance for use by all connections
- * @return A DatabaseClient for accessing MarkLogic
- */
- private DatabaseClient initializeDatabaseClient() {
- String host = env.getProperty("marklogic.rest.host");
- Integer port = Integer.parseInt(env.getProperty("marklogic.rest.port"));
- String username = env.getProperty("marklogic.rest.user");
- String password = env.getProperty("marklogic.rest.password");
- DatabaseClientFactory.DigestAuthContext context = new DatabaseClientFactory.DigestAuthContext(username, password);
- DatabaseClient client = DatabaseClientFactory.newClient(host, port, context);
- return client;
- }
- /**
- * Get a new query manager
- * @return queryMgr - a QueryManager object.
- */
- public QueryManager newQueryManager() { return client.newQueryManager(); }
- }
- @RequestMapping("/keywords")
- public HashSet<Keyword> list() {
- KeywordService kws = new KeywordService();
- return kws.listKeywords();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement