1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
52
53
54
55
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
70
71 public String determineTemplatePath(String actionResult, RenderingModel model ) {
72 return this.getTemplatePath();
73 }
74
75
76
77
78
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)
168 .append("type", this.type)
169 .append("description", this.description)
170 .append("dialog", this.dialog)
171 .append("title", this.title)
172 .append("templatePath", this.templatePath)
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 }