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 @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
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
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 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
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
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 }