📄 songbean.java
字号:
// $Id: SongBean.java,v 1.1 2002/07/18 09:08:03 per_nyfelt Exp $package webapp;import org.apache.log4j.Category;import javax.servlet.http.*;import song.Song;import song.SongServices;/** * An Application object for the (persistent) song.Song data object. * A song.Song is someone who is active in the organization. * This Bean is does nifty web-oriented stuff like validating fields * and making sure everything is kosher before creating a persistent * song. * * @version $Revision: 1.1 $ $Date: 2002/07/18 09:08:03 $ * @author James Stiefel */public class SongBean { //local stuff to deal with life Song m_real_song = null; String m_message = new String(); String m_submit_action = null; String m_handle = "-2"; //real fields String m_title = new String(); String m_author = new String(); String m_copyright = new String(); String m_publisher = new String(); private static Category logger = Category.getInstance(SongBean.class.getName()); /** * Sets the WebApp object. There should be a single WebApp per context. * The JSP should set it in here before it expects this guy to do any * important work, like talk to the database. * */ public void setSubmit(String sub) { m_submit_action = sub; } public String getSubmit() { return m_submit_action; } /** * Initialize this object (application data values only) * */ public void initThis(){ m_real_song = null; m_handle = "-2"; //real fields m_title = new String(); m_author = new String(); m_copyright = new String(); m_publisher = new String(); } public boolean processAddRequest(HttpServletRequest request) { logger.debug("processAddRequest(): action =" + m_submit_action); boolean created = createIfValid(); if (created){ return true; } else { return false; } } public boolean processUpdateRequest(HttpServletRequest request) { logger.debug("processUpdateRequest(): action =" + m_submit_action); /* Note: this is a simple sample application. It does not properly update the song index if you update the title of the song. The persistent song.Song's title will be updated, but it will still be indexed by the old title; lookups by (the new) title will fail. */ if (isValidForUpdate()) { try { thisToReal(); return true; } catch (Exception e) { logger.error("processUpdateRequest() failed to update"); } } return false; } public Song songForTitle(String title) { if( title == null || title.length() == 0){ logger.info("Bean songForTitle: NULL name."); m_real_song = null; initThis(); return null; } m_real_song = SongServices.songForTitle(title); if (m_real_song == null){ logger.warn("songForTitle: NOT retrieved."); initThis(); }else { realToThis(); } return m_real_song; } public Song songForHandle(String handle) { if( handle == null || handle.length() == 0){ logger.error("Bean songForHandle: NULL handle."); m_real_song = null; initThis(); return null; } m_real_song = SongServices.songForHandle(handle); if (m_real_song == null){ logger.warn("songForHandle: NOT retrieved."); initThis(); }else { realToThis(); } return m_real_song; } /** * If the data is deemed valid, then a new persistent song.Song object * is created, and its values set according to the data. * * @return true if successful */ public boolean createIfValid(){ logger.debug("Bean creation: entering creation function."); if (this.isValidForAdd()){ logger.debug("Bean creation: valid song."); try { m_real_song = SongServices.createSong(m_title); if (m_real_song == null){ logger.warn("Bean creation: song creation failed."); return false; } else { logger.debug("Bean creation: song created successfully."); } } catch (Exception e){ logger.error("Bean creation: Ozone create Object failed. song.Song NOT created.", e); return false; } thisToReal(); return true; } return false; } /** * Moves data values from this transient store to the associated * persisteant Memeber * */ private void thisToReal(){ m_real_song.setTitle (this.getTitle()); m_real_song.setAuthor (this.getAuthor()); m_real_song.setCopyright (this.getCopyright()); m_real_song.setPublisher (this.getPublisher()); } /** * Moves data values from the associated persistent song * to this temporary song. * */ private void realToThis(){ this.setTitle (m_real_song.getTitle()); this.setAuthor (m_real_song.getAuthor()); this.setCopyright (m_real_song.getCopyright()); this.setPublisher (m_real_song.getPublisher()); } /** * Retrieves the object's unique (database) Handle * * @return the object handle */ public String handle(){ if(m_real_song != null){ return m_real_song.handle(); } //or local string copy...hopefully a valid handle return m_handle; } /** * Retrieves the object's unique (database) objectID * This is primarly to satisfy the interface OzoneRemote. * We don't really use it, but we want to implement the same interface to keep us honest. * * @return the object handle */ /** * Sets Title. Username is the name used to sign on to the site. * Username may differ from the song.Song's actual name, and must be * unique systemwide. */ public void setTitle (String title){ m_title = title.trim(); } /** * Retrieves Title, a song.Song's unique name. */ public String getTitle (){ return m_title; } /** * Sets Author. * */ public void setAuthor (String author){ m_author = author.trim(); } /** * Retrieves Author */ public String getAuthor (){ return m_author; } /** * Sets Copyright */ public void setCopyright (String copyright){ m_copyright = copyright.trim(); } /** * Retrieves Copyright */ public String getCopyright (){ return m_copyright; } /** * Sets Publisher */ public void setPublisher (String publisher){ m_publisher = publisher.trim(); } /** * Retrieves first name */ public String getPublisher (){ return m_publisher; } //Validation routines /** * Tests fields to see if they are filled in. * Validations includes title uniqueness test against dB */ public boolean isValidForAdd(){ m_message = new String(); boolean success = true; if (SongServices.getAllSongs().findSong(m_title) != null){ m_message += "song.Song title already exists in database.<br/>"; success = false; } if (! validateTitle()) success = false; if (!validateAuthor()) success = false; if (!validateCopyright()) success = false; if (!validatePublisher()) success = false; return success; } /** * Tests fields to see if they are filled in. * No username validation is performed since we won't be updating it. * */ public boolean isValidForUpdate(){ m_message = new String(); boolean success = true; if ( m_title.length() == 0 ){ m_message += "Title cannot be blank.<br/>"; success = false; } //if renaming, make sure new name is not taken if ( !m_title.toUpperCase().equals(m_real_song.getTitle().toUpperCase()) ){ if (SongServices.getAllSongs().findSong(m_title) != null){ m_message += "New title already exists in database.<br/>"; success = false; } } if (!validateAuthor()) success = false; if (!validateCopyright()) success = false; if (!validatePublisher()) success = false; return success; } /** * Gets any error messages generated by the validation. */ public String getMessage() { return m_message; } public boolean validateTitle() { if (m_title == null){ m_message += "Title null.<br/>"; return false; } else if (m_title.length() == 0){ m_message += "Title empty.<br/>"; return false; } //all good return true; } public boolean validateCopyright(){ if (m_copyright == null){ m_message += "Copyright null.<br/>"; return false; } else if (m_copyright.length() == 0){ //song don't have to be copyrighted } //all good return true; } public boolean validatePublisher() { if (m_publisher == null){ m_message += "Publisher null.<br/>"; return false; } else if (m_publisher.length() == 0){ //songs don't have to be published } //all good return true; } public boolean validateAuthor() { if (m_author == null){ m_message += "Author null.<br/>"; return false; } else if (m_author.length() == 0){ //don't have to know the author } //all good return true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -