Added by Manuel Molaschi, last edited by Manuel Molaschi on Sep 22, 2009  (view change)

Labels:

openutils-mgnlgroovy openutils-mgnlgroovy Delete
Enter labels to add to this page:
Wait Image 
Looking for a label? Just start typing.

Here a groovy script to use when you have broken item states in some nodes on your magnolia public instance and you can't delete / republish / move / ... the content node.

WARNING: Use it and your own risk!

Download and install the openutils-mgnlgroovy module in your magnolia instance (see http://lab.openmindonline.it/lab/products/groovy.html ).

In admin central go to Tools > Groovy shell

Copy the following groovy script and paste in "Groovy script" code area.

package net.sourceforge.openutils.mgnlgroovy.scripts;

import info.magnolia.cms.beans.config.ContentRepository
import info.magnolia.cms.core.Content
import info.magnolia.cms.core.HierarchyManager
import info.magnolia.cms.core.ItemType
import info.magnolia.cms.core.NodeData
import info.magnolia.cms.core.Path
import info.magnolia.cms.security.AccessManager
import info.magnolia.cms.security.Permission
import info.magnolia.cms.util.ContentUtil
import info.magnolia.cms.util.NodeDataUtil
import info.magnolia.context.MgnlContext

import org.slf4j.Logger
import org.slf4j.LoggerFactory

import javax.jcr.AccessDeniedException
import javax.jcr.RepositoryException

import java.io.StringWriter
import java.io.PrintWriter

import org.apache.jackrabbit.core.ItemImpl
import org.apache.jackrabbit.core.WorkspaceImpl
import org.apache.jackrabbit.core.state.NodeState

import java.util.ArrayList

str = ""

repo = "YOUR_REPO"
nodeUUID = "NODE_UUID"
brokenStateUUID = "ITEM_STATE_UUID"

hm = MgnlContext.getSystemContext().getHierarchyManager(YOUR_REPO)
try {
  c = hm.getContentByUUID(nodeUUID)
  itemid = c.getJCRNode().getId()
  ns = hm.getWorkspace().getItemStateManager().getItemState(itemid)
  if (ns.hasChildNodeEntries()) {
    // use temp array to avoid ConcurrentModificationException
    tmp = new ArrayList(ns.getChildNodeEntries());
    for (int i = tmp.size() - 1; i >= 0; i--) {
      entry = tmp.get(i);
      // recursively remove child node
      if (entry.getId().getUUID().toString().equals(brokenStateUUID))
      {
        ns.removeChildNodeEntry(entry.getId())
      }
    }
  }
  c.delete()
  hm.save()
}
catch(Throwable t)
{
  StringWriter sw = new StringWriter()
  PrintWriter pw = new PrintWriter(sw)
  t.printStackTrace(pw)
  str = sw.toString()
}

return str

Replace

  • YOUR_REPO with your repo string
  • NODE_UUID with the uuid of the node holding broken item state
  • ITEM_STATE_UUID with the broken item state UUID

And now click run (...)

Sorry but...

hm = MgnlContext.getSystemContext().getHierarchyManager(YOUR_REPO)

shuld be:

hm = MgnlContext.getSystemContext().getHierarchyManager(repo)

Strap

Posted by Anonymous at Feb 19, 2010 11:19 | Reply To This

For the love of God, keep writnig these articles.

Posted by Anonymous at Nov 12, 2011 21:14 | Reply To This

gWBBpi <a href="http://rhnenpyakmzy.com/">rhnenpyakmzy</a>

Posted by Anonymous at Nov 13, 2011 10:04 | Reply To This

WRf4ye , [url=http://bdvrrvfjvdca.com/]bdvrrvfjvdca[/url], [link=http://gaerymgzrcwq.com/]gaerymgzrcwq[/link], http://porlyjxtrwxs.com/

Posted by Anonymous at Nov 13, 2011 19:54 | Reply To This

V2RrOL <a href="http://eniggcrqptwi.com/">eniggcrqptwi</a>

Posted by Anonymous at Nov 15, 2011 17:35 | Reply To This

2lyiRK , [url=http://qiebjykfwwed.com/]qiebjykfwwed[/url], [link=http://ehewambbqytk.com/]ehewambbqytk[/link], http://ygvhjifdmiqm.com/

Posted by Anonymous at Nov 16, 2011 12:19 | Reply To This

A new script version that find all broken state, create a little report and erase the node (if you want...).

It's useful run the script with

check(c.getJCRNode(), hm, false)

in order to have a report for broken nodes.When you're sure to run the script and delete all nodes, switch third param to true.

Before run script, set the two variables repo and path with your repository and your path.

package net.sourceforge.openutils.mgnlgroovy.scripts;

import info.magnolia.cms.core.HierarchyManager
import info.magnolia.context.MgnlContext

import org.apache.jackrabbit.core.ItemImpl
import org.apache.jackrabbit.core.WorkspaceImpl
import org.apache.jackrabbit.core.state.NodeState
import org.apache.jackrabbit.uuid.UUID
import org.apache.jackrabbit.core.NodeId

import info.magnolia.cms.util.*

import java.util.ArrayList

repo = "YOUR_REPO"
path = "YOUR_PATH"

str = ""

def check(node, hm, fix){

  node.getNodes().each({
    check(it, hm, fix)
  })

  itemid = node.getId()
  ns = hm.getWorkspace().getItemStateManager().getItemState(itemid)
  def isbroken = false
  if (ns.hasChildNodeEntries()) {
    // use temp array to avoid ConcurrentModificationException
    tmp = new ArrayList(ns.getChildNodeEntries());
    for (int i = tmp.size() - 1; i >= 0; i--) {
      entry = tmp.get(i);
      try
      {
         // if a state is broken, this call will throw a NoSuchItemStateException
         hm.getWorkspace().getItemStateManager().getItemState(entry.getId())
      }
      catch(Exception ex)
      {
        str += "Broken state on node ${node.getPath()} with UUID: ${node.getUUID().toString()} (broken state id: ${entry.getId().getUUID().toString()})"
        if (fix)
        {
          str += "... broken state removed"
          ns.removeChildNodeEntry(entry.getId())
          isbroken = true
        }
        str += "\n"
      }
    }
  }

  if (fix && isbroken)
  {
    node.remove()
    hm.save()
  }
}

hm = MgnlContext.getSystemContext().getHierarchyManager(repo)
try {
  c = hm.getContent(path)

  str = "Started broken item states seeker on node ${c.getHandle()}"
  check(c.getJCRNode(), hm, false)
}
catch(Throwable t)
{
  StringWriter sw = new StringWriter()
  PrintWriter pw = new PrintWriter(sw)
  t.printStackTrace(pw)
  str = sw.toString()
}

return str

Enjoy.

  • Special thanks to Manuel, his groovy kung-fu it's stronger than my. -

Walking in the presence of giants here. Cool thinking all aronud!

Posted by Anonymous at Nov 12, 2011 03:14 | Reply To This

BROasN <a href="http://reypsfdjvizw.com/">reypsfdjvizw</a>

Posted by Anonymous at Nov 12, 2011 11:00 | Reply To This

vmckb8 , [url=http://sljqjyfdgjda.com/]sljqjyfdgjda[/url], [link=http://bafjghzjstsq.com/]bafjghzjstsq[/link], http://eaozbtqzdycd.com/

Posted by Anonymous at Nov 12, 2011 16:21 | Reply To This

1f9vts <a href="http://pkodgstfjlfr.com/">pkodgstfjlfr</a>

Posted by Anonymous at Nov 14, 2011 11:10 | Reply To This

RodsQL , [url=http://esdjjewiirlb.com/]esdjjewiirlb[/url], [link=http://myfqyrsnreyu.com/]myfqyrsnreyu[/link], http://newozontwhwh.com/

Posted by Anonymous at Nov 16, 2011 12:09 | Reply To This

I'm quite pleased with the infortamion in this one. TY!

Posted by Anonymous at Nov 13, 2011 05:50 | Reply To This

vx3Aur <a href="http://flmjhbtfuatx.com/">flmjhbtfuatx</a>

Posted by Anonymous at Nov 13, 2011 15:16 | Reply To This

onvoPp , [url=http://ykvtzsvphjvp.com/]ykvtzsvphjvp[/url], [link=http://kjuzmkjacvoy.com/]kjuzmkjacvoy[/link], http://osglmovarbkc.com/

Posted by Anonymous at Nov 13, 2011 20:30 | Reply To This

WA9rOU <a href="http://ivhoayjfnqwp.com/">ivhoayjfnqwp</a>

Posted by Anonymous at Nov 15, 2011 18:14 | Reply To This

OcR2KL , [url=http://uzlqypzolnsw.com/]uzlqypzolnsw[/url], [link=http://avmfhbxxmmid.com/]avmfhbxxmmid[/link], http://mwaemnqwqmkq.com/

Posted by Anonymous at Nov 17, 2011 12:07 | Reply To This