View Javadoc
1   /**
2    * This file Copyright (c) 2012-2018 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.v7.data.Property;
46  
47  /**
48   * Base implementation of an {@link com.vaadin.v7.data.Item} wrapping/representing a {@link Node}.
49   * Implements {@link 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  @Deprecated
80  public class JcrNodeAdapter extends AbstractJcrNodeAdapter {
81  
82      private static final Logger log = LoggerFactory.getLogger(JcrNodeAdapter.class);
83  
84      public JcrNodeAdapter(Node jcrNode) {
85          super(jcrNode);
86          try {
87              setNodeName(jcrNode.getName());
88          } catch (RepositoryException e) {
89              log.error("Could not access the node name", e);
90          }
91      }
92  
93      /**
94       * Get Vaadin Property from a Jcr Property.
95       * If the Property was already modified, get this Property from the local changedProperties map - else
96       * delegate to super implementation.
97       *
98       * @param propertyId id of the property to be retrieved
99       * @return the Property with the provided propertyId
100      */
101     @Override
102     public Property getItemProperty(Object propertyId) {
103         // since we add props as strings, we need to look for them in same fashion
104         propertyId = propertyId.toString();
105         return getChangedProperties().containsKey(propertyId) ? getChangedProperties().get(propertyId) : super.getItemProperty(propertyId);
106     }
107 
108     @Override
109     public Collection<?> getItemPropertyIds() {
110         return Collections.unmodifiableCollection(getChangedProperties().keySet());
111     }
112 
113     @Override
114     public boolean addItemProperty(Object propertyId, Property property) throws UnsupportedOperationException {
115         log.debug("Adding new Property Item named [{}] with value [{}]", propertyId, property.getValue());
116 
117         // Store Property.
118         getChangedProperties().put(propertyId.toString(), property);
119 
120         try {
121             // mark property read-only if current item is protected
122             boolean readOnly = isNodeProtected(getJcrItem()) || isPropertyProtected(propertyId.toString());
123             if (readOnly) {
124                 property.setReadOnly(true);
125             }
126         } catch (RepositoryException e) {
127             log.error("Failed to evaluate if node is protected", e);
128         }
129 
130         // Remove a property from a removedProperties map
131         getRemovedProperties().remove(propertyId);
132 
133         return true;
134     }
135 
136     /**
137      * Remove a property from an Item.
138      * If the property was already modified (or just added), remove it for the changedProperties Map
139      * If the JCR node had the property before, mark it for deletion into the removedProperties Map.
140      */
141     @Override
142     public boolean removeItemProperty(Object id) {
143         boolean res = false;
144 
145         if (getChangedProperties().containsKey(id)) {
146             getChangedProperties().remove(id);
147             res = true;
148         }
149         if (jcrItemHasProperty((String) id)) {
150             markPropertyForDeletion((String) id);
151             res = true;
152         }
153         return res;
154     }
155 
156     private boolean jcrItemHasProperty(String propertyName) {
157         try {
158             return getJcrItem().hasProperty(propertyName);
159         } catch (RepositoryException e) {
160             log.error("Could not determine if property [{}] exists", propertyName, e);
161             return false;
162         }
163     }
164 
165     @Override
166     public boolean isNew() {
167         return false;
168     }
169 }