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