001: // $Id: SongBean.java,v 1.1 2002/07/18 09:08:03 per_nyfelt Exp $
002:
003: package webapp;
004:
005: import Song;
006: import SongCollection;
007: import SongServices;
008: import webapp.WebApp;
009:
010: import org.apache.log4j.Category;
011:
012: import javax.servlet.http.*;
013:
014: import org.ozoneDB.*;
015: import org.ozoneDB.core.ObjectID;
016:
017: /**
018: * An Application object for the (persistent) Song data object.
019: * A Song is someone who is active in the organization.
020: * This Bean is does nifty web-oriented stuff like validating fields
021: * and making sure everything is kosher before creating a persistent
022: * song.
023: *
024: * @version $Revision: 1.1 $ $Date: 2002/07/18 09:08:03 $
025: * @author James Stiefel
026: */
027:
028: public class SongBean {
029:
030: //local stuff to deal with life
031: Song m_real_song = null;
032: String m_message = new String();
033:
034: String m_submit_action = null;
035:
036: String m_handle = "-2";
037:
038: //real fields
039: String m_title = new String();
040: String m_author = new String();
041: String m_copyright = new String();
042: String m_publisher = new String();
043:
044: private static Category logger = Category
045: .getInstance(SongBean.class.getName());
046:
047: /**
048: * Sets the WebApp object. There should be a single WebApp per context.
049: * The JSP should set it in here before it expects this guy to do any
050: * important work, like talk to the database.
051: *
052: */
053:
054: public void setSubmit(String sub) {
055: m_submit_action = sub;
056: }
057:
058: public String getSubmit() {
059: return m_submit_action;
060: }
061:
062: /**
063: * Initialize this object (application data values only)
064: *
065: */
066:
067: public void initThis() {
068: m_real_song = null;
069:
070: m_handle = "-2";
071:
072: //real fields
073: m_title = new String();
074: m_author = new String();
075: m_copyright = new String();
076: m_publisher = new String();
077: }
078:
079: public boolean processAddRequest(HttpServletRequest request) {
080:
081: logger.debug("processAddRequest(): action =" + m_submit_action);
082:
083: boolean created = createIfValid();
084:
085: if (created) {
086: return true;
087: } else {
088: return false;
089: }
090: }
091:
092: public boolean processUpdateRequest(HttpServletRequest request) {
093:
094: logger.debug("processUpdateRequest(): action ="
095: + m_submit_action);
096:
097: /* Note: this is a simple sample application. It does not properly
098: update the song index if you update the title of the song. The persistent
099: Song's title will be updated, but it will still be indexed by the old title;
100: lookups by (the new) title will fail. */
101:
102: if (isValidForUpdate()) {
103: try {
104: this ToReal();
105: return true;
106: } catch (Exception e) {
107: logger.error("processUpdateRequest() failed to update");
108: }
109: }
110: return false;
111: }
112:
113: public Song songForTitle(String title) {
114:
115: if (title == null || title.length() == 0) {
116: logger.info("Bean songForTitle: NULL name.");
117: m_real_song = null;
118: initThis();
119: return null;
120: }
121:
122: m_real_song = SongServices.songForTitle(title);
123: if (m_real_song == null) {
124: logger.warn("songForTitle: NOT retrieved.");
125: initThis();
126: } else {
127: realToThis();
128: }
129:
130: return m_real_song;
131: }
132:
133: public Song songForHandle(String handle) {
134:
135: if (handle == null || handle.length() == 0) {
136: logger.error("Bean songForHandle: NULL handle.");
137: m_real_song = null;
138: initThis();
139: return null;
140: }
141:
142: m_real_song = SongServices.songForHandle(handle);
143: if (m_real_song == null) {
144: logger.warn("songForHandle: NOT retrieved.");
145: initThis();
146: } else {
147: realToThis();
148: }
149:
150: return m_real_song;
151: }
152:
153: /**
154: * If the data is deemed valid, then a new persistent Song object
155: * is created, and its values set according to the data.
156: *
157: * @return true if successful
158: */
159: public boolean createIfValid() {
160: logger.debug("Bean creation: entering creation function.");
161:
162: if (this .isValidForAdd()) {
163: logger.debug("Bean creation: valid song.");
164: try {
165:
166: m_real_song = SongServices.createSong(m_title);
167:
168: if (m_real_song == null) {
169: logger
170: .warn("Bean creation: song creation failed.");
171: return false;
172: } else {
173: logger
174: .debug("Bean creation: song created successfully.");
175: }
176: } catch (Exception e) {
177: logger
178: .error(
179: "Bean creation: Ozone create Object failed. Song NOT created.",
180: e);
181: return false;
182: }
183: this ToReal();
184:
185: return true;
186: }
187: return false;
188: }
189:
190: /**
191: * Moves data values from this transient store to the associated
192: * persisteant Memeber
193: *
194: */
195: private void this ToReal() {
196: m_real_song.setTitle(this .getTitle());
197: m_real_song.setAuthor(this .getAuthor());
198: m_real_song.setCopyright(this .getCopyright());
199: m_real_song.setPublisher(this .getPublisher());
200: }
201:
202: /**
203: * Moves data values from the associated persistent song
204: * to this temporary song.
205: *
206: */
207: private void realToThis() {
208:
209: this .setTitle(m_real_song.getTitle());
210: this .setAuthor(m_real_song.getAuthor());
211: this .setCopyright(m_real_song.getCopyright());
212: this .setPublisher(m_real_song.getPublisher());
213: }
214:
215: /**
216: * Retrieves the object's unique (database) Handle
217: *
218: * @return the object handle
219: */
220: public String handle() {
221:
222: if (m_real_song != null) {
223: return m_real_song.handle();
224: }
225:
226: //or local string copy...hopefully a valid handle
227: return m_handle;
228: }
229:
230: /**
231: * Retrieves the object's unique (database) objectID
232: * This is primarly to satisfy the interface OzoneRemote.
233: * We don't really use it, but we want to implement the same interface to keep us honest.
234: *
235: * @return the object handle
236: */
237:
238: /**
239: * Sets Title. Username is the name used to sign on to the site.
240: * Username may differ from the Song's actual name, and must be
241: * unique systemwide.
242: */
243: public void setTitle(String title) {
244: m_title = title.trim();
245: }
246:
247: /**
248: * Retrieves Title, a Song's unique name.
249: */
250: public String getTitle() {
251: return m_title;
252: }
253:
254: /**
255: * Sets Author.
256: *
257: */
258: public void setAuthor(String author) {
259: m_author = author.trim();
260: }
261:
262: /**
263: * Retrieves Author
264: */
265: public String getAuthor() {
266: return m_author;
267: }
268:
269: /**
270: * Sets Copyright
271: */
272: public void setCopyright(String copyright) {
273: m_copyright = copyright.trim();
274: }
275:
276: /**
277: * Retrieves Copyright
278: */
279: public String getCopyright() {
280: return m_copyright;
281: }
282:
283: /**
284: * Sets Publisher
285: */
286: public void setPublisher(String publisher) {
287: m_publisher = publisher.trim();
288: }
289:
290: /**
291: * Retrieves first name
292: */
293: public String getPublisher() {
294: return m_publisher;
295: }
296:
297: //Validation routines
298: /**
299: * Tests fields to see if they are filled in.
300: * Validations includes title uniqueness test against dB
301: */
302: public boolean isValidForAdd() {
303: m_message = new String();
304:
305: boolean success = true;
306:
307: if (SongServices.getAllSongs().findSong(m_title) != null) {
308: m_message += "Song title already exists in database.<br/>";
309: success = false;
310: }
311: if (!validateTitle())
312: success = false;
313: if (!validateAuthor())
314: success = false;
315: if (!validateCopyright())
316: success = false;
317: if (!validatePublisher())
318: success = false;
319:
320: return success;
321:
322: }
323:
324: /**
325: * Tests fields to see if they are filled in.
326: * No username validation is performed since we won't be updating it.
327: *
328: */
329: public boolean isValidForUpdate() {
330: m_message = new String();
331:
332: boolean success = true;
333:
334: if (m_title.length() == 0) {
335: m_message += "Title cannot be blank.<br/>";
336: success = false;
337: }
338: //if renaming, make sure new name is not taken
339: if (!m_title.toUpperCase().equals(
340: m_real_song.getTitle().toUpperCase())) {
341: if (SongServices.getAllSongs().findSong(m_title) != null) {
342: m_message += "New title already exists in database.<br/>";
343: success = false;
344: }
345: }
346:
347: if (!validateAuthor())
348: success = false;
349: if (!validateCopyright())
350: success = false;
351: if (!validatePublisher())
352: success = false;
353:
354: return success;
355:
356: }
357:
358: /**
359: * Gets any error messages generated by the validation.
360: */
361: public String getMessage() {
362: return m_message;
363: }
364:
365: public boolean validateTitle() {
366: if (m_title == null) {
367: m_message += "Title null.<br/>";
368: return false;
369: } else if (m_title.length() == 0) {
370: m_message += "Title empty.<br/>";
371: return false;
372: }
373:
374: //all good
375: return true;
376: }
377:
378: public boolean validateCopyright() {
379: if (m_copyright == null) {
380: m_message += "Copyright null.<br/>";
381: return false;
382: } else if (m_copyright.length() == 0) {
383: //song don't have to be copyrighted
384: }
385:
386: //all good
387: return true;
388: }
389:
390: public boolean validatePublisher() {
391: if (m_publisher == null) {
392: m_message += "Publisher null.<br/>";
393: return false;
394: } else if (m_publisher.length() == 0) {
395: //songs don't have to be published
396: }
397:
398: //all good
399: return true;
400: }
401:
402: public boolean validateAuthor() {
403: if (m_author == null) {
404: m_message += "Author null.<br/>";
405: return false;
406: } else if (m_author.length() == 0) {
407: //don't have to know the author
408: }
409:
410: //all good
411: return true;
412: }
413:
414: }
|