KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > webapp > SongBean


1 // $Id: SongBean.java,v 1.1 2002/07/18 09:08:03 per_nyfelt Exp $
2

3 package webapp;
4
5 import org.apache.log4j.Category;
6
7 import javax.servlet.http.*;
8
9 import song.Song;
10 import song.SongServices;
11
12 /**
13  * An Application object for the (persistent) song.Song data object.
14  * A song.Song is someone who is active in the organization.
15  * This Bean is does nifty web-oriented stuff like validating fields
16  * and making sure everything is kosher before creating a persistent
17  * song.
18  *
19  * @version $Revision: 1.1 $ $Date: 2002/07/18 09:08:03 $
20  * @author James Stiefel
21  */

22
23 public class SongBean {
24
25     //local stuff to deal with life
26
Song m_real_song = null;
27     String JavaDoc m_message = new String JavaDoc();
28
29     String JavaDoc m_submit_action = null;
30
31     String JavaDoc m_handle = "-2";
32
33     //real fields
34
String JavaDoc m_title = new String JavaDoc();
35     String JavaDoc m_author = new String JavaDoc();
36     String JavaDoc m_copyright = new String JavaDoc();
37     String JavaDoc m_publisher = new String JavaDoc();
38
39     private static Category logger = Category.getInstance(SongBean.class.getName());
40
41
42     /**
43      * Sets the WebApp object. There should be a single WebApp per context.
44      * The JSP should set it in here before it expects this guy to do any
45      * important work, like talk to the database.
46      *
47      */

48
49     public void setSubmit(String JavaDoc sub) {
50         m_submit_action = sub;
51     }
52
53     public String JavaDoc getSubmit() {
54         return m_submit_action;
55     }
56
57     /**
58      * Initialize this object (application data values only)
59      *
60      */

61
62     public void initThis(){
63         m_real_song = null;
64
65         m_handle = "-2";
66
67         //real fields
68
m_title = new String JavaDoc();
69         m_author = new String JavaDoc();
70         m_copyright = new String JavaDoc();
71         m_publisher = new String JavaDoc();
72     }
73
74     public boolean processAddRequest(HttpServletRequest request) {
75
76         logger.debug("processAddRequest(): action =" + m_submit_action);
77
78         boolean created = createIfValid();
79
80         if (created){
81             return true;
82         } else {
83             return false;
84         }
85     }
86
87     public boolean processUpdateRequest(HttpServletRequest request) {
88
89         logger.debug("processUpdateRequest(): action =" + m_submit_action);
90
91         /* Note: this is a simple sample application. It does not properly
92         update the song index if you update the title of the song. The persistent
93         song.Song's title will be updated, but it will still be indexed by the old title;
94         lookups by (the new) title will fail. */

95
96         if (isValidForUpdate()) {
97             try {
98                 thisToReal();
99                 return true;
100             } catch (Exception JavaDoc e) {
101                 logger.error("processUpdateRequest() failed to update");
102             }
103         }
104         return false;
105     }
106
107
108     public Song songForTitle(String JavaDoc title) {
109
110         if( title == null || title.length() == 0){
111              logger.info("Bean songForTitle: NULL name.");
112              m_real_song = null;
113              initThis();
114              return null;
115         }
116
117        m_real_song = SongServices.songForTitle(title);
118        if (m_real_song == null){
119            logger.warn("songForTitle: NOT retrieved.");
120            initThis();
121        }else {
122            realToThis();
123        }
124
125         return m_real_song;
126     }
127
128     public Song songForHandle(String JavaDoc handle) {
129
130         if( handle == null || handle.length() == 0){
131             logger.error("Bean songForHandle: NULL handle.");
132             m_real_song = null;
133             initThis();
134             return null;
135         }
136
137        m_real_song = SongServices.songForHandle(handle);
138        if (m_real_song == null){
139            logger.warn("songForHandle: NOT retrieved.");
140            initThis();
141        }else {
142            realToThis();
143        }
144
145         return m_real_song;
146     }
147
148     /**
149      * If the data is deemed valid, then a new persistent song.Song object
150      * is created, and its values set according to the data.
151      *
152      * @return true if successful
153      */

154     public boolean createIfValid(){
155         logger.debug("Bean creation: entering creation function.");
156
157         if (this.isValidForAdd()){
158             logger.debug("Bean creation: valid song.");
159             try {
160
161                 m_real_song = SongServices.createSong(m_title);
162
163                 if (m_real_song == null){
164                     logger.warn("Bean creation: song creation failed.");
165                     return false;
166                 } else {
167                     logger.debug("Bean creation: song created successfully.");
168                 }
169             } catch (Exception JavaDoc e){
170                 logger.error("Bean creation: Ozone create Object failed. song.Song NOT created.", e);
171                 return false;
172             }
173             thisToReal();
174
175             return true;
176         }
177         return false;
178     }
179
180     /**
181      * Moves data values from this transient store to the associated
182      * persisteant Memeber
183      *
184      */

185     private void thisToReal(){
186         m_real_song.setTitle (this.getTitle());
187         m_real_song.setAuthor (this.getAuthor());
188         m_real_song.setCopyright (this.getCopyright());
189         m_real_song.setPublisher (this.getPublisher());
190     }
191
192
193     /**
194      * Moves data values from the associated persistent song
195      * to this temporary song.
196      *
197      */

198     private void realToThis(){
199
200         this.setTitle (m_real_song.getTitle());
201         this.setAuthor (m_real_song.getAuthor());
202         this.setCopyright (m_real_song.getCopyright());
203         this.setPublisher (m_real_song.getPublisher());
204     }
205
206
207     /**
208      * Retrieves the object's unique (database) Handle
209      *
210      * @return the object handle
211      */

212     public String JavaDoc handle(){
213
214         if(m_real_song != null){
215             return m_real_song.handle();
216         }
217
218         //or local string copy...hopefully a valid handle
219
return m_handle;
220     }
221     /**
222      * Retrieves the object's unique (database) objectID
223      * This is primarly to satisfy the interface OzoneRemote.
224      * We don't really use it, but we want to implement the same interface to keep us honest.
225      *
226      * @return the object handle
227      */

228
229     /**
230      * Sets Title. Username is the name used to sign on to the site.
231      * Username may differ from the song.Song's actual name, and must be
232      * unique systemwide.
233      */

234     public void setTitle (String JavaDoc title){
235         m_title = title.trim();
236     }
237
238     /**
239      * Retrieves Title, a song.Song's unique name.
240      */

241     public String JavaDoc getTitle (){
242         return m_title;
243     }
244
245     /**
246      * Sets Author.
247      *
248      */

249     public void setAuthor (String JavaDoc author){
250        m_author = author.trim();
251     }
252
253     /**
254      * Retrieves Author
255      */

256     public String JavaDoc getAuthor (){
257         return m_author;
258     }
259
260     /**
261      * Sets Copyright
262      */

263     public void setCopyright (String JavaDoc copyright){
264        m_copyright = copyright.trim();
265     }
266
267     /**
268      * Retrieves Copyright
269      */

270     public String JavaDoc getCopyright (){
271         return m_copyright;
272     }
273
274     /**
275      * Sets Publisher
276      */

277     public void setPublisher (String JavaDoc publisher){
278         m_publisher = publisher.trim();
279     }
280
281     /**
282      * Retrieves first name
283      */

284     public String JavaDoc getPublisher (){
285         return m_publisher;
286     }
287
288
289     //Validation routines
290
/**
291      * Tests fields to see if they are filled in.
292      * Validations includes title uniqueness test against dB
293      */

294     public boolean isValidForAdd(){
295         m_message = new String JavaDoc();
296
297         boolean success = true;
298
299         if (SongServices.getAllSongs().findSong(m_title) != null){
300             m_message += "song.Song title already exists in database.<br/>";
301             success = false;
302         }
303         if (! validateTitle())
304             success = false;
305         if (!validateAuthor())
306             success = false;
307         if (!validateCopyright())
308             success = false;
309         if (!validatePublisher())
310             success = false;
311
312         return success;
313
314     }
315
316     /**
317      * Tests fields to see if they are filled in.
318      * No username validation is performed since we won't be updating it.
319      *
320      */

321     public boolean isValidForUpdate(){
322         m_message = new String JavaDoc();
323
324         boolean success = true;
325
326         if ( m_title.length() == 0 ){
327             m_message += "Title cannot be blank.<br/>";
328             success = false;
329         }
330         //if renaming, make sure new name is not taken
331
if ( !m_title.toUpperCase().equals(m_real_song.getTitle().toUpperCase()) ){
332             if (SongServices.getAllSongs().findSong(m_title) != null){
333                 m_message += "New title already exists in database.<br/>";
334                 success = false;
335             }
336         }
337
338         if (!validateAuthor())
339             success = false;
340         if (!validateCopyright())
341             success = false;
342         if (!validatePublisher())
343             success = false;
344
345         return success;
346
347     }
348
349
350     /**
351      * Gets any error messages generated by the validation.
352      */

353     public String JavaDoc getMessage() {
354         return m_message;
355     }
356
357     public boolean validateTitle() {
358         if (m_title == null){
359             m_message += "Title null.<br/>";
360             return false;
361         } else if (m_title.length() == 0){
362             m_message += "Title empty.<br/>";
363             return false;
364         }
365
366         //all good
367
return true;
368     }
369
370     public boolean validateCopyright(){
371         if (m_copyright == null){
372             m_message += "Copyright null.<br/>";
373             return false;
374         } else if (m_copyright.length() == 0){
375             //song don't have to be copyrighted
376
}
377
378         //all good
379
return true;
380     }
381     public boolean validatePublisher() {
382         if (m_publisher == null){
383             m_message += "Publisher null.<br/>";
384             return false;
385         } else if (m_publisher.length() == 0){
386             //songs don't have to be published
387
}
388
389         //all good
390
return true;
391     }
392     public boolean validateAuthor() {
393         if (m_author == null){
394             m_message += "Author null.<br/>";
395             return false;
396         } else if (m_author.length() == 0){
397             //don't have to know the author
398
}
399
400         //all good
401
return true;
402     }
403
404 }
405
Popular Tags