Groovy script to delete broken references to non-existent images in documents

57 views
Skip to first unread message

Rodrigo de Paula Cunha

unread,
Dec 11, 2017, 3:29:19 PM12/11/17
to Hippo Community

So, I created a groovy script to identify and delete portrait images in our hippo gallery but those images were referenced by documents, now the website displays those images blank and I can see in the document nodes that the references are broken. I tried putting together a script to delete the references but was not successful so far.


Can someone please point what am I missing? the code to remove the image reference node is commented out, I'm just trying to list the broken references for now.


Really appreciate any input! 


Thanks!


import org.apache.commons.io.IOUtils
import org.apache.jackrabbit.JcrConstants
import org.apache.jackrabbit.commons.JcrUtils
import org.hippoecm.frontend.plugins.gallery.imageutil.ImageUtils
import org.hippoecm.frontend.plugins.gallery.imageutil.ScalingParameters
import org.hippoecm.frontend.plugins.gallery.processor.ScalingGalleryProcessor
import org.hippoecm.repository.api.HippoNodeType
import org.hippoecm.repository.gallery.HippoGalleryNodeType
import org.onehippo.repository.update.BaseNodeUpdateVisitor

import javax.jcr.Node
import javax.jcr.NodeIterator
import javax.jcr.RepositoryException
import javax.jcr.Session
import javax.jcr.query.Query
import javax.jcr.query.QueryManager
import javax.jcr.query.QueryResult

/**
 * Groovy script to update broken references to images.
 */

class RemovePortraitImageReferences extends BaseNodeUpdateVisitor {

   
private Session session;

   
void initialize(Session session) throws RepositoryException {

       
try {

             
this.session = session;

       
} catch (RepositoryException e) {

            log
.error("Exception while retrieving session", e)

       
}

        log
.info "Initialized script ${this.getClass().getName()}"

   
}


   
boolean doUpdate(Node node) {

       
if (node.getProperty("jcr:primaryType").getValue().getString().equals("hippo:handle")) {

                 
return checkAndRemoveBrokenImageReferencesForProperty(node);
       
}

       
return false
   
}

   
@Override
   
boolean undoUpdate(final Node node) throws RepositoryException, UnsupportedOperationException {

       
return false

   
}

   
boolean checkAndRemoveBrokenImageReferencesForProperty(Node node) {

        log
.info "Checking node property:  ${node.path}";

       
boolean retVal = false;

       
//iterate over each child node of type hipo gallery
       
for (NodeIterator iterator = node.getNodes({"oak:hippogallery"});iterator.hasNext();) {

           
Node imageNode = iterator.nextNode();

           
String imagePath = imageNode.getProperty("hippo:docbase");

           
if (JcrUtils.getNodeIfExists(imagePath, this.session)==null) {

                log
.warn "Image reference does not exist: ${imagePath}";

                retVal
= true;

               
//imageNode.remove();

           
}
       
}

       
return retVal;

   
}
}




Marijan Milicevic

unread,
Dec 11, 2017, 5:24:01 PM12/11/17
to hippo-c...@googlegroups.com
On Mon, Dec 11, 2017 at 9:29 PM, Rodrigo de Paula Cunha <rcu...@gmail.com> wrote:

So, I created a groovy script to identify and delete portrait images in our hippo gallery but those images were referenced by documents, now the website displays those images blank and I can see in the document nodes that the references are broken. I tried putting together a script to delete the references but was not successful so far.


Can someone please point what am I missing? the code to remove the image reference node is commented out, I'm just trying to list the broken references for now.



well, you are not changing anything...

this:


if (JcrUtils.getNodeIfExists(imagePath, this.session)==null) {

                log.warn "Image reference does not exist: ${imagePath}";

                retVal = true;

                //imageNode.remove();

            }





should probably be something like:




if (JcrUtils.getNodeIfExists(imagePath, this.session)==null) {

                log.warn "Image reference does not exist: ${imagePath}";

                retVal = true;

              imageNode.setProperty("hippo:docbase","cafebabe-cafe-babe-cafe-babecafebabe");
 }



cheers
marijan






 

--
Hippo Community Group: The place for all discussions and announcements about Hippo CMS (and HST, repository etc. etc.)
 
To post to this group, send email to hippo-community@googlegroups.com
RSS: https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
---
You received this message because you are subscribed to the Google Groups "Hippo Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hippo-community+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/hippo-community.
For more options, visit https://groups.google.com/d/optout.

Rodrigo de Paula Cunha

unread,
Jan 17, 2018, 4:53:34 PM1/17/18
to Hippo Community
I'm replying to my own question with the answer on how this got implemented in case someone ever needs it:

import org.apache.commons.io.IOUtils
import org.apache.jackrabbit.JcrConstants
import org.apache.jackrabbit.commons.JcrUtils
import org.hippoecm.frontend.plugins.gallery.imageutil.ImageUtils
import org.hippoecm.frontend.plugins.gallery.imageutil.ScalingParameters
import org.hippoecm.frontend.plugins.gallery.processor.ScalingGalleryProcessor
import org.hippoecm.repository.api.HippoNodeType
import org.hippoecm.repository.gallery.HippoGalleryNodeType
import org.onehippo.repository.update.BaseNodeUpdateVisitor
import org.hippoecm.repository.decorating.PropertyDecorator



import javax.jcr.Node
import javax.jcr.NodeIterator
import javax.jcr.RepositoryException
import javax.jcr.Session
import javax.jcr.query.Query
import javax.jcr.query.QueryManager
import javax.jcr.query.QueryResult


/**
 * Groovy script to remove broken image references
 *
 *
 */

class RemoveBrokenImageReferences extends BaseNodeUpdateVisitor {

 
   
private Node rootNode;

   
private Session session;


   
void initialize(Session session) throws RepositoryException {
       
try {

           
this.rootNode = session.getRootNode();

           
this.session = session;
       
} catch (RepositoryException e) {

            log
.error("Error while initializing script variables", e)

       
}


        log
.info "Initialized script ${this.getClass().getName()}"
   
}


   
boolean doUpdate(Node node) {

       
return checkAndRemoveBrokenImageReference(node);

   
}


   
@Override
   
boolean undoUpdate(final Node node) throws RepositoryException, UnsupportedOperationException {
       
return false
   
}



   
boolean checkAndRemoveBrokenImageReference(Node imageNode) {
       
//log.info "Checking node:  ${imageNode.path}";
     
       
if (!imageNode.isNodeType("hippogallerypicker:imagelink")) {
            log
.info "Node is not of type hippogallerypicker:imagelink, skipping..."    
           
return false;
       
}




       
String imagePath = imageNode.getProperty("hippo:docbase").getString();
       
//log.warn "Checking image path: ${imagePath}";
           
       
Node imgRefNode = null;
       
try {
                imgRefNode
= this.session.getNodeByIdentifier(imagePath);
       
} catch (javax.jcr.ItemNotFoundException ex) {
                imgRefNode
= null;
       
}


       
if (imgRefNode==null) {
         
            log
.warn "[DELETE] Image reference does not exist: ${imageNode.path}(${imagePath})";


            imageNode
.remove();


           
return true;
       
} else {
           
//log.info "[SKIP] Image reference exists ${imageNode.path}(${imagePath}), skipping..."
           
           
return false;
       
}


   
}
   
}

Reply all
Reply to author
Forward
0 new messages