Need Help With Setting The Value Of An Object Property Thru a DocumentUpdatingEvent

I have an issue where I need to update the value of an object property on a page when (or before) the page is updated.

The logic is simple - If the user changes the status to “Closed” then update the closed_dt. The script works fine and the event is triggered. However, When the event fires the “set” for the attached object " SHPQUALITYObject" the script fails.

WHY!!!

Please Help

       if (newStatus == "Closed") {
           // Set Closed Date as today
           SHPQUALITYObject.set("closed_dt", xwiki.getDate())
           //Append the Comment
           docSource.setComment(SHPDocComment + " Status CLOSED")
        }

The Entire Code

{{groovy}}
  def docSource = xcontext.method.input.get(1)
  def SHPDocObject = docSource.getObject("SHPDocuments.SHPDocumentClass")
  if (SHPDocObject) {
  //Lets See if its in Status Control
  def SHPTDObject = docSource.getObject("SHPTD.SHPTDClass")      
  def SHPQUALITYObject = docSource.getObject("SHPQUALITY.SHPQUALITYClass")      
  if (SHPQUALITYObject) {
     //Get the status of This Document (the one thats changing)
     def newStatus = SHPQUALITYObject.get("status").value
     //Get the old Document (the one that already exists if there is one )
     if (xwiki.exists(docSource.fullName))  {         
         def oldDoc = xwiki.getDocument(docSource.fullName)
         def oldSHPQUALITYObject = oldDoc.getObject("SHPQUALITY.SHPQUALITYClass")      
         //dont use .value here as it dosnt work (unknown Reason)
         def oldStatus = oldSHPQUALITYObject.get("status")
         if (oldStatus !=  newStatus) {
            //Get the old Comment
            def SHPDocComment = docSource.getComment()
            // Check What Status Changed and Set Appropriate Stuff                     
           if (newStatus == "Closed") {
               // Set Closed Date as today
               SHPQUALITYObject.set("closed_dt", xwiki.getDate())
               //Append the Comment
               docSource.setComment(SHPDocComment + " Status CLOSED")
            }
           if (oldStatus == "Closed") {
               // Remove Closed Date
               SHPQUALITYObject.set("closed_dt",null)
               //Append the Comment
               docSource.setComment(SHPDocComment + " Status REOPENED")
            }
         }
     }
 }  
  }
{{/groovy}}

You should not use xwiki.getDocument. You can get the previous XWikiDocument using docSource.originalDocument.

Note that if you are only listening to DocumentUpdatingEvent you won’t be called for new document (just making sure you are aware of it :slight_smile: ).

What do you mean by “fail” exactly ? You get some error ?

I don’t really understand what is the link between the first extract and “The Entire Code” since can’t find it in it.

This Line Causes the Script to Fail - I can read from the object (get) but I cant “Set” the property
SHPQUALITYObject.set("closed_dt", xwiki.getDate())
I know this because the comment isnt updated and the attribute of the object isn’t set.

Full Script (with suggestions added) Same issue as before

{{groovy}}
  def docSource = xcontext.method.input.get(1)
  def SHPDocObject = docSource.getObject("SHPDocuments.SHPDocumentClass")
  if (SHPDocObject) {
      //Lets See if its in Status Control
      def SHPQUALITYObject = docSource.getObject("SHPQUALITY.SHPQUALITYClass")      
      if (SHPQUALITYObject) {
         //Get the status of This Document (the one thats changing)
         def newStatus = SHPQUALITYObject.get("status").value
         //Get the old Document (the one that already exists if there is one )
         def oldDoc = docSource.getOriginalDocument()
         def oldSHPQUALITYObject = oldDoc.getObject("SHPQUALITY.SHPQUALITYClass")      
         def oldStatus = oldSHPQUALITYObject.get("status").value
          if (newStatus == "Closed") {
               // Set Closed Date as today
               SHPQUALITYObject.set("closed_dt", xwiki.getDate())
               //Append the Comment
               docSource.setComment("CLOSED")
          }
       }
  }
{{/groovy}}

You might want to make sure your script is executed by adding something like System.out.println(“It works !”), from what I remember you don’t get any error for things like syntax error. Maybe there is some groovy issue we did not noticed.

it seems the context is the key here - I found an example that explicitly got the context and added it as a parm for the set (with implicit imports…) xcontext does not work

This works Just fine… But WHY???

import org.xwiki.context.*
....
  def crtContext = Utils.getComponent(Execution.class).getContext().getProperty('xwikicontext')
....
SHPQUALITYObject.set("closed_dt", xwiki.getDate(),crtContext)

Full Code

{{groovy}}
import org.xwiki.observation.*
import org.xwiki.observation.event.*
import org.xwiki.bridge.event.*
import org.xwiki.model.reference.*
import org.xwiki.model.*
import com.xpn.xwiki.web.*
import com.xpn.xwiki.*
import org.xwiki.context.*
  // DONT USE SINGLE QUOTES!!!!!!!!!
  def crtContext = Utils.getComponent(Execution.class).getContext().getProperty('xwikicontext')
  def docSource = xcontext.method.input.get(1)
  def SHPDocObject = docSource.getObject("SHPDocuments.SHPDocumentClass")
  if (SHPDocObject) {
      //Lets See if its in Status Control
      def SHPTDObject = docSource.getObject("SHPTD.SHPTDClass")      
      def SHPQUALITYObject = docSource.getObject("SHPQUALITY.SHPQUALITYClass")      
      if (SHPQUALITYObject) {
         //Get the status of This Document (the one thats changing)
         def newStatus = SHPQUALITYObject.get("status").value
         //Get the old Document (the one that already exists if there is one )
         if (xwiki.exists(docSource.fullName))  {         
             //def oldDoc = xwiki.getDocument(docSource.fullName)
             def oldDoc = docSource.getOriginalDocument()
             def oldSHPQUALITYObject = oldDoc.getObject("SHPQUALITY.SHPQUALITYClass")      
             def oldStatus = oldSHPQUALITYObject.get("status").value
             if (oldStatus !=  newStatus) {
                //Get the old Comment
                def SHPDocComment = docSource.getComment()
                // Check What Status Changed and Set Appropriate Stuff                     
               if (newStatus == "Closed") {
                   // Set Closed Date as today
                   SHPQUALITYObject.set("closed_dt", xwiki.getDate(),crtContext)
                   //Append the Comment
                   docSource.setComment("CLOSED")
                }
               if (oldStatus == "Closed") {
                   // Remove Closed Date
                   SHPQUALITYObject.set("closed_dt", "",crtContext)
                   //Append the Comment
                   docSource.setComment("REOPENED")
                }
             }
         }
     }  
  }
{{/groovy}}
1 Like