View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.model;
21  
22  public abstract class AbstractCrud implements Crud {
23  
24    private String defaultOutcome;
25  
26    private boolean showDetail;
27  
28    private boolean itemEditable;
29  
30    protected AbstractCrud() {
31      reset();
32    }
33  
34    /**
35     * @see Crud#getShowDetail()
36     */
37    public final boolean getShowDetail() {
38      return showDetail;
39    }
40  
41    /**
42     * @see Crud#isItemEditable()
43     */
44    public final boolean isItemEditable() {
45      return itemEditable;
46    }
47  
48    /**
49     * @see Crud#deleteItem()
50     */
51    public final String deleteItem() {
52      doDeleteItem();
53      return getDefaultOutcome();
54    }
55  
56    /**
57     * <p>
58     * Hook for the implementation of business logic, after invoking the action
59     * {@link AbstractCrud#deleteItem()}. If the execution of the
60     * business logic completed successfully, the method has to return <i>true</i>.
61     * Otherwise the method has to return <i>false</i>.
62     * </p>
63     * 
64     * @return true if the method completed sucessfully, false if not
65     */
66    protected abstract boolean doDeleteItem();
67  
68    /**
69     * @see Crud#showItem()
70     */
71    public final String showItem() {
72      itemEditable = false;
73      showDetail = doShowItem();
74      return getDefaultOutcome();
75    }
76  
77    /**
78     * @see Crud#editItem()
79     */
80    public final String editItem() {
81      itemEditable = true;
82      showDetail = doShowItem();
83      return getDefaultOutcome();
84    }
85  
86    /**
87     * <p>
88     * Hook for the implementation of business logic, after invoking the action
89     * {@link AbstractCrud#showItem()}. If the execution of the
90     * business logic completed successfully, the method has to return <i>true</i>.
91     * Otherwise the method has to return <i>false</i>.
92     * </p>
93     * 
94     * @return true if the method completed sucessfully, false if not
95     */
96    protected abstract boolean doShowItem();
97  
98    /**
99     * @see Crud#createItem()
100    */
101   public final String createItem() {
102     itemEditable = true;
103     showDetail = doCreateItem();
104     return getDefaultOutcome();
105   }
106 
107   /**
108    * <p>
109    * Hook for the implementation of business logic, after invoking the action
110    * {@link AbstractCrud#createItem()}. If the execution of the
111    * business logic completed successfully, the method has to return <i>true</i>.
112    * Otherwise the method has to return <i>false</i>.
113    * </p>
114    * 
115    * @return true if the method completed sucessfully, false if not
116    */
117   protected abstract boolean doCreateItem();
118 
119   /**
120    * @see Crud#saveItem()
121    */
122   public final String saveItem() {
123     showDetail = !doSaveItem();
124     return getDefaultOutcome();
125   }
126 
127   /**
128    * <p>
129    * Hook for the implementation of business logic, after invoking the action
130    * {@link AbstractCrud#saveItem()}. If the execution of the
131    * business logic completed successfully, the method has to return <i>true</i>.
132    * Otherwise the method has to return <i>false</i>.
133    * </p>
134    * 
135    * @return true if the method completed sucessfully, false if not
136    */
137   protected abstract boolean doSaveItem();
138 
139   /**
140    * @see Crud#cancelItem()
141    */
142   public final String cancelItem() {
143     // doCancelItem();
144     showDetail = false;
145     return getDefaultOutcome();
146   }
147 
148   //
149   // /**
150   // * <p>
151   // * Hook for the implementation of business logic, after invoking the action
152   // {@link AbstractCrud#cancelItem()}.
153   // * </p>
154   // */
155   // public abstract void doCancelItem();
156 
157   /**
158    * @return The outcome for the actions of the crud component
159    */
160   protected final String getDefaultOutcome() {
161     return this.defaultOutcome;
162   }
163 
164   /**
165    * Set the outcome for all actions of the crud component.
166    * 
167    * @param defaultOutcome
168    *          The outcome for all actions of the crud component
169    */
170   public final void setDefaultOutcome(final String defaultOutcome) {
171     this.defaultOutcome = defaultOutcome;
172   }
173 
174   /**
175    * Helper mehtod to reset the controllers attributes.
176    */
177   protected void reset() {
178     showDetail = false;
179     itemEditable = false;
180   }
181 
182 }