07-19-2021 05:18 AM
Create a web script to get the parameters of the document in java
Example url
localhost:8080/alfresco/s/get-document-data?nodeRef=workspace://SpacesStore/3b3597e5-b5ec-41d5-b63b-54b050dccd1b&property=cm:name
For implementation we use NodeService
nodeService.getProperty (new NodeRef (nodeRef), LmrContentModel.getQname (property));
As a result the script should return Json object of a kind
{ "nodeRef": "workspace: // SpacesStore / 3b3597e5-b5ec-41d5-b63b-54b050dccd1b", "value": "value property - the one we got from nodRef" }
Create a web script to retrieve all subfolder settings along the way.
Please help!
07-19-2021 05:46 AM
¡Hi!
You need to develop a Java-backed Web Scripts. Take a look into this examples.
You can find more information here.
Cheers!
07-20-2021 02:39 AM
So thank you, I reviewed but did not achieve the result. I can't understand:
nodeService.getProperty (new NodeRef (nodeRef), LmrContentModel.getQname (property))
and outputting these properties to json.
07-20-2021 03:03 AM
Hi!
If it's your first java backend webscript, try to do easy as possible. E.g. a hello world basic webscript, then based on this one try to adapted till you have the one you need it.
If you follow the tutorial I've shared with you in my previous post, you can see how can configure in the descriptor (xml) the output desired (as json). In the controller (java class), you'll need to use the ServiceRegistry to seach the node you wish to have the data, and extract the information you'll need it. Finally, you can format the view (ftl) to expose the results as you wish (based on json).
Be patience, try to do step by step and you'll see the results.
Cheers,
Cristina.
07-21-2021 04:35 AM
public class SimpleWebScript extends AbstractWebScript { @Override public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException { try { List<ChildAssociationRef> nodeRef = nodeService.getChildAssocs(companyHomeRef); for (ChildAssociationRef childAssoc : nodeRef) { NodeRef childNodeRef = childAssoc.getChildRef(); JSONObject obj = new JSONObject(); obj.put("nodeRef", childAssoc); obj.put("value", childAssoc.getQName()); String jsonString = obj.toString(); res.getWriter().write(jsonString); ServiceRegistry serviceRegistry = (ServiceRegistry) context.getBean("ServiceRegistry"); } } catch (JSONException e) { throw new WebScriptException("Unable to serialize JSON"); } } }
<beans> <bean id="webscript.org.alfresco.simple.get" class="org.alfresco.repository.SimpleWebScript" parent="webscript"> </bean> </beans>
<webscript> <shortname>Document</shortname> <description></description> <url>/demoSimple?nodeRef={nodeRef}&attach={attach?}</url> <format default="json"></format> <authentication>user</authentication> <family>Alfresco Document</family> </webscript>
Here I want to get the parameters of the document here is the code but it doesn't work, what's wrong with it?
07-21-2021 07:16 AM
Any errors in the log when you call to your webscript?
Then you are getting from the url a noderef but you are not using that value in your java. You should do something like:
String noderef = req.getParameter("nodeRef");
With that value charged, you can manipulate that reference that you're pasing on the url. Same case for "attach".
07-22-2021 01:14 AM
DocumentScript.java
import org.activiti.engine.impl.util.json.JSONException; import org.apache.chemistry.opencmis.commons.impl.json.JSONObject; import org.springframework.extensions.webscripts.*; import java.io.IOException; public class DocumentScript extends AbstractWebScript { public static JSONObject obj = new JSONObject(); @Override public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException { try { String noderef = req.getParameter("nodeRef"); String valueRef = res.getEncodeResourceUrlFunction("value"); obj.put("nodeRef", noderef); obj.put("value", valueRef); String jsonString = obj.toString(); res.getWriter().write(jsonString); } catch (JSONException e) { throw new WebScriptException("Unable to serialize JSON"); } } }
document-script-context.xml
<bean class="alfresco.extension.templates.webscripts.repository.DocumentScript" parent="templates.webscripts.repository"> </bean>
document-script.get.desc.xml
<webscript> <shortname>Documents</shortname> <description>JSON data </description> <url>/script-document?q={keyword}</url> <authentication>user</authentication> <format default="html"/> <family>Alfresco Script</family> </webscript>
document-script.get.html.ftl
{ "obj" : [ <#list obj as Obj> { "nodeRef" : "${Obj.nodeRef}", "value" : "${Obj.value}" } <#if Obj_has_next>,</#if> </#list> ] }
There is an error, please write what is wrong here.
06220015 Wrapped Exception (with status template): 06220002 Error during processing of the template 'The following has evaluated to null or missing: ==> obj [in template "repository/document-script.get.html.ftl" at line 3, column 8] Tip: If the failing expression is known to be legally null/missing, either specify a default value with myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthessis: (myOptionVar.foo)!myDefault, (myOptionVar.foo)?? The failing instruction: ==> #list obj as Obj [in template "repository/document-script.get.html.ftl" at line 3, column 1]'.
07-22-2021 01:59 AM
Hi!
You're failing in a basic concepts: I'll really suggest you to try and develop examples using Jeff potts tutorial and learn through the practice. Learn about what is a noderef, which services have you available, how can you search its content and manage it, etc.
Then, try to achieve your goal.
I know in the beggining, it can be a little frustating but you need to be patience.
Also, there are (old) books than can help you too. Like Alfresco One 5.x Developer's Guide.
Cheers,
Cristina.
Explore our Alfresco products with the links below. Use labels to filter content by product module.