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.ui.api.ModelConstants;
37
38 import java.util.HashMap;
39 import java.util.Map;
40 import java.util.Map.Entry;
41
42 import javax.jcr.Item;
43 import javax.jcr.RepositoryException;
44
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 import com.vaadin.data.Property;
49
50
51
52
53 public abstract class AbstractJcrAdapter implements JcrItemAdapter {
54
55 private static final Logger log = LoggerFactory.getLogger(AbstractJcrAdapter.class);
56
57 protected static final String UNIDENTIFIED = "?";
58
59 private String workspace;
60
61 private String itemId;
62
63 private final Map<String, Property> changedProperties = new HashMap<String, Property>();
64
65 private final Map<String, Property> removedProperties = new HashMap<String, Property>();
66
67 public AbstractJcrAdapter(Item jcrItem) {
68 initCommonAttributes(jcrItem);
69 }
70
71
72
73
74 protected void initCommonAttributes(Item jcrItem) {
75 try {
76 workspace = jcrItem.getSession().getWorkspace().getName();
77 itemId = JcrItemUtil.getItemId(jcrItem);
78 } catch (RepositoryException e) {
79 log.error("Could not retrieve workspace or path of JCR Item.", e);
80 itemId = UNIDENTIFIED;
81 workspace = UNIDENTIFIED;
82 }
83 }
84
85 @Override
86 public String getWorkspace() {
87 return workspace;
88 }
89
90 @Override
91 public String getItemId() {
92 return itemId;
93 }
94
95 public void setItemId(String itemId) {
96 this.itemId = itemId;
97 }
98
99 @Override
100 public javax.jcr.Item getJcrItem() {
101 try {
102 return JcrItemUtil.getJcrItem(workspace, itemId);
103 } catch (RepositoryException re) {
104 log.warn("Not able to retrieve the JcrItem ", re.getMessage());
105 return null;
106 }
107 }
108
109 @Override
110 public boolean hasChangedProperties() {
111 return changedProperties.size() > 0;
112 }
113
114
115
116 protected Map<String, Property> getChangedProperties() {
117 return changedProperties;
118 }
119
120 protected Map<String, Property> getRemovedProperties() {
121 return removedProperties;
122 }
123
124
125
126
127 protected void updateProperties(Item item) throws RepositoryException {
128 for (Entry<String, Property> entry : changedProperties.entrySet()) {
129 if (entry.getValue().isReadOnly()) {
130 continue;
131 }
132 updateProperty(item, entry.getKey(), entry.getValue());
133 if (ModelConstants.JCR_NAME.equals(entry.getKey())) {
134
135 item = getJcrItem();
136 }
137 }
138 }
139
140
141
142
143
144
145 protected abstract void updateProperty(Item item, String propertyId, Property property);
146
147 }