Pages

Monday, 20 January 2014

How to refresh a page when a component is modified

A change was made to a component by an author. The page need to be refreshed in order to immediately reflect the change. Is there a way to automate this such that the author does not need to refresh the page? 

Resolution
The cq:listeners [cq:EditListenersConfig] is used to refresh the HTML page after a certain action is performed on a component. The cq:listeners node should be located as shown:
/component-name [cq:Component]
   /cq:editConfig [cq:editConfig]
      /cq:listeners [cq:EditListenersConfig]


NOTE: Text in the "[]" is the nodetype. Text after "/" is the node name -- should be extact name except for the "component-name".

The following properties are associated with cq:listeners node are:
aftercreate
afterdelete
afteredit
afterinsert
aftermove
afterremove
 

There are three possible values that can be assigned to the properties above -- either "REFRESH_SELF", "REFRESH_PARENT", "REFRESH_PAGE".

So, if you want your text component to cause a page refresh after each edit, you would create the following structure:
...
/mytextcomponent [cq:Component]
   /cq:editConfig [cq:editConfig]
      /cq:listeners [cq:EditListenersConfig]
         - afteredit {REFRESH_PAGE}             <= property {value}
   mytextcomponent.jsp                          <= code


Refer:

http://helpx.adobe.com/experience-manager/kb/RefreshPageWhenModifyDialog.html
 
 
 

Friday, 10 January 2014

what is the use of Activator.java in CQ ?


Symptoms

A change was made to a component by an author. The page need to be refreshed in order to immediately reflect the change. Is there a way to automate this such that the author does not need to refresh the page?


Resolution

The cq:listeners [cq:EditListenersConfig] is used to refresh the HTML page after a certain action is performed on a component. The cq:listeners node should be located as shown: 


/component-name [cq:Component]

   /cq:editConfig [cq:editConfig]

      /cq:listeners [cq:EditListenersConfig]


NOTE: Text in the "[]" is the nodetype. Text after "/" is the node name -- should be extact name except for the "component-name".


The following properties are associated with cq:listeners node are:

aftercreate

afterdelete

afteredit

afterinsert

aftermove

afterremove

There are three possible values that can be assigned to the properties above -- either "REFRESH_SELF", "REFRESH_PARENT", "REFRESH_PAGE".


So, if you want your text component to cause a page refresh after each edit, you would create the following structure:

...

/mytextcomponent [cq:Component]

   /cq:editConfig [cq:editConfig]

      /cq:listeners [cq:EditListenersConfig]

         - afteredit {REFRESH_PAGE}             <= property {value}

   mytextcomponent.jsp                          <= code

Refer:

 


Implementing a Custom Predicate Evaluator for the Query Builder

Check Authentication in CQ

AuthCheckerServlet.java

import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
@SlingServlet(paths = "/bin/permissioncheck/html", generateComponent = true, generateService = true)
public class AuthCheckerServlet extends SlingSafeMethodsServlet {
    public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {       
        String uri = request.getParameter("uri");       
        Resource resource = request.getResourceResolver().resolve(uri);       
        if(ResourceUtil.isNonExistingResource(resource)) {           
            response.setStatus(SlingHttpServletResponse.SC_FORBIDDEN);           
            } else {           
            response.setStatus(SlingHttpServletResponse.SC_OK);           
        }       
    }   
} 


Parbase

Parbase is a key component as it allows components to inherit attributes from other components, similar to subclasses in object oriented languages such as Java, C++, and so on. 
For example, when you open the /libs/foundation/components/text node in the CRX Explorer, you see that it has a property named sling:resourceSuperType, which references the parbase component. The parbase here defines tree scripts to render images, titles, and so on, so that all components subclassed from this parbase can use this script.
Users do not need access to the parbase.

Parsys Vs iParsys

Paragraph System (parsys)

The paragraph system (parsys) is a compound component that allows authors to add components of different types to a page and contains all other paragraph components. Each paragraph type is represented as a component. The paragraph system itself is also a component, which contains the other paragraph components.

You configure which components users see by making them available to the user in Design mode.

For example, the content of a product page may contain the following:
  • An image of the product (in the form of an image or textimage paragraph)
  • The product description (as a text paragraph)
  • A table with technical data (as a table paragraph)
  • A form users fill out (as a forms begin, forms element, and forms end paragraph)
file

List of components available for use...
See individual components.

Inheritance Paragraph System (iparsys)

The inherited paragraph system is a paragraph system that also allows you to inherit the created paragraphs from the parent. You add paragraphs to iparsys at for example, /content/geometrixx/en/products and as result, all the subpages of products that also have iparsys with the same name inherit the created paragraphs from the parent. On each level, you can add more paragraphs, which are then inherited by the children pages. You can also cancel paragraph inheritance at a level at any time.

Simply, iparsys is a parsys that inherits it's content from the ancestor pages.

file

Disable Inheritance
If the checkbox is selected, child pages do not inherit the paragraph of this page. 

The following example shows the iparsys component in Geometrixx:

file


HierarchyNodeInheritanceValueMap

Let us understand this with below example, 

Given: /content/parent/page/jcr:content/footer/image/@width, the HierarchyNodeInheritanceValueMap will search for a footer/image/@width property in:
  • /content/parent/page/jcr:content/footer/image/@width
  • /content/parent/jcr:content/footer/image/@width
  • /content/jcr:content/footer/image/@width
Having not found it in any of those locations, it will then return null

Note that HierarchyNodeInheritanceValueMap searches only the page hierarchy. It will not (for instance), look in:
  • /content/parent/page/jcr:content/footer/@width

Refer:

https://dev.day.com/docs/en/cq/current/javadoc/com/day/cq/commons/inherit/HierarchyNodeInheritanceValueMap.html 

 

Wrapping a binary into cq:Page and reverse replicate it

TestServlet.java

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFactory;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SlingServlet(paths = "/bin/test", extensions = "txt")
public class TestServlet extends SlingSafeMethodsServlet {
    private static final Logger LOG = LoggerFactory.getLogger(TestServlet.class);
    @Reference
    private ResourceResolverFactory resolverFactory;   
    private static final String PARENT = "/content/usergenerated/content";   
    public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
                ResourceResolver resourceResolver = null;
        try {           
            resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);           
            Session session = resourceResolver.adaptTo(Session.class);           
            reverseReplicateBinary(session, PARENT, "page123",           
            new ByteArrayInputStream("this is sample".getBytes()));           
            response.getWriter().println("session saved");           
            } catch (LoginException e) {           
            LOG.error("Can't get resolver", e);           
            } catch (RepositoryException e) {           
            LOG.error("Can't get resolver", e);           
            } finally {           
            if (resourceResolver != null) {               
                resourceResolver.close();               
            }           
        }       
    }   
    private void reverseReplicateBinary(Session session, String parentPath, String name, InputStream is)
        throws RepositoryException {       
        ValueFactory valueFactory = session.getValueFactory();       
        Node parent = session.getNode(parentPath); Node page = JcrUtils.getOrCreateUniqueByPath(parent, name, "cq:Page");
        Node jcrContent = page.addNode(Property.JCR_CONTENT, "cq:PageContent");       
        Node file = jcrContent.addNode("file", "nt:file");       
        Node resource = file.addNode(Property.JCR_CONTENT, "nt:resource");       
        resource.setProperty(Property.JCR_DATA, valueFactory.createBinary(is));       
        session.save(); jcrContent.setProperty("cq:lastModified", Calendar.getInstance());       
        jcrContent.setProperty("cq:lastModifiedBy", session.getUserID());       
        jcrContent.setProperty("cq:distribute", false);       
        session.save();       
    }   
}


Converting InputStream to String

    private String convertToString(InputStream inputStreamObj)             throws IOException {         if (inputStreamObj != null) {     ...