View Javadoc
1   /**
2    * This file Copyright (c) 2007-2012 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Checks mgnl:forum node type definition file for occurrence of mgnl:metaData and mgnl:ordering_info.
55   *
56   * @author had
57   * @version $Id: $
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              // don't crash if the repository.xml has been customized and doesn't use the magnolia.repositories.home
76              // property for the base path
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                 // we are done here
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 }