Alfresco Share form field visibility depending on other field value

It’s not a common scenario, but sometimes user requirements include dynamic field control visibility depending on a value stored in other field from the same fieldset.

Let’s imagine that we have to show a field named cm:comment only if the field named cm:title includes the sentence Including comments.

Declaring the Share Form Control configuration for our custom form control named my-textfield.ftl should include required parameters.

<config evaluator="node-type" condition="cm:content"&gt;
  <forms&gt;
    <form&gt;
      <appearance&gt;
        <field id="cm:comment" label="Comments"&gt;
          <control template="/my-textfield.ftl"&gt;
		    <control-param name="visibilityField"&gt;cm_title</control-param&gt;
		    <control-param name="visibilityCondition"&gt;Including comments</control-param&gt;
          </control&gt;
        </field&gt;
      </appearance&gt;
    </form&gt;
  </forms&gt;
</config&gt;

In the my-textfield.ftl Share form control implementation, these values can be handled to show or hide the DIV containing the HTML control.

<!-- Naming field DIV to control visibility --&gt;
<div class="form-field" id="${fieldHtmlId}-control-div"&gt;
   <#if form.mode == "view"&gt;
      <div class="viewmode-field"&gt;
         <span class="viewmode-label"&gt;${field.label?html}:</span&gt;
         <span class="viewmode-value"&gt;${field.value?html}</span&gt;
      </div&gt;
   <#else&gt;
      <label for="${fieldHtmlId}"&gt;${field.label?html}:<#if field.mandatory&gt;<span class="mandatory-indicator"&gt;*</span&gt;</#if&gt;</label&gt;
      <input id="${fieldHtmlId}" type="text" name="${field.name}" value="${field.value}" 
            <#if field.disabled&gt;disabled="true"</#if&gt; /&gt;
   </#if&gt;
</div&gt;

<!-- Checking conditions when rendering field --&gt;
<script type="text/javascript"&gt;//<![CDATA[

	<#if field.control.params.visibilityCondition??&gt;
	YAHOO.util.Event.onContentReady("${fieldHtmlId}", function ()
	{
	
	       // Find value for field "visibilityField"
	       var value = "";
	       <#list form.fields?keys as formfield&gt;
			  <#if formfield == "prop_" + field.control.params.visibilityField?js_string&gt;
			      value = "${form.fields[formfield].value}";
			  </#if&gt;
	       </#list&gt;
	       
	       // Set value coming from control parameters
	       var targetValue = "${field.control.params.visibilityCondition}";
	       
	       // Hide full DIV if values are equal
	       if (value == targetValue) {
	           YAHOO.util.Dom.get("${fieldHtmlId}-control-div").style.display = 'none';
	       }
	       
	}, this);
	</#if&gt;

//]]&gt;</script&gt;

Sometimes Alfresco Share can be challenging when trying to find the right way to produce a new feature, but everything is easier than expected in the end!

Alfresco, counting more than 1000 elements

Many people need to count elements inside the repository. In a common repository, having more than 1,000 elements from the same type or aspect is a regular scenario.

In this blog post, several ways of counting elements in Alfresco repository are exposed.

Problem statement

How many nodes having businessDocument aspect are in the repository?

Let’s assume the following content model: a base aspect named ust:businessDocument and two inheriting aspects named ust:inboundDoc and ust:outboundDoc

<aspects>
    <aspect name="ust:businessDocument">
        <properties>
            <property name="ust:docDate">
                <type>d:datetime</type>
            </property>
        </properties>
    </aspect>
    <aspect name="ust:inboundDoc">
        <parent>ust:businessDocument</parent>
        <properties>
            <property name="ust:receivedDate">
                <type>d:datetime</type>
            </property>
        </properties>
    </aspect>
    <aspect name="ust:outboundDoc">
        <parent>ust:businessDocument</parent>
        <properties>
            <property name="ust:sentDate">
                <type>d:datetime</type>
            </property>
        </properties>
    </aspect>
</aspects>

So every content having any of these three aspects must be included.

For this sample, I’ve prepared a repository with 2,403 nodes including any of these aspects.

Using CMIS

A simple Groovy script can be developed by using a simple CMIS Query

import org.apache.chemistry.opencmis.commons.*
import org.apache.chemistry.opencmis.commons.data.*
import org.apache.chemistry.opencmis.commons.enums.*
import org.apache.chemistry.opencmis.client.api.*
import org.apache.chemistry.opencmis.client.util.*

String cql = "SELECT cmis:objectId FROM ust:businessDocument"

OperationContext opCon = session.createOperationContext();
opCon.setMaxItemsPerPage(1000000);

ItemIterable<QueryResult> results = session.query(cql, false, opCon)

println "--------------------------------------"
println "Total number: ${results.totalNumItems}"
println "Has more: ${results.hasMoreItems}"
println "--------------------------------------"


--------------------------------------
Total number: 1000
Has more: true
--------------------------------------

However, CMIS (and also FTS) can only retrieve 1,000 elements. You can play with paging and skipping, but there is no (simple) way to obtain more than 1,000.

Using Database

It’s not recommended to play with Alfresco Database, but it looks like this is the right chance to do it.

Let’s start with a simple query to see what happens.

SELECT count(1)
FROM alf_node AS n,
  alf_node_aspects AS a,
  alf_qname AS q,
  alf_namespace AS ns,
  alf_store AS s
WHERE a.qname_id = q.id
  AND a.node_id = n.id
  AND q.ns_id = ns.id
  AND n.store_id = s.id
  AND s.protocol = 'workspace'
  AND s.identifier = 'SpacesStore'
  AND ns.uri = 'http://www.ust-global.com/model/business/1.0'
  AND q.local_name in ('businessDocument');
 count
-------
   801
(1 row)

It looks like parent aspects are not related with the node, so we need to include every inherited aspect in the query.

SELECT count(1)
FROM alf_node AS n,
  alf_node_aspects AS a,
  alf_qname AS q,
  alf_namespace AS ns,
  alf_store AS s
WHERE a.qname_id = q.id
  AND a.node_id = n.id
  AND q.ns_id = ns.id
  AND n.store_id = s.id
  AND s.protocol = 'workspace'
  AND s.identifier = 'SpacesStore'
  AND ns.uri = 'http://www.ust-global.com/model/business/1.0'
  AND q.local_name in ('businessDocument', 'inboundDoc', 'outboundDoc');
 count
-------
  2403
(1 row)

So we have the number we were looking for, but we are scanning alf_node table to get it: database performance could be degraded!

Using SOLR

Skipping all Alfresco overload, we can use directly SOLR Engine to perform a query in alfresco core

https://localhost/solr/alfresco/afts?q=ASPECT:%22ust:businessDocument%22

<response>
	<lst name="responseHeader">
		<int name="status">0</int>
		<int name="QTime">6</int>
		<lst name="params">
			<str name="q">ASPECT:"ust:businessDocument"</str>
		</lst>
	</lst>
	<result name="response" numFound="2403" start="0">
		<doc>
			<str name="id">_DEFAULT_!8000000000000019!8000000000005279</str>
			<long name="_version_">0</long>
			<long name="DBID">21113</long>
		</doc>
	</result>
	<bool name="processedDenies">false</bool>
</response>

So we found that numFound 2,403 by using a simple query and without degrading Alfresco performance.

REST API

After talking a while with Younes Regaieg and Axel Faust at Alfresco IRC, I realised that there is also a way to provoke a SOLR query when using REST API invocation. When including a query that cannot be processed as TMQ, Alfresco is also counting the elements when using REST API.

This is why we are adding name:* to the query in the following code snippet.

[POST]

https://localhost/alfresco/api/-default-/public/search/versions/1/search

[Payload]

{
  "query": 
  {
     "language": "afts", 
     "query": "name:* AND ASPECT:\"ust:businessDocument\""
  }
}

Results are included in totalItems field in the response:

{
    "list": {
        "pagination": {
            "count": 100,
            "hasMoreItems": true,
            "totalItems": 2403,
            "skipCount": 0,
            "maxItems": 100
        },

The same thing can be obtained in a different way, but there is always an alternative that is better than the others. Some experimentation on test environments can save you more troubles in your real service!

How to create a Site for Alfresco using Java API

Some time ago, Site creation in Alfresco was driven by Share web application. So using a simple SiteService.createSite from repository Java API is not enough to provide access to the site from Share.

Below some useful snippets are listed.

Creating the Site

// Default site preset
String sitePreset = "site-dashboard";
SiteInfo siteInfo = siteService.createSite(sitePreset,
    "test-site", 
    "Test Site",
    "Test Site description", 
    SiteVisibility.PRIVATE);

Adding site members

Including at least a Manager is advisable.

siteService.setMembership("test-site", 
    "admin",
    SiteRole.SiteManager.toString());

Creating dashboard

This step is required to provide access from Share.

private void createDefaultDashboard(SiteInfo siteInfo) {
    
    NodeService nodeService = serviceRegistry.getNodeService();
    FileFolderService fileFolderService = serviceRegistry.getFileFolderService();
    ContentService contentService = serviceRegistry.getContentService();
    
    FileInfo surfConfig = fileFolderService.create(siteInfo.getNodeRef(), "surf-config", ContentModel.TYPE_FOLDER);
    Map<QName, Serializable> properties = new HashMap<QName, Serializable>();
    properties.put(ContentModel.PROP_CASCADE_HIDDEN, Boolean.TRUE);
    properties.put(ContentModel.PROP_CASCADE_INDEX_CONTROL, Boolean.TRUE);
    nodeService.addAspect(surfConfig.getNodeRef(), ContentModel.ASPECT_HIDDEN, properties);
    // Hint from Bertrand Forest
    properties = new HashMap<QName, Serializable>();
    properties.put(ContentModel.PROP_IS_INDEXED, Boolean.FALSE);
    properties.put(ContentModel.PROP_IS_CONTENT_INDEXED, Boolean.FALSE);
    nodeService.addAspect(surfConfig.getNodeRef(), ContentModel.ASPECT_INDEX_CONTROL, properties);
    
    FileInfo pages = fileFolderService.create(surfConfig.getNodeRef(), "pages", ContentModel.TYPE_FOLDER);
    FileInfo site = fileFolderService.create(pages.getNodeRef(), "site", ContentModel.TYPE_FOLDER);
    FileInfo siteName = fileFolderService.create(site.getNodeRef(), siteInfo.getShortName(), ContentModel.TYPE_FOLDER);
    
    Map<QName, Serializable> props = new HashMap<QName, Serializable>(1);
    props.put(ContentModel.PROP_NAME, "dashboard.xml");  

    NodeRef node = nodeService.createNode(
                            siteName.getNodeRef(), 
                        ContentModel.ASSOC_CONTAINS, 
                        QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "dashboard.xml"),
                        ContentModel.TYPE_CONTENT, 
                        props).getChildRef();
                        
    ContentWriter writer = contentService.getWriter(node, ContentModel.PROP_CONTENT, true);
    writer.setMimetype(MimetypeMap.MIMETYPE_XML);
    writer.setEncoding("UTF-8");
    // TODO Create dashboard.xml file by using an external file resource instead of a hand-coded String
    writer.putContent("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 
            "<page>\n" + 
            "      <title>Collaboration Site Dashboard</title>\n" + 
            "      <title-id>page.siteDashboard.title</title-id>\n" + 
            "      <description>Collaboration site's dashboard page</description>\n" + 
            "      <description-id>page.siteDashboard.description</description-id>\n" + 
            "      <authentication>user</authentication>\n" + 
            "      <template-instance>dashboard-2-columns-wide-left</template-instance>\n" + 
            "      <properties>\n" + 
            "        <sitePages>[{\"pageId\": \"documentlibrary\"}]</sitePages>\n" + 
            "      <theme/><dashboardSitePage>true</dashboardSitePage></properties>\n" + 
            "    <page-type-id>generic</page-type-id></page>");
    
}

Creating Site containers

There are different containers that should be created (wiki, forum,…) but having a Document Library is required for many use cases.

NodeRef documentLibraryNodeRef =  siteService.createContainer(siteInfo.getShortName(), SiteService.DOCUMENT_LIBRARY, null, null);

Some operations are not described in Alfresco documentation, but they can be guessed inspecting internal structure of the repository.