View Javadoc

1   /* ===================================================================
2    * Copyright 2002-04 SRI International.
3    * Released under the MOZILLA PUBLIC LICENSE Version 1.1
4    * which can be obtained at http://www.mozilla.org/MPL/MPL-1.1.html
5    * This software is distributed on an "AS IS"
6    * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
7    * See the License for the specific language governing rights and
8    * limitations under the License.
9    * =================================================================== */
10  package com.sri.emo.controller;
11  
12  import com.jcorporate.expresso.core.controller.*;
13  import com.jcorporate.expresso.core.db.DBException;
14  import com.sri.common.controller.AbstractDBController;
15  import com.sri.common.controller.IFooterStateHandler;
16  import com.sri.common.controller.IHeaderStateHandler;
17  import com.sri.emo.dbobj.INodeHandler;
18  import com.sri.emo.dbobj.NodeType;
19  import com.sri.emo.dbobj.PartsFactory;
20  
21  import java.util.ArrayList;
22  import java.util.Iterator;
23  import java.util.TreeSet;
24  
25  
26  /***
27   * Common methods (header, footer) which are typically called via a strategy pattern.
28   *
29   * @author Larry Hamel
30   */
31  public class EmoAction extends AbstractDBController
32          implements IFooterStateHandler, IHeaderStateHandler {
33  
34      /***
35  	 * 
36  	 */
37  	private static final long serialVersionUID = 1L;
38  
39  	public EmoAction() {
40      }
41  
42      /***
43       * Return the title of this Controller
44       *
45       * @return java.lang.String The Title of the controller
46       */
47      public String getTitle() {
48          return "EmoAction Controller";
49      } /* getTitle() */
50  
51      /***
52       * footer will be executed here.
53       */
54      public void runFooterState(ControllerRequest request,
55                                 ControllerResponse response) throws ControllerException {
56          try {
57              addFooter(request, response);
58          } catch (DBException e) {
59              throw new ControllerException(e);
60          }
61      }
62  
63      /***
64       * // footer contains links to real and 'fake' entities
65       */
66      protected void addFooter(ControllerRequest request, ControllerResponse response) throws ControllerException,
67              DBException {
68  
69          //Add no footer or header if embedded
70          if (isEmbeddedMode(request)) {
71              return;
72          }
73  
74          TreeSet real = PartsFactory.getEntities();
75          ArrayList fake = PartsFactory.getFakeEntities();
76          real.addAll(fake);
77  
78          Transition trans = null;
79  
80          if (real.size() > 0) {
81              Block footerblock = new Block(FOOTER);
82              response.add(footerblock);
83  
84              for (Iterator iterator = real.iterator(); iterator.hasNext();) {
85                  NodeType type = (NodeType) iterator.next();
86  
87                  Block row = new Block(ROW_BLOCK);
88                  footerblock.add(row);
89  
90                  if (type.hasCustomHandler()) {
91                      INodeHandler handler = type.getCustomHandler();
92                      trans = handler.getListTransition(request);
93                  } else {
94                      trans = NodeAction.getListTransition(type.getEntityName());
95                  }
96  
97                  row.add(trans);
98  
99                  row.add(new Output(NodeType.DISPLAY_TITLE, type.getDisplayName()));
100             }
101         }
102     }
103 
104     protected void addHeader(final ControllerRequest request, final ControllerResponse response) throws DBException,
105             ControllerException {
106 
107         //If embedded no header or footer.
108         if (isEmbeddedMode(request)) {
109             return;
110         }
111 
112         // add trans for model editing (kind of misnomer, since privileges may not extend to edit
113         response.add(new Transition("", PartAction.class, PartAction.LIST_ENTITIES));
114     }
115 
116     /***
117      * a special state call will execute this state
118      */
119     public void runHeaderState(ControllerRequest request,
120                                ControllerResponse response) throws ControllerException {
121         try {
122             addHeader(request, response);
123         } catch (DBException e) {
124             throw new ControllerException(e);
125         }
126     }
127 }