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.form.field.transformer.multi;
35
36 import info.magnolia.annotation.deprecation.MgnlDeprecated;
37
38 import info.magnolia.jcr.util.NodeTypes;
39 import info.magnolia.jcr.util.NodeUtil;
40 import info.magnolia.jcr.wrapper.JCRMgnlPropertiesFilteringNodeWrapper;
41 import info.magnolia.objectfactory.Components;
42 import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
43 import info.magnolia.ui.form.field.definition.ConfiguredFieldDefinition;
44 import info.magnolia.ui.form.field.transformer.basic.BasicTransformer;
45 import info.magnolia.ui.vaadin.integration.jcr.JcrNewNodeAdapter;
46 import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
47
48 import java.util.ArrayList;
49 import java.util.Collections;
50 import java.util.Comparator;
51 import java.util.Iterator;
52 import java.util.List;
53
54 import javax.inject.Inject;
55 import javax.jcr.Node;
56 import javax.jcr.PropertyIterator;
57 import javax.jcr.RepositoryException;
58
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61
62 import com.google.common.collect.ImmutableList;
63 import com.vaadin.v7.data.Item;
64 import com.vaadin.v7.data.util.PropertysetItem;
65
66
67
68
69
70
71
72
73
74
75
76 @Deprecated
77 @MgnlDeprecated(since = "6.2.3.", description = "Without direct replacement.")
78 public class MultiValueChildNodeTransformer extends BasicTransformer<PropertysetItem> {
79
80 private static final Logger log = LoggerFactory.getLogger(MultiValueChildNodeTransformer.class);
81
82 private String childNodeType = NodeTypes.ContentNode.NAME;
83
84
85
86
87 @Deprecated
88 public MultiValueChildNodeTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<PropertysetItem> type) {
89 this(relatedFormItem, definition, type, Components.getComponent(I18NAuthoringSupport.class));
90 }
91
92 @Inject
93 public MultiValueChildNodeTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<PropertysetItem> type, I18NAuthoringSupport i18NAuthoringSupport) {
94 super(relatedFormItem, definition, type, i18NAuthoringSupport);
95 }
96
97 @Override
98 public PropertysetItem readFromItem() {
99
100 String childNodeName = definePropertyName();
101 PropertysetItem newValues = new PropertysetItem();
102
103 try {
104 JcrNodeAdaptergnolia/ui/vaadin/integration/jcr/JcrNodeAdapter.html#JcrNodeAdapter">JcrNodeAdapter child = getOrCreateChildItem((JcrNodeAdapter) relatedFormItem, childNodeName);
105
106 if (!(child instanceof JcrNewNodeAdapter)) {
107 int pos = 0;
108
109 List<Object> ids = new ArrayList<Object>(child.getItemPropertyIds());
110 Collections.sort(ids, new Comparator<Object>() {
111 @Override
112 public int compare(Object o1, Object o2) {
113 int int1 = Integer.valueOf(((String) o1));
114 int int2 = Integer.valueOf(((String) o2));
115 return int1 - int2;
116 }
117 });
118
119 for (Object id : ids) {
120 newValues.addItemProperty(pos, child.getItemProperty(id));
121 pos += 1;
122 }
123 }
124 } catch (RepositoryException re) {
125 log.warn("Not able to access the child node of '{}'", ((JcrNodeAdapter) relatedFormItem).getNodeName());
126 }
127 return newValues;
128 }
129
130 @Override
131 public void writeToItem(PropertysetItem newValue) {
132
133 String childNodeName = definePropertyName();
134 try {
135
136 JcrNodeAdaptergnolia/ui/vaadin/integration/jcr/JcrNodeAdapter.html#JcrNodeAdapter">JcrNodeAdapter child = getOrCreateChildItem((JcrNodeAdapter) relatedFormItem, childNodeName);
137
138
139 ImmutableList<Object> propertyIds = ImmutableList.copyOf(child.getItemPropertyIds());
140 for (Object id : propertyIds) {
141 if (newValue.getItemProperty(Integer.valueOf((String) id)) == null) {
142 child.removeItemProperty(id);
143 }
144 }
145
146 if (newValue != null) {
147 Iterator<?> it = newValue.getItemPropertyIds().iterator();
148 while (it.hasNext()) {
149 Object id = it.next();
150 child.addItemProperty(id.toString(), newValue.getItemProperty(id));
151 }
152 }
153 } catch (RepositoryException re) {
154 log.warn("Not able to access the child node of '{}'", NodeUtil.getName(((JcrNodeAdapter) relatedFormItem).getJcrItem()));
155 }
156 }
157
158
159
160
161 private JcrNodeAdapter./info/magnolia/ui/vaadin/integration/jcr/JcrNodeAdapter.html#JcrNodeAdapter">JcrNodeAdapter getOrCreateChildItem(JcrNodeAdapter parent, String childNodeName) throws RepositoryException {
162
163 JcrNodeAdapter child = null;
164 Node rootNode = parent.getJcrItem();
165 if (rootNode.hasNode(childNodeName)) {
166 child = new JcrNodeAdapter(rootNode.getNode(childNodeName));
167 Node childNode = new JCRMgnlPropertiesFilteringNodeWrapper(rootNode.getNode(childNodeName));
168 PropertyIterator iterator = childNode.getProperties();
169 while (iterator.hasNext()) {
170
171 child.getItemProperty(iterator.nextProperty().getName());
172 }
173 } else {
174 child = new JcrNewNodeAdapter(rootNode, childNodeType, childNodeName);
175 }
176 parent.addChild(child);
177 return child;
178 }
179
180
181
182 }