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.framework.setup;
35
36 import info.magnolia.module.InstallContext;
37 import info.magnolia.module.delta.AbstractRepositoryTask;
38 import info.magnolia.module.delta.TaskExecutionException;
39
40 import java.io.InputStream;
41 import java.util.Map;
42
43 import javax.jcr.Node;
44 import javax.jcr.RepositoryException;
45 import javax.jcr.Session;
46
47 import org.apache.commons.lang3.StringUtils;
48 import org.yaml.snakeyaml.Yaml;
49
50
51
52
53 public class RemoveFieldTypeDefinitionsFromJcrTask extends AbstractRepositoryTask {
54
55 private static final String FIELD_TYPES_NODE = "fieldTypes";
56 private static final String DEFINITION_CLASS_PROPERTY = "definitionClass";
57 private static final String FACTORY_CLASS_PROPERTY = "factoryClass";
58
59 private static final Yaml YAML = new Yaml();
60
61 private final String modulePath;
62
63 private final Map<String, String> jcrFieldTypes;
64
65 public RemoveFieldTypeDefinitionsFromJcrTask(String modulePath, Map<String, String> jcrFieldTypes) {
66 super("Remove field type definitions from JCR", String.format("Remove JCR field type definitions from %s module as they are now being moved to YAML", StringUtils.substringAfterLast(modulePath, "/")));
67 this.modulePath = modulePath;
68 this.jcrFieldTypes = jcrFieldTypes;
69 }
70
71 @Override
72 @SuppressWarnings("unchecked")
73 public void execute(InstallContext ctx) throws TaskExecutionException {
74 super.execute(ctx);
75 }
76
77 @Override
78 protected void doExecute(InstallContext ctx) throws RepositoryException {
79 Session session = ctx.getConfigJCRSession();
80 if (session.nodeExists(modulePath)) {
81 Node moduleNode = session.getNode(modulePath);
82 if (moduleNode.hasNode(FIELD_TYPES_NODE)) {
83 Node fieldTypesNode = moduleNode.getNode(FIELD_TYPES_NODE);
84 for (Map.Entry<String, String> entry : jcrFieldTypes.entrySet()) {
85 if (fieldTypesNode.hasNode(entry.getKey())) {
86 Node oldFieldType = fieldTypesNode.getNode(entry.getKey());
87 InputStream stream = getClass().getResourceAsStream(String.format("/%s/fieldTypes/%s.yaml", StringUtils.substringAfterLast(modulePath, "/"), entry.getValue()));
88 if (stream != null) {
89 Map<String, String> newDefinition = YAML.load(stream);
90
91 if (oldFieldType.hasProperty(DEFINITION_CLASS_PROPERTY) && !oldFieldType.getProperty(DEFINITION_CLASS_PROPERTY).getString().equals(newDefinition.get(DEFINITION_CLASS_PROPERTY))) {
92 ctx.warn(String.format("Field type definition class is different from default one. Expected: [%s], got: [%s] in [%s].", newDefinition.get(DEFINITION_CLASS_PROPERTY), oldFieldType.getProperty(DEFINITION_CLASS_PROPERTY).getString(), entry.getKey()));
93 }
94
95 if (oldFieldType.hasProperty(FACTORY_CLASS_PROPERTY) && !oldFieldType.getProperty(FACTORY_CLASS_PROPERTY).getString().equals(newDefinition.get(FACTORY_CLASS_PROPERTY))) {
96 ctx.warn(String.format("Field type factory class is different from default one. Expected: [%s], got: [%s] in [%s].", newDefinition.get(FACTORY_CLASS_PROPERTY), oldFieldType.getProperty(FACTORY_CLASS_PROPERTY).getString(), entry.getKey()));
97 }
98 }
99 oldFieldType.remove();
100 }
101 }
102
103 if (!fieldTypesNode.hasNodes()) {
104 fieldTypesNode.remove();
105 }
106 }
107 }
108 }
109 }