@Ignore @Test public void shouldAddNewFieldsFromJsonString() throws Exception { setupComplexDocuments(); Document testDoc = (Document) session.newRequest( DocumentService.GetDocumentChild).setHeader( Constants.HEADER_NX_SCHEMAS, "*").setInput(new PathRef("/")).set( "name", "testDoc").execute(); DocumentModel testDocModel = coreSession.getChild( coreSession.getRootDocument().getRef(), "testDoc"); assertNotNull(testDoc); assertNotNull(testDocModel); assertNotNull(testDoc.getProperties().get("ds:fields")); assertEquals(testDoc.getProperties().getList("ds:fields").size(), 0); File fieldsAsJsonFile = FileUtils.getResourceFileFromContext("creationFields.json"); assertNotNull(fieldsAsJsonFile); String fieldsDataAsJSon = IOUtils.toString(new FileReader(fieldsAsJsonFile)); fieldsDataAsJSon = fieldsDataAsJSon.replaceAll("\n", ""); fieldsDataAsJSon = fieldsDataAsJSon.replaceAll("\r", ""); OperationContext ctx = new OperationContext(coreSession); ctx.setInput(testDocModel); OperationChain chain = new OperationChain("testChain"); chain.add(AddEntryToComplexProperties.ID).set("xpath", "ds:fields").set( "ComplexJsonProperties", fieldsDataAsJSon); DocumentModel resultDoc = (DocumentModel) service.run(ctx, chain); java.util.ArrayList<?> dbFields = (java.util.ArrayList<?>) resultDoc.getPropertyValue("ds:fields"); assertEquals(5, dbFields.size()); fieldsAsJsonFile = FileUtils.getResourceFileFromContext("newField.json"); assertNotNull(fieldsAsJsonFile); fieldsDataAsJSon = IOUtils.toString(new FileReader(fieldsAsJsonFile)); fieldsDataAsJSon = fieldsDataAsJSon.replaceAll("\n", ""); fieldsDataAsJSon = fieldsDataAsJSon.replaceAll("\r", ""); ctx = new OperationContext(coreSession); ctx.setInput(testDocModel); chain = new OperationChain("testChain"); chain.add(AddEntryToComplexProperties.ID).set("xpath", "ds:fields").set( "ComplexJsonProperties", fieldsDataAsJSon); resultDoc = (DocumentModel) service.run(ctx, chain); dbFields = (java.util.ArrayList<?>) resultDoc.getPropertyValue("ds:fields"); assertEquals(7, dbFields.size()); }
@OperationMethod(collector = DocumentModelCollector.class) public DocumentModel run(DocumentModel doc) throws OperationException, IOException { Property complexMeta = doc.getProperty(xpath); ListType ltype = (ListType) complexMeta.getField().getType(); if (!ltype.getFieldType().isComplexType()) { throw new OperationException("Property type is not supported by this operation"); } List<Object> newVals = ComplexTypeJSONDecoder.decodeList(ltype, ComplexJsonProperties); for (Object newVal : newVals) { complexMeta.addValue(newVal); } if (save) { doc = session.saveDocument(doc); session.save(); } return doc; }
AddEntryToComplexProperties { @OperationMethod(collector = DocumentModelCollector.class) public DocumentModel run(DocumentModel doc) throws OperationException, IOException { Property complexMeta = doc.getProperty(xpath); ListType ltype = (ListType) complexMeta.getField().getType(); if (!ltype.getFieldType().isComplexType()) { throw new OperationException("Property type is not supported by this operation"); } List<Object> newVals = ComplexTypeJSONDecoder.decodeList(ltype, ComplexJsonProperties); for (Object newVal : newVals) { complexMeta.addValue(newVal); } if (save) { doc = session.saveDocument(doc); session.save(); } return doc; } }
AddEntryToComplexProperties { @OperationMethod(collector = DocumentModelCollector.class) public DocumentModel run(DocumentModel doc) throws OperationException, IOException { Property complexMeta = doc.getProperty(xpath); ListType ltype = (ListType) complexMeta.getField().getType(); if (!ltype.getFieldType().isComplexType()) { throw new OperationException("Property type is not supported by this operation"); } List<Object> newVals = ComplexTypeJSONDecoder.decodeList(ltype, ComplexJsonProperties); for (Object newVal : newVals) { complexMeta.addValue(newVal); } if (save) { doc = session.saveDocument(doc); session.save(); } return doc; }  }
AddEntryToComplexProperties { @OperationMethod(collector = DocumentModelCollector.class) public DocumentModel run(DocumentModel doc) throws OperationException, IOException { Property complexMeta = doc.getProperty(xpath); ListType ltype = (ListType) complexMeta.getField().getType(); if (!ltype.getFieldType().isComplexType()) { throw new OperationException("Property type is not supported by this operation"); } List<Object> newVals = ComplexTypeJSONDecoder.decodeList(ltype, ComplexJsonProperties); for (Object newVal : newVals) { complexMeta.addValue(newVal); } if (save) { doc = session.saveDocument(doc); session.save(); } return doc; }  @OperationMethod(collector = DocumentModelCollector.class) DocumentModel run(DocumentModel doc); }
AddEntryToComplexProperties { @OperationMethod(collector = DocumentModelCollector.class) public DocumentModel run(DocumentModel doc) throws OperationException, IOException { Property complexMeta = doc.getProperty(xpath); ListType ltype = (ListType) complexMeta.getField().getType(); if (!ltype.getFieldType().isComplexType()) { throw new OperationException("Property type is not supported by this operation"); } List<Object> newVals = ComplexTypeJSONDecoder.decodeList(ltype, ComplexJsonProperties); for (Object newVal : newVals) { complexMeta.addValue(newVal); } if (save) { doc = session.saveDocument(doc); session.save(); } return doc; }  @OperationMethod(collector = DocumentModelCollector.class) DocumentModel run(DocumentModel doc); static final String ID; }