4 Replies Latest reply on Jul 11, 2013 8:48 AM by jocamp

    Set a dynamic default property when a page is created?

    jocamp

      Is there a way to set a default property that is dynamic for a page when it is created? For example, i want to capture a user id from our system and store it in an "author" property when the page is first created. This value can be changed to a different user by the author but in case they don't set it i want to have it default on page creation.

       

      I guess a better question would be, is there somewhere i can put code so that it will be executed when a page is created?

       

      I considered writing a custom widget and putting this in page properties, but that won't actually run unless page properties has been opened.

       

      Thanks

        • 1. Re: Set a dynamic default property when a page is created?
          dgordon86 Level 1

          Hi,

           

          You could create a workflow with a single process step that copies the value of jcr:createdBy (userID who created the page) and into another property named 'author' and saves the property to the jcr:content node. This workflow can be configured to run automatically whenever a new page is created: http://dev.day.com/docs/en/cq/current/workflows/wf-using/workflow_howto.html#Starting Workflows When Nodes Change.

           

          You could then override the page properties dialog for your page to allow subsequent editors to modify this 'author' property.

           

          The workflow process step could be a simple ecma script that might look something like this:

           

          var workflowData = workItem.getWorkflowData();

          if (workflowData.getPayloadType() == "JCR_PATH") {

              var path = workflowData.getPayload().toString();

              if (workflowSession.getSession().itemExists(path)) {

           

                  //get user who created the author

                  var jcrNode = workflowSession.getSession().getItem(path + "/jcr:content");

                  var userID= jcrNode.getProperty("jcr:createdBy");

           

                  //set author property

                  var authorProp = jcrNode.setProperty("author", userID.toString());

                  jcrNode.save();

           

              } else {

                  log.warn("Item does not exist: " + path);

              }

          }

          1 person found this helpful
          • 2. Re: Set a dynamic default property when a page is created?
            Yogesh Upadhyay Level 4

            Or you can extend /libs/cq/ui/widgets/source/widgets/wcm/Page.Actions.js to have your own servlet handle page creation. Or you can create event handler that will run on Node Added (You should check node type to cq:Page) event and move property around example http://experiencedelivers.adobe.com/cemblog/en/experiencedelivers/2012/04/event_handling_i ncq.html

             

            Yogesh

            www.wemblog.com

            1 person found this helpful
            • 3. Re: Set a dynamic default property when a page is created?
              jocamp Level 1

              Thanks Daniel & Yogesh, both excellent ideas i hadn't thought of. I may try registering an event handler for the NODE_ADDED event but if that doesn't work the workflow idea sounds pretty easy to implement.

              • 4. Re: Set a dynamic default property when a page is created?
                jocamp Level 1

                For anyone in the future, here is how i implemented the event handler -

                 

                @Component
                public class PageCreationObservation implements EventListener {
                    Logger log = LoggerFactory.getLogger(this.getClass());
                    private Session adminSession;
                
                    @Reference
                    SlingRepository repository;
                
                    @Activate
                    public void activate(ComponentContext context) throws Exception {
                        log.info("Activating PageCreationObservation");
                        try {
                            String[] nodetypes = {"cq:Page"};
                
                            adminSession = repository.loginAdministrative(null);
                            adminSession.getWorkspace().getObservationManager().addEventListener(
                                this, //handler
                                Event.NODE_ADDED, //binary combination of event types
                                "/content/appname", //path
                                true, //is Deep?
                                null, //uuids filter
                                nodetypes, //nodetypes filter
                                false
                            );
                        } catch (RepositoryException e) {
                            log.error("Unable to register session",e);
                            throw new Exception(e);
                        }
                    }
                
                    @Deactivate
                    public void deactivate(){
                        if (adminSession != null) {
                            adminSession.logout();
                        }
                    }
                
                    public void onEvent(EventIterator eventIterator) {
                        try {
                            while (eventIterator.hasNext()) {
                                Event newEvent = eventIterator.nextEvent();
                
                                log.info("something has been added : {}", newEvent.getPath());
                
                                //Check if node exists and it is a content node of the newly created page
                                if (adminSession.nodeExists(newEvent.getPath())
                                        && adminSession.getNode(newEvent.getPath()).getProperty("jcr:primaryType").getString().equals("cq:PageContent")) {
                                    Node contentNode = adminSession.getNode(newEvent.getPath());
                
                                    if (contentNode.getProperty("jcr:createdBy") != null) {
                                        contentNode.setProperty("author", contentNode.getProperty("jcr:createdBy").getString());
                                    }
                                }
                            }
                
                            adminSession.save();
                        } catch(Exception e){
                            log.error("Error while treating page creation events",e);
                        }
                    }
                }