View Javadoc
1   /**
2    * This file Copyright (c) 2012-2017 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.PropertyUtil;
39  
40  import java.util.Collection;
41  import java.util.Collections;
42  import java.util.HashSet;
43  import java.util.Set;
44  
45  import javax.jcr.Item;
46  import javax.jcr.Node;
47  import javax.jcr.PropertyType;
48  import javax.jcr.RepositoryException;
49  import javax.jcr.Value;
50  import javax.jcr.ValueFactory;
51  
52  import org.slf4j.Logger;
53  import org.slf4j.LoggerFactory;
54  
55  import com.vaadin.v7.data.Property;
56  import com.vaadin.v7.data.util.ObjectProperty;
57  
58  /**
59   * Represents a JCR property as a Vaadin data Item with three fixed properties for its name, type and value.
60   */
61  public class JcrPropertyAdapter extends AbstractJcrAdapter {
62  
63      public static final String VALUE_PROPERTY = "value";
64  
65      public static final String TYPE_PROPERTY = "type";
66  
67      public static final Set<String> PROPERTY_IDS = Collections.unmodifiableSet(new HashSet<String>() {{
68          add(ModelConstants.JCR_NAME);
69          add(TYPE_PROPERTY);
70          add(VALUE_PROPERTY);
71      }});
72  
73      private static final Logger log = LoggerFactory.getLogger(JcrPropertyAdapter.class);
74  
75      public JcrPropertyAdapter(javax.jcr.Property jcrProperty) {
76          super(jcrProperty);
77      }
78  
79      @Override
80      protected void initCommonAttributes(Item jcrItem) {
81          if (jcrItem instanceof javax.jcr.Property) {
82              try {
83                  Node parent = jcrItem.getParent();
84                  setItemId(new JcrPropertyItemId(parent.getIdentifier(), jcrItem.getSession().getWorkspace().getName(), jcrItem.getName()));
85              } catch (RepositoryException e) {
86                  setItemId(new JcrPropertyItemId(UNIDENTIFIED, UNIDENTIFIED, UNIDENTIFIED));
87              }
88          }
89      }
90  
91      @Override
92      public boolean isNode() {
93          return false;
94      }
95  
96      @Override
97      public JcrPropertyItemId getItemId() {
98          return (JcrPropertyItemId) super.getItemId();
99      }
100 
101     @Override
102     public javax.jcr.Property getJcrItem() {
103         return (javax.jcr.Property)super.getJcrItem();
104     }
105 
106     @Override
107     public javax.jcr.Property applyChanges() throws RepositoryException {
108         javax.jcr.Property jcrItem = getJcrItem();
109         super.updateProperties(jcrItem);
110         // We fetch it again from the JCR session in case the name changed
111         return getJcrItem();
112     }
113 
114     @Override
115     public Property getItemProperty(Object id) {
116         if (getChangedProperties().containsKey(id)) {
117             return getChangedProperties().get(id);
118         }
119         Object value;
120         try {
121             javax.jcr.Property jcrProperty = getJcrItem();
122             if (ModelConstants.JCR_NAME.equals(id)) {
123                 value = jcrProperty.getName();
124             } else if (VALUE_PROPERTY.equals(id)) {
125                 value = PropertyUtil.getPropertyValueObject(jcrProperty.getParent(), String.valueOf(jcrProperty.getName()));
126             } else if (TYPE_PROPERTY.equals(id)) {
127                 value = PropertyType.nameFromValue(jcrProperty.getType());
128             } else {
129                 return null;
130             }
131         } catch (RepositoryException re) {
132             log.error("Could not get property for " + id, re);
133             throw new RuntimeRepositoryException(re);
134         }
135 
136         Property property = new ObjectProperty<>(value);
137         getChangedProperties().put((String) id, property);
138         return property;
139     }
140 
141     @Override
142     public Collection<?> getItemPropertyIds() {
143         return PROPERTY_IDS;
144     }
145 
146     /**
147      * Updates one of the three supported properties or does nothing if the given propertyId is something else.
148      */
149     @Override
150     protected void updateProperty(Item item, String propertyId, Property property) {
151         if (!(item instanceof javax.jcr.Property)) {
152             return;
153         }
154         javax.jcr.Property jcrProperty = (javax.jcr.Property) item;
155 
156         if (ModelConstants.JCR_NAME.equals(propertyId)) {
157             String jcrName = (String) property.getValue();
158             if (jcrName != null && !jcrName.isEmpty()) {
159                 try {
160                     if (!(jcrName.equals(jcrProperty.getName()))) {
161 
162                         // make sure new path is available
163                         jcrName = Path.getUniqueLabel(jcrProperty.getSession(), jcrProperty.getParent().getPath(), jcrName);
164 
165                         // rename the node
166                         javax.jcr.Property newProperty = PropertyUtil.renameProperty(jcrProperty, jcrName);
167 
168                         // update internal state
169                         setItemId(new JcrPropertyItemId(newProperty.getParent().getIdentifier(), getWorkspace(), newProperty.getName()));
170                     }
171                 } catch (RepositoryException e) {
172                     log.error("Could not rename JCR property", e);
173                 }
174             }
175         } else if (VALUE_PROPERTY.equals(propertyId)) {
176             if (property.getValue() != null) {
177                 try {
178                     ValueFactory valueFactory = jcrProperty.getSession().getValueFactory();
179 
180                     Value newValue = PropertyUtil.createValue(property.getValue(), valueFactory);
181                     jcrProperty.setValue(newValue);
182                 } catch (RepositoryException e) {
183                     log.error("Could not set JCR property", e);
184                 }
185             }
186         } else if (TYPE_PROPERTY.equals(propertyId)) {
187             if (property.getValue() != null) {
188 
189                 // get new type from string
190                 int newType;
191                 try {
192                     newType = PropertyType.valueFromName(property.getValue().toString());
193                 } catch (IllegalArgumentException e) {
194                     log.warn("Could not set new type for JCR property, unknown type string", e);
195                     return;
196                 }
197 
198                 // set old value with new type
199                 try {
200                     String valueString = jcrProperty.getValue().getString();
201                     ValueFactory valueFactory = jcrProperty.getSession().getValueFactory();
202 
203                     Value newValue = PropertyUtil.createValue(valueString, newType, valueFactory);
204                     jcrProperty.setValue(newValue);
205                 } catch (RepositoryException e) {
206                     log.error("Could not set new type for JCR property", e);
207                 }
208             }
209         }
210     }
211 
212     @Override
213     public boolean addItemProperty(Object id, Property property) {
214         throw new UnsupportedOperationException("addItemProperty");
215     }
216 
217     @Override
218     public boolean removeItemProperty(Object id) throws UnsupportedOperationException {
219         throw new UnsupportedOperationException("removeItemProperty");
220     }
221 
222     @Override
223     public boolean isNew() {
224         return false;
225     }
226 }