1 package song; 2 3 5 6 import org.ozoneDB.ExternalDatabase; 7 8 import java.util.Collection ; 9 import java.util.Iterator ; 10 11 18 19 public class SongApp { 20 21 22 public static void main( String [] args ) throws Exception { 23 24 if (args.length == 0) { 25 printUsage(); 26 System.exit( 1 ); 27 } 28 29 ExternalDatabase db = ExternalDatabase.openDatabase( "ozonedb:remote://localhost:3333" ); 31 db.reloadClasses(); 32 System.out.println( "Connected ..." ); 33 34 SongServices.init(db); 35 36 if (args[0].equals("AddSong" )) { 37 String title = args[1]; 38 String author = args[2]; 39 String publisher = args[3]; 40 String copyright = args[4]; 41 42 addSong(title, author, publisher, copyright); 43 44 } else if (args[0].equals("RemoveSong" )){ 45 String title = args[1]; 46 removeSong(title); 47 48 } else if (args[0].equals("PrintSongByTitle" )){ 49 String title = args[1]; 50 printSongForTitle(title); 51 52 } else if (args[0].equals("PrintSongByHandle" )){ 53 String handle = args[1]; 54 printSongForHandle(handle); 55 56 } else if (args[0].equals("PrintAllSongs" )){ 57 printAllSongs(); 58 } else { 59 60 printUsage(); 61 } 62 63 SongServices.term(); 64 db.close(); 65 } 66 67 private static void printUsage(){ 68 System.out.println( "usage: ojvm song.SongApp <action>"); 69 System.out.println( "where <action> is:"); 70 System.out.println( " AdddSong <title> <author> <publisher> <copyright>"); 71 System.out.println( " RemoveSong <title>"); 72 System.out.println( " PrintSongByTitle <title>"); 73 System.out.println( " PrintSongByHandle <handle>"); 74 System.out.println( " PrintAllSongs"); 75 } 76 77 78 private static void addSong(String title, String author, String publisher, String copyright) throws Exception { 79 Song song = SongServices.createSong(title); 80 song.setAuthor(author); 81 song.setPublisher(publisher); 82 song.setCopyright(copyright); 83 printSong(song); 84 85 } 86 private static void removeSong(String title){ 87 SongServices.deleteSong(title); 88 } 89 private static void printSongForTitle(String title){ 90 Song song = SongServices.songForTitle(title); 91 printSong(song); 92 } 93 private static void printSongForHandle(String handle){ 94 Song song = SongServices.songForHandle(handle); 95 printSong(song); 96 } 97 private static void printAllSongs(){ 98 Collection songs = SongServices.getAllSongs().getAllSongs().values(); 99 System.out.println(songs.size() + " songs in collection."); 100 101 Iterator it = songs.iterator(); 102 Song song; 103 while (it.hasNext()){ 104 song = (Song)it.next(); 105 printSong(song); 106 } 107 108 } 109 110 private static void printSong(Song song) { 111 if (song != null) { 112 System.out.println( "song.Song <" +song.handle() + "> title: " 113 + song.getTitle () + " | by: " 114 + song.getAuthor() + " | published by: " 115 + song.getPublisher() + " | copyright: " 116 + song.getCopyright() 117 ); 118 } else { 119 System.out.println("printSong: input song is null."); 120 } 121 } 122 123 } 124 125 126 | Popular Tags |