View Javadoc

1   /**
2    * This file Copyright (c) 2010-2010 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.templatingcomponents.components;
35  
36  import info.magnolia.cms.beans.config.ServerConfiguration;
37  import info.magnolia.cms.core.AggregationState;
38  import info.magnolia.cms.core.Content;
39  import info.magnolia.cms.core.ItemType;
40  import info.magnolia.cms.i18n.MessagesManager;
41  import info.magnolia.cms.security.Permission;
42  import info.magnolia.module.templating.ParagraphManager;
43  import info.magnolia.module.templating.RenderableDefinition;
44  import info.magnolia.module.templating.TemplateManager;
45  import info.magnolia.module.templatingcomponents.AuthoringUiComponent;
46  import org.apache.commons.lang.StringUtils;
47  
48  import javax.jcr.RepositoryException;
49  import java.io.IOException;
50  import java.util.List;
51  
52  /**
53   * Common subclass for ui components, provides utility methods and defaults.
54   * Implementations should expose setter methods for their specific parameters (so that template-specific wrappers
55   * can set parameters). (no need to clutter things up with getters). Implementation might also expose static factory
56   * methods, which can take care of default values, i.e for labels.
57   *
58   *
59   * @author gjoseph
60   * @version $Revision: $ ($Author: $)
61   */
62  public abstract class AbstractAuthoringUiComponent implements AuthoringUiComponent {
63      private static final String DEFAULT_I18N_BASENAME = "info.magnolia.module.templatingcomponents.messages";
64  
65      private final ServerConfiguration server;
66      private final AggregationState aggregationState;
67      private final TemplateManager templateManager;
68      private final ParagraphManager paragraphManagerManager;
69  
70      protected AbstractAuthoringUiComponent(final ServerConfiguration server, final AggregationState aggregationState) {
71          this.server = server;
72          this.aggregationState = aggregationState;
73  
74          this.templateManager = TemplateManager.getInstance();
75          this.paragraphManagerManager = ParagraphManager.getInstance();
76      }
77  
78      protected ServerConfiguration getServer() {
79          return server;
80      }
81  
82      protected AggregationState getAggregationState() {
83          return aggregationState;
84      }
85  
86      public void render(Appendable out) throws IOException {
87          if (!shouldRender()) {
88              return;
89          }
90          try {
91              doRender(out);
92          } catch (RepositoryException e) {
93              throw new RuntimeException(e);
94          }
95      }
96  
97      protected abstract void doRender(Appendable out) throws IOException, RepositoryException;
98  
99      /**
100      * Override this method if you need to "do something" once the component is rendered, i.e cleanup the context.
101      */
102     public void postRender() {
103     }
104 
105     /**
106      * Returns the "current content" from the aggregation state.
107      * Override this method if your component needs a different target node.
108      */
109     protected Content currentContent() {
110         final Content currentContent = aggregationState.getCurrentContent();
111         if (currentContent == null) {
112             throw new IllegalStateException("Could not determine currentContent from AggregationState, currentContent is null");
113         }
114         return currentContent;
115     }
116 
117     /**
118      * Override this method if the component needs to be rendered under different conditions.
119      */
120     protected boolean shouldRender() {
121         return (server.isAdmin() && aggregationState.getMainContent().isGranted(Permission.SET));
122     }
123 
124     /**
125      * The given node, if it has a mgnl:template property in it's metadata, will be used in conjunction with
126      * TemplateManager and ParagraphManager to figure out the i18nBasename to use to translate this key.
127      */
128     protected String getMessage(Content context, String key) {
129         final String i18Basename = getI18BasenameFor(context);
130         return getMessage(i18Basename, key);
131     }
132 
133     protected String getI18BasenameFor(Content content) {
134         final String templateName = content.getMetaData().getTemplate();
135         final RenderableDefinition renderable;
136         if (content.isNodeType(ItemType.CONTENT.getSystemName())) {
137             renderable = templateManager.getTemplateDefinition(templateName);
138         } else {
139             renderable = paragraphManagerManager.getParagraphDefinition(templateName);
140         }
141         if (renderable != null && renderable.getI18nBasename() != null) {
142             return renderable.getI18nBasename();
143         } else {
144             return DEFAULT_I18N_BASENAME;
145         }
146     }
147 
148     protected String getMessage(String basename, String key) {
149         String s = MessagesManager.getMessages(basename).getWithDefault(key, key);
150         if (key.equals(s)) {
151             // fallback to our default bundle if the specific one did not contain the key - and working around DefaultMessagesImpl.get()'s behaviour of adding ??? around unknown keys
152             s = MessagesManager.getMessages(DEFAULT_I18N_BASENAME).getWithDefault(key, key);
153         }
154         return s;
155     }
156 
157     /**
158      * Utility method - our current gui components (magnolia-gui) expect comma separated strings.
159      */
160     protected String asString(List<String> strings) {
161         return StringUtils.join(strings, ',');
162     }
163 
164 }