View Javadoc
1   /**
2    * This file Copyright (c) 2013-2016 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.ui.mediaeditor.registry;
35  
36  import info.magnolia.cms.util.ModuleConfigurationObservingManager;
37  import info.magnolia.jcr.node2bean.Node2BeanException;
38  import info.magnolia.jcr.node2bean.Node2BeanProcessor;
39  import info.magnolia.jcr.predicate.NodeTypePredicate;
40  import info.magnolia.jcr.util.NodeTypes;
41  import info.magnolia.jcr.util.NodeUtil;
42  import info.magnolia.jcr.util.NodeVisitor;
43  import info.magnolia.module.ModuleRegistry;
44  import info.magnolia.objectfactory.Components;
45  import info.magnolia.ui.mediaeditor.definition.ConfiguredMediaEditorDefinition;
46  import info.magnolia.ui.mediaeditor.definition.MediaEditorDefinition;
47  
48  import java.util.ArrayList;
49  import java.util.HashSet;
50  import java.util.List;
51  import java.util.Set;
52  
53  import javax.inject.Inject;
54  import javax.inject.Singleton;
55  import javax.jcr.Node;
56  import javax.jcr.RepositoryException;
57  
58  import org.apache.commons.lang3.StringUtils;
59  import org.slf4j.Logger;
60  import org.slf4j.LoggerFactory;
61  
62  /**
63   * Populates {@link MediaEditorRegistry} by scanning the nodes in the config workspace.
64   */
65  @Singleton
66  public class ConfiguredMediaEditorDefinitionManager  extends ModuleConfigurationObservingManager {
67  
68      private final Logger log = LoggerFactory.getLogger(getClass());
69  
70      public static final String MEDIA_EDITORS_CONFIG_NODE_NAME = "mediaEditors";
71  
72      private Set<String> registeredIds = new HashSet<String>();
73  
74      private final MediaEditorRegistry registry;
75  
76      @Inject
77      protected ConfiguredMediaEditorDefinitionManager(ModuleRegistry moduleRegistry, MediaEditorRegistry registry) {
78          super(MEDIA_EDITORS_CONFIG_NODE_NAME, moduleRegistry);
79          this.registry = registry;
80      }
81  
82      @Override
83      protected void reload(List<Node> nodes) throws RepositoryException {
84          final List<MediaEditorDefinition> definitions = new ArrayList<MediaEditorDefinition>();
85          for (Node node : nodes) {
86              NodeUtil.visit(node, new NodeVisitor() {
87                  @Override
88                  public void visit(Node current) throws RepositoryException {
89                      for (Node editorNode : NodeUtil.getNodes(current, NodeTypes.ContentNode.NAME)) {
90                          if (isMediaEditor(editorNode)) {
91                              // Handle as dialog only if it has sub nodes indicating that it is actually representing a dialog.
92                              // This will filter the fields in dialogs used by the extends mechanism.
93                              MediaEditorDefinition definition = createMediaEditorDefinition(editorNode);
94                              if (definition != null) {
95                                  definitions.add(definition);
96                              }
97                          } else {
98                              log.warn("node " + editorNode.getName() + " will not be handled as Dialog.");
99                          }
100                     }
101                 }
102             }, new NodeTypePredicate(NodeTypes.Content.NAME));
103         }
104         this.registeredIds = registry.unregisterAndRegister(registeredIds, definitions);
105     }
106 
107     protected MediaEditorDefinition createMediaEditorDefinition(Node editorNode) throws RepositoryException {
108         String id = createId(editorNode);
109         try {
110             ConfiguredMediaEditorDefinition def =  (ConfiguredMediaEditorDefinition) Components.getComponent(Node2BeanProcessor.class).toBean(editorNode, MediaEditorDefinition.class);
111             if (def != null) {
112                 def.setId(id);
113             }
114             return def;
115         } catch (Node2BeanException e) {
116             log.error("Unable to create a definition for editor [" + id + "]: " + e);
117         } catch (RepositoryException e) {
118             log.error("Unable to create a definition for editor [" + id + "]: " + e);
119         }
120         return null;
121     }
122 
123     protected String createId(Node configNode) throws RepositoryException {
124         final String path = configNode.getPath();
125         final String[] pathElements = path.split("/");
126         final String moduleName = pathElements[2];
127         return moduleName + ":" + StringUtils.removeStart(path, "/modules/" + moduleName + "/" + MEDIA_EDITORS_CONFIG_NODE_NAME + "/");
128     }
129 
130     private boolean isMediaEditor(Node mediaEditorNode) {
131         return true;
132     }
133 }