View Javadoc

1   /**
2    * This file Copyright (c) 2008-2011 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.templating;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.context.MgnlContext;
38  import info.magnolia.objectfactory.Classes;
39  import info.magnolia.objectfactory.MgnlInstantiationException;
40  import info.magnolia.rendering.template.configured.ConfiguredTemplateDefinition;
41  
42  import java.lang.reflect.InvocationTargetException;
43  import java.util.HashMap;
44  import java.util.Map;
45  
46  import org.apache.commons.beanutils.BeanUtils;
47  import org.apache.commons.lang.ArrayUtils;
48  import org.apache.commons.lang.builder.ToStringBuilder;
49  
50  /**
51   * Base implementation for paragraph and template definitions. Provides the {@link #modelClass} property which is used in the method {@link #newModel(Content, RenderableDefinition , RenderingModel)}
52   * 
53   * @version $Id$
54   * @deprecated since 4.5, use {@link info.magnolia.rendering.template.configured.ConfiguredRenderableDefinition} instead.
55   */
56  @Deprecated
57  public class AbstractRenderable extends ConfiguredTemplateDefinition implements RenderableDefinition {
58      private String name;
59      private String title;
60      private String templateScript;
61      private String dialog;
62      private String renderType;
63      private String description;
64      private String i18nBasename;
65  
66      // TODO: check whether we shouldn't use LinkedHashMap here - would preserve order!
67      private Map parameters = new HashMap();
68  
69      /**
70       * Return always the {@link #templateScript} property.
71       */
72      @Override
73      public String determineTemplatePath(String actionResult, RenderingModel model) {
74          return this.getTemplateScript();
75      }
76  
77      /**
78       * Instantiates the model based on the class defined by the {@link #modelClass} property. The class must provide a constructor similar to {@link RenderingModelImpl#RenderingModelImpl(Content, RenderableDefinition, RenderingModel)}. All the request parameters are then mapped to the model's properties.
79       */
80      @Override
81      public RenderingModel newModel(Content content, RenderableDefinition definition, RenderingModel parentModel) throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
82          try {
83              final RenderingModel model = Classes.getClassFactory().newInstance((Class<? extends RenderingModel>) getModelClass(), new Class[] { Content.class, definition.getClass(), RenderingModel.class }, content, definition, parentModel);
84              final Map<String, String> params = MgnlContext.getParameters();
85              if (params != null) {
86                  BeanUtils.populate(model, params);
87              }
88              return model;
89          } catch (MgnlInstantiationException e) {
90              throw new IllegalArgumentException(MISSING_CONSTRUCTOR_MESSAGE + "Can't instantiate " + getModelClass(), e);
91          }
92      }
93  
94      @Override
95      public String getName() {
96          return this.name;
97      }
98  
99      @Override
100     public String getTitle() {
101         return this.title;
102     }
103 
104     /**
105      * @deprecated since 4.5 - use {@link #getTemplateScript()} instead
106      */
107     @Deprecated
108     @Override
109     public String getTemplatePath() {
110         return getTemplateScript();
111     }
112 
113     @Override
114     public String getTemplateScript() {
115         return this.templateScript;
116     }
117 
118     /**
119      * @deprecated since 4.5 - use {@link #getRenderType()} instead
120      */
121     @Deprecated
122     @Override
123     public String getType() {
124         return getRenderType();
125     }
126 
127     @Override
128     public String getRenderType() {
129         return renderType;
130     }
131 
132     @Override
133     public String getDescription() {
134         return this.description;
135     }
136 
137     @Override
138     public void setDescription(String description) {
139         this.description = description;
140     }
141 
142     @Override
143     public void setName(String name) {
144         this.name = name;
145     }
146 
147     /**
148      * @deprecated since 4.5 - use {@link #setTemplateScript(String)} instead
149      */
150     @Deprecated
151     public void setTemplatePath(String templatePath) {
152         setTemplateScript(templatePath);
153     }
154 
155     @Override
156     public void setTemplateScript(String templateScript) {
157         this.templateScript = templateScript;
158     }
159 
160     /**
161      * @deprecated since 4.5 - use {@link #setRenderType(String)} instead
162      */
163     @Deprecated
164     public void setType(String type) {
165         setRenderType(type);
166     }
167 
168     @Override
169     public void setRenderType(String renderType) {
170         this.renderType = renderType;
171     }
172 
173     @Override
174     public void setTitle(String title) {
175         this.title = title;
176     }
177 
178     @Override
179     public String getDialog() {
180         return this.dialog;
181     }
182 
183     @Override
184     public void setDialog(String dialog) {
185         this.dialog = dialog;
186     }
187 
188     @Override
189     public String getI18nBasename() {
190         return this.i18nBasename;
191     }
192 
193     @Override
194     public void setI18nBasename(String basename) {
195         this.i18nBasename = basename;
196     }
197 
198     @Override
199     public Map getParameters() {
200         return this.parameters;
201     }
202 
203     @Override
204     public void setParameters(Map params) {
205         this.parameters = params;
206     }
207 
208     @Override
209     public String toString() {
210         return new ToStringBuilder(this)
211         .append("name", this.name)
212         .append("type", this.renderType)
213         .append("description", this.description)
214         .append("dialog", this.dialog)
215         .append("title", this.title)
216         .append("templateScript", this.templateScript)
217         .toString();
218     }
219 
220     private static final Class<?>[] MODEL_CONSTRUCTOR_TYPES = new Class[] { Content.class, RenderableDefinition.class, RenderingModel.class };
221     private static final String MISSING_CONSTRUCTOR_MESSAGE;
222     static {
223         MISSING_CONSTRUCTOR_MESSAGE = "A model class must define a constructor with types " + ArrayUtils.toString(MODEL_CONSTRUCTOR_TYPES) + ". ";
224     }
225 }