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.forum.setup;
35
36 import info.magnolia.cms.core.Path;
37 import info.magnolia.cms.core.SystemProperty;
38 import info.magnolia.module.InstallContext;
39 import info.magnolia.module.delta.AbstractCondition;
40 import info.magnolia.module.forum.DefaultForumManager;
41 import org.jdom.Document;
42 import org.jdom.Element;
43 import org.jdom.JDOMException;
44 import org.jdom.input.SAXBuilder;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 import java.io.File;
49 import java.io.IOException;
50 import java.util.Collection;
51
52
53
54
55
56
57
58
59 public class CheckNodeTypesDefinition extends AbstractCondition {
60 private static final Logger log = LoggerFactory.getLogger(CheckNodeTypesDefinition.class);
61
62 public CheckNodeTypesDefinition() {
63 super("Check existing node types definition", "Verifies existing node types definitions; make sure mgnl:MetaData is known and the outdated mgnl:ordering_info isn't registered.");
64 }
65
66 @Override
67 @SuppressWarnings("unchecked")
68 public boolean check(InstallContext installContext) {
69 String home = SystemProperty.getProperty("magnolia.repositories.home");
70 File repoHome = new File(Path.getAbsoluteFileSystemPath(home));
71
72 File nodeTypeFile = new File(repoHome, "magnolia/repository/nodetypes/custom_nodetypes.xml");
73
74 if (!nodeTypeFile.exists()) {
75
76
77 return true;
78 }
79
80 SAXBuilder builder = new SAXBuilder();
81 Document doc;
82 try {
83 doc = builder.build(nodeTypeFile);
84 Collection<Element> nodeTypes = doc.getRootElement().getChildren();
85 for (Element nodeType : nodeTypes) {
86 if (!DefaultForumManager.FORUM_NODETYPE.equals(nodeType.getAttribute("name").getValue())) {
87 continue;
88 }
89 Collection<Element> properties = nodeType.getChildren("propertyDefinition");
90 boolean foundProp = false;
91 for (Element propDef : properties) {
92 if ("*".equals(propDef.getAttribute("name").getValue())) {
93 foundProp = true;
94 break;
95 }
96 if ("mgnl:ordering_info".equals(propDef.getAttribute("name").getValue())) {
97 foundProp = true;
98 break;
99 }
100 }
101 Collection<Element> children = nodeType.getChildren("childNodeDefinition");
102 boolean foundChildDef = false;
103 for (Element childDef : children) {
104 if ("MetaData".equals(childDef.getAttribute("name").getValue())
105 && "mgnl:metaData".equals(childDef.getAttribute("defaultPrimaryType").getValue())
106 && childDef.getAttribute("autoCreated").getBooleanValue()
107 && childDef.getAttribute("mandatory").getBooleanValue()) {
108 foundChildDef = true;
109 break;
110 }
111 }
112 if (!foundProp || !foundChildDef) {
113 final String msg = new StringBuilder("Required ")
114 .append(!foundProp ? "mgnl:ordering_info property definition " : "")
115 .append(!foundProp && !foundChildDef ? "and " : "")
116 .append(!foundChildDef ? "mandatory mgnl:metaData child node definition " : "")
117 .append(!foundProp && !foundChildDef ? "were" : "was")
118 .append(" not found. Please update custom_nodetypes.xml to include the definition in the mgnl:forum type definiton manually or run the update script as described in the release notes. In either case you will need to restart this instance in order for your changes to take effect.")
119 .toString();
120 installContext.error(msg, new Exception(msg));
121 return false;
122 }
123
124 break;
125 }
126 } catch (JDOMException e) {
127 log.error("Failed to parse custom_nodetypes.xml due to " + e.getMessage(), e);
128 return false;
129 } catch (IOException e) {
130 log.error("Failed to access custom_nodetypes.xml due to " + e.getMessage(), e);
131 return false;
132 }
133 return true;
134 }
135 }