logo
header art

Building an Enterprise-Ready Web Application with Struts

June 23, 2005

Business Objects

The only business object in the system is the com.skillfusion.examples.struts.bizobjs.Recipe.

001 /*
002  * Recipe.java created on Apr 26, 2005 at 4:15:31 PM
003  *
004  * strutsExample is a simple web application that allows a user to search and
005  * view a set of recipes.
006  * Copyright (C) 2005  SkillFusion Software
007  *
008  * This program is free software; you can redistribute it and/or modify
009  * it under the terms of the GNU General Public License as published by
010  * the Free Software Foundation; either version 2 of the License, or
011  * (at your option) any later version.
012  *
013  * This program is distributed in the hope that it will be useful,
014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016  * GNU General Public License for more details.
017  *
018  * You should have received a copy of the GNU General Public License
019  * along with this program; if not, write to the Free Software
020  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
021  */
022 package com.skillfusion.examples.struts.bizobjs;
023 
024 import org.apache.commons.logging.Log;
025 import org.apache.commons.logging.LogFactory;
026 
027 /**
028  * A very simple class that represents the essential elements of a recipe.
029  *
030  @author hank
031  */
032 public class Recipe {
033     /**
034      * Logger for this class.
035      */
036     private static final Log LOGGER = LogFactory.getLog(Recipe.class);
037 
038     /**
039      * The name of this <code>Recipe</code>.
040      */
041     private String name;
042 
043     /**
044      * Miscellaneous information about this <code>Recipe</code>.
045      */
046     private String misc;
047 
048     /**
049      * Complete set of preparation instructions for this <code>Recipe</code>.
050      */
051     private String prepInstructions;
052 
053     /**
054      * Complete set of cooking instructions for this <code>Recipe</code>.
055      */
056     private String cookInstructions;
057 
058     /**
059      * Complete list of ingredients needed for this <code>Recipe</code>.
060      */
061     private String ingredients;
062 
063     /**
064      * Retrieves a reference to <code>cookInstructions</code>.
065      *
066      @return The <code>String</code> stored in <code>cookInstructions</code>.
067      */
068     public final String getCookInstructions() {
069         if (LOGGER.isTraceEnabled()) {
070             LOGGER.trace("getCookInstructions() - start");
071         }
072 
073         if (LOGGER.isTraceEnabled()) {
074             LOGGER.trace("getCookInstructions() - end - return value = "
075                     this.cookInstructions);
076         }
077         return this.cookInstructions;
078     }
079 
080     /**
081      * Sets the value of <code>cookInstructions</code>.
082      *
083      @param newCookInstructions
084      *            A <code>String</code> containing the new value for
085      *            <code>cookInstructions</code>.
086      */
087     public final void setCookInstructions(final String newCookInstructions) {
088         if (LOGGER.isTraceEnabled()) {
089             LOGGER.trace("setCookInstructions(String newCookInstructions = "
090                     + newCookInstructions + ") - start");
091         }
092 
093         this.cookInstructions = newCookInstructions;
094 
095         if (LOGGER.isTraceEnabled()) {
096             LOGGER.trace("setCookInstructions(String newCookInstructions = "
097                     + newCookInstructions + ") - end");
098         }
099     }
100 
101     /**
102      * Retrieves a reference to <code>ingredients</code>.
103      *
104      @return The <code>String</code> stored in <code>ingredients</code>.
105      */
106     public final String getIngredients() {
107         if (LOGGER.isTraceEnabled()) {
108             LOGGER.trace("getIngredients() - start");
109         }
110 
111         if (LOGGER.isTraceEnabled()) {
112             LOGGER.trace("getIngredients() - end - return value = "
113                     this.ingredients);
114         }
115         return this.ingredients;
116     }
117 
118     /**
119      * Sets the value of <code>ingredients</code>.
120      *
121      @param newIngredients
122      *            A <code>String</code> containing the new value for
123      *            <code>ingredients</code>.
124      */
125     public final void setIngredients(final String newIngredients) {
126         if (LOGGER.isTraceEnabled()) {
127             LOGGER.trace("setIngredients(String newIngredients = "
128                     + newIngredients + ") - start");
129         }
130 
131         this.ingredients = newIngredients;
132 
133         if (LOGGER.isTraceEnabled()) {
134             LOGGER.trace("setIngredients(String newIngredients = "
135                     + newIngredients + ") - end");
136         }
137     }
138 
139     /**
140      * Retrieves a reference to <code>misc</code>.
141      *
142      @return The <code>String</code> stored in <code>misc</code>.
143      */
144     public final String getMisc() {
145         if (LOGGER.isTraceEnabled()) {
146             LOGGER.trace("getMisc() - start");
147         }
148 
149         if (LOGGER.isTraceEnabled()) {
150             LOGGER.trace("getMisc() - end - return value = " this.misc);
151         }
152         return this.misc;
153     }
154 
155     /**
156      * Sets the value of <code>misc</code>.
157      *
158      @param newMisc
159      *            A <code>String</code> containing the new value for
160      *            <code>misc</code>.
161      */
162     public final void setMisc(final String newMisc) {
163         if (LOGGER.isTraceEnabled()) {
164             LOGGER.trace("setMisc(String newMisc = " + newMisc + ") - start");
165         }
166 
167         this.misc = newMisc;
168 
169         if (LOGGER.isTraceEnabled()) {
170             LOGGER.trace("setMisc(String newMisc = " + newMisc + ") - end");
171         }
172     }
173 
174     /**
175      * Retrieves a reference to <code>name</code>.
176      *
177      @return The <code>String</code> stored in <code>name</code>.
178      */
179     public final String getName() {
180         if (LOGGER.isTraceEnabled()) {
181             LOGGER.trace("getName() - start");
182         }
183 
184         if (LOGGER.isTraceEnabled()) {
185             LOGGER.trace("getName() - end - return value = " this.name);
186         }
187         return this.name;
188     }
189 
190     /**
191      * Sets the value of <code>name</code>.
192      *
193      @param newName
194      *            A <code>String</code> containing the new value for
195      *            <code>name</code>.
196      */
197     public final void setName(final String newName) {
198         if (LOGGER.isTraceEnabled()) {
199             LOGGER.trace("setName(String newName = " + newName + ") - start");
200         }
201 
202         this.name = newName;
203 
204         if (LOGGER.isTraceEnabled()) {
205             LOGGER.trace("setName(String newName = " + newName + ") - end");
206         }
207     }
208 
209     /**
210      * Retrieves a reference to <code>prepInstructions</code>.
211      *
212      @return The <code>String</code> stored in <code>prepInstructions</code>.
213      */
214     public final String getPrepInstructions() {
215         if (LOGGER.isTraceEnabled()) {
216             LOGGER.trace("getPrepInstructions() - start");
217         }
218 
219         if (LOGGER.isTraceEnabled()) {
220             LOGGER.trace("getPrepInstructions() - end - return value = "
221                     this.prepInstructions);
222         }
223         return this.prepInstructions;
224     }
225 
226     /**
227      * Sets the value of <code>prepInstructions</code>.
228      *
229      @param newPrepInstructions
230      *            A <code>String</code> containing the new value for
231      *            <code>prepInstructions</code>.
232      */
233     public final void setPrepInstructions(final String newPrepInstructions) {
234         if (LOGGER.isTraceEnabled()) {
235             LOGGER.trace("setPrepInstructions(String newPrepInstructions = "
236                     + newPrepInstructions + ") - start");
237         }
238 
239         this.prepInstructions = newPrepInstructions;
240 
241         if (LOGGER.isTraceEnabled()) {
242             LOGGER.trace("setPrepInstructions(String newPrepInstructions = "
243                     + newPrepInstructions + ") - end");
244         }
245     }
246 
247     /**
248      * Compares this <code>Recipe</code> to <tt>toCompare</tt> to determine
249      * if they are the same. It determines this by doing a case-insensitive
250      <code>String</code> comparison of each property on <code>Recipe</code>.
251      *
252      @param toCompare
253      *            the <code>Recipe</code> to be compared to this
254      *            <code>Recipe</code>.
255      @return <code>true</code> if all the properties on <tt>toCompare</tt>
256      *         are the same as, or differ only in case from, the corresponding
257      *         properties on this <code>Recipe</code>.
258      */
259     public final boolean isSameAs(final Recipe toCompare) {
260         if (LOGGER.isTraceEnabled()) {
261             LOGGER.trace("isSameAs(Recipe toCompare = " + toCompare
262                     ") - start");
263         }
264 
265         /* Perform simple tests first to return as quickly as possible */
266         if (null == toCompare) {
267             if (LOGGER.isTraceEnabled()) {
268                 LOGGER.trace("isSameAs(Recipe toCompare = " + toCompare
269                         ") - end - return value = " false);
270             }
271             return false// Its null, so it can't equal this one
272         }
273         if (this.equals(toCompare)) {
274             if (LOGGER.isTraceEnabled()) {
275                 LOGGER.trace("isSameAs(Recipe toCompare = " + toCompare
276                         ") - end - return value = " true);
277             }
278             return true// Its the same object, so return true
279         }
280 
281         /*
282          * Run a series of test to determine if they are the same, but return
283          * false as soon the first test fails
284          */
285         // Compare the name properties of the recipes
286         if (null == toCompare.getName()) {
287             if (null != this.getName()) {
288                 if (LOGGER.isTraceEnabled()) {
289                     LOGGER.trace("isSameAs(Recipe toCompare = " + toCompare
290                             ") - end - return value = " false);
291                 }
292                 return false;
293             }
294         else if (!toCompare.getName().equalsIgnoreCase(this.getName())) {
295             if (LOGGER.isTraceEnabled()) {
296                 LOGGER.trace("isSameAs(Recipe toCompare = " + toCompare
297                         ") - end - return value = " false);
298             }
299             return false;
300         }
301 
302         // Compare the misc properties of the recipes
303         if (null == toCompare.getMisc()) {
304             if (null != this.getMisc()) {
305                 if (LOGGER.isTraceEnabled()) {
306                     LOGGER.trace("isSameAs(Recipe toCompare = " + toCompare
307                             ") - end - return value = " false);
308                 }
309                 return false;
310             }
311         else if (!toCompare.getMisc().equalsIgnoreCase(this.getMisc())) {
312             if (LOGGER.isTraceEnabled()) {
313                 LOGGER.trace("isSameAs(Recipe toCompare = " + toCompare
314                         ") - end - return value = " false);
315             }
316             return false;
317         }
318 
319         // Compare the prepInstructions properties of the recipes
320         if (null == toCompare.getPrepInstructions()) {
321             if (null != this.getPrepInstructions()) {
322                 if (LOGGER.isTraceEnabled()) {
323                     LOGGER.trace("isSameAs(Recipe toCompare = " + toCompare
324                             ") - end - return value = " false);
325                 }
326                 return false;
327             }
328         else if (!toCompare.getPrepInstructions().equalsIgnoreCase(
329                 this.getPrepInstructions())) {
330             if (LOGGER.isTraceEnabled()) {
331                 LOGGER.trace("isSameAs(Recipe toCompare = " + toCompare
332                         ") - end - return value = " false);
333             }
334             return false;
335         }
336 
337         // Compare the cookInstructions properties of the recipes
338         if (null == toCompare.getCookInstructions()) {
339             if (null != this.getCookInstructions()) {
340                 if (LOGGER.isTraceEnabled()) {
341                     LOGGER.trace("isSameAs(Recipe toCompare = " + toCompare
342                             ") - end - return value = " false);
343                 }
344                 return false;
345             }
346         else if (!toCompare.getCookInstructions().equalsIgnoreCase(
347                 this.getCookInstructions())) {
348             if (LOGGER.isTraceEnabled()) {
349                 LOGGER.trace("isSameAs(Recipe toCompare = " + toCompare
350                         ") - end - return value = " false);
351             }
352             return false;
353         }
354 
355         // Compare the ingredients properties of the recipes
356         if (null == toCompare.getIngredients()) {
357             if (null != this.getIngredients()) {
358                 if (LOGGER.isTraceEnabled()) {
359                     LOGGER.trace("isSameAs(Recipe toCompare = " + toCompare
360                             ") - end - return value = " false);
361                 }
362                 return false;
363             }
364         else if (!toCompare.getIngredients().equalsIgnoreCase(
365                 this.getIngredients())) {
366             if (LOGGER.isTraceEnabled()) {
367                 LOGGER.trace("isSameAs(Recipe toCompare = " + toCompare
368                         ") - end - return value = " false);
369             }
370             return false;
371         }
372 
373         // If we fell all the way through these tests, finally return true.
374         if (LOGGER.isTraceEnabled()) {
375             LOGGER.trace("isSameAs(Recipe toCompare = " + toCompare
376                     ") - end - return value = " true);
377         }
378         return true;
379     }
380 }

It is a simple model of a real-world recipe containing properties for its name, ingredient list, preparation instructions, cooking instructions, and miscellaneous information. Beyond the getters and setters required by any bean, the only logic on Recipe is a means of comparing one Recipe to another to see if the two are functionally equivalent, a method called isSameAs(). There's nothing Struts-specific about this class, so we'll spend no more time on it.