1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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.data.Property;
56
57
58
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 protected void initCommonAttributes(Item jcrItem) {
80 if (jcrItem instanceof javax.jcr.Property) {
81 try {
82 Node parent = jcrItem.getParent();
83 setItemId(new JcrPropertyItemId(parent.getIdentifier(), jcrItem.getSession().getWorkspace().getName(), jcrItem.getName()));
84 } catch (RepositoryException e) {
85 setItemId(new JcrPropertyItemId(UNIDENTIFIED, UNIDENTIFIED, UNIDENTIFIED));
86 }
87 }
88 }
89
90 @Override
91 public boolean isNode() {
92 return false;
93 }
94
95 @Override
96 public JcrPropertyItemId getItemId() {
97 return (JcrPropertyItemId) super.getItemId();
98 }
99
100 @Override
101 public javax.jcr.Property getJcrItem() {
102 return (javax.jcr.Property)super.getJcrItem();
103 }
104
105 @Override
106 public javax.jcr.Property applyChanges() throws RepositoryException {
107 javax.jcr.Property jcrItem = getJcrItem();
108 super.updateProperties(jcrItem);
109
110 return getJcrItem();
111 }
112
113 @Override
114 public Property getItemProperty(Object id) {
115 if (getChangedProperties().containsKey(id)) {
116 return getChangedProperties().get(id);
117 }
118 Object value;
119 int type = PropertyType.STRING;
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 type = jcrProperty.getType();
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 DefaultProperty property = new DefaultProperty(value.getClass(), 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
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
164 jcrName = Path.getUniqueLabel(jcrProperty.getSession(), jcrProperty.getParent().getPath(), jcrName);
165
166
167 javax.jcr.Property newProperty = PropertyUtil.renameProperty(jcrProperty, jcrName);
168
169
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 String valueString = property.getValue().toString();
180 int valueType = jcrProperty.getType();
181 ValueFactory valueFactory = jcrProperty.getSession().getValueFactory();
182
183 Value newValue = PropertyUtil.createValue(valueString, valueType, valueFactory);
184 jcrProperty.setValue(newValue);
185 } catch (RepositoryException e) {
186 log.error("Could not set JCR property", e);
187 }
188 }
189 } else if (TYPE_PROPERTY.equals(propertyId)) {
190 if (property.getValue() != null) {
191
192
193 int newType;
194 try {
195 newType = PropertyType.valueFromName(property.getValue().toString());
196 } catch (IllegalArgumentException e) {
197 log.warn("Could not set new type for JCR property, unknown type string", e);
198 return;
199 }
200
201
202 try {
203 String valueString = jcrProperty.getValue().getString();
204 ValueFactory valueFactory = jcrProperty.getSession().getValueFactory();
205
206 Value newValue = PropertyUtil.createValue(valueString, newType, valueFactory);
207 jcrProperty.setValue(newValue);
208 } catch (RepositoryException e) {
209 log.error("Could not set new type for JCR property", e);
210 }
211 }
212 }
213 }
214
215 @Override
216 public boolean addItemProperty(Object id, Property property) {
217 throw new UnsupportedOperationException("addItemProperty");
218 }
219
220 @Override
221 public boolean removeItemProperty(Object id) throws UnsupportedOperationException {
222 throw new UnsupportedOperationException("removeItemProperty");
223 }
224
225 @Override
226 public boolean isNew() {
227 return false;
228 }
229 }