View Javadoc
1   /**
2    * This file Copyright (c) 2012-2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.vaadin.integration.jcr;
35  
36  import info.magnolia.cms.core.Path;
37  import info.magnolia.jcr.RuntimeRepositoryException;
38  import info.magnolia.jcr.util.NodeUtil;
39  import info.magnolia.jcr.util.PropertyUtil;
40  
41  import java.util.HashMap;
42  import java.util.LinkedHashMap;
43  import java.util.Map;
44  import java.util.Map.Entry;
45  
46  import javax.jcr.Item;
47  import javax.jcr.Node;
48  import javax.jcr.RepositoryException;
49  
50  import org.apache.commons.lang3.StringUtils;
51  import org.slf4j.Logger;
52  import org.slf4j.LoggerFactory;
53  
54  import com.vaadin.data.Property;
55  
56  /**
57   * Abstract implementation of an {@link com.vaadin.data.Item} wrapping/representing a {@link javax.jcr.Node}. Implements {Property.ValueChangeListener} in order to inform/change JCR
58   * property when a Vaadin property has changed. Access JCR repository for all read Jcr Property.
59   */
60  public abstract class AbstractJcrNodeAdapter extends AbstractJcrAdapter {
61  
62      private static final Logger log = LoggerFactory.getLogger(AbstractJcrNodeAdapter.class);
63  
64      private String primaryNodeType;
65  
66      private final Map<String, AbstractJcrNodeAdapter> children = new LinkedHashMap<String, AbstractJcrNodeAdapter>();
67  
68      private final Map<String, AbstractJcrNodeAdapter> removedChildren = new HashMap<String, AbstractJcrNodeAdapter>();
69  
70      private AbstractJcrNodeAdapter parent;
71  
72      private String nodeName;
73  
74      private boolean childItemChanges = false;
75  
76      public AbstractJcrNodeAdapter(Node jcrNode) {
77          super(jcrNode);
78      }
79  
80      @Override
81      public boolean isNode() {
82          return true;
83      }
84  
85      @Override
86      protected void initCommonAttributes(Item jcrItem) {
87          super.initCommonAttributes(jcrItem);
88          Node node = (Node) jcrItem;
89          try {
90              if (StringUtils.isBlank(primaryNodeType)) {
91                  primaryNodeType = node.getPrimaryNodeType().getName();
92              }
93          } catch (RepositoryException e) {
94              log.error("Could not determine primaryNodeType name of JCR node", e);
95              primaryNodeType = UNIDENTIFIED;
96          }
97      }
98  
99      protected void setPrimaryNodeTypeName(String primaryNodeTypeName) {
100         this.primaryNodeType = primaryNodeTypeName;
101     }
102 
103     /**
104      * Return the Primary node type Name. This Node type is defined based on the related JCR Node.
105      * In case of new Node, the Type is passed during the construction of the new Item or if not
106      * defined, the Type is equivalent to the Parent Node Type.
107      */
108     public String getPrimaryNodeTypeName() {
109         return primaryNodeType;
110     }
111 
112     protected Map<String, AbstractJcrNodeAdapter> getRemovedChildren() {
113         return removedChildren;
114     }
115 
116     /**
117      * Return the corresponding node directly from the JCR repository. <b> The returned Node does
118      * not contains all changes done on the current Item, but it's a representation of the current
119      * stored Jcr node. </b> To get the Jcr Node including the changes done on the current Item, use
120      * applyChanges().
121      */
122     @Override
123     public Node getJcrItem() {
124         return (Node)super.getJcrItem();
125     }
126 
127     /**
128      * Add a new JCR Property.
129      */
130     @Override
131     public boolean addItemProperty(Object id, Property property) {
132         // REMOVE ME: Never called as overrides by sub class.
133         log.debug("Add new Property Item name " + id + " with value " + property.getValue());
134         try {
135             Node node = getJcrItem();
136             String propertyName = (String) id;
137             if (!node.hasProperty(propertyName)) {
138                 // Create Property.
139                 node.setProperty(propertyName, (String) property.getValue());
140                 getChangedProperties().put((String)id, property);
141                 return true;
142             } else {
143                 log.warn("Property " + id + " already exist.");
144                 return false;
145             }
146         } catch (RepositoryException e) {
147             log.error("Unable to add JCR property", e);
148             return false;
149         }
150     }
151 
152     /**
153      * @return the property if it already exist on the JCR Item a new property if this property
154      *         refer to the JCR Node name null if the property doesn't exist yet.
155      */
156     @Override
157     public Property getItemProperty(Object id) {
158         Object value;
159         Class type = String.class;
160         try {
161             final Node jcrNode = getJcrItem();
162             if (!jcrNode.hasProperty((String) id)) {
163                 if (ModelConstants.JCR_NAME.equals(id)) {
164                     value = jcrNode.getName();
165                 } else {
166                     return null;
167                 }
168             } else {
169                 value = PropertyUtil.getPropertyValueObject(jcrNode, String.valueOf(id));
170                 type = value.getClass();
171             }
172         } catch (RepositoryException e) {
173             throw new RuntimeRepositoryException(e);
174         }
175         DefaultProperty property = new DefaultProperty(type, value);
176         getChangedProperties().put((String) id, property);
177         return property;
178     }
179 
180     /**
181      * Returns the JCR Node represented by this Item with changes applied. Updates both properties and child nodes. Will
182      * create new properties, set new values and remove those requested for removal. Child nodes will also be added,
183      * updated or removed.
184      */
185     @Override
186     public Node applyChanges() throws RepositoryException {
187         // get Node from repository
188         Node node = getJcrItem();
189 
190         // Update Node properties and children
191         updateProperties(node);
192         updateChildren(node);
193 
194         return node;
195     }
196 
197     /**
198      * Updates and removes children based on the {@link #children} and {@link #removedChildren} maps.
199      *
200      * TODO: Has been made public as of MGNLUI-3124 resolution. Needs further API improvement (e.g. no-arg version or possibly some other better way to update the JCR node internals).
201      */
202     public void updateChildren(Node node) throws RepositoryException {
203         if (!children.isEmpty()) {
204             for (AbstractJcrNodeAdapter child : children.values()) {
205                 // Update child node as well
206                 child.applyChanges();
207             }
208         }
209         // Remove child node if needed
210         if (!removedChildren.isEmpty()) {
211             for (AbstractJcrNodeAdapter removedChild : removedChildren.values()) {
212                 if (node.hasNode(removedChild.getNodeName())) {
213                     node.getNode(removedChild.getNodeName()).remove();
214                 }
215             }
216         }
217     }
218 
219     @Override
220     public void updateProperties(Item item) throws RepositoryException {
221         super.updateProperties(item);
222         if (item instanceof Node) {
223             // Remove Properties
224             Node node = (Node) item;
225             for (Entry<String, Property> entry : getRemovedProperties().entrySet()) {
226                 if (node.hasProperty(entry.getKey())) {
227                     node.getProperty(entry.getKey()).remove();
228                 }
229             }
230             getRemovedProperties().clear();
231         }
232     }
233 
234     /**
235      * Update or remove property. Property with flag saveInfo to false will not be updated. Property
236      * can refer to node property (like name, title) or node.MetaData property like
237      * (MetaData/template). Also handle the specific case of node renaming. If property JCR_NAME is
238      * present, Rename the node.
239      * In case the value has changed to null, it will be removed. When being called from {@link #updateProperties}
240      * we have to make sure it is removed before running in here as {@link #removeItemProperty(java.lang.Object)}
241      * is manipulating the {@link #changedProperties} list directly.
242      */
243     @Override
244     protected void updateProperty(Item item, String propertyId, Property property) {
245         if (!(item instanceof Node)) {
246             return;
247         }
248         Node node = (Node) item;
249         if (ModelConstants.JCR_NAME.equals(propertyId)) {
250             String jcrName = (String) property.getValue();
251             try {
252                 if (jcrName != null && !jcrName.isEmpty() && !jcrName.equals(node.getName())) {
253 
254                     // make sure new path is available
255                     jcrName = Path.getValidatedLabel(jcrName);
256                     jcrName = Path.getUniqueLabel(node.getSession(), node.getParent().getPath(), jcrName);
257 
258                     NodeUtil.renameNode(node, jcrName);
259 
260                     setItemId(JcrItemUtil.getItemId(node));
261                 }
262             } catch (RepositoryException e) {
263                 log.error("Could not rename JCR Node.", e);
264             }
265         } else if (propertyId != null && !propertyId.isEmpty()) {
266             if (property.getValue() != null) {
267                 try {
268                     PropertyUtil.setProperty(node, propertyId, property.getValue());
269                 } catch (RepositoryException e) {
270                     log.error("Could not set JCR Property {}", propertyId, e);
271                 }
272             } else {
273                 removeItemProperty(propertyId);
274                 log.debug("Property '{}' has a null value: Will be removed", propertyId);
275             }
276         }
277     }
278 
279     /**
280      * @param nodeName name of the child node
281      * @return child if part of the children, or null if not defined.
282      */
283     public AbstractJcrNodeAdapter getChild(String nodeName) {
284         return children.get(nodeName);
285     }
286 
287     public Map<String, AbstractJcrNodeAdapter> getChildren() {
288         return children;
289     }
290 
291     /**
292      * Add a child adapter to the current Item. <b>Only Child Nodes part of this Map will
293      * be persisted into Jcr.</b>
294      */
295     public AbstractJcrNodeAdapter addChild(AbstractJcrNodeAdapter child) {
296         childItemChanges = true;
297         if (removedChildren.containsKey(child.getNodeName())) {
298             removedChildren.remove(child.getNodeName());
299         }
300         child.setParent(this);
301         if (children.containsKey(child.getNodeName())) {
302             children.remove(child.getNodeName());
303         }
304         return children.put(child.getNodeName(), child);
305     }
306 
307     /**
308      * Remove a child Node from the child list. <b>When removing an JcrNodeAdapter, this child
309      * will be added to the Remove Child List even if this Item was not part of the current children
310      * list. All Item part from the removed list are removed from the Jcr repository.</b>
311      */
312     public boolean removeChild(AbstractJcrNodeAdapter toRemove) {
313         childItemChanges = true;
314         removedChildren.put(toRemove.getNodeName(), toRemove);
315         return children.remove(toRemove.getNodeName()) != null;
316     }
317 
318     /**
319      * Return the current Parent Item (If Item is a child). Parent is set by calling addChild(...
320      */
321     public AbstractJcrNodeAdapter getParent() {
322         return parent;
323     }
324 
325     public void setParent(AbstractJcrNodeAdapter parent) {
326         this.parent = parent;
327     }
328 
329     /**
330      * Return the current Node Name. For new Item, this is the name set in the new Item constructor
331      * or null if not yet defined.
332      */
333     public String getNodeName() {
334         return this.nodeName;
335     }
336 
337     public void setNodeName(String nodeName) {
338         this.nodeName = nodeName;
339     }
340 
341     /**
342      * @return true if an {@link com.vaadin.data.Item} was added or removed, false otherwise.
343      */
344     public boolean hasChildItemChanges() {
345         return childItemChanges;
346     }
347 }