View Javadoc

1   /*
2    * ObjectSourcePageValueEditorAction.java
3    * Created on 10 Июль 2006 г., 13:30
4    */
5   
6   package org.weda.store.actions;
7   
8   import java.util.List;
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  import org.weda.action.ActionContainer;
12  import org.weda.action.ActionState;
13  import org.weda.action.impl.AbstractAction;
14  import org.weda.action.impl.ActionStateImpl;
15  import org.weda.enhance.InjectHivemindObject;
16  import org.weda.property.Constraint;
17  import org.weda.store.ObjectSource;
18  import org.weda.store.ObjectSourceRegistry;
19  import org.weda.store.impl.ObjectSourceSetConstraint;
20  import org.weda.tapestry.component.BaseEditor;
21  import org.weda.workflow.Workflow;
22  import org.weda.workflow.impl.DefaultPath;
23  
24  /**
25   *
26   * @author Mikhail Titov
27   */
28  public class ObjectSourcePageValueEditorAction extends AbstractAction {
29      private final static Log log = 
30              LogFactory.getLog(ObjectSourcePageValueEditorAction.class);
31      
32      @InjectHivemindObject()
33      private static ObjectSourceRegistry objectSourceRegistry;
34      @InjectHivemindObject()
35      private static Workflow workflow;
36      
37      public ActionState getActionState(ActionContainer targetObject) 
38          throws Exception 
39      {
40          BaseEditor editor = (BaseEditor)targetObject;
41          boolean available = 
42                  editor.getValueEditorPageName()!=null
43                  && getConstraint(editor)!=null;
44          return new ActionStateImpl(available, true);
45      }
46  
47      public Object execute(ActionContainer targetObject) throws Exception {
48          BaseEditor editor = (BaseEditor)targetObject;
49          ObjectSourceSetConstraint constraint = getConstraint(editor);
50          ObjectSource objectSource = getObjectSource(constraint);
51          Object currentValue=editor.getModel().getValuePretender();
52          if (currentValue==null)
53              currentValue=editor.getModel().getValue();
54          if (currentValue!=null)
55              objectSource.refresh(currentValue);
56          else
57              objectSource.close();        
58          objectSource.setMode(ObjectSource.Mode.SELECT);
59          DefaultPath currentPage = 
60                  new DefaultPath(workflow.getCurrentPage(), false);
61          workflow.pushPath(currentPage);
62          DefaultPath selectPage = 
63                  new DefaultPath(editor.getValueEditorPageName(), false);        
64          workflow.pushPath(selectPage);
65          if (log.isDebugEnabled())
66              log.debug(
67                      "value editor page name is "
68                          +editor.getValueEditorPageName());
69          return null;
70      }
71  
72      public Object afterLinkedActionExecute(
73              Object targetObject
74              , Object linkedTargetObject, boolean cancelAction) 
75          throws Exception 
76      {
77          BaseEditor editor = (BaseEditor)targetObject;
78          ObjectSourceSetConstraint constraint = getConstraint(editor);
79          ObjectSource source = getObjectSource(constraint);    
80          if (!cancelAction){
81              int selectedRow = source.getSelectedRow();
82              if (selectedRow == -1)
83                  throw new Exception(
84                      String.format(
85                          "No selected rows in objectSource (%s)"
86                          , constraint.getObjectSourceName()));
87              Object[] row = source.getRowAt(selectedRow);
88              Object value = row[source.getBaseClassPosition()];
89              if (editor.isDirectValueSet())
90                  editor.getModel().setValue(value);
91              else
92                  editor.getModel().setValuePretender(value);
93          }
94          source.setMode(ObjectSource.Mode.VIEW);
95          source.close();        
96          return null;
97      }
98      
99      private ObjectSource getObjectSource(ObjectSourceSetConstraint constraint)
100         throws Exception
101     {
102         if (log.isDebugEnabled()){
103             log.debug("objectSourceRegistry is null: "+(objectSourceRegistry==null));
104             log.debug("constraint is null: "+(constraint==null));
105         }
106         return objectSourceRegistry.getObjectSource(
107                     constraint.getObjectSourceName());
108     }
109     
110     private ObjectSourceSetConstraint getConstraint(BaseEditor editor) 
111         throws Exception
112     {
113         if (log.isDebugEnabled())
114             log.debug(
115                     String.format(
116                         "Editor info: modelName (%s)"
117                         , editor.getModel().getName()));
118         ObjectSourceSetConstraint cons = null;
119         List<Constraint> constraints = 
120                 editor.getModel().getPropertyDescriptor().getConstraints();
121         if (log.isDebugEnabled())
122             log.debug("descriptor contains constraints: "+(constraints!=null));
123         if (constraints!=null)
124             for (Constraint constraint: constraints)
125                 if (constraint instanceof ObjectSourceSetConstraint){
126                     cons = (ObjectSourceSetConstraint)constraint;
127                     break;
128                 }
129         if (cons==null)
130             log.warn("ObjectSourceSetConstraint not found");
131         return cons;
132     }
133     
134 }