KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > song > SongServices


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

5 import org.ozoneDB.*;
6
7 /**
8  * Aplication singleton for song services.
9  * song.Song services basically encompass application logic relating
10  * to songs -- adding, removing, fetching.
11  * It does stuff like managing the MasterCollection,
12  * and doing case-insensitive indexing.
13  *
14  * @version $Revision: 1.1 $ $Date: 2002/05/17 08:28:18 $
15  * @author James Stiefel
16  */

17
18 public class SongServices {
19
20     //collection of all songs
21
private static SongCollection allSongs = null;
22
23     //the database
24
private static OzoneInterface db = null;
25
26
27
28     /**
29      * Initialize the system - establish database connection, etc.
30      *
31      */

32     public static void init(OzoneInterface database) {
33
34         db = database;
35
36         try {
37
38             //get/create AllSongs collection
39
try {
40
41                 allSongs = (SongCollection)db.objectForName("_AllSongs");
42
43                 //this is where it ends up if not found
44
if (allSongs == null) {
45                     System.out.println("init(): _AllSongs collection null. Creating...");
46                                 allSongs = (SongCollection)db.createObject( SongCollectionImpl.class.getName(),
47                                       OzoneInterface.Public, "_AllSongs");
48                 } else {
49                     System.out.println("init() allSongs found.");
50                 }
51
52             //allSongs not found - create it.
53
} catch (org.ozoneDB.PermissionDeniedException e) {
54                 System.out.println("init(): _AllSongs collection not found. Creating...");
55
56                 allSongs = (SongCollection)db.createObject( SongCollectionImpl.class.getName(),
57                                       OzoneInterface.Public, "_AllSongs");
58
59             }
60         } catch (Exception JavaDoc e) {
61             System.out.println("init(): Problem getting or creating allSong collection.");
62             e.printStackTrace();
63
64         }
65     }
66
67     /**
68      * Close down the application -- disconnect from the database, etc.
69      *
70      */

71     public static void term() {
72         //close down
73
allSongs = null;
74         db = null;
75     }
76
77     /**
78       * Retrieves the collection of all songs in the system.
79       *
80       */

81     public static SongCollection getAllSongs() {
82         return allSongs;
83     }
84
85     /**
86      * Create a new song.Song in the system. If it can't create a song.Song
87      * for any reason, it will throw an exception.
88      *
89      * @param title the song's title. You best check that it is unique
90      * before you try. If is already taken, you'll get an exception.
91      *
92      * @return A proxy object for the newly created song.Song object.
93      * @throws java.lang.Exception to signal a problem creating the new song
94      * (e.g. a song with that title already exists).
95      */

96     public static Song createSong(String JavaDoc title) throws Exception JavaDoc {
97
98         Song song = null;
99         try {
100
101             song = (Song)db.createObject( SongImpl.class.getName(),
102                                       OzoneInterface.Public);
103
104             song.setTitle(title);
105             allSongs.addSong(title.toUpperCase(), song);
106
107         } catch (Exception JavaDoc e){
108             System.out.println("createSong(): something went wrong adding to Ozone.");
109             throw e;
110         }
111
112         return song;
113     }
114
115     /**
116      * Deletes a song from the database.
117      * Note that (until garbage collection is implemented),
118      * any references to that song will be dangling pointers, so use extreme
119      * caution when deleting a song.Song.
120      * Be sure you have first eliminated references to it in other objects.
121      *
122      * @param title The song's title
123      *
124      * @return true if successful. False if not found.
125      */

126     public static boolean deleteSong(String JavaDoc title) {
127         System.out.println("deleteSong <" + title + ">" );
128
129         Song song = allSongs.deleteSong(title.toUpperCase());
130
131         return (song != null);
132     }
133
134
135
136     /**
137      * Get a song.Song from the system by it's title.
138      *
139      * @param title The song's title.
140      *
141      * @return A proxy object for the requested song.Song object, or null if not found.
142      */

143     public static Song songForTitle(String JavaDoc title){
144
145         Song song = null;
146         try {
147             song = allSongs.findSong(title.toUpperCase());
148
149             if (song == null) {
150                 System.out.println("song.Song not found by title <" + title + ">" );
151             }
152         } catch (Exception JavaDoc e){
153             System.out.println("songForHandle(): something went wrong finding song.");
154             e.printStackTrace();
155         }
156
157         return song;
158     }
159
160     /**
161      * Get a song.Song from the system by its Handle.
162      *
163      * @param handle the user's unique ID
164      *
165      * @return A proxy object for the requested song.Song object, or null if not found.
166      */

167     public static Song songForHandle(String JavaDoc handle) {
168
169         Song song = null;
170         try {
171
172             song = (Song)db.objectForHandle(handle);
173
174             if (song == null) {
175                 System.out.println("song.Song not found by handle <" + handle + ">" );
176             }
177         } catch (Exception JavaDoc e){
178             System.out.println("songForHandle(): something went wrong finding song.");
179             e.printStackTrace();
180         }
181
182         return song;
183     }
184
185
186 }
187
188
189
Popular Tags