View Javadoc
1   /**
2    * This file Copyright (c) 2012-2014 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 java.util.Collection;
37  import java.util.Collections;
38  
39  import javax.jcr.Node;
40  import javax.jcr.RepositoryException;
41  
42  import org.slf4j.Logger;
43  import org.slf4j.LoggerFactory;
44  
45  import com.vaadin.data.Property;
46  
47  /**
48   * Base implementation of an {@link com.vaadin.data.Item} wrapping/representing a {@link javax.jcr.Node}.
49   * Implements {@link com.vaadin.data.Property.ValueChangeListener} in order to inform/change JCR property when a
50   * Vaadin property has changed.
51   * <pre>
52   *  <p>
53   *      Jcr properties are read from Repository as long as they are not modified.
54   *  <p>
55   *  Jcr properties are updated or created if they:
56   *  <ul>
57   *      <li>Previously existed and where modified.
58   *      <li>Newly created and set (an empty created property is not stored into Jcr repository)
59   *  </ul>
60   *  <p>
61   *      Create a JcrNodeAdapter:
62   *  <ul>
63   *      <li>Just create a new JcrNodeAdapter with the related Jcr Node as parameter.
64   *  </ul>
65   *  <p>
66   *      Properties:
67   *  <ul>
68   *      <li>getItemProperty(Object id) will return the current stored JCR property if not yet modified or the modified one.
69   *      <li>If the property do not exist null will be returned.
70   *      <li>In this case we have to create a new Property and attach this property to the JcrNodeAdapter, i.e.
71   *  <p>
72   *  <code>
73   *      property p = DefaultPropertyUtil.newDefaultProperty(...)
74   *      jcrNodeAdapter.addItemProperty(...)
75   *  </code>
76   *  </ul>
77   * </pre>
78   */
79  public class JcrNodeAdapter extends AbstractJcrNodeAdapter {
80  
81      private static final Logger log = LoggerFactory.getLogger(JcrNodeAdapter.class);
82  
83      public JcrNodeAdapter(Node jcrNode) {
84          super(jcrNode);
85          try {
86              setNodeName(jcrNode.getName());
87          } catch (RepositoryException e) {
88              log.error("Could not access the node name", e);
89          }
90      }
91  
92      /**
93       * Get Vaadin Property from a Jcr Property.
94       * If the Property was already modified, get this Property from the local changedProperties map - else
95       * delegate to super implementation.
96       *
97       * @param propertyId id of the property to be retrieved
98       * @return the Property with the provided propertyId
99       */
100     @Override
101     public Property getItemProperty(Object propertyId) {
102         return getChangedProperties().containsKey(propertyId) ? getChangedProperties().get(propertyId) : super.getItemProperty(propertyId);
103     }
104 
105     @Override
106     public Collection<?> getItemPropertyIds() {
107         return Collections.unmodifiableCollection(getChangedProperties().keySet());
108     }
109 
110     @Override
111     public boolean addItemProperty(Object propertyId, Property property) throws UnsupportedOperationException {
112         log.debug("Adding new Property Item named [{}] with value [{}]", propertyId, property.getValue());
113 
114         // Store Property.
115         getChangedProperties().put(propertyId.toString(), property);
116 
117         return true;
118     }
119 
120     /**
121      * Remove a property from an Item.
122      * If the property was already modified, remove it for the changedProperties Map and
123      * add it to the removedProperties Map.
124      * Else fill the removedProperties Map with the retrieved property.
125      */
126     @Override
127     public boolean removeItemProperty(Object id) {
128         boolean res = false;
129         if (getChangedProperties().containsKey(id)) {
130             getRemovedProperties().put((String) id, getChangedProperties().get(id));
131             res = true;
132         } else if (jcrItemHasProperty((String) id)) {
133             getRemovedProperties().put((String) id, super.getItemProperty(id));
134             res = true;
135         }
136         return res;
137     }
138 
139     private boolean jcrItemHasProperty(String propertyName) {
140         try {
141             return getJcrItem().hasProperty(propertyName);
142         } catch (RepositoryException e) {
143             log.error("Could not determine if property [{}] exists", propertyName, e);
144             return false;
145         }
146     }
147 
148     @Override
149     public boolean isNew() {
150         return false;
151     }
152 }