01: package org.osbl.agent.model.action;
02:
03: import org.conform.BeanMeta;
04: import org.conform.PropertyMeta;
05: import org.osbl.agent.model.RuleContext;
06:
07: /**
08: * This Action sets a Date (or supported class) field to the current time.
09: *
10: * @author Sebastian Nozzi.
11: */
12: public class SetCurrentTimeAction extends SetValueAction {
13:
14: /** The token that will point to the current time. Users can enter it in the UI. */
15: static public final String predefinedNow = "now";
16:
17: /**
18: * Checks if a given class is a supported time class.
19: * Examples could be java.sql.Timestamp, java.util.Date, etc.
20: *
21: * @param timeClass the class to analyse
22: *
23: * @return true, if is a supported time class
24: */
25: public static boolean isSupportedTimeClass(Class timeClass) {
26: // Returns true if the class is one of the time related classes
27: // supported by this SetCurrentTimeAction.
28: return (java.sql.Timestamp.class.isAssignableFrom(timeClass)
29: || java.sql.Date.class.isAssignableFrom(timeClass)
30: || java.sql.Time.class.isAssignableFrom(timeClass) || java.util.Date.class
31: .isAssignableFrom(timeClass));
32: }
33:
34: /* (non-Javadoc)
35: * @see org.osbl.agent.model.action.SetPropertyAction#execute(org.osbl.agent.model.RuleContext)
36: */
37: public void execute(RuleContext context) {
38:
39: // Here we'll have an instance of whichever class applies,
40: // set to our current time.
41: Object currentDateTime = null;
42:
43: // Using the propertyMeta's type, see what class we need to instantiate
44: // and set this to the current time...
45:
46: Class type = Object.class;
47:
48: Object beanMeta = context.get(context.BEAN_META);
49: if (beanMeta instanceof BeanMeta) {
50: PropertyMeta propertyMeta = ((BeanMeta) beanMeta)
51: .getProperty(getPropertyMetaName());
52: type = propertyMeta.getType();
53: }
54:
55: if (type.isAssignableFrom(java.util.Date.class))
56: currentDateTime = new java.util.Date();
57: else if (type.isAssignableFrom(java.sql.Time.class))
58: currentDateTime = new java.sql.Time(new java.util.Date()
59: .getTime());
60: else if (type.isAssignableFrom(java.sql.Timestamp.class))
61: currentDateTime = new java.sql.Timestamp(
62: new java.util.Date().getTime());
63: else if (type.isAssignableFrom(java.sql.Date.class))
64: currentDateTime = new java.sql.Date(new java.util.Date()
65: .getTime());
66:
67: context.put(predefinedNow, currentDateTime);
68:
69: // ...and execute the action using the evaluation context
70: super.execute(context);
71: }
72:
73: }
|