View Javadoc

1   package org.weda.action.impl;
2   
3   import java.util.ArrayList;
4   import java.util.HashMap;
5   import java.util.HashSet;
6   import java.util.List;
7   import java.util.Map;
8   import java.util.Map.Entry;
9   import java.util.Set;
10  import org.apache.commons.lang.ObjectUtils;
11  import org.weda.action.ActionDescriptor;
12  import org.weda.action.ActionDescriptorException;
13  import org.weda.action.ActionParameter;
14  import org.weda.action.ActionParameterValue;
15  import org.weda.action.ActionTargetClass;
16  import org.weda.action.LinkedAction;
17  import org.weda.action.Parameter.Direction;
18  import org.weda.converter.ValueTypeConverter;
19  import org.weda.enhance.InjectHivemindObject;
20  import org.weda.message.Messages;
21  import org.weda.message.impl.InplaceMessage;
22  import org.weda.property.ObjectDescriptorRegistry;
23  import org.weda.property.PropertyDescriptor;
24  import org.weda.property.PropertyValue;
25  import org.weda.property.PropertyValueException;
26  
27  /**
28   *
29   * @author tim
30   */
31  public class ActionDescriptorImpl 
32          implements ActionDescriptor, java.io.Serializable
33  {
34      @InjectHivemindObject()
35      private static PropertyValue propertyValue;
36      @InjectHivemindObject()
37      private static ObjectDescriptorRegistry objectDescriptorRegistry;
38      @InjectHivemindObject()
39      private static ValueTypeConverter converter;
40      
41      private String name;
42      private String description;
43      private Class actionClass;
44      private String accessKey;
45      private String targetFrame;
46      private Set<ActionTargetClass> targetClasses = 
47          new HashSet<ActionTargetClass>();
48      private List<ActionParameter> actionParameters = 
49              new ArrayList<ActionParameter>();
50      private List<ActionParameterValue> parametersValues;
51      private Map<Integer, Object> compiledParameterValues;
52      private Map<String, ActionParameter> actionParametersMap = 
53              new HashMap<String, ActionParameter>();
54      private List<LinkedAction> linkedActions = new ArrayList<LinkedAction>(2);    
55      private Messages messages;
56      
57      public void init() throws ActionDescriptorException {
58          if (parametersValues != null){
59              try{
60                  compiledParameterValues = new HashMap<Integer, Object>();
61                  for (ActionParameterValue parameterValue: parametersValues){
62                      String propertyName = parameterValue.getName();
63                      ActionParameter actionParameter = 
64                              getActionParameter(propertyName);
65                      if (actionParameter.getDirection()==Direction.READ)
66                          throw new ActionDescriptorException(
67                                  String.format(
68                                      "Can't use action (%s) parameter (%s) " +
69                                      "for set value operation because " +
70                                      "of it's read only parameter"
71                                      , actionClass.getName(), propertyName));                    
72                      Integer id = 
73                              propertyValue.compileSetter(
74                                  actionClass, propertyName);
75                      PropertyDescriptor paramDesc = 
76                              objectDescriptorRegistry.getPropertyDescriptor(
77                                  actionClass, propertyName);
78                      Object value = converter.convert(
79                              paramDesc.getPropertyClass()
80                              , parameterValue.getValue()
81                              , parameterValue.getPattern());
82                      compiledParameterValues.put(id, value);
83                  }
84              }catch(Exception e){
85                  throw new ActionDescriptorException(
86                          String.format(
87                              "Error while initializgin parameter " +
88                              "values for action (%s)"
89                              , actionClass.getName())
90                          , e);
91              }
92          }
93      }
94      
95      public Set<ActionTargetClass> getTargetClasses() {
96          return targetClasses;
97      }
98      
99      public void addTargetClass(ActionTargetClass targetClass){
100         targetClasses.add(targetClass);
101     }
102     
103     public void addLinkedAction(LinkedAction linkedAction){
104         linkedActions.add(linkedAction);
105     }
106 
107     public String getName() {
108         return messages.replaceInPlace(name);
109     }
110     
111     public void setName(String name){
112         this.name=name;
113     }
114 
115     public String getDescription() {        
116         return messages.replaceInPlace(description)
117                 +(accessKey==null? "":" ["+getAccessKey()+"]");
118     }
119     
120     public void setDescription(String description){
121         this.description=description;
122     }
123 
124     public List<ActionParameter> getActionParameters() {
125         return actionParameters;
126     }
127     
128     public void addActionParameter(ActionParameter actionParameter) {
129         actionParameters.add(actionParameter);
130         actionParametersMap.put(actionParameter.getName(), actionParameter);
131     }
132 
133     public Class getActionClass() {
134         return actionClass;
135     }
136     
137     public void setActionClass(Class actionClass){
138         this.actionClass=actionClass;
139     }
140 
141     public ActionParameter getActionParameter(String name) 
142         throws ActionDescriptorException 
143     {
144         ActionParameter parameter = actionParametersMap.get(name);
145         if (parameter==null)
146             throw new ActionDescriptorException(
147                     String.format(
148                         "Parameter (%s) not defined for action " +
149                         "(name: %s, class: %s)"
150                         , name, getName(), getActionClass().getName()));
151         return parameter;
152     }
153 
154     public Messages getMessages() {
155         return messages;
156     }
157 
158     public void setMessages(Messages messages) {
159         this.messages = messages;
160     }
161 
162     public List<LinkedAction> getLinkedActions() {
163         return linkedActions;
164     }
165     
166     public boolean equals(Object o){
167         if (o instanceof ActionDescriptor){
168             ActionDescriptor desc = (ActionDescriptor)o;
169             return ObjectUtils.equals(name, desc.getName())
170                 && actionClass.equals(desc.getActionClass());
171         }
172         return false;
173     }
174 
175     public String getTargetFrame() {
176         return targetFrame;
177     }
178 
179     public void setTargetFrame(String targetFrame) {
180         this.targetFrame = targetFrame;
181     }
182 
183     public void setActionParametersValues(Object action) 
184         throws ActionDescriptorException 
185     {
186         if (compiledParameterValues!=null){
187             try {
188                 for (Entry<Integer, Object> entry: 
189                         compiledParameterValues.entrySet())
190                 {
191                     propertyValue.setValue(
192                             action, entry.getKey(), entry.getValue());
193                 }
194             } catch (PropertyValueException ex) {
195                 throw new ActionDescriptorException(
196                         String.format(
197                             "Error while setting parameters values " +
198                             "for action (%s)"
199                             , actionClass.getName())
200                         , ex);
201             }
202         }
203     }
204 
205     public void addActionParameterValue(ActionParameterValue parameterValue) {
206         if (parametersValues==null)
207             parametersValues = new ArrayList<ActionParameterValue>(5);
208         parametersValues.add(parameterValue);
209     }
210 
211     public String getAccessKey() {
212         return messages.replaceInPlace(accessKey);
213     }
214 
215     public void setAccessKey(String accessKey) {
216         this.accessKey = accessKey;
217     }
218 
219 
220 }