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.v7.data.Property;
56 import com.vaadin.v7.data.util.ObjectProperty;
57
58
59
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
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
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
163 jcrName = Path.getUniqueLabel(jcrProperty.getSession(), jcrProperty.getParent().getPath(), jcrName);
164
165
166 javax.jcr.Property newProperty = PropertyUtil.renameProperty(jcrProperty, jcrName);
167
168
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
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
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 }