1 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 22 23 public class SongBean { 24 25 Song m_real_song = null; 27 String m_message = new String (); 28 29 String m_submit_action = null; 30 31 String m_handle = "-2"; 32 33 String m_title = new String (); 35 String m_author = new String (); 36 String m_copyright = new String (); 37 String m_publisher = new String (); 38 39 private static Category logger = Category.getInstance(SongBean.class.getName()); 40 41 42 48 49 public void setSubmit(String sub) { 50 m_submit_action = sub; 51 } 52 53 public String getSubmit() { 54 return m_submit_action; 55 } 56 57 61 62 public void initThis(){ 63 m_real_song = null; 64 65 m_handle = "-2"; 66 67 m_title = new String (); 69 m_author = new String (); 70 m_copyright = new String (); 71 m_publisher = new String (); 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 95 96 if (isValidForUpdate()) { 97 try { 98 thisToReal(); 99 return true; 100 } catch (Exception e) { 101 logger.error("processUpdateRequest() failed to update"); 102 } 103 } 104 return false; 105 } 106 107 108 public Song songForTitle(String 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 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 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 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 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 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 212 public String handle(){ 213 214 if(m_real_song != null){ 215 return m_real_song.handle(); 216 } 217 218 return m_handle; 220 } 221 228 229 234 public void setTitle (String title){ 235 m_title = title.trim(); 236 } 237 238 241 public String getTitle (){ 242 return m_title; 243 } 244 245 249 public void setAuthor (String author){ 250 m_author = author.trim(); 251 } 252 253 256 public String getAuthor (){ 257 return m_author; 258 } 259 260 263 public void setCopyright (String copyright){ 264 m_copyright = copyright.trim(); 265 } 266 267 270 public String getCopyright (){ 271 return m_copyright; 272 } 273 274 277 public void setPublisher (String publisher){ 278 m_publisher = publisher.trim(); 279 } 280 281 284 public String getPublisher (){ 285 return m_publisher; 286 } 287 288 289 294 public boolean isValidForAdd(){ 295 m_message = new String (); 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 321 public boolean isValidForUpdate(){ 322 m_message = new String (); 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 ( !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 353 public String 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 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 } 377 378 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 } 388 389 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 } 399 400 return true; 402 } 403 404 } 405 | Popular Tags |