Jahia in English > Update JCR node...

0 (0 Good)
0 (0 Bad)

Update JCR node but keep as "not modified"

by  thbaymet »  2015/11/10 16:17

Hi.

When I run the following groovy script to add/update some properties to nodes, then in Jahia on Edit mode my pages are marked as "modified but not published".

//
def isMultiValued = node.getProperty(oldAttrName).isMultiple();

if (isMultiValued) {
    Value [] values = node.getProperty(oldAttrName).getValues();
    node.setProperty(newAttrName, values);
    log.info("={}= - property is successfully set with array values", newAttrName);
} else {
    def oldAttrValue = node.getProperty(oldAttrName).getString();
    node.setProperty(newAttrName, oldAttrValue);
    log.info("={}= - property is successfully set with value {}", newAttrName, oldAttrValue);
}

I would like to know if there any way to keep the "published status" of the nodes ?

Thanks.

Regards.

  • Update JCR node but keep as "not modified"
    2015/11/10 16:17

    thbaymet <p>Hi.</p> <p>When I run the following groovy script to add/update some properties to nodes, then in Jahia on Edit mode my pages are marked as &quot;modified but not published&quot;.</p> <pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Source Code Pro';font-size:10,6pt;"> <span style="color:#808080;">// </span><span style="color:#cc7832;">def </span>isMultiValued = <span style="color:#808080;">node</span>.getProperty(<span style="color:#808080;">oldAttrName</span>).isMultiple(); <span style="color:#cc7832;">if </span>(isMultiValued) { Value [] values = <span style="color:#808080;">node</span>.getProperty(<span style="color:#808080;">oldAttrName</span>).getValues(); <span style="color:#808080;">node</span>.setProperty(<span style="color:#808080;">newAttrName</span>, values); <span style="color:#808080;">log</span>.info(<span style="color:#6a8759;">&quot;={}= - property is successfully set with array values&quot;</span>, <span style="color:#808080;">newAttrName</span>); } <span style="color:#cc7832;">else </span>{ <span style="color:#cc7832;">def </span>oldAttrValue = <span style="color:#808080;">node</span>.getProperty(<span style="color:#808080;">oldAttrName</span>).getString(); <span style="color:#808080;">node</span>.setProperty(<span style="color:#808080;">newAttrName</span>, oldAttrValue); <span style="color:#808080;">log</span>.info(<span style="color:#6a8759;">&quot;={}= - property is successfully set with value {}&quot;</span>, <span style="color:#808080;">newAttrName</span>, oldAttrValue); }</pre> <p>I would like to know if there any way to keep the &quot;<strong>published status</strong>&quot; of the nodes ?</p> <p>Thanks.</p> <p>Regards.</p>

  • Number of messages  19
    Registration date Nov 10, 2015
    0 (0 Good)
    0 (0 Bad)

    Re: Update JCR node but keep as "not modified"

    by  amidani »  2015/11/10 16:38

    Hi,

    Try to add autoPublish mixin on your node.

    <your_node>.addMixin("jmix:autoPublish");

    then set your properties.

    Kind Regards.

    Hi.

    When I run the following groovy script to add/update some properties to nodes, then in Jahia on Edit mode my pages are marked as "modified but not published".

    //
    def isMultiValued = node.getProperty(oldAttrName).isMultiple();
    
    if (isMultiValued) {
        Value [] values = node.getProperty(oldAttrName).getValues();
        node.setProperty(newAttrName, values);
        log.info("={}= - property is successfully set with array values", newAttrName);
    } else {
        def oldAttrValue = node.getProperty(oldAttrName).getString();
        node.setProperty(newAttrName, oldAttrValue);
        log.info("={}= - property is successfully set with value {}", newAttrName, oldAttrValue);
    }

    I would like to know if there any way to keep the "published status" of the nodes ?

    Thanks.

    Regards.

     

     

     

  • Re: Update JCR node but keep as "not modified"
    2015/11/10 16:38

    amidani <p>Hi,</p> <p>Try to add autoPublish mixin on your node.</p> <p>&lt;your_node&gt;.addMixin(<span style="color:#658aba;font-weight:bold;">&quot;jmix:autoPublish&quot;</span>);</p> <p>then set your properties.</p> <p>Kind Regards.</p> <blockquote> <p>Hi.</p> <p>When I run the following groovy script to add/update some properties to nodes, then in Jahia on Edit mode my pages are marked as &quot;modified but not published&quot;.</p> <pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Source Code Pro';font-size:10,6pt;"> <span style="color:#808080;">// </span><span style="color:#cc7832;">def </span>isMultiValued = <span style="color:#808080;">node</span>.getProperty(<span style="color:#808080;">oldAttrName</span>).isMultiple(); <span style="color:#cc7832;">if </span>(isMultiValued) { Value [] values = <span style="color:#808080;">node</span>.getProperty(<span style="color:#808080;">oldAttrName</span>).getValues(); <span style="color:#808080;">node</span>.setProperty(<span style="color:#808080;">newAttrName</span>, values); <span style="color:#808080;">log</span>.info(<span style="color:#6a8759;">&quot;={}= - property is successfully set with array values&quot;</span>, <span style="color:#808080;">newAttrName</span>); } <span style="color:#cc7832;">else </span>{ <span style="color:#cc7832;">def </span>oldAttrValue = <span style="color:#808080;">node</span>.getProperty(<span style="color:#808080;">oldAttrName</span>).getString(); <span style="color:#808080;">node</span>.setProperty(<span style="color:#808080;">newAttrName</span>, oldAttrValue); <span style="color:#808080;">log</span>.info(<span style="color:#6a8759;">&quot;={}= - property is successfully set with value {}&quot;</span>, <span style="color:#808080;">newAttrName</span>, oldAttrValue); }</pre> <p>I would like to know if there any way to keep the &quot;<strong>published status</strong>&quot; of the nodes ?</p> <p>Thanks.</p> <p>Regards.</p> </blockquote> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p>

  • Number of messages  2
    Registration date Nov 10, 2015
    0 (0 Good)
    0 (0 Bad)

    Re: Re: Update JCR node but keep as "not modified"

    by  thbaymet »  2015/11/10 16:44

    Hi,

    I don't want contributors to loose page status, it will be great if we can keep the pages "published" if they were already published before the script and keep them "modified but not published" if they were modified by contributors before the script. I don't want to publish changes after the script but keep the old status as it was.

    Will your solution handle this approch ?

    Thanks.

  • Re: Re: Update JCR node but keep as "not modified"
    2015/11/10 16:44

    thbaymet <p>Hi,</p> <p>I don&#39;t want contributors <strong>to loose page status</strong>, it will be great if we can keep the pages &quot;published&quot; if they were already published before the script and keep them &quot;modified but not published&quot; if they were modified by contributors before the script. I <em>don&#39;t want to publish changes</em> after the script but keep the old status as it was.</p> <p>Will your solution handle this approch ?</p> <p>Thanks.</p>

  • Number of messages  19
    Registration date Nov 10, 2015
    0 (0 Good)
    0 (0 Bad)

    Re: Re: Re: Update JCR node but keep as "not modified"

    by  amidani »  2015/11/10 17:22

    Adding the autoPublish mixin on a node will result to publishing this node automatically when you save your JCR session. Be careful the node will be auto published whenever you save the session, if it's a node that can be modified by contributors in the same time as you, you can try to call the jcr publication service to publish the node. I'm not aware of how to publish only your modification and not others, even if you do this in a separate session when others update the same node on the main session the last to save will probably get an error, because his image is not up to date.

    Hope it helps. 

  • Re: Re: Re: Update JCR node but keep as "not modified"
    2015/11/10 17:22

    amidani <p>Adding the autoPublish mixin on a node will result to publishing this node automatically&nbsp;when you save your JCR session. Be careful the node will be auto published whenever&nbsp;you save the session, if it&#39;s a node that can be modified by contributors in the same time as you,&nbsp;you can try to call the jcr publication service to publish the node. I&#39;m not aware of how to publish only your modification and not others, even if you do this in a separate session when others update&nbsp;the same node on the main session the last to save will probably get an error, because his image is not up to date.</p> <p>Hope it helps.&nbsp;</p>

  • Number of messages  2
    Registration date Nov 10, 2015
    1 (1 Good)
    0 (0 Bad)

    Re: Update JCR node but keep as "not modified"

    by  shyrkov »  2015/11/10 21:02

    Hello,

    you could try using the underlying (low-level) access as follows:

            JCRObservationManager.setAllEventListenersDisabled(true);
            try {
                    Node realNode = node.getRealNode();

                    // do operations directly on realNode, i.e.

                    // realNode.setProperty(...) etc.

                    session.save();
                }
            } finally {
                JCRObservationManager.setAllEventListenersDisabled(false);
            } 

    Kind regards

    Sergiy

  • Re: Update JCR node but keep as "not modified"
    2015/11/13 11:35

    shyrkov <p>Hello,</p> <p>you could try using the underlying (low-level) access as follows:</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; JCRObservationManager.setAllEventListenersDisabled(true);<br /> &nbsp; &nbsp; &nbsp; &nbsp; try {<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Node realNode = node.getRealNode();</p> <p><span style="line-height: 20.8px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span>// do operations directly on&nbsp;<span style="line-height: 20.8px;">realNode, i.e.</span></p> <p><span style="line-height: 20.8px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="line-height: 20.8px;">// realNode.setProperty(...) etc.</span></p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; session.save();<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br /> &nbsp; &nbsp; &nbsp; &nbsp; } finally {<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JCRObservationManager.setAllEventListenersDisabled(false);<br /> &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;</p> <p>Kind regards</p> <p>Sergiy</p>

  • Number of messages  407
    Registration date Nov 10, 2015
    0 (0 Good)
    0 (0 Bad)

    Re: Re: Update JCR node but keep as "not modified"

    by  thbaymet »  2015/11/13 11:34

    Hi.

    Yes !!! , your idea is great smiley. The correct method is setEventsDisabled.

    public static void setEventsDisabled(Boolean eventsDisabled) {
        JCRObservationManager.eventsDisabled.set(eventsDisabled);
    }

    I've tested and it is working correctly.

    JCRObservationManager.setEventsDisabled(true);
        log.info("{} - {}", language, node.getPath());
        for (coupleElements in attrArray) {
            log.info("={}= with ={}=", coupleElements[0], coupleElements[1]);
            updateNewAttribute(node, coupleElements[0], coupleElements[1], language);
        }
        session.save();
    JCRObservationManager.setEventsDisabled(false);

    Thanks very much for this handfull help.

    Best Regards.

  • Re: Re: Update JCR node but keep as "not modified"
    2015/11/13 11:34

    thbaymet <p>Hi.</p> <p>Yes !!! , your idea is great <img alt="smiley" height="23" src="https://www.jahia.com/modules/ckeditor/javascript/plugins/smiley/images/regular_smile.png" title="smiley" width="23" />. The correct method is <em>setEventsDisabled</em>.</p> <pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Courier 10 Pitch';font-size:10,6pt;"> <span style="color:#cc7832;">public static void </span><span style="color:#ffc66d;">setEventsDisabled</span>(Boolean eventsDisabled) { JCRObservationManager.eventsDisabled.set(eventsDisabled)<span style="color:#cc7832;">; </span>}</pre> <p>I&#39;ve tested and it is working correctly.</p> <pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Courier 10 Pitch';font-size:10,6pt;"> <span style="color:#808080;">JCRObservationManager</span>.setEventsDisabled(<span style="color:#cc7832;">true</span>); <span style="color:#808080;">log</span>.info(<span style="color:#6a8759;">&quot;{} - {}&quot;</span>, <span style="color:#808080;">language</span>, <span style="color:#808080;">node</span>.getPath()); <span style="color:#cc7832;">for </span>(coupleElements <span style="color:#cc7832;">in </span><span style="color:#808080;">attrArray</span>) { <span style="color:#808080;">log</span>.info(<span style="color:#6a8759;">&quot;={}= with ={}=&quot;</span>, coupleElements[<span style="color:#6897bb;">0</span>], coupleElements[<span style="color:#6897bb;">1</span>]); <span style="color:#808080;">updateNewAttribute</span>(<span style="color:#808080;">node</span>, coupleElements[<span style="color:#6897bb;">0</span>], coupleElements[<span style="color:#6897bb;">1</span>], <span style="color:#808080;">language</span>); } <span style="color:#808080;">session</span>.save(); <span style="color:#808080;">JCRObservationManager</span>.setEventsDisabled(<span style="color:#cc7832;">false</span>);</pre> <p>Thanks very much for this handfull help.</p> <p>Best Regards.</p>

  • Number of messages  19
    Registration date Nov 13, 2015
    0 (0 Good)
    0 (0 Bad)

    Re: Re: Re: Update JCR node but keep as "not modified"

    by  claprun »  2015/11/16 15:05

    Hi,

    Just make sure you encapsulate the call to setEventsDisabled(false) in a finally block so that it gets executed even if the rest of your code fails.

    Best,

  • Re: Re: Re: Update JCR node but keep as "not modified"
    2015/11/16 15:05

    claprun <p>Hi,</p> <p>Just make sure you encapsulate the call to setEventsDisabled(false) in a finally block so that it gets executed even if the rest of your code fails.</p> <p>Best,</p>

  • Number of messages  5
    Registration date Nov 16, 2015
    Contact
    Share
    Feedback

    Get in touch

    Whether you are a current user or if you are just evaluating Jahia, we are here to help.

    Contact us

    Share this page