dev
dev

Reputation: 55

CQ5 aka AEM - Finding the source of a copied page programmatically

I'm currently exploring AEM and was wondering if it's possible to identify the "source" of a page. To clarify what I mean exactly:

If you copy a page by using the "Copy" and "Paste" options in CQ5 WCM (not a live copy, just a normal copy of a page) is it possible to programmatically determine which page your new page is based on, i.e. which page you copied to create the new page?

Upvotes: 1

Views: 617

Answers (2)

Michal Chudy
Michal Chudy

Reputation: 1893

I would approach rather from a different point of view. When you copy the page, there is no relation nor reference between these. However, you can prepare such mechanism on your own. This could work as following:

First, you need to implement Filter, which will intercept each request.

Request form, which is responsible for creating new Page in WCM looks like that:

cmd:copyPage
_charset_:utf-8
srcPath:/content/src
destParentPath:/content/dest
before:

Then the Filter, that will catch copy requests should be sth like that:

@Component(immediate = true)
@Service
@Properties({ @Property(name = "filter.scope", value = "REQUEST") })
public class CopyPageFilter implements Filter {

    private static final String WCM_COMMAND_SERVLET = "/bin/wcmcommand";

    private static final String CMD = "cmd";

    private static final String COPY_PAGE = "copyPage";

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        if (isValid((SlingHttpServletRequest) request)) {
            // store the src-dst for a while
        } else {
            chain.doFilter(request, response);
        }
    }

    private boolean isValid(SlingHttpServletRequest request) {
        return WCM_COMMAND_SERVLET.equals(request.getPathInfo())
                && COPY_PAGE.equals(request.getParameter(CMD));
    }

    @Override
    public void init(FilterConfig config) throws ServletException {
        // nothing to initialize
    }

    @Override
    public void destroy() {
        // nothing to destroy
    }

}

It will allow you to temporarily store relation between source and destination. The next step would be to implement SlingPostProcessor that will store information about source in the newly created page.

@Component(immediate = true)
@Service
public class BootstrapGridPostProcessorService implements SlingPostProcessor {

    @Reference
    private CopyPageFilter copyPageFilter;

    @Override
    public void process(SlingHttpServletRequest request, List<Modification> modifications)
            throws RepositoryException {
        // 1. Check if this modification is a page creation
        // 2. Check if in CopyPageFilter we have info about source for our destination
        // 3. To the newly created page add a weak reference (uuid) or path to the source
    }

}

That's it. We added relation between copied pages that we can further use.

Important I'm quite sure that in Touch UI there is another servlet responsible for page creation. Therefore, your Filter should take that into consideration.

Upvotes: 2

mish
mish

Reputation: 1065

You could look for pages with the same name (startsWith(...)) and the same ResourceType. But as far as I know, as soon as the Page is pasted - there is no connection to the "source" Page. Further, you could compare the names of the children of the Pages content resource (jcr:content node)

Upvotes: 1

Related Questions