AEM dialog with extraClientlibs – do not shot yourself in the foot

AEM dialog with extraClientlibs – do not shot yourself in the foot
The below steps will help you to avoid common mistakes during implementation

There is a common case when we have to extend the AEM component dialog with custom JS code for some cases: field custom validation, to load external data, update field value on any condition, and so on.

Everything is going well until we have a few components or JS logic is not greater than the “hello world” component. But when the project grows alongside with components number we will face dependency hell.

In the scope of components dialogs, we have to keep in mind that every custom include of “extraClientlibs” will live on the page until page reload therefore JS functions could easily break logic in other dialogs during the page editing.

The below steps will help you to avoid common mistakes during implementation:

  1. DO not include every custom JS client library into the cq.authoring.dialog category:
    categories="[cq.authoring.dialog]"

    This ClientLibs category has to be used ONLY for utility methods that could be used across all dialogs and wouldn’t break anything.

  2. Every component dialog with custom JS code have to be placed in a separate category, easy to use package or folder path as a category name, this prevents category name overlap:
    categories="[project-a.components.product.wizzard.authoring]"
  3. Make a rule to put validation that we are working with the required dialog at the JS beginning, if not – bypass the execution:
    (function (document, $) {
    
    "use strict";
    
    const DIALOG_RESOURCE_TYPE = "project-a/components/product/wizzard";
    
    function isTargetDialog($formElement, dlgResourceType) {
    	const resourceType = $formElement.find("input[name='./sling:resourceType']").val();
    	return resourceType === dlgResourceType;
    }
    
    $(document).on("dialog-ready", function () {
    	const $formElement = $(this).find('coral-dialog form.foundation-form');
    	if (!isTargetDialog($formElement, DIALOG_RESOURCE_TYPE)) {
    		console.debug("Skip the listener, not a ", DIALOG_RESOURCE_TYPE, " dialog.");
    		return;
    	}
    	...
    });
    })(document, Granite.$);

As we can see these three steps will help to keep code clean and maintainable.

OAK Query aborted

Every AEM developer faced with below exception which means that we are exceeded the limit of nodes for reading:

java.lang.UnsupportedOperationException: The query read or traversed more than 100000 nodes. To avoid affecting other tasks, processing was stopped.
	at org.apache.jackrabbit.oak.query.FilterIterators.checkReadLimit(FilterIterators.java:70)
	at org.apache.jackrabbit.oak.plugins.index.Cursors.checkReadLimit(Cursors.java:67)
	at org.apache.jackrabbit.oak.plugins.index.lucene.LucenePropertyIndex$LucenePathCursor$1.next(LucenePropertyIndex.java:1730)
	at org.apache.jackrabbit.oak.plugins.index.lucene.LucenePropertyIndex$LucenePathCursor$1.next(LucenePropertyIndex.java:1711)
	at com.google.common.collect.Iterators$7.computeNext(Iterators.java:646)
	at com.google.common.collect.AbstractIterator.tryToComputeNext(AbstractIterator.java:143)
	at com.google.common.collect.AbstractIterator.hasNext(AbstractIterator.java:138)
	at org.apache.jackrabbit.oak.plugins.index.Cursors$PathCursor.hasNext(Cursors.java:216)
	at org.apache.jackrabbit.oak.plugins.index.lucene.LucenePropertyIndex$LucenePathCursor.hasNext(LucenePropertyIndex.java:1751)
	at org.apache.jackrabbit.oak.query.ast.SelectorImpl.next(SelectorImpl.java:432)
	at org.apache.jackrabbit.oak.query.QueryImpl$RowIterator.fetchNext(QueryImpl.java:824)
	at org.apache.jackrabbit.oak.query.QueryImpl$RowIterator.hasNext(QueryImpl.java:851)
	at org.apache.jackrabbit.oak.jcr.query.QueryResultImpl$1.fetch(QueryResultImpl.java:98)

In this situation, we can:

  • define the oak index for a query
  • try to redesign taxonomy of a repository or structure if it might be improved
  • update oak indexes to cover the query
  • use oak:Unstructured instead of nt:unstructured and sling:Folder instead of sling:OrderedFolder in a case where an order is not required
  • etc.

During the development, if we want to skip this error for a while we can increase LimitReads and LimitInMemory parameters for OAK Query Engine:

  1. navigate to  QueryEngineSettings
  2.  increase selected parameters:
  3. this is for development stage only, please, do not push this on production

Links to resources for help:

  1. Oak Index Definition Generator

  2. Best Practices for Queries and Indexing

Enable AEM Javadoc hints

To consume the whole power of IDE we must use Javadoc hints. Below is a short “how-to” on how to enable these hints for AEM 6.4:

  1. Open “Project structure” (short-key on Windows: Ctrl + Alt + Shift + S):
  2. Find the “Maven: com.adobe.aem:uber-jar:apis:6.4.4” (or another uber jar which you are using for the project) and click plus icon with the Earth

  3. Put the “https://helpx.adobe.com/experience-manager/6-4/sites/developing/using/reference-materials/javadoc/index.html” as a documentation URL.

After all of these actions you’ll be able to see API doc hints (on Windows with Ctrl + Q) :

Split Apache OAK stores

Split Apache OAK stores

Overview

One of big projects drawbacks is a size of Apache OAK which is growing very fast due to big amount of assets , binary files, background processes which are care about repository data consistency, updates and so on. Of cause we can use compaction every week but there is exists another approach – split segment and blob store out of Tar files (which is an OOTB store implementation). One of the available option is to split NodeStore and BlobStore. More about BlobStore you can read here – click. Continue reading “Split Apache OAK stores”

Sling, Git, Windows and Filename too long

There is a common error on Windows systems when you are working with GIT – Filename too long:

error: unable to create file contrib/extensions/distribution/sample/src/main/resources/SLING-CONTENT/libs/sling/distribution/install.publish/impersonate-reverse/org.apache.sling.distribution.packaging.impl.exporter.AgentDistributionPackageExporterFactory-impersonate-reverse.json: Filename too long

What does it means?

It means that by default GIT on Windows has a 260 characters limit due to system limitations (not all Windows apps are properly working with long file names). This can be critical if you will try for example will try to clone Sling application.

Solution

To remove this limitation we should open Windows Power Shell or CMD as administrator and execute command below:

git config --system core.longpaths true

This command will enable long path support and now we could maintain path up to 32,767 characters. For more information you can read this article.

New Configuration Approach for AEM 6

Since OSGI R6 we can beautify old style configurations for AEM 6 and create more readable, maintainable and loosely coupled code. In this target Declarative Services 1.3 (DS 1.3)will assists to us. Let’s consider how this can be reached step by step in scope of AEM 6 and Java 8. I wouldn’t provide a long description with explanation all pros and cons of this approach, what Declarative Services is and other things. Anyway you can simply google it if it’s interesting for you. Continue reading “New Configuration Approach for AEM 6”

Create Adobe AEM project from scratch in 5 steps with Apache Maven

When newcomer starts to develop a new project the question arises in the rapid establishment of a skeleton for future application. Every AEM project other than “Hello World” should contains server-side business logic, environment specific configurations, page templates, component templates, unit and tests and so on. To keep all this code clean and maintainable we should group it into small pieces (modules). The best practice is to hold Java classes and other content separately. Here we come to the aid of the Apache Maven. Continue reading “Create Adobe AEM project from scratch in 5 steps with Apache Maven”

JCR-SQL2 Query with Examples

Introduction

OOTB Apache OAK allow to create queries on SQL(deprecated), XPath(deprecated), JCR-JQOM and JCR-SQL2. It’s obvious that there is not a good point to explain deprecated SQL and XPath. This is a little How-To article which try to cower JCR-SQL2 queries for Apache Jackrabbit/OAK. Apache OAK has a more strict JCR-SQL2 syntax therefore some queries which are working correct on Apache Jackrabbit wouldn’t be executed on Apache OAK so we should always hold this in mind. Anyway let’s deal with JCR-SQL2 queries.
Continue reading “JCR-SQL2 Query with Examples”

How to make JSP scriplets on Java 7, Java 8 and above on Apache Sling and Adobe AEM

Sling implementation allow to render JSP with scriplets (of cause we using scriplets instead of Groovy console for hot-fixes). By default JSP compiler allow to write code compatible with Java 6 therefore if on server we has JVM version greater than 6 and desire to write code for recent JVM with additional benefits like Diamond operators from Java 7 or Lambdas and Streams from Java 8 we should configure JSP Scripts Handler to process required Java version otherwise we will see the compilation error: Continue reading “How to make JSP scriplets on Java 7, Java 8 and above on Apache Sling and Adobe AEM”