info.magnolia.module.groovy.support.nodes
Class MgnlGroovyNode

java.lang.Object
  extended by info.magnolia.cms.core.ContentHandler
      extended by info.magnolia.cms.core.AbstractContent
          extended by info.magnolia.cms.util.ContentWrapper
              extended by info.magnolia.module.groovy.support.nodes.MgnlGroovyNode
All Implemented Interfaces:
info.magnolia.cms.core.Content, java.lang.Cloneable

public class MgnlGroovyNode
extends info.magnolia.cms.util.ContentWrapper

A special groovish implementation of Magnolia's ContentWrapper. This makes it possible, for example, navigating the nodes in a Magnolia repository with a . (dot) notation and access their properties with . or .@ notation much like it happens when using the result of parsing an xml with groovy's XmlSlurper. For example, here is how, in a groovy script, one could navigate to and print the node data named abstract:

 hm = ctx.getHierarchyManager('website')
 node = hm.getContent(hm, '/demo-project')
 println node.about.history.abstract
 
The above example can also be made more groovy-like by using the @ notation to access the attribute
 node.about.history.@abstract
 
As node is also a Content, you can call any of its methods. E.g.
 println node.metaData.template
 println node.about.children.title
 println node.about.parent (this can return null)
 
IMPORTANT: The .children shortcut, unlikely Content.getChildren(), always returns ItemType.CONTENTs AND ItemType.CONTENTNODEs . If you only need either type, then call directly one of the Content.getChildren() methods on the parent node.

If you want to look at the attributes or data for a certain node you can simply call
 println node.about.nodeData
 
It is also possible to assign values to node data or create new ones. E.g.
 node.boo = 3.14d
 node.coo = true
 node.foo = 'some text'
 node.doo = 100
 
will assign the values on the right hand side to the node data on left hand side. Should those not exist, they will be automatically created. Furthermore, the correct type will be detected based on the value assigned (i.e. Boolean, String, Long or Double).

IMPORTANT: All the above assignments will be in-memory only unless explicitly persisted via a call to save() on a parent node.

Version:
$Id$
Author:
fgrilli

Nested Class Summary
 
Nested classes/interfaces inherited from interface info.magnolia.cms.core.Content
info.magnolia.cms.core.Content.ContentFilter
 
Field Summary
protected static org.slf4j.Logger log
           
 
Fields inherited from class info.magnolia.cms.core.ContentHandler
hierarchyManager
 
Constructor Summary
MgnlGroovyNode(info.magnolia.cms.core.Content content)
           
 
Method Summary
 boolean equals(java.lang.Object obj)
           
 java.lang.Object get(java.lang.String key)
          Provides lookup of elements by non-namespaced name.
 java.util.List<info.magnolia.cms.core.Content> getAt(groovy.xml.QName name)
          Deprecated. This method is wrong and luckily unused by this module. Use getByName(String) instead.
protected  info.magnolia.cms.core.Content getByName(java.lang.String name)
          Provides lookup of elements by name.
protected  java.lang.Object getNodeDataValue(java.lang.String attributeName)
          Returns the value for the given attribute (nodeData).
 int hashCode()
           
 java.util.Iterator<info.magnolia.cms.core.Content> iterator()
           
 java.lang.String name()
           
protected static void setMetaClass(groovy.lang.MetaClass metaClass, java.lang.Class nodeClass)
           
 java.lang.String toString()
           
protected  info.magnolia.cms.core.Content wrap(info.magnolia.cms.core.Content node)
           
 
Methods inherited from class info.magnolia.cms.util.ContentWrapper
addMixin, addVersion, addVersion, createContent, delete, deleteNodeData, getAllVersions, getAncestor, getAncestors, getBaseVersion, getChildByName, getChildren, getContent, getHandle, getHierarchyManager, getIndex, getItemType, getJCRNode, getLevel, getLock, getMetaData, getMixinNodeTypes, getName, getNodeDataCollection, getNodeType, getNodeTypeName, getParent, getTemplate, getTitle, getUUID, getVersionedContent, getVersionedContent, getVersionHistory, getWorkspace, getWrappedContent, hasContent, hasMetaData, holdsLock, isGranted, isLocked, isModified, isNodeData, isNodeType, lock, lock, newNodeDataInstance, orderBefore, refresh, removeMixin, removeVersionHistory, restore, restore, restore, restoreByLabel, save, setWrappedContent, unlock, updateMetaData, wrap, wrapContentNodes, wrapNodeDatas
 
Methods inherited from class info.magnolia.cms.core.AbstractContent
createContent, createContent, createNodeData, createNodeData, createNodeData, createNodeData, createNodeData, delete, getBinaryNodeDatas, getChildren, getChildren, getChildren, getChildren, getChildren, getChildren, getNodeData, getNodeDataCollection, hasChildren, hasChildren, hasNodeData, setNodeData, setNodeData, setNodeData, setNodeData, setNodeData, setNodeData, setNodeData, setNodeData, setNodeData, setNodeData
 
Methods inherited from class info.magnolia.cms.core.ContentHandler
clone, getAccessManager, setAccessManager, setHierarchyManager
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface info.magnolia.cms.core.Content
getAccessManager
 

Field Detail

log

protected static final org.slf4j.Logger log
Constructor Detail

MgnlGroovyNode

public MgnlGroovyNode(info.magnolia.cms.core.Content content)
Method Detail

setMetaClass

protected static void setMetaClass(groovy.lang.MetaClass metaClass,
                                   java.lang.Class nodeClass)

iterator

public java.util.Iterator<info.magnolia.cms.core.Content> iterator()

name

public java.lang.String name()

get

public java.lang.Object get(java.lang.String key)
Provides lookup of elements by non-namespaced name.

Parameters:
key - the name (or shortcut key) of the node(s) of interest
Returns:
the nodes which match key

wrap

protected info.magnolia.cms.core.Content wrap(info.magnolia.cms.core.Content node)
Overrides:
wrap in class info.magnolia.cms.util.ContentWrapper

getNodeDataValue

protected java.lang.Object getNodeDataValue(java.lang.String attributeName)
Returns the value for the given attribute (nodeData). The Java types returned vary according to the underlying PropertyType.


getAt

public java.util.List<info.magnolia.cms.core.Content> getAt(groovy.xml.QName name)
Deprecated. This method is wrong and luckily unused by this module. Use getByName(String) instead.

Provides lookup of elements by QName.

Parameters:
name - the QName of interest
Returns:
the nodes matching name

getByName

protected info.magnolia.cms.core.Content getByName(java.lang.String name)
Provides lookup of elements by name. Also handles QNames

Parameters:
name - the name of interest
Returns:
the nodes matching name

toString

public java.lang.String toString()
Overrides:
toString in class info.magnolia.cms.util.ContentWrapper

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object


Copyright © 2010 Magnolia International Ltd.. All Rights Reserved.