02-22-2022 03:50 AM
Hello:
I'm trying to update a node loading a new binary content and versioning it inside a new API REST. The code I post it works, but the problem is that after checkin, the cm:name changes..... How can I solve that problem? Alfresco version: 4.2.f Thank you.
NodeRef nodeCheckedOut = coci.checkout(nr); Map<String,Serializable> vproprs = new HashMap<String,Serializable>(); vproprs.put(Version.PROP_DESCRIPTION, "Nuova versione contenuto"); vproprs.put("name", fileName); logger.info("Eseguito CHECK OUT del nodo. " + nodeCheckedOut); ContentWriter writer = cs.getWriter(nodeCheckedOut, ContentModel.PROP_CONTENT, true); writer.setMimetype(mimetype); //propsOriginalFile = ns.getProperties(nodeCheckedOut); //propsOriginalFile.putAll(propsFile); ns.setProperties(nodeCheckedOut, propsFile); //writer.setEncoding(encoding); try { writer.putContent(input); }catch(ContentIOException cie) { logger.error("Errori con update del contenuto del documento checked out: " + nodeCheckedOut ); errori.concat("Errori con update del contenuto del documento checked out: " + nodeCheckedOut ); model.put("errore",errori); return model; } newDoc = coci.checkin(nodeCheckedOut, vproprs);
02-22-2022 03:52 PM
The code you have shared has some reference of code that is not availble in your snippet. Could you please share the full code to review?
Also can you eloborate what exactly you are trying to achieve? Are you trying to keep the old name that was before versioning or you want to change to new one ?
02-23-2022 08:46 AM
Hello, Abhinav:
thank you for your answer. Here is the Webscsript code. Basically I'm trying to update de binary content from a node and I hope to get once the checkin is done, the original file name (cm:name). But now what happens is that after doing the checkin, the cm:name is changed into UUID value..... How can I solve this? Thank you.
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) { NodeService ns = this.getServiceRegistry().getNodeService(); Map<String, Object> model = new HashMap<String, Object>(); Map<QName, Serializable> propsFile = null; Map<QName, Serializable> propsOriginalFile = null; String nodeId = ""; String fileName = ""; String contentbase64=""; String mimetype = ""; String doctype = ""; String docdesc = ""; String docorigfilename = ""; String iddomandaposiz = ""; String errori = new String(); ChildAssociationRef child = null; NodeRef newDoc = null; ContentService cs = this.getServiceRegistry().getContentService(); CheckOutCheckInService coci = this.getServiceRegistry().getCheckOutCheckInService(); VersionService vs = this.getServiceRegistry().getVersionService(); //raccolta parametri e trasformazione FormData form = (FormData) req.parseContent(); FormData.FormField[] fields = form.getFields(); for (FormData.FormField field : fields) { switch (field.getName()) { case "nodeid": nodeId += field.getValue(); if (nodeId == null || nodeId =="" || nodeId ==" "){ errori.concat("Parametro idfolder non valorizzato."); model.put("errori", errori); return model; } break; case "nomefile": fileName += field.getValue(); if (fileName == null || fileName =="" || fileName ==" "){ errori.concat("Parametro nomefile non valorizzato."); model.put("errori", errori); return model; } break; case "contentbase64": contentbase64 += field.getValue(); if (contentbase64 == null || contentbase64 =="" || contentbase64 ==" "){ errori.concat("Parametro contentbase64 non valorizzato."); model.put("errori", errori); } break; case "mimetype": mimetype += field.getValue(); if (mimetype == null || mimetype =="" || mimetype ==" "){ errori.concat("Parametro mimetype non valorizzato."); model.put("errori", errori); } break; case "doctype": doctype += field.getValue(); if (doctype == null || doctype =="" || doctype ==" "){ errori.concat("Parametro doctype non valorizzato."); model.put("errori", errori); return model; } break; case "docdesc": docdesc += field.getValue(); break; case"docorigfilename": docorigfilename += field.getValue(); break; case "iddomandaposiz": iddomandaposiz += field.getValue(); break; } } NodeRef nr = new NodeRef("workspace://SpacesStore/" + nodeId); if(!ns.exists(nr)) { logger.error("Non esiste il nodo. Impossibile fare update " + nr); errori.concat("Non esiste il nodo. Impossibile fare update " + nr); model.put("errori", errori); return model; } propsFile = new HashMap<>(); ByteArrayInputStream input = new ByteArrayInputStream(DatatypeConverter.parseBase64Binary(contentbase64)); switch(doctype) { case "fcs:doc": propsFile.put(PROP_DOC_DESCRIZIONE, docdesc); propsFile.put(PROP_DOC_ORIGINAL_FILENAME, docorigfilename); break; case "fcs:posizioniDoc": propsFile.put(PROP_DOC_POSIZIONI_ASPECT_ID_DOMANDA, iddomandaposiz); break; default: logger.error("Doctype non riconosciuto: " + doctype); errori.concat("Doctype non riconosciuto: " + doctype); model.put("errore",errori); return model; } if(coci.isCheckedOut(nr)) { logger.info("Non si esegue CHECK OUT del nodo. E' già in checkout"); ContentWriter writer = cs.getWriter(nr, ContentModel.PROP_CONTENT, true); writer.setMimetype(mimetype); ns.setProperties(nr, propsFile); try { writer.putContent(input); }catch(ContentIOException cie) { logger.error("Errori con update del contenuto del documento: " + nr ); errori.concat("Errori con update del contenuto del documento: " + nr ); model.put("errore",errori); return model; } }else { NodeRef nodeCheckedOut = coci.checkout(nr); Map<String,Serializable> vproprs = new HashMap<String,Serializable>(); vproprs.put(Version.PROP_DESCRIPTION, "Nuova versione contenuto"); ContentWriter writer = cs.getWriter(nodeCheckedOut, ContentModel.PROP_CONTENT, true); writer.setMimetype(mimetype); ns.setProperties(nodeCheckedOut, propsFile); try { writer.putContent(input); }catch(ContentIOException cie) { logger.error("Errori con update del contenuto del documento checked out: " + nodeCheckedOut ); errori.concat("Errori con update del contenuto del documento checked out: " + nodeCheckedOut ); model.put("errore",errori); return model; } newDoc = coci.checkin(nodeCheckedOut, vproprs); logger.debug("Checkin del file: " + newDoc); } model.put("content", newDoc.getId()); model.put("errore", errori); model.put("mimetype", mimetype); return model; }
02-24-2022 04:28 PM
I see the issue, you are creating a new map of properties and doing setProperties call on nodeService.
Instead,
1- Get the properties and then set properties
Map<QName, Serializable> propsFile = ns.getProperties(nr);
// your code
if(coci.isCheckedOut(nr)) {
// your code
ns.setProperties(nr, propsFile);
} else {
ns.setProperties(nodeCheckedOut, propsFile);
}
or
2- Use addProperties to put updated properties back to the node.
Map<QName, Serializable> propsFile = new HashMap<QName, Serializable>(nr); // your code if(coci.isCheckedOut(nr)) { // your code ns.addProperties(nr, propsFile); } else { ns.addProperties(nodeCheckedOut, propsFile); }
Your updated code:
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) { NodeService ns = this.getServiceRegistry().getNodeService(); Map<String, Object> model = new HashMap<String, Object>(); Map<QName, Serializable> propsFile = null; Map<QName, Serializable> propsOriginalFile = null; String nodeId = ""; String fileName = ""; String contentbase64=""; String mimetype = ""; String doctype = ""; String docdesc = ""; String docorigfilename = ""; String iddomandaposiz = ""; String errori = new String(); ChildAssociationRef child = null; NodeRef newDoc = null; ContentService cs = this.getServiceRegistry().getContentService(); CheckOutCheckInService coci = this.getServiceRegistry().getCheckOutCheckInService(); VersionService vs = this.getServiceRegistry().getVersionService(); //raccolta parametri e trasformazione FormData form = (FormData) req.parseContent(); FormData.FormField[] fields = form.getFields(); for (FormData.FormField field : fields) { switch (field.getName()) { case "nodeid": nodeId += field.getValue(); if (nodeId == null || nodeId =="" || nodeId ==" "){ errori.concat("Parametro idfolder non valorizzato."); model.put("errori", errori); return model; } break; case "nomefile": fileName += field.getValue(); if (fileName == null || fileName =="" || fileName ==" "){ errori.concat("Parametro nomefile non valorizzato."); model.put("errori", errori); return model; } break; case "contentbase64": contentbase64 += field.getValue(); if (contentbase64 == null || contentbase64 =="" || contentbase64 ==" "){ errori.concat("Parametro contentbase64 non valorizzato."); model.put("errori", errori); } break; case "mimetype": mimetype += field.getValue(); if (mimetype == null || mimetype =="" || mimetype ==" "){ errori.concat("Parametro mimetype non valorizzato."); model.put("errori", errori); } break; case "doctype": doctype += field.getValue(); if (doctype == null || doctype =="" || doctype ==" "){ errori.concat("Parametro doctype non valorizzato."); model.put("errori", errori); return model; } break; case "docdesc": docdesc += field.getValue(); break; case"docorigfilename": docorigfilename += field.getValue(); break; case "iddomandaposiz": iddomandaposiz += field.getValue(); break; } } NodeRef nr = new NodeRef("workspace://SpacesStore/" + nodeId); if(!ns.exists(nr)) { logger.error("Non esiste il nodo. Impossibile fare update " + nr); errori.concat("Non esiste il nodo. Impossibile fare update " + nr); model.put("errori", errori); return model; } propsFile = ns.getProperties(nr); ByteArrayInputStream input = new ByteArrayInputStream(DatatypeConverter.parseBase64Binary(contentbase64)); switch(doctype) { case "fcs:doc": propsFile.put(PROP_DOC_DESCRIZIONE, docdesc); propsFile.put(PROP_DOC_ORIGINAL_FILENAME, docorigfilename); break; case "fcs:posizioniDoc": propsFile.put(PROP_DOC_POSIZIONI_ASPECT_ID_DOMANDA, iddomandaposiz); break; default: logger.error("Doctype non riconosciuto: " + doctype); errori.concat("Doctype non riconosciuto: " + doctype); model.put("errore",errori); return model; } if(coci.isCheckedOut(nr)) { logger.info("Non si esegue CHECK OUT del nodo. E' già in checkout"); ContentWriter writer = cs.getWriter(nr, ContentModel.PROP_CONTENT, true); writer.setMimetype(mimetype); ns.setProperties(nr, propsFile); try { writer.putContent(input); }catch(ContentIOException cie) { logger.error("Errori con update del contenuto del documento: " + nr ); errori.concat("Errori con update del contenuto del documento: " + nr ); model.put("errore",errori); return model; } }else { NodeRef nodeCheckedOut = coci.checkout(nr); Map<String,Serializable> vproprs = new HashMap<String,Serializable>(); vproprs.put(Version.PROP_DESCRIPTION, "Nuova versione contenuto"); ContentWriter writer = cs.getWriter(nodeCheckedOut, ContentModel.PROP_CONTENT, true); writer.setMimetype(mimetype); ns.setProperties(nodeCheckedOut, propsFile); try { writer.putContent(input); }catch(ContentIOException cie) { logger.error("Errori con update del contenuto del documento checked out: " + nodeCheckedOut ); errori.concat("Errori con update del contenuto del documento checked out: " + nodeCheckedOut ); model.put("errore",errori); return model; } newDoc = coci.checkin(nodeCheckedOut, vproprs); logger.debug("Checkin del file: " + newDoc); } model.put("content", newDoc.getId()); model.put("errore", errori); model.put("mimetype", mimetype); return model; }
02-28-2022 04:28 AM
Hello, Abhinav:
thank you for your answer. I tried your suggestion, but I get an error:
Caused by: org.alfresco.service.cmr.repository.DuplicateChildNodeNameException: Duplicate child name not allowed:
This happens in this line: ns.setProperties(nodeCheckedOut, propsFile);
}else { NodeRef nodeCheckedOut = coci.checkout(nr); Map<String,Serializable> vproprs = new HashMap<String,Serializable>(); vproprs.put(Version.PROP_DESCRIPTION, "Nuova versione contenuto"); ContentWriter writer = cs.getWriter(nodeCheckedOut, ContentModel.PROP_CONTENT, true); writer.setMimetype(mimetype); ns.setProperties(nodeCheckedOut, propsFile);
It's like the CHECKED OUT file is created at the same level of the original file.....
Any idea to solve this issue?
Thank you .
Regards.
03-04-2022 05:37 PM
You can try checkin after content updates and then set the properties.
Explore our Alfresco products with the links below. Use labels to filter content by product module.