View Javadoc

1   /**
2    * This file Copyright (c) 2008-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.templating;
35  
36  import java.lang.reflect.InvocationTargetException;
37  import java.util.HashMap;
38  import java.util.Map;
39  
40  import org.apache.commons.beanutils.BeanUtils;
41  import org.apache.commons.lang.ArrayUtils;
42  import org.apache.commons.lang.builder.ToStringBuilder;
43  
44  import info.magnolia.cms.core.Content;
45  import info.magnolia.context.MgnlContext;
46  import info.magnolia.objectfactory.Classes;
47  import info.magnolia.objectfactory.MgnlInstantiationException;
48  
49  
50  /**
51   * Base implementation for paragraph and template definitions. Provides the
52   * {@link #modelClass} property which is used in the method
53   * {@link #newModel(Content, RenderableDefinition , RenderingModel)}
54   * @author pbracher
55   * @version $Id: AbstractRenderable.java 32667 2010-03-13 00:37:06Z gjoseph $
56   */
57  public class AbstractRenderable implements RenderableDefinition {
58      private String name;
59      private String title;
60      private String templatePath;
61      private String dialog;
62      private String type;
63      private String description;
64      private String i18nBasename;
65      private Class<? extends RenderingModel> modelClass = RenderingModelImpl.class;
66      private Map parameters = new HashMap();
67  
68      /**
69       * Return always the {@link #templatePath} property.
70       */
71      public String determineTemplatePath(String actionResult, RenderingModel model ) {
72          return this.getTemplatePath();
73      }
74  
75      /**
76       * Instantiates the model based on the class defined by the {@link #modelClass} property. The class must provide a
77       * constructor similar to {@link RenderingModelImpl#RenderingModelImpl(Content, RenderableDefinition, RenderingModel)}.
78       * All the request parameters are then mapped to the model's properties.
79       */
80      public RenderingModel newModel(Content content, RenderableDefinition definition, RenderingModel parentModel) throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
81          try {
82              final RenderingModel model = Classes.getClassFactory().newInstance(getModelClass(), new Class[]{Content.class, definition.getClass(), RenderingModel.class}, content, definition, parentModel);
83              final Map<String, String> params = MgnlContext.getParameters();
84              if (params != null) {
85                  BeanUtils.populate(model, params);
86              }
87              return model;
88          } catch (MgnlInstantiationException e) {
89              throw new IllegalArgumentException(MISSING_CONSTRUCTOR_MESSAGE + "Can't instantiate " + getModelClass(), e);
90          }
91      }
92  
93      public String getName() {
94          return this.name;
95      }
96  
97      public String getTitle() {
98          return this.title;
99      }
100 
101     public String getTemplatePath() {
102         return this.templatePath;
103     }
104 
105     public String getType() {
106         return type;
107     }
108 
109     public String getDescription() {
110         return this.description;
111     }
112 
113     public void setDescription(String description) {
114         this.description = description;
115     }
116 
117     public void setName(String name) {
118         this.name = name;
119     }
120 
121     public void setTemplatePath(String templatePath) {
122         this.templatePath = templatePath;
123     }
124 
125     public void setType(String type) {
126         this.type = type;
127     }
128 
129     public void setTitle(String title) {
130         this.title = title;
131     }
132 
133     public String getDialog() {
134         return this.dialog;
135     }
136 
137     public void setDialog(String dialog) {
138         this.dialog = dialog;
139     }
140 
141     public String getI18nBasename() {
142         return this.i18nBasename;
143     }
144 
145     public void setI18nBasename(String basename) {
146         this.i18nBasename = basename;
147     }
148 
149     public Map getParameters() {
150         return this.parameters;
151     }
152 
153     public void setParameters(Map params) {
154         this.parameters = params;
155     }
156 
157     public Class<? extends RenderingModel> getModelClass() {
158         return this.modelClass;
159     }
160 
161     public void setModelClass(Class<? extends RenderingModel> modelClass) {
162         this.modelClass = modelClass;
163     }
164 
165     public String toString() {
166         return new ToStringBuilder(this)
167         .append("name", this.name) //$NON-NLS-1$
168         .append("type", this.type) //$NON-NLS-1$
169         .append("description", this.description) //$NON-NLS-1$
170         .append("dialog", this.dialog) //$NON-NLS-1$
171         .append("title", this.title) //$NON-NLS-1$
172         .append("templatePath", this.templatePath) //$NON-NLS-1$
173         .toString();
174     }
175 
176     private static final Class<?>[] MODEL_CONSTRUCTOR_TYPES = new Class[]{Content.class, RenderableDefinition.class, RenderingModel.class};
177     private static final String MISSING_CONSTRUCTOR_MESSAGE;
178     static {
179         MISSING_CONSTRUCTOR_MESSAGE = "A model class must define a constructor with types " + ArrayUtils.toString(MODEL_CONSTRUCTOR_TYPES) + ". ";
180     }
181 }