private String storeDocument(String uri, String xml) { try { Properties props = new Properties(); props.setProperty(pn_document_data_format, "XML"); DocumentAccessor doc = xRepo.getDocumentManagement().storeDocument(uri, xml, props); //logger.trace("storeDocument.exit; result: {}", result); return doc.getUri(); } catch (BagriException ex) { return null; } }
@GET @Path("/{name}") @Produces(MediaType.APPLICATION_JSON) @ApiOperation(value = "getCollectionDocuments: return Document uris belonging to the specified Collection") public Collection<String> getCollectionDocuments(@PathParam("name") String name) { DocumentManagement docMgr = getDocManager(); Properties props = new Properties(); props.setProperty(pn_document_headers, String.valueOf(DocumentAccessor.HDR_URI)); try (ResultCursor<DocumentAccessor> docs = docMgr.getDocuments("collections.contains(" + name + "), txFinish = 0", props)) { List<String> result = new ArrayList<>(); //itr.size()); for (DocumentAccessor doc: docs) { result.add(doc.getUri()); } return result; } catch (Exception ex) { logger.error("getCollectionDocuments.error", ex); throw new WebApplicationException(ex, Status.INTERNAL_SERVER_ERROR); } }
private java.util.Collection<String> getUris(String pattern, String properties) throws Exception { Properties props = propsFromString(properties); if (props == null) { props = new Properties(); } props.setProperty(pn_document_headers, String.valueOf(DocumentAccessor.HDR_URI)); Iterable<DocumentAccessor> uris = (Iterable<DocumentAccessor>) docManager.getDocuments(pattern, props); List<String> result = new ArrayList<>(); //uris.size() for (DocumentAccessor doc: uris) { result.add(doc.getUri()); } //uris.close(); logger.debug("getUris; returning {} uris", result); return result; }
names.add(uri.getUri());
@ManagedOperation(description="Register Document") @ManagedOperationParameters({ @ManagedOperationParameter(name = "docFile", description = "A full path to XML file to register"), @ManagedOperationParameter(name = "properties", description = "A list of properties in key=value form separated by semicolon")}) public String registerDocument(String docFile, String properties) { String uri = Paths.get(docFile).getFileName().toString(); try { String xml = FileUtils.readTextFile(docFile); DocumentAccessor doc = docManager.storeDocument(uri, xml, propsFromString(properties)); return doc.getUri(); } catch (IOException | BagriException ex) { logger.error("registerDocument.error: {}", ex.getMessage(), ex); throw new RuntimeException(ex.getMessage()); } }
@POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @ApiOperation(value = "postDocument: creates a new Document or new version if Document with provided uri already exists") public Response postDocument(final DocumentParams params) { DocumentManagement docMgr = getDocManager(); try { logger.trace("postDocument; got params: {}", params); DocumentAccessor doc = docMgr.storeDocument(params.uri, params.content, params.props); logger.trace("postDocument; got document: {}", doc); DocumentBean dr = new DocumentBean(doc.getUri(), doc.getCreateadAt(), doc.getCreatedBy(), doc.getContentType(), doc.getEncoding(), doc.getSizeInBytes()); return Response.created(UriBuilder.fromPath("/docs/" + dr.uri).build()).entity(dr).build(); } catch (Exception ex) { logger.error("postDocument.error", ex); throw new WebApplicationException(ex, Status.INTERNAL_SERVER_ERROR); //return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ex.getMessage()).build(); } }
public void storeSecurityTest() throws Exception { long txId = 0; try { txId = getTxManagement().beginTransaction(); } catch (BagriException ex) { if (ex.getErrorCode() != BagriException.ecTransNoNested) { throw ex; } } uris.add(createDocumentTest(sampleRoot + getFileName("security1500.xml")).getUri()); uris.add(createDocumentTest(sampleRoot + getFileName("security5621.xml")).getUri()); uris.add(createDocumentTest(sampleRoot + getFileName("security9012.xml")).getUri()); uris.add(createDocumentTest(sampleRoot + getFileName("security29674.xml")).getUri()); if (txId > 0) { getTxManagement().commitTransaction(txId); } }
@Override public Sequence call(XPathContext context, Sequence[] arguments) throws XPathException { String pattern = arguments[0].head().getStringValue(); Properties props = null; if (arguments.length > 1) { props = sequence2Properties(arguments[1]); } else { props = new Properties(); } props.setProperty(pn_document_headers, String.valueOf(DocumentAccessor.HDR_URI)); try (ResultCursor<DocumentAccessor> uris = xdm.getDocuments(pattern, props)) { List<AtomicValue> result = new ArrayList<>(); //uris.size()); for (DocumentAccessor da: uris) { result.add(new StringValue(da.getUri())); } return new AtomicArray(result); } catch (Exception ex) { throw new XPathException(ex); } } };
public void storeOrderTest() throws Exception { long txId = 0; try { txId = getTxManagement().beginTransaction(); } catch (BagriException ex) { if (ex.getErrorCode() != BagriException.ecTransNoNested) { throw ex; } } uris.add(createDocumentTest(sampleRoot + getFileName("order123.xml")).getUri()); uris.add(createDocumentTest(sampleRoot + getFileName("order654.xml")).getUri()); if (txId > 0) { getTxManagement().commitTransaction(txId); } }
@ManagedOperation(description="Updates already registerd Document") @ManagedOperationParameters({ @ManagedOperationParameter(name = "uri", description = "A new uri for the file or null"), @ManagedOperationParameter(name = "docFile", description = "A full path to XML file to register"), @ManagedOperationParameter(name = "properties", description = "A list of properties in key=value form separated by semicolon")}) public String updateDocument(String uri, String docFile, String properties) { try { String content = FileUtils.readTextFile(docFile); DocumentAccessor doc = docManager.storeDocument(uri, content, propsFromString(properties)); return doc.getUri(); } catch (IOException | BagriException ex) { logger.error("updateDocument.error: {}", ex.getMessage(), ex); throw new RuntimeException(ex.getMessage()); } }
docInfo.put("key", doc.getDocumentKey()); docInfo.put("version", doc.getVersion()); docInfo.put("uri", doc.getUri()); docInfo.put("size", doc.getSizeInBytes()); docInfo.put("encoding", doc.getEncoding());
@Test public void createSecurityTest() throws Exception { long txId = getTxManagement().beginTransaction(); String uri = getFileName("security1500.xml"); DocumentAccessor doc = createDocumentTest(sampleRoot + uri); assertNotNull(doc); uris.add(doc.getUri()); assertEquals(txId, doc.getTxStart()); assertEquals(0L, doc.getTxFinish()); assertEquals(uri, doc.getUri()); getTxManagement().commitTransaction(txId); }
public void storeCustomerTest() throws Exception { long txId = 0; try { txId = getTxManagement().beginTransaction(); } catch (BagriException ex) { if (ex.getErrorCode() != BagriException.ecTransNoNested) { throw ex; } } uris.add(createDocumentTest(sampleRoot + getFileName("custacc.xml")).getUri()); if (txId > 0) { getTxManagement().commitTransaction(txId); } }
@Test public void selectSecurityTest() throws Exception { long txId = getTxManagement().beginTransaction(); String fileName = sampleRoot + getFileName("security1500.xml"); String xml = readTextFile(fileName); DocumentAccessor doc = createDocumentTest(fileName); assertNotNull(doc); uris.add(doc.getUri()); assertEquals(txId, doc.getTxStart()); assertEquals(0L, doc.getTxFinish()); getTxManagement().commitTransaction(txId); Properties props = getDocumentProperties(); props.setProperty(pn_document_headers, String.valueOf(DocumentAccessor.HDR_CONTENT)); doc = getDocManagement().getDocument(doc.getUri(), props); assertNotNull(doc); assertNotNull(doc.getContent()); String content = doc.getContent(); assertEquals(xml.length(), content.length()); }
@Override public Sequence call(XPathContext context, Sequence[] arguments) throws XPathException { String uri = arguments[0].head().getStringValue(); Item item = arguments[1].head(); Object content = SaxonUtils.itemToObject(item); Properties props = null; if (arguments.length > 2) { props = sequence2Properties(arguments[2]); } try { DocumentAccessor doc = xdm.storeDocument(uri, content, props); return new AnyURIValue(doc.getUri()); } catch (BagriException ex) { throw new XPathException(ex); } }
DocumentAccessor doc = createDocumentTest(sampleRoot + getFileName("security1500.xml")); assertNotNull(doc); uris.add(doc.getUri()); assertEquals(txId, doc.getTxStart()); assertEquals(0L, doc.getTxFinish()); getTxManagement().commitTransaction(txId); int version = doc.getVersion(); String uri = doc.getUri(); uris.add(doc.getUri()); assertEquals(txId, doc.getTxStart()); assertEquals(0L, doc.getTxFinish()); assertEquals(++version, doc.getVersion()); assertEquals(uri, doc.getUri()); getTxManagement().commitTransaction(txId); doc = updateDocumentTest(uri, sampleRoot + getFileName("security5621.xml")); assertNotNull(doc); uris.add(doc.getUri()); assertEquals(txId, doc.getTxStart()); assertEquals(0L, doc.getTxFinish()); assertEquals(++version, doc.getVersion()); assertEquals(uri, doc.getUri()); getTxManagement().commitTransaction(txId);
@Test public void removeSecurityTest() throws Exception { long txId = getTxManagement().beginTransaction(); DocumentAccessor doc = createDocumentTest(sampleRoot + getFileName("security1500.xml")); assertNotNull(doc); uris.add(doc.getUri()); assertEquals(txId, doc.getTxStart()); assertEquals(0L, doc.getTxFinish()); getTxManagement().commitTransaction(txId); long docKey = doc.getDocumentKey(); long txId2 = getTxManagement().beginTransaction(); removeDocumentTest(doc.getUri()); doc = getDocManagement().getDocument(doc.getUri(), getDocumentProperties()); // now it is null.. think about it - is it correct or not? //assertNotNull(doc); //assertEquals(txId, doc.getTxStart()); //assertEquals(txId2, doc.getTxFinish()); //assertEquals(docId, doc.getDocumentId()); //assertEquals(docKey, doc.getDocumentKey()); //assertTrue(doc.getVersion() == ++version); //assertEquals(doc.getUri(), uri); getTxManagement().commitTransaction(txId2); //uris.add(doc.getUri()); //doc = getDocManagement().getDocument(docKey); //assertNull(doc); }