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.module.mail.app.field.transformer;
35
36 import info.magnolia.jcr.util.NodeUtil;
37 import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
38 import info.magnolia.ui.form.field.definition.CompositeFieldDefinition;
39 import info.magnolia.ui.form.field.definition.ConfiguredFieldDefinition;
40 import info.magnolia.ui.form.field.transformer.basic.BasicTransformer;
41 import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
42
43 import java.lang.reflect.Modifier;
44 import java.util.ArrayList;
45 import java.util.Collection;
46 import java.util.HashMap;
47 import java.util.List;
48 import java.util.Map;
49 import java.util.stream.Collectors;
50
51 import javax.inject.Inject;
52 import javax.jcr.Node;
53 import javax.jcr.RepositoryException;
54
55 import org.apache.commons.lang.StringUtils;
56
57 import com.vaadin.v7.data.Property;
58 import com.vaadin.v7.data.util.ObjectProperty;
59 import com.vaadin.v7.data.util.PropertysetItem;
60
61
62
63
64 public class MailAuthenticationTransformer extends BasicTransformer<PropertysetItem> {
65
66 private static final String AUTHENTICATION = "authentication";
67
68 private Map<String, List<String>> fieldNames = new HashMap<>();
69
70 @Inject
71 public MailAuthenticationTransformer(JcrNodeAdapter relatedFormItem, CompositeFieldDefinition definition, Class<PropertysetItem> type, I18NAuthoringSupport i18NAuthoringSupport) throws RepositoryException {
72 super(new AuthenticationConfigurationAdapter(relatedFormItem), definition, type, i18NAuthoringSupport);
73 List<ConfiguredFieldDefinition> fields = definition.getFields();
74 fields.forEach(field -> {
75 if (field instanceof CompositeFieldDefinition && field.getName() != null) {
76 if (!fieldNames.containsKey(field.getName())) {
77 fieldNames.put(field.getName(), new ArrayList<>());
78 }
79 fieldNames.get(field.getName()).addAll(((CompositeFieldDefinition) field).getFieldNames());
80 }
81 });
82 }
83
84 @Override
85 public void writeToItem(PropertysetItem newValues) {
86 for (Object id : newValues.getItemPropertyIds()) {
87 String propertyName = (String) id;
88 if (fieldNames.containsKey(propertyName)) {
89 PropertysetItem values = (PropertysetItem) newValues.getItemProperty(propertyName).getValue();
90
91 for (Object ida : values.getItemPropertyIds()) {
92 String name = (String) ida;
93 Property<?> property = relatedFormItem.getItemProperty(name);
94 if (property == null || property.getValue() == null) {
95 relatedFormItem.addItemProperty(name, values.getItemProperty(name));
96 }
97 }
98 }
99 }
100
101 String propertyName = definePropertyName();
102 if (newValues.getItemProperty(propertyName) != null) {
103 relatedFormItem.addItemProperty(propertyName, newValues.getItemProperty(propertyName));
104 }
105 }
106
107 @Override
108 public PropertysetItem readFromItem() {
109 PropertysetItem newValue = new PropertysetItem();
110 fieldNames.forEach((compositeKeyName, fields) -> {
111 PropertysetItem compositeFieldNewValues = new PropertysetItem();
112 fields.forEach(propertyName -> {
113 if (relatedFormItem.getItemProperty(propertyName) != null) {
114 compositeFieldNewValues.addItemProperty(propertyName, relatedFormItem.getItemProperty(propertyName));
115 }
116 });
117 newValue.addItemProperty(compositeKeyName, new ObjectProperty<>(compositeFieldNewValues, PropertysetItem.class));
118 });
119 newValue.addItemProperty(AUTHENTICATION, relatedFormItem.getItemProperty("class") == null ? new ObjectProperty<>(StringUtils.EMPTY) : relatedFormItem.getItemProperty("class"));
120 return newValue;
121 }
122
123 private static class AuthenticationConfigurationAdapter extends JcrNodeAdapter {
124
125 private AuthenticationConfigurationAdapter(JcrNodeAdapter parent) throws RepositoryException {
126 super(getAuthenticationNode(parent));
127 parent.addChild(this);
128 }
129
130 private static Node getAuthenticationNode(JcrNodeAdapter parent) throws RepositoryException {
131 return NodeUtil.createPath(parent.getJcrItem(), AUTHENTICATION, parent.getPrimaryNodeTypeName(), true);
132 }
133
134 @Override
135 protected void updateProperty(javax.jcr.Item item, String propertyId, Property property) {
136 try {
137 if (AUTHENTICATION.equals(propertyId)) {
138 super.updateProperty(item, "class", property);
139 } else if (getAuthenticationClassProperties().contains(propertyId)) {
140 super.updateProperty(item, propertyId, property);
141 } else if (getChangedProperties().containsKey(AUTHENTICATION)) {
142 final Node node = (Node) item;
143 if (node.hasProperty(propertyId)) {
144 node.getProperty(propertyId).remove();
145 }
146 }
147 } catch (RepositoryException | ClassNotFoundException e) {
148 throw new RuntimeException(e);
149 }
150 }
151
152 private Collection<String> getAuthenticationClassProperties() throws ClassNotFoundException {
153 return !getChangedProperties().containsKey(AUTHENTICATION) ? new ArrayList<>() :
154 java.util.Arrays.stream(Class.forName((String) getChangedProperties().get(AUTHENTICATION).getValue()).getDeclaredFields())
155 .filter(field -> !Modifier.isFinal(field.getModifiers()))
156 .map(field -> StringUtils.substringAfterLast(field.toString(), "."))
157 .collect(Collectors.toList());
158 }
159 }
160 }