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