1
2
3
4
5
6 package org.weda.tapestry.component;
7
8 import java.util.ArrayList;
9 import java.util.Arrays;
10 import java.util.LinkedList;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.Set;
14 import org.apache.commons.collections.CollectionUtils;
15 import org.apache.commons.lang.ObjectUtils;
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.apache.hivemind.ApplicationRuntimeException;
19 import org.apache.tapestry.IBinding;
20 import org.apache.tapestry.IForm;
21 import org.apache.tapestry.IMarkupWriter;
22 import org.apache.tapestry.IRequestCycle;
23 import org.apache.tapestry.TapestryUtils;
24 import org.apache.tapestry.annotations.ComponentClass;
25 import org.apache.tapestry.annotations.InjectObject;
26 import org.apache.tapestry.annotations.Parameter;
27 import org.apache.tapestry.annotations.Persist;
28 import org.weda.Constants;
29 import org.weda.action.ActionContainer;
30 import org.weda.action.ActionContainerProvider;
31 import org.weda.action.ActionExecutionContext;
32 import org.weda.action.ActionRegistry;
33 import org.weda.action.ActionRegistryException;
34 import org.weda.action.ActionState;
35 import org.weda.action.ExecutedActionInfo;
36 import org.weda.cache.CacheManager;
37 import org.weda.cache.CacheService;
38 import org.weda.cache.impl.SimpleCacheEntity;
39 import org.weda.enhance.InjectHivemindObject;
40 import org.weda.model.EditorModel;
41 import org.weda.property.Constraint;
42 import org.weda.property.PropertyDescriptor;
43 import org.weda.model.EditorModelException;
44 import org.weda.property.SetConstraint;
45 import org.weda.property.SetValue;
46
47 /**
48 *
49 * @author Mikhail Titov
50 */
51 @ComponentClass(allowBody=false, allowInformalParameters=false)
52 public abstract class BaseEditor
53 extends AbstractEditor
54 implements ActionContainer, ActionContainerProvider
55 {
56 private final static Log log = LogFactory.getLog(BaseEditor.class);
57 private final static String VALUE_EDITOR_PAGE_NAME_STATE_NAME
58 = "valueEditorPageName";
59 private int valueListIndex;
60 private String modelName;
61
62 private IBinding valueEditorPageNameBinding;
63
64 @InjectObject("service:org.weda.action.ActionRegistry")
65 public abstract ActionRegistry getActionRegistry();
66 @InjectObject("service:org.weda.cache.CacheManager")
67 public abstract CacheManager getCacheManager();
68
69
70 @Parameter(required=false, defaultValue="ognl:10")
71 public abstract int getCols();
72 @Parameter(required=false, defaultValue="ognl:1")
73 public abstract int getRows();
74 @Parameter(required=false)
75 public abstract String getValue();
76 @Parameter(required=false, defaultValue="ognl:null")
77 public abstract String getValueEditorPageName();
78 @Parameter(required=false, defaultValue="ognl:false")
79 public abstract boolean isDirectValueSet();
80
81 private Object getEditorValue(){
82 SimpleCacheEntity cache = getEditorValueCache();
83 return cache == null ? null : cache.getValue();
84 }
85
86 private void setEditorValue(Object val){
87 SimpleCacheEntity cache = getEditorValueCache();
88 if (cache==null){
89 cache = new SimpleCacheEntity(val);
90 getCacheManager().getCacheService().setCacheEntity(
91 getEditorValueCacheId(), cache);
92 }else
93 cache.setValue(val);
94 }
95
96 private SimpleCacheEntity getEditorValueCache(){
97 return
98 (SimpleCacheEntity)
99 getCacheManager().getCacheService().get(
100 getEditorValueCacheId());
101 }
102
103 private void clearEditorValue() {
104 getCacheManager().getCacheService().release(getEditorValueCacheId());
105 }
106
107 private String getEditorValueCacheId(){
108 return getPage().getPageName()
109 +"#"+BaseEditor.class.getName()+"#"+getId()+"#"+getModelName();
110 }
111
112 public String getEditorClass(){
113 return Constants.EDITOR_CLASS;
114 }
115
116 public boolean isSelectValueFromList() throws Exception {
117 return getModel().hasValuesList() && getValueEditorPageName()==null;
118 }
119
120 public boolean getValueSelection(int index) throws Exception {
121 EditorModel model = getModel();
122 boolean res =
123 ObjectUtils.equals(
124 model.getValuesList()[index].getValue()
125 , super.getModelValue());
126 if (res && model.isEditable())
127 setEditorValue(super.getModelValue());
128 return res;
129 }
130
131 public void setValueSelection(int index, boolean value) throws Exception {
132 if (value) {
133 EditorModel model = getModel();
134 if (model.isEditable()) {
135 Object valueToSet = getModel().getValuesList()[index].getValue();
136 if (!ObjectUtils.equals(getEditorValue(), valueToSet))
137 model.setValuePretender(valueToSet);
138 }
139 model.resetValuesList();
140 }
141 }
142
143 public void makeUpdate() throws Exception {
144 String errorMessage=null;
145 EditorModel model = getModel();
146 if ( model.isValuePretenderSetted()
147 || !getEditorGroup().getModelGroup().isMultiLeveledModelValues())
148 {
149 Object value;
150 if (model.isValuePretenderSetted())
151 value = getModel().getValuePretender();
152 else
153 value = getModel().getValue();
154 PropertyDescriptor desc = model.getPropertyDescriptor();
155 if (model.isNeedConversion()){
156 Class valueClass=desc.getPropertyClass();
157 value = getConverterService().convert(
158 valueClass, value, desc.getPattern());
159 }
160 model.setValue(value);
161 }
162 clearEditorValue();
163 }
164
165 public String getValueAsString() {
166 try{
167 Object res = null;
168 String strRes = null;
169 if (isParameterBound("value") && getModelValue()!=null)
170 res = getValue();
171 else{
172 EditorModel model = getModel();
173 if (isSelectValueFromList()){
174 if (model.isValuesListAliased())
175 res = model.getValuesList()[getValueListIndex()]
176 .getAlias();
177 else
178 res = convertValueToString(
179 model.getValuesList()[getValueListIndex()]
180 .getValue());
181 }else
182 res = convertModelValueToString();
183 }
184 if (res instanceof java.lang.String)
185 strRes = (String)res;
186 else if (res != null)
187 strRes = res.toString();
188 if (!isSelectValueFromList() && getModel().isEditable())
189 setEditorValue(strRes);
190 if (log.isDebugEnabled())
191 log.debug(
192 String.format(
193 "<<< getValueAsString. Model name: %s, value: %s"
194 , getModelName(), strRes));
195 return strRes;
196 }catch(Exception e){
197 throw new ApplicationRuntimeException(e);
198 }
199 }
200
201 private Object convertModelValueToString() throws Exception {
202 if (log.isDebugEnabled())
203 log.debug(">> convertModelValueToString");
204 Object res;
205 EditorModel model = getModel();
206 if (model.isValuePretenderSetted()){
207 if (log.isDebugEnabled())
208 log.debug("value pretender is setted");
209 res = model.getValuePretender();
210 }else {
211 if (log.isDebugEnabled())
212 log.debug("value pretender is not setted");
213 if (model.isNeedConversion()){
214 res = convertValueToString(model.getValue());
215 }else
216 res = model.getValue();
217 }
218 return res;
219 }
220
221 private Object convertValueToString(Object value) throws Exception {
222 PropertyDescriptor desc =
223 getModel().getPropertyDescriptor();
224 Object res = getConverterService().convert(
225 String.class, value
226 , desc.getPattern());
227 return res;
228 }
229
230 public void setValueAsString(String value) throws Exception {
231 EditorModel model = getModel();
232 if (log.isDebugEnabled())
233 log.debug(
234 String.format(
235 ">> setValueAsString. Value: %s, Model name: %s"
236 , value, model.getName()));
237 if (model.isEditable()) {
238 if (!ObjectUtils.equals(getEditorValue(), value)){
239 model.setValuePretender(value);
240 if (log.isDebugEnabled())
241 log.debug("model value pretender is setted");
242 }
243 }
244 }
245
246 public List<ActionContainer> getActionContainers() throws Exception {
247 List<ActionContainer> containers = new LinkedList<ActionContainer>();
248 containers.add(this);
249 if (getModel() instanceof ActionContainer)
250 containers.add((ActionContainer)getModel());
251 return containers;
252 }
253
254 public Object getModelValue() {
255 try {
256 EditorModel model = getModel();
257 if (model.hasValuesList())
258 return model.getValuesList()[getValueListIndex()].getValue();
259 else
260 return super.getModelValue();
261 } catch (EditorModelException ex) {
262 throw new ApplicationRuntimeException(ex);
263 }
264 }
265
266 public int getValueListIndex() {
267 return valueListIndex;
268 }
269
270 public void setValueListIndex(int valueListIndex) {
271 this.valueListIndex = valueListIndex;
272 }
273
274 public ExecutedActionInfo executeAction(ActionExecutionContext context)
275 throws ActionRegistryException
276 {
277 context.setTargetObject(this);
278 return getActionRegistry().executeAction(context);
279 }
280
281 public boolean hasRegisteredActions() {
282 return getActionRegistry().hasRegisteredActions(BaseEditor.class, this);
283 }
284
285 public Set<Class> getDisabledActions() {
286 return null;
287 }
288
289 public List<ActionState> getActionsStates() throws ActionRegistryException {
290 return getActionRegistry().getActionsStates(this, BaseEditor.class);
291 }
292
293 public String getActionContainerName() {
294 return getId();
295 }
296
297 protected void prepareForRender(IRequestCycle cycle) {
298 valueEditorPageNameBinding = null;
299 ActionPanelHelper.addActionContainerProvider(cycle, this);
300
301 modelName = getModelName();
302 IForm form = (IForm)cycle.getAttribute(TapestryUtils.FORM_ATTRIBUTE);
303 boolean formRewinding = form!=null && form.isRewinding();
304 log.debug(
305 String.format(
306 ">>!!prepareForRender BaseEditor. " +
307 "ModelName: %s, is rewinding: %s, form is null: %s, " +
308 "is form rewinding: %s"
309 , modelName, cycle.isRewinding(), form==null, formRewinding));
310 super.prepareForRender(cycle);
311 }
312
313 public Map getState() {
314 Map state = super.getState();
315 state.put(VALUE_EDITOR_PAGE_NAME_STATE_NAME, getValueEditorPageName());
316 return state;
317 }
318
319 public void restoreState(Map state) {
320 super.restoreState(state);
321 if (state!=null){
322 valueEditorPageNameBinding = getBinding("valueEditorPageName");
323 IBinding newBinding =
324 getBindingFactory().createBinding(
325 this, ""
326 , (String)state.get(VALUE_EDITOR_PAGE_NAME_STATE_NAME)
327 , getLocation());
328 setBinding("valueEditorPageName", newBinding);
329 }
330 }
331
332 public void resetState(Map state) {
333 super.resetState(state);
334 if (state != null)
335 setBinding("valueEditorPageName", valueEditorPageNameBinding);
336 }
337
338
339
340 }