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