public void execute(Connection connection) throws XMLDBException, EXistException { Collection collection = connection.getCollection("/db"); XQueryService service = (XQueryService) collection.getService("XQueryService", "1.0"); service.declareVariable("filename", ""); service.declareVariable("count", "0"); String query = IMPORT + xqueryContent; System.out.println("query: " + query); CompiledExpression compiled = service.compile(query); try { for (int i = 0; i < count; i++) { File nextFile = new File(directory, prefix + i + ".xml"); service.declareVariable("filename", nextFile.getName()); service.declareVariable("count", new IntegerValue(i)); ResourceSet results = service.execute(compiled); Writer out = new OutputStreamWriter(new FileOutputStream(nextFile), "UTF-8"); for (ResourceIterator iter = results.getIterator(); iter.hasMoreResources(); ) { Resource r = iter.nextResource(); out.write(r.getContent().toString()); } out.close(); } } catch (IOException e) { throw new EXistException("exception while storing generated data: " + e.getMessage(), e); } }
/** * This function makes the call to the XML database and retrieves all the file metadata for use in * ECGridToolkit. The data is first retrieved in a series of XML blocks, and then they are * converted into StudyEntry Objects and returned to the user * * @param userID - The ID of the user. This is used to check which files the user has submitted * and thus has access to * @return The list of StudyEntry Objects taken from the file metadata in the XML database */ public ArrayList<StudyEntry> getEntries(String userID) { ArrayList<StudyEntry> tempList = new ArrayList<StudyEntry>(); try { // create first query to get the entire studyEntry block, the collection() XQuery function // does this across // all documents in the XML Collection // The goal of this query is to find all the data we need for the StudyEntry object based on // which user submitted them. For all the documents searched, the query looks to see // if any files for that subject's ECG repository were submitted by that user. If any files // were submitted, retrieve their metadata contained in the studyEntry block. String sQuery = studyBuilder.defaultFor() + studyBuilder.defaultWhere(userID) + studyBuilder.defaultOrderBy() + studyBuilder.defaultReturn(); System.out.println("Query to be executed = " + sQuery); ResourceSet resultSet = executeQuery(sQuery); ResourceIterator iter = resultSet.getIterator(); Resource selection = null; Resource fileSelection = null; int listIndex = 0; int subjectCount = 1; while (iter.hasMoreResources()) { selection = iter.nextResource(); String studyEntryResult = (selection.getContent()).toString(); // Now we will create an XStream object and then put that into our StudyEntry objects // the StudyEntry objects are de-serialized versions of the studyEntry blocks. XStream xmlStream = new XStream(); xmlStream.alias("studyEntry", StudyEntry.class); xmlStream.alias("recordDetails", RecordDetails.class); xmlStream.alias("fileDetails", FileDetails.class); xmlStream.addImplicitCollection(RecordDetails.class, "fileDetails"); StudyEntry newStudy = (StudyEntry) xmlStream.fromXML(studyEntryResult); // System.out.println(newStudy); // Add it to the return array tempList.add(newStudy); } } catch (Exception ex) { System.out.println( "StudyEntryUtility.getEntries(): AN EXCEPTION HAS BEEN CAUGHT! IF A LIST IS RETURNED, IT WILL BE EMPTY!!!"); ex.printStackTrace(); } return tempList; }
public ArrayList<String> queryStudyTypes(String userID, String dataName) { System.out.println("In function queryStudyTypes"); ArrayList<String> tempList = new ArrayList<String>(); try { // create first query to get the entire studyEntry block, the collection() XQuery function // does this across // all documents in the XML Collection // The goal of this query is to find all the data we need for the StudyEntry object based on // which user submitted them. For all the documents searched, the query looks to see // if any files for that subject's ECG repository were submitted by that user. If any files // were submitted, retrieve their metadata contained in the studyEntry block. System.out.println("queryDatatypes:"); String sQuery = studyBuilder.forStudyEntry() + studyBuilder.customNameBracket(dataName, EnumStudyTreeNode.DATATYPE) + studyBuilder.userDefinedWhere(userID) + studyBuilder.customOrderBySubjectID() + studyBuilder.returnTreeNodeDistinct(EnumStudyTreeNode.STUDY); System.out.println("Query to be executed = " + sQuery); ResourceSet resultSet = executeQuery(sQuery); ResourceIterator iter = resultSet.getIterator(); Resource selection = null; tempList = this.checkForDuplicates(iter, selection); for (String listing : tempList) { int i = 1; System.out.println("Entry " + i + " = " + listing); i++; } } catch (Exception ex) { System.out.println( "ExistMainDatabase.queryStudies(): AN EXCEPTION HAS BEEN CAUGHT! IF A LIST IS RETURNED, IT WILL BE EMPTY!!!"); ex.printStackTrace(); } return tempList; }
/** * Main method of the example class. * * @param args (ignored) command-line arguments * @throws Exception exception */ public static void main(final String[] args) throws Exception { System.out.println("=== XMLDBQuery ===\n"); System.out.println("* Run query via XML:DB:"); // Collection instance Collection coll = null; try { // Register the database Class<?> c = Class.forName(DRIVER); Database db = (Database) c.newInstance(); DatabaseManager.registerDatabase(db); // Receive the database coll = DatabaseManager.getCollection(DBNAME); // Receive the XPath query service XPathQueryService service = (XPathQueryService) coll.getService("XPathQueryService", "1.0"); // Execute the query and receives all results ResourceSet set = service.query(QUERY); // Create a result iterator ResourceIterator iter = set.getIterator(); // Loop through all result items while (iter.hasMoreResources()) { // Receive the next results Resource res = iter.nextResource(); // Write the result to the console System.out.println(res.getContent()); } } catch (final XMLDBException ex) { // Handle exceptions System.err.println("XML:DB Exception occured " + ex.errorCode); } finally { // Close the collection if (coll != null) coll.close(); } }
/** * This method performs an XPath query to the database and returns the results as a Vector of * Strings. * * @param collection The name of the collection to look for the document. * @param query A string containing an XPath expression which shall act as a query against the * database. * @param username The identifier of the user calling the method used for authentication. * @param password The password of the user calling the method used for authentication. * @return A Vector containing the answers to the query as Strings. */ public Vector query(String collection, String query, String username, String password) { Vector response = new Vector(); try { Class cl = Class.forName(_driver); Database database = (Database) cl.newInstance(); DatabaseManager.registerDatabase(database); Collection col = DatabaseManager.getCollection(_URI + collection, username, password); XPathQueryService service = (XPathQueryService) col.getService("XPathQueryService", "1.0"); service.setProperty("indent", "yes"); ResourceSet result = service.query(query); ResourceIterator i = result.getIterator(); while (i.hasMoreResources()) { Resource r = i.nextResource(); response.add((String) r.getContent()); } } catch (Exception e) { e.printStackTrace(); } return response; }
public static void main(String args[]) throws Exception { String driver = "org.exist.xmldb.DatabaseImpl"; Class cl = Class.forName(driver); Database database = (Database) cl.newInstance(); DatabaseManager.registerDatabase(database); Collection col = DatabaseManager.getCollection("xmldb:exist:///db", "admin", ""); XPathQueryService service = (XPathQueryService) col.getService("XPathQueryService", "1.0"); service.setProperty("indent", "yes"); ResourceSet result = service.query( "for $s in //intervention/(speaker|writer)/affiliation[@EPparty ='PSE'] return data($s/../../(speech|writing)/@ref)"); ResourceIterator i = result.getIterator(); while (i.hasMoreResources()) { Resource r = i.nextResource(); System.out.println((String) r.getContent()); } // shut down the database DatabaseInstanceManager manager = (DatabaseInstanceManager) col.getService("DatabaseInstanceManager", "1.0"); manager.shutdown(); }
/** * This function makes the call to the XML database and retrieves all the file metadata for use in * ECGridToolkit. The data is first retrieved in a series of XML blocks, and then they are * converted into StudyEntry Objects and returned to the user * * @param userID - The ID of the user. This is used to check which files the user has submitted * and thus has access to * @param studyID - The ID of the study selected to search * @param datatype - The type of data that will be observed * @return The list of StudyEntry Objects taken from the file metadata in the XML database */ public ArrayList<StudyEntry> getEntries(String userID, String studyID, String datatype) { System.out.println("In function getEntries"); ArrayList<StudyEntry> tempList = new ArrayList<StudyEntry>(); try { // create first query to get the entire studyEntry block, the collection() XQuery function // does this across // all documents in the XML Collection // The goal of this query is to find all the data we need for the StudyEntry object based on // which user submitted them. For all the documents searched, the query looks to see // if any files for that subject's ECG repository were submitted by that user. If any files // were submitted, retrieve their metadata contained in the studyEntry block. // CompiledExpression query = subjectQuery.compile("for $x in collection('" + // allConstants.getDBCollection() + "')//record where collection('" + // allConstants.getDBCollection() + "')//record/studyEntry/submitterID=\"" + userID + "\" // order by $x/studyEntry/subjectID return $x/studyEntry"); // *** // 02/25/2013 - Brandon Benitez: Commented this out temporarily in case the query builder // version of this failed. // *** // String sQuery = "for $x in collection('" + dbHandle.getMainCollection() + // "')//record/studyEntry[studyID=\"" + studyID + "\"] where $x/submitterID=\"" + userID + "\" // return $x[datatype=\"" + datatype + "\"]"; String sQuery = studyBuilder.forStudyEntry() + // studyBuilder.customNameBracket(studyID, EnumStudyTreeNode.STUDY) + studyBuilder.whereUser2(userID) + studyBuilder.andWhereStudy(studyID) + studyBuilder.customOrderBySubjectID() + studyBuilder.returnX() + studyBuilder.customNameBracket(datatype, EnumStudyTreeNode.DATATYPE); System.out.println("Query to be executed = " + sQuery); ResourceSet resultSet = executeQuery(sQuery); ResourceIterator iter = resultSet.getIterator(); Resource selection = null; Resource fileSelection = null; int listIndex = 0; int subjectCount = 1; while (iter.hasMoreResources()) { selection = iter.nextResource(); String studyEntryResult = (selection.getContent()).toString(); // Now we will create an XStream object and then put that into our StudyEntry objects // the StudyEntry objects are de-serialized versions of the studyEntry blocks. XStream xmlStream = new XStream(); xmlStream.alias("studyEntry", StudyEntry.class); xmlStream.alias("recordDetails", RecordDetails.class); xmlStream.alias("fileDetails", FileDetails.class); xmlStream.addImplicitCollection(RecordDetails.class, "fileDetails"); StudyEntry newStudy = (StudyEntry) xmlStream.fromXML(studyEntryResult); // System.out.println(newStudy); // Add it to the return array tempList.add(newStudy); } } catch (Exception ex) { System.out.println( "StudyEntryUtility.getEntries(): AN EXCEPTION HAS BEEN CAUGHT! IF A LIST IS RETURNED, IT WILL BE EMPTY!!!"); ex.printStackTrace(); } return tempList; }