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.editor;
35
36 import static info.magnolia.ui.editor.JcrIndexedMultiPropertyValueProvider.TMP_REMOVED;
37
38 import info.magnolia.jcr.util.NodeTypes;
39 import info.magnolia.jcr.util.NodeUtil;
40 import info.magnolia.jcr.wrapper.DelegatePropertyWrapper;
41 import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
42 import info.magnolia.ui.field.EditorPropertyDefinition;
43
44 import java.util.ArrayList;
45 import java.util.List;
46 import java.util.Locale;
47 import java.util.Optional;
48
49 import javax.jcr.Node;
50 import javax.jcr.Property;
51 import javax.jcr.RepositoryException;
52 import javax.jcr.Session;
53 import javax.jcr.Value;
54
55 import com.machinezoo.noexception.Exceptions;
56
57
58
59
60
61
62
63 @Deprecated
64 public class MultiValueAsMultipleProperties implements ItemProviderStrategy<Property, Node> {
65
66 private final EditorPropertyDefinition relatedFieldDefinition;
67 private final LocaleContext localeContext;
68 private final I18NAuthoringSupport<Node> i18NAuthoringSupport;
69
70 public MultiValueAsMultipleProperties(LocaleContext localeContext, I18NAuthoringSupport<Node> i18NAuthoringSupport, EditorPropertyDefinition relatedFieldDefinition) {
71 this.relatedFieldDefinition = relatedFieldDefinition;
72 this.localeContext = localeContext;
73 this.i18NAuthoringSupport = i18NAuthoringSupport;
74 }
75
76 @Override
77 public Optional<Property> read(Node reference) {
78 return read(reference, i18NAuthoringSupport.getDefaultLocale(reference));
79 }
80
81 @Override
82 public Optional<Property> read(Node node, Locale locale) {
83 return Optional.of(node)
84 .map(Exceptions.wrap().function(parent -> getMultiFieldNode(parent, locale)))
85 .map(Wrapper::new);
86 }
87
88 private Node getMultiFieldNode(Node node, Locale locale) throws RepositoryException {
89 return NodeUtil.createPath(node, getLocalisedParentNodeName(locale), NodeTypes.ContentNode.NAME);
90 }
91
92 private String getLocalisedParentNodeName(Locale locale) {
93 String parentNodeName = relatedFieldDefinition.getName();
94 if (!localeContext.getDefault().equals(locale)) {
95 parentNodeName = i18NAuthoringSupport.deriveLocalisedPropertyName(parentNodeName, locale);
96 }
97 return parentNodeName;
98 }
99
100
101
102
103
104
105 @Deprecated
106 public static class Definition extends JcrPropertyProvider.Definition {
107
108 public Definition() {
109 setImplementationClass((Class) MultiValueAsMultipleProperties.class);
110 }
111 }
112
113 private static class Wrapper extends DelegatePropertyWrapper {
114
115 private final Node node;
116
117 private Wrapper(Node node) {
118 super(null);
119 this.node = node;
120 }
121
122 @Override
123 public Value[] getValues() throws RepositoryException {
124 int i = 0;
125 final List<Value> values = new ArrayList<>();
126 while (node.hasProperty(String.valueOf(i))) {
127 values.add(node.getProperty(String.valueOf(i)).getValue());
128 i++;
129 }
130 return values.toArray(new Value[] {});
131 }
132
133 @Override
134 public void setValue(Value[] values) throws RepositoryException {
135 for (int i = 0; i < values.length; i++) {
136 node.setProperty(String.valueOf(i), TMP_REMOVED.equals(values[i].getString()) ? null : values[i]);
137 }
138 }
139
140 @Override
141 public Session getSession() throws RepositoryException {
142 return node.getSession();
143 }
144
145 @Override
146 public boolean isNode() {
147 return false;
148 }
149 }
150 }