KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > song > SongApp


1 package song;
2
3 // $Id: song.SongApp.java,v 1.1 2002/05/17 08:28:18 per_nyfelt Exp $
4

5
6 import org.ozoneDB.ExternalDatabase;
7
8 import java.util.Collection JavaDoc;
9 import java.util.Iterator JavaDoc;
10
11 /**
12  * A simple test application to get a litle understanding of how to
13  * use handles under ozone.
14  *
15  * @version $Revision: 1.1 $
16  * @author James Stiefel
17  */

18
19 public class SongApp {
20
21
22     public static void main( String JavaDoc[] args ) throws Exception JavaDoc {
23
24         if (args.length == 0) {
25             printUsage();
26             System.exit( 1 );
27         }
28
29         // create and open a new database connection
30
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 JavaDoc title = args[1];
38             String JavaDoc author = args[2];
39             String JavaDoc publisher = args[3];
40             String JavaDoc copyright = args[4];
41
42             addSong(title, author, publisher, copyright);
43
44         } else if (args[0].equals("RemoveSong" )){
45             String JavaDoc title = args[1];
46             removeSong(title);
47
48         } else if (args[0].equals("PrintSongByTitle" )){
49             String JavaDoc title = args[1];
50             printSongForTitle(title);
51
52         } else if (args[0].equals("PrintSongByHandle" )){
53             String JavaDoc 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 JavaDoc title, String JavaDoc author, String JavaDoc publisher, String JavaDoc copyright) throws Exception JavaDoc{
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 JavaDoc title){
87         SongServices.deleteSong(title);
88     }
89     private static void printSongForTitle(String JavaDoc title){
90         Song song = SongServices.songForTitle(title);
91         printSong(song);
92     }
93     private static void printSongForHandle(String JavaDoc handle){
94         Song song = SongServices.songForHandle(handle);
95         printSong(song);
96     }
97     private static void printAllSongs(){
98         Collection JavaDoc songs = SongServices.getAllSongs().getAllSongs().values();
99         System.out.println(songs.size() + " songs in collection.");
100
101         Iterator JavaDoc 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