Wednesday, 11 April 2018

Event Handler and Event Listener

Event Handler:: The sling level events can be captured by implementing the event handler interface. A property called EventConstants.EVENT_TOPIC has to be fined which signifies what event has to be captured. In this case the event handler catches the page being created. handleEvent(Event event)  method is implemented which can have the code to implement the business logic. Like we cna set a new property on creation of a page
 Example::
@Component(service = EventHandler.class, immediate = true, property = {
Constants.SERVICE_DESCRIPTION + "=Your Description here",
EventConstants.EVENT_TOPIC + "=com/day/cq/wcm/core/page" })

Event Listener:: The event handling at the jcr level can be captured by implementing the event listener interface. It is mostly concerned about the low level jcr events like node or property movement, addition, deletion etc.  The onEvent(EventIterator events) can be used to include the business logic, like deleting some property on node being copied. Observation manager can be used is used to listen to listen to events like Event.NODE_ADDED.

Example:: observationManager.addEventListener(this, Event.NODE_ADDED, path, true, null, null, false); :: In this example the event listener listens to the node being added to the jcr repository.


Wednesday, 28 February 2018

How to include a component

Add these two property to the node.

sling:resourceType="granite/ui/components/foundation/include"
path="/path/to/the/component"

Wednesday, 17 January 2018

Vault Clipse Set up

Hello, There are some steps that has to be followed to set up vault clipse to synchronize the content between eclipse and aem crx.

Note:: Your AEM jar can be anywhere in the file system of your computer. The below paths are just for reference.
Step 1::

Download the 2.4 version of file vault from the below link.
https://www.adobeaemcloud.com/content/companies/public/adobe/filevault/filevault.html

For my windows the below archive worked perfect
 vault-cli-2.4.40-bin.tar.gz
Download as .tar.gz archive

Step 2::

*Copy the downloaded archive at C:\Users\anshuman\AEM\crx-quickstart\opt\filevault\vault-cli-2.4.40-bin.tar.gz

*This is the crx folder generated when we execute the AEM jar

*unzip the file here

Step 3::

Now download vault clipse 2.4 form eclipse market place and set vault directory one level above bin.

Vault Directory::C:\Users\anshuman\AEM\crx-quickstart\opt\filevault\vault-cli-2.4.40-bin.tar\vault-cli-2.4.40

You can also change your temporary directory to
C:\Temp

Step 4::

Also set the "path" of the system environment variable to
C:\Users\anshuman\AEM\crx-quickstart\opt\filevault\vault-cli-2.4.40-bin.tar\vault-cli-2.4.40\bin

Note:: If the export or import operation is throwing out some error then try to delete everything from the temporary folder.

Import:: Pushes the content from eclipse to Crx.
Export:: Pulls the content from Crx to eclipse.

Tuesday, 16 January 2018

How to fetch the values stored at page's jcr:content node

This fetches the page name.
<h1>${currentPage.Name}</h1>

This fetches title of the page.
<h2>${pageProperties['jcr:title']}</h2>

How to include clientlib in the html file using sightly

The client library can be included by using the below code::
The first line declares a clientlib object, which is implemented as a template. Then css or js is called based on the categories.

<sly data-sly-use.clientlib="/libs/granite/sightly/templates/clientlib.html"></sly>
<sly data-sly-call="${clientlib.css @ categories='category.name'}"></sly>

To call css
data-sly-call="${clientlib.js @ categories='clientlib1,clientlib2'}" 

data-sly-call="${clientlib.css @ categories='clientlib1,clientlib2'}"

To call both css and js
data-sly-call="${clientlib.all @ categories='clientlib1,clientlib2'}"/>

Monday, 4 December 2017

Tag Field Vs Tags Picker

It is always recommended to use tag field (/libs/cq/gui/components/coral/common/form/tagfield) over tag picker, since the tagpicker is deprecated from AEM 6.3. The tagfield can be put into validation for a mandatory field by simply adding a property required(Boolean) true.

The ootb component is very easy to be overlayed and hence customize its functionality. Render.jsp is responsible for the logic and rendering(visual) of the tags.

Tuesday, 21 November 2017

How to send back the JSON object response from a servlet??

jsonobjj is the Json Object that is sent in the response. flush() sends the binary output stream.

response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.print(jsonobjj);
out.flush();

What is the difference between JSON.parse and JSON.stringify??

JSON.parse - Converts a string of JSON text into a javascript object.

JSON.stringify - Converts a javascript object into JSON text and stores that JSON text in a string.

Thursday, 16 November 2017

How to call a servlet in the js??

At times we come across a requirement where we have to call a servlet in our js file. This can be achieved as shown below::

var value;
$.get('/bin/testservlet', function(data) {console.log('test',data);
                                                    value=data});

/bin/testservlet= This is the path property set in the servlet. The request looks for the servlet that has this path property set.

data=contains the response sent back from the servlet. The response returned can then be used for the needed logic.

In this example the get method is executed.

Monday, 13 November 2017

How to fetch the configuration in aem cq

The Osgi configurations can be fetched programmatically to be consumed in any service.

Configuration node can be created by creating a node of Type sling:OsgiConfig.

The Osgi configurations can be accessed from http://localhost:4502/system/console/configMgr.

The following code uses ConfigurationAdmin to fetch the config.

@Reference

private ConfigurationAdmin configAdmin;

Configuration cnf=configAdmin.getConfiguration("<name-of-the-Confignode-created>");
 Dictionary<String, Object>str=cnf.getProperties(); //To get the properties stored at that node.
String val=str.get("<property-name>"); //To get the specific propert by name



Saturday, 11 November 2017

What's new in AEM 6.3?

As we know that in this digital era portability is in demand. Every business associate have to be in touch with their business on the go. So to make AEM more compatible with palm devices(mobile /tablet) the drift is towards touch ui aspect of experience manager.

There are few changes to the way services, workflows are handled. SCR annotations (org.apache.felix.scr.annotations.*) are replaced by OSGi annotations(org.osgi.service.component.annotations.*).

The release notes for AEM 6.3 is available at
https://helpx.adobe.com/experience-manager/6-3/release-notes.html

Osgi Component and Service

@Component(name=”Demo Service” service=DemoService.class,immediate=true)
public class DemoService {
                     public String getValue()
  {
   return "DemoService";
  }
}

Implementing Servlets


@Component(service=Servlet.class,
          property={
                  Constants.SERVICE_DESCRIPTION + "=Demo Servlet",
                  "sling.servlet.methods=" + HttpConstants.METHOD_GET,
                  "sling.servlet.resourceTypes="+ "com.demo.osgiannotation/components/structure/page",
                 "sling.servlet.paths="+ "/bin/servlet",
                  "sling.servlet.extensions=" + "txt"
          })
public class DemoServlet extends SlingSafeMethodsServlet {
{
@Override
  protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)throws IOException
  {
      response.getWriter().print("This get method is from the servlet");
  }
}


Implementing Workflow Process Step

@Component(service = WorkflowProcess.class, property = {"process.label=Activate Page" })
public class DemoWorkflow implements WorkflowProcess {
public void execute(WorkItem workitem, WorkflowSession wfsession,MetaDataMap metaDataMap)
{
log.info(“This execute method is from Workflow”);
}
}


extraClientlibs Usage

The purpose of property "extraClientlibs" is to selectively load client library for a dialog. This prevents the unnecessary load o...