1 package info.magnolia.beancoder; 2 3 import info.magnolia.cms.core.Content; 4 import info.magnolia.cms.core.ItemType; 5 import info.magnolia.cms.core.NodeData; 6 import info.magnolia.cms.util.ContentUtil; 7 import info.magnolia.cms.util.DateUtil; 8 import info.magnolia.cms.util.NodeDataUtil; 9 import openwfe.org.jcr.Item; 10 import openwfe.org.jcr.JcrException; 11 import openwfe.org.jcr.Node; 12 import openwfe.org.jcr.Property; 13 14 import javax.jcr.RepositoryException; 15 import java.io.InputStream ; 16 import java.util.Date ; 17 import java.util.Iterator ; 18 19 22 public class MgnlNode implements Node { 23 Content mnode; 24 25 public MgnlNode(Content mnode) { 26 this.mnode = mnode; 27 } 28 29 public Iterator getProperties() throws JcrException { 30 return new MgnlPropertyIterator(mnode); 31 } 32 33 public Iterator getNodes() throws JcrException { 34 return new MgnlNodeIterator(mnode); 35 } 36 37 public Property setProperty(String string, Object object) throws JcrException { 38 Property property = getProperty(string); 39 try { 40 final NodeData nodeData = ((NodeData) property.getWrappedInstance()); 41 if (object instanceof String ) 42 nodeData.setValue((String ) object); 43 else if (object instanceof Date ) 44 nodeData.setValue(DateUtil.getUTCCalendarFromLocalDate((Date ) object)); 45 else if (object instanceof InputStream ) 46 nodeData.setValue((InputStream )object); 47 else if (object instanceof Double ) 48 nodeData.setValue(((Double )object).doubleValue()); 49 else if (object instanceof Long ) 50 nodeData.setValue(((Long )object).longValue()); 51 else if (object instanceof Boolean ) 52 nodeData.setValue(((Boolean )object).booleanValue()); 53 else 54 throw new JcrException("Does not support object of kind:"+object.getClass().getName()); 55 } catch (RepositoryException e) { 56 throw new JcrException(e.getMessage(),e); 57 } 58 return property; 59 } 60 61 public String getPath() throws JcrException { 62 return mnode.getHandle(); 63 } 64 65 public Object getWrappedInstance() throws JcrException { 66 return mnode; 67 } 68 69 public boolean hasProperty(String propertyName) throws JcrException { 70 try { 71 return mnode.hasNodeData(propertyName); 72 } catch (RepositoryException e) { 73 throw new JcrException(e.getMessage()); 74 } 75 } 76 77 public Property getProperty(String propertyName) throws JcrException { 78 try { 79 return new MgnlProperty(this, NodeDataUtil.getOrCreate(mnode,propertyName)); 80 } catch (RepositoryException e) { 81 throw new JcrException(e.getMessage()); 82 } 83 } 84 85 public Property setProperty(String propertyName, String value) throws JcrException { 86 try { 87 NodeData nodeData = NodeDataUtil.getOrCreate(mnode,propertyName); 88 nodeData.setValue(value); 89 return new MgnlProperty(this,nodeData); 90 } catch (RepositoryException e) { 91 throw new JcrException(e.getMessage()); 92 } 93 } 94 95 public Property setProperty(String propertyName, long value) throws JcrException { 96 try { 97 NodeData nodeData = NodeDataUtil.getOrCreate(mnode,propertyName); 98 nodeData.setValue(value); 99 return new MgnlProperty(this,nodeData); 100 } catch (RepositoryException e) { 101 throw new JcrException(e.getMessage()); 102 } 103 } 104 105 public boolean hasNode(String relPath) throws JcrException { 106 try { 107 return mnode.hasContent(relPath); 108 } catch (RepositoryException e) { 109 throw new JcrException(e.getMessage()); 110 } 111 } 112 113 public Node getNode(String relPath) throws JcrException { 114 try { 115 Content c = ContentUtil.getOrCreateContent(mnode,relPath, ItemType.CONTENTNODE); 116 return new MgnlNode(c); 117 } catch (RepositoryException e) { 118 throw new JcrException(e.getMessage()); 119 } 120 } 121 122 public Node addNode(String newNodeName) throws JcrException { 123 try { 124 return new MgnlNode(mnode.createContent(newNodeName)); 125 } catch (RepositoryException e) { 126 throw new JcrException(e.getMessage()); 127 } 128 } 129 130 public String getName() throws JcrException { 131 return mnode.getName(); 132 } 133 134 public Item getParent() throws JcrException { 135 try { 136 return new MgnlNode(mnode.getParent()); 137 } catch (RepositoryException e) { 138 throw new JcrException(e.getMessage()); 139 } 140 } 141 142 public boolean isNode() throws JcrException { 143 return true; 144 } 145 146 public void save() throws JcrException { 147 try { 148 mnode.save(); 149 } catch (RepositoryException e) { 150 throw new JcrException(e.getMessage()); 151 } 152 } 153 154 class MgnlNodeIterator implements Iterator { 155 Content node; 156 private Iterator internalIterator; 157 158 public MgnlNodeIterator(Content node) { 159 this.node = node; 160 this.internalIterator = node.getChildren().iterator(); 161 } 162 163 public void remove() { 164 internalIterator.remove(); 165 } 166 167 public boolean hasNext() { 168 return internalIterator.hasNext(); 169 } 170 171 public Object next() { 172 return new MgnlNode((Content)internalIterator.next()); 173 } 174 } 175 176 class MgnlPropertyIterator implements Iterator { 177 private Content mnode; 178 private Iterator internalIterator; 179 180 public MgnlPropertyIterator(Content mnode) { 181 this.mnode = mnode; 182 this.internalIterator = mnode.getNodeDataCollection().iterator(); 183 } 184 185 public void remove() { 186 internalIterator.remove(); 187 } 188 189 public boolean hasNext() { 190 return internalIterator.hasNext(); 191 } 192 193 public Object next() { 194 return new MgnlProperty(new MgnlNode(mnode),(NodeData)internalIterator.next()); 195 } 196 } 197 198 201 public Property setProperty(String propertyName, String value, int noideaofwhatisthis) throws JcrException { 202 return setProperty(propertyName, value); 208 } 209 210 public void remove() throws JcrException { 211 try { 212 this.mnode.delete(); 213 } catch (RepositoryException e) { 214 throw new JcrException(e.getMessage(),e); 215 } 216 217 } 218 } 219 | Popular Tags |