View Javadoc

1   /*
2    * $Id: CommandImpl.java 121 2004-11-01 13:05:23Z josem $
3    *
4    * Tarsis
5    * Copyright (C) 2002 Talika Open Source Group
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   *
21   */
22  
23  package org.talika.tarsis.command;
24  
25  import java.util.Collections;
26  import java.util.HashMap;
27  import java.util.LinkedList;
28  import java.util.Collection;
29  import java.util.List;
30  import java.util.Map;
31  
32  import org.talika.tarsis.command.action.Action;
33  import org.talika.tarsis.command.view.View;
34  
35  /**
36   * Implementation of <code>Command</code> interface.
37   *
38   * @author  Jose M. Palomar
39   * @version $Revision: 121 $
40   */
41  public final class CommandImpl implements Command {
42  
43      // Fields
44      /**
45       * Name of command.
46       */
47      private final String name;
48  
49      /**
50       * Package of command.
51       */
52      private final String pak;
53  
54      /**
55       * Action instance of command.
56       */
57      private Action action;
58  
59      /**
60       * Map with all views defined for command.
61       */
62      private final Map views;
63  
64      /**
65       * Input view of command.
66       */
67      private final View inputView;
68  
69      /**
70       * List of parameters of command.
71       */
72      private final List parameters;
73  
74      /**
75       * Can command be cached?.
76       */
77      private final boolean cacheable;
78  
79      /**
80       * Can command be duplicated?.
81       */
82      private final boolean duplicable;
83  
84      /**
85       * Must command be validated?.
86       */
87      private final boolean validable;
88  
89      /**
90       * Must command be invoked in secure mode (HTTPS)?.
91       */
92      private final boolean secure;
93  
94      /**
95       * Type of command.
96       */
97      private final int type;
98  
99      // Constructors
100     /**
101      * Constructs a new <code>CommandImpl</code> object with specified name and
102      * package.
103      *
104      * @param name String name of command.
105      * @param pak String package of command.
106      */
107     public CommandImpl(String name, String pak) {
108         this.name = name;
109         this.pak = pak;
110         this.views = Collections.EMPTY_MAP;
111         this.inputView = null;
112         this.parameters = Collections.EMPTY_LIST;
113         this.cacheable = false;
114         this.duplicable = false;
115         this.validable = false;
116         this.secure = false;
117         this.type = Command.STATELESS;
118     }
119 
120     /**
121      * Constructs a new <code>CommandImpl</code> object with specified parameters.
122      *
123      * @param name String name of command.
124      * @param pak String package of command.
125      * @param action Action action of command.
126      * @param views View[] views of command.
127      * @param inputView View input view of command.
128      * @param parameters CommandParameter[] parameters of command.
129      * @param cacheable boolean can command be cached.
130      * @param duplicable boolean can command be duplicate.
131      * @param validable boolean must command be validated.
132      * @param secure boolean must command be invoked HTTPS.
133      * @param type int type of command.
134      */
135     public CommandImpl(String name, String pak, Action action, View[] views,
136             View inputView, CommandParameter[] parameters, boolean cacheable,
137             boolean duplicable, boolean validable, boolean secure, int type) {
138 
139         this.name = name;
140         this.pak = pak;
141         this.action = action;
142         this.views = new HashMap();
143         this.views.put(View.DEFAULT_VIEW_NAME, views[0]);
144         for (int i = 0; i < views.length; i++) {
145             this.views.put(views[i].getName(), views[i]);
146         }
147         this.inputView = inputView;
148         this.parameters = new LinkedList();
149         for (int i = 0; i < parameters.length; i++) {
150             this.parameters.add(parameters[i]);
151         }
152         this.cacheable = cacheable;
153         this.duplicable = duplicable;
154         this.validable = validable;
155         this.secure = secure;
156         this.type = type;
157 
158     }
159 
160     // Methods
161     /**
162      * Returns name of command.
163      *
164      * @return String name of command.
165      * @see org.talika.tarsis.command.Command#getName()
166      */
167     public String getName() {
168         return name;
169     }
170 
171     /**
172      * Returns package of command.
173      *
174      * @return String package of command.
175      * @see org.talika.tarsis.command.Command#getPackage()
176      */
177     public String getPackage() {
178         return pak;
179     }
180 
181     /**
182      * Returns full name of command.
183      *
184      * @return String full name of command.
185      * @see org.talika.tarsis.command.Command#getFullName()
186      */
187     public String getFullName() {
188         return pak + PACKAGE_SEPARATOR + name;
189     }
190 
191     /**
192      * Returns action of command.
193      *
194      * @return Action action of command.
195      * @see org.talika.tarsis.command.Command#getAction()
196      */
197     public Action getAction() {
198         return action;
199     }
200 
201     /**
202      * Sets action of command.
203      *
204      * @param action Action action instance to set.
205      */
206     protected void setAction(Action action) {
207         this.action = action;
208     }
209 
210     /**
211      * Returns the view of command with matching name.
212      *
213      * @param viewName String name of view.
214      * @return View view of command with matching name.
215      * @see org.talika.tarsis.command.Command#getView(String)
216      */
217     public View getView(String viewName) {
218         return (View) views.get(viewName);
219     }
220 
221     /**
222      * Returns the default view of this command.
223      *
224      * @return View default view of command.
225      * @see org.talika.tarsis.command.Command#getDefaultView()
226      */
227     public View getDefaultView() {
228         return (View) views.get(View.DEFAULT_VIEW_NAME);
229     }
230 
231     /**
232      * Returns the input view of command.
233      *
234      * @return View input view of command.
235      * @see org.talika.tarsis.command.Command#getInputView()
236      */
237     public View getInputView() {
238         return inputView;
239     }
240 
241     /**
242      * Adds a view to command.
243      *
244      * @param view View view to be added.
245      */
246     protected void addView(View view) {
247         this.views.put(view.getName(), view);
248     }
249 
250     /**
251      * Returns an array with the parameters of command.
252      *
253      * @return CommandParameter[] parameters of command.
254      * @see org.talika.tarsis.command.Command#getParameters()
255      */
256     public CommandParameter[] getParameters() {
257         return (CommandParameter[]) parameters.toArray(new CommandParameter[parameters.size()]);
258     }
259 
260     /**
261      * Adds a parameter to command.
262      *
263      * @param parameter CommandParameter parameter to be added.
264      */
265     protected void addParameter(CommandParameter parameter) {
266         this.parameters.add(parameter);
267     }
268 
269     /**
270      * Adds a collection of parameters to command.
271      *
272      * @param parameters Collection collection of parameters to be added.
273      */
274     protected void addParameters(Collection parameters) {
275         this.parameters.addAll(parameters);
276     }
277 
278     /**
279      * Returns <code>true</code> if the command response can be cached by browsers.
280      *
281      * @return boolean <code>true</code> if the command response can be cached by
282      * browsers.
283      * @see org.talika.tarsis.command.Command#isCacheable()
284      */
285     public boolean isCacheable() {
286         return cacheable;
287     }
288 
289     /**
290      * Returns <code>true</code> if the command can be repeated with same sync ticket.
291      *
292      * @return boolean <code>true</code> if the command can be repeated with same sync
293      * ticket.
294      * @see org.talika.tarsis.command.Command#isDuplicable()
295      */
296     public boolean isDuplicable() {
297         return duplicable;
298     }
299 
300     /**
301      * Returns <code>true</code> if command parameters must be validated.
302      *
303      * @return boolean <code>true</code> if command parameters must be validated.
304      * @see org.talika.tarsis.command.Command#isValidable()
305      */
306     public boolean isValidable() {
307         return validable;
308     }
309 
310     /**
311      * Returns <code>true</code> if command parameters must be executed in secure
312      * mode (HTTPS).
313      *
314      * @return boolean <code>true</code> if command parameters must be executed in
315      * secure mode (HTTPS).
316      * @see org.talika.tarsis.command.Command#isSecure()
317      */
318     public boolean isSecure() {
319         return secure;
320     }
321 
322     /**
323      * Returns the type of command.
324      *
325      * @return int type of command.
326      * @see org.talika.tarsis.command.Command#getType()
327      */
328     public int getType() {
329         return type;
330     }
331 
332 }