1
2
3
4
5
6 package org.weda.tapestry.component;
7
8 import java.util.Arrays;
9 import java.util.List;
10 import java.util.Set;
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.apache.tapestry.IRequestCycle;
14 import org.apache.tapestry.annotations.ComponentClass;
15 import org.apache.tapestry.annotations.InjectObject;
16 import org.apache.tapestry.request.IUploadFile;
17 import org.weda.Constants;
18 import org.weda.action.ActionContainer;
19 import org.weda.action.ActionContainerProvider;
20 import org.weda.action.ActionEvent;
21 import org.weda.action.ActionExecutionContext;
22 import org.weda.action.ActionListener;
23 import org.weda.action.ActionRegistry;
24 import org.weda.action.ActionRegistryException;
25 import org.weda.action.ActionState;
26 import org.weda.action.ExecutedActionInfo;
27 import org.weda.data.impl.ViewDataAction;
28 import org.weda.model.EditorModel;
29 import org.weda.model.impl.EditorModelDataId;
30 import org.weda.property.PropertyDescriptor;
31
32 /**
33 *
34 * @author Mikhail Titov
35 */
36 @ComponentClass(allowBody=false)
37 public abstract class FileEditor
38 extends AbstractEditor
39 implements ActionContainer, ActionContainerProvider, ActionListener
40 {
41 private final static Log log = LogFactory.getLog(FileEditor.class);
42
43 @InjectObject("service:org.weda.action.ActionRegistry")
44 public abstract ActionRegistry getActionRegistry();
45
46 public abstract IUploadFile getFile();
47
48 public String getEditorClass(){
49 return Constants.EDITOR_CLASS;
50 }
51
52 public void makeUpdate() throws Exception {
53 String errorMessage=null;
54 EditorModel model = getModel();
55 IUploadFile file = getFile();
56 if (file!=null){
57 try{
58 if (model.isNeedConversion()){
59 PropertyDescriptor desc =
60 model.getPropertyDescriptor();
61 Class valueClass=desc.getPropertyClass();
62 Object value =
63 getConverterService().convert(
64 valueClass, file.getStream(), null);
65 model.setValue(value);
66 }else
67 model.setValue(getModel().getValuePretender());
68 }finally{
69 file.getStream().close();
70 }
71 }else if (model.isValuePretenderSetted()){
72 Object value = model.getValuePretender();
73 if (model.isNeedConversion())
74 value = getConverterService().convert(
75 model.getPropertyDescriptor().getPropertyClass()
76 , value, null);
77 model.setValue(getModel().getValuePretender());
78 }
79 }
80
81 public ExecutedActionInfo executeAction(ActionExecutionContext context)
82 throws ActionRegistryException
83 {
84 context.setTargetObject(this);
85 return getActionRegistry().executeAction(context);
86 }
87
88 public boolean hasRegisteredActions() {
89 return getActionRegistry().hasRegisteredActions(this.getClass(), this);
90 }
91
92 public Set<Class> getDisabledActions() {
93 return null;
94 }
95
96 public List<ActionState> getActionsStates() throws ActionRegistryException {
97 return getActionRegistry().getActionsStates(this, FileEditor.class);
98 }
99
100 public String getActionContainerName() {
101 return getId();
102 }
103
104 public List<ActionContainer> getActionContainers() throws Exception {
105 return Arrays.asList((ActionContainer)this);
106 }
107
108 public void beforeExecute(ActionEvent event) throws Exception
109 {
110 ActionExecutionContext context = event.getExecutionContext();
111 if ( ViewDataAction.class.equals(
112 context.getActionDescriptor().getActionClass())
113 && this == context.getTargetObject())
114 {
115 EditorModelDataId dataId =
116 new EditorModelDataId(
117 getEditorGroup().getModelGroup().getName()
118 , getModel().getName());
119 context.setActionParameterValue("dataId", dataId);
120 }
121 }
122
123 public void afterExecute(ActionEvent event) throws Exception {
124 }
125
126 protected void prepareForRender(IRequestCycle cycle) {
127 super.prepareForRender(cycle);
128 ActionPanelHelper.addActionContainerProvider(cycle, this);
129 ActionPanelHelper.addActionListener(cycle, this);
130 }
131
132 }