Expand my Community achievements bar.

SOLVED

How to restore a version programmaticaly?

Avatar

Level 4

Hi,

I want to restore the version programmatically, so went through the PageManager API and found restore method.

PageManager pageManager = resourceResolver.adaptTo(PageManager.class);

Collection<Revision> revisions = pageManager.getRevisions(currentPage.getPath(), null);
                        for(Revision r :revisions){
                            currentId=r.getId();
                            break;
                        }

                try{

                         pageManager.restore(currentPage.getPath(), currentId);

                    }catch(Exception e)

 

The above code snippet restoring the version and crash my page too. Please help me to proceed

It shows NullPointerException exception, The path and Id is not null :(

Error Log:

28.04.2015 18:54:50.591 *ERROR* [127.0.0.1 [1430227490410] GET /libs/wcm/core/content/pageinfo.json HTTP/1.1] org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Uncaught Throwable java.lang.NullPointerException
    at com.day.cq.wcm.core.impl.AnnotationsInfoProvider.updatePageInfo(AnnotationsInfoProvider.java:78)
    at com.day.cq.wcm.core.impl.servlets.PageInfoServlet.doGet(PageInfoServlet.java:187)
    at org.apache.sling.api.servlets.SlingSafeMethodsServlet.mayService(SlingSafeMethodsServlet.java:268)
    at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:344)
    at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:375)
    at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:508)
  
28.04.2015 18:54:50.669 *ERROR* [127.0.0.1 [1430227490662] GET /content/km/15259/15288.html HTTP/1.1] org.apache.jsp.libs.foundation.components.primary.cq.Page.Page_jsp resource has no content. path=/content/test/15259/15288.html referrer=null

1 Accepted Solution

Avatar

Correct answer by
Level 7

michaelvino86 wrote...

 

                        for(Revision r :revisions){
                            currentId=r.getId();
                            break;
                        }

                try{

                         pageManager.restore(currentPage.getPath(), currentId);

                    }catch(Exception e)

 

When you are using revision to restore, make sure it has content resource (pagecontent) attached with that revision id. Simply make following check before break and see if it solves the problem:

Check to make -

for(Revision r :revisions){ //selecting version only if it has content attached if(r.hasContent()){ log.info('selected version id - '+r.getId()); currentId=r.getId(); break; } }

More on revision API - https://docs.adobe.com/docs/en/cq/5-6-1/javadoc/com/day/cq/wcm/api/Revision.html#hasContent%28%29

If above doesn't solve the issue, then can there be a case that page's location has been moved after the version was created, never evaluated what will be the impact of restoring version then.

- Runal

View solution in original post

3 Replies

Avatar

Employee Advisor

Hi,

the only chance I see is that your page does not have a "jcr:content" node. Can you check?

kind regards,
Jörg

Avatar

Correct answer by
Level 7

michaelvino86 wrote...

 

                        for(Revision r :revisions){
                            currentId=r.getId();
                            break;
                        }

                try{

                         pageManager.restore(currentPage.getPath(), currentId);

                    }catch(Exception e)

 

When you are using revision to restore, make sure it has content resource (pagecontent) attached with that revision id. Simply make following check before break and see if it solves the problem:

Check to make -

for(Revision r :revisions){ //selecting version only if it has content attached if(r.hasContent()){ log.info('selected version id - '+r.getId()); currentId=r.getId(); break; } }

More on revision API - https://docs.adobe.com/docs/en/cq/5-6-1/javadoc/com/day/cq/wcm/api/Revision.html#hasContent%28%29

If above doesn't solve the issue, then can there be a case that page's location has been moved after the version was created, never evaluated what will be the impact of restoring version then.

- Runal

Avatar

Level 4

Thanks for your reply.

I have noticed that its restore the version but the problem is its required a refresh for restore the contents.

How to reload the page when the workflow current step completed?