View Javadoc
1   /**
2    * This file Copyright (c) 2014-2015 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.rendering.module.setup;
35  
36  import info.magnolia.jcr.util.NodeTypes;
37  import info.magnolia.jcr.util.NodeUtil;
38  import info.magnolia.module.InstallContext;
39  import info.magnolia.module.delta.AbstractRepositoryTask;
40  import info.magnolia.module.delta.TaskExecutionException;
41  
42  import javax.jcr.Node;
43  import javax.jcr.NodeIterator;
44  import javax.jcr.Property;
45  import javax.jcr.RepositoryException;
46  import javax.jcr.Session;
47  
48  
49  /**
50   * Task for installing context attributes to renderers.
51   */
52  public class InstallRendererContextAttributeTask extends AbstractRepositoryTask {
53  
54      public static final String MODULES_ROOT_DIR = "/modules/";
55      public static final String RENDERERS_NODE = "renderers";
56      public static final String CONTEXT_ATTRIBUTES = "contextAttributes";
57      public static final String COMPONENT_CLASS = "componentClass";
58  
59      private final String modulePath;
60      private final String name;
61      private final String componentClass;
62      private final String renderer;
63  
64      public InstallRendererContextAttributeTask(String moduleName, String renderer, String name, String componentClass) {
65          super("Renderer context attribute install task", String.format("Installing '%s' context attribute in module '%s'.", name, moduleName));
66          this.renderer = renderer;
67          this.modulePath = MODULES_ROOT_DIR + moduleName;
68          this.name = name;
69          this.componentClass = componentClass;
70      }
71  
72      @Override
73      protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException {
74          Session session = installContext.getConfigJCRSession();
75  
76          if (session.nodeExists(modulePath)) {
77              Node module = session.getNode(modulePath);
78  
79              if (module.hasNode(RENDERERS_NODE + "/" + renderer)) {
80                  Node rendererNode = module.getNode(RENDERERS_NODE).getNode(renderer);
81  
82                  Node context = NodeUtil.createPath(rendererNode, CONTEXT_ATTRIBUTES, NodeTypes.ContentNode.NAME);
83                  NodeIterator functions = context.getNodes();
84                  boolean installed = false;
85  
86                  while (functions.hasNext()) {
87                      Node function = functions.nextNode();
88  
89                      if (function.hasProperty(COMPONENT_CLASS)) {
90                          Property property = function.getProperty(COMPONENT_CLASS);
91                          if (componentClass.equals(property.getString())) {
92                              installed = true;
93                              break;
94                          }
95                      }
96                  }
97                  if (installed) {
98                      installContext.info(String.format("Context attribute with class '%s' already installed, skipping.", componentClass));
99                      return;
100                 }
101                 else if (context.hasNode(name)) {
102                     installContext.info(String.format("Context attribute with name '%s' already installed, skipping.", name));
103                     return;
104                 }
105                 else {
106                     Node contextAttribute = context.addNode(name, NodeTypes.ContentNode.NAME);
107                     contextAttribute.setProperty("name", name);
108                     contextAttribute.setProperty(COMPONENT_CLASS, componentClass);
109                 }
110             }
111             else {
112                 installContext.info(String.format("Renderer '%s' is not installed. Context attribute is not installed.", renderer));
113             }
114         }
115         else {
116             installContext.info(String.format("Module '%s' is not installed. Context attribute is not installed.", modulePath));
117         }
118 
119     }
120 }