// $Id: SongApp.java,v 1.1 2002/05/17 08:28:18 per_nyfelt Exp $
import java.lang.String;
import java.util.Date;
import java.util.Iterator;
import java.util.Collection;
import java.util.AbstractMap;
import org.ozoneDB.*;
import Song;
import SongCollection;
import SongImpl;
import SongCollectionImpl;
import SongServices;
/**
* A simple test application to get a litle understanding of how to
* use handles under ozone.
*
* @version $Revision: 1.1 $
* @author James Stiefel
*/
public class SongApp {
public static void main( String[] args ) throws Exception {
if (args.length == 0) {
printUsage();
System.exit( 1 );
}
// create and open a new database connection
ExternalDatabase db = ExternalDatabase.openDatabase( "ozonedb:remote://localhost:3333" );
db.reloadClasses();
System.out.println( "Connected ..." );
SongServices.init(db);
if (args[0].equals("AddSong" )) {
String title = args[1];
String author = args[2];
String publisher = args[3];
String copyright = args[4];
addSong(title, author, publisher, copyright);
} else if (args[0].equals("RemoveSong" )){
String title = args[1];
removeSong(title);
} else if (args[0].equals("PrintSongByTitle" )){
String title = args[1];
printSongForTitle(title);
} else if (args[0].equals("PrintSongByHandle" )){
String handle = args[1];
printSongForHandle(handle);
} else if (args[0].equals("PrintAllSongs" )){
printAllSongs();
} else {
printUsage();
}
SongServices.term();
db.close();
}
private static void printUsage(){
System.out.println( "usage: ojvm SongApp <action>");
System.out.println( "where <action> is:");
System.out.println( " AdddSong <title> <author> <publisher> <copyright>");
System.out.println( " RemoveSong <title>");
System.out.println( " PrintSongByTitle <title>");
System.out.println( " PrintSongByHandle <handle>");
System.out.println( " PrintAllSongs");
}
private static void addSong(String title, String author, String publisher, String copyright) throws Exception{
Song song = SongServices.createSong(title);
song.setAuthor(author);
song.setPublisher(publisher);
song.setCopyright(copyright);
printSong(song);
}
private static void removeSong(String title){
SongServices.deleteSong(title);
}
private static void printSongForTitle(String title){
Song song = SongServices.songForTitle(title);
printSong(song);
}
private static void printSongForHandle(String handle){
Song song = SongServices.songForHandle(handle);
printSong(song);
}
private static void printAllSongs(){
Collection songs = SongServices.getAllSongs().getAllSongs().values();
System.out.println(songs.size() + " songs in collection.");
Iterator it = songs.iterator();
Song song;
while (it.hasNext()){
song = (Song)it.next();
printSong(song);
}
}
private static void printSong(Song song) {
if (song != null) {
System.out.println( "Song <" +song.handle() + "> title: "
+ song.getTitle () + " | by: "
+ song.getAuthor() + " | published by: "
+ song.getPublisher() + " | copyright: "
+ song.getCopyright()
);
} else {
System.out.println("printSong: input song is null.");
}
}
}