Save This Page
Home » openjdk-7 » net.sourceforge » neurosdbm » db » [javadoc | source]
    1   //
    2   //  Neuros Database Manipulator
    3   //  Copyright (C) 2003  Neuros Database Manipulator
    4   //
    5   //  This program is free software; you can redistribute it and/or modify
    6   //  it under the terms of the GNU General Public License as published by
    7   //  the Free Software Foundation; either version 2 of the License, or
    8   //  (at your option) any later version.
    9   //
   10   //  This program is distributed in the hope that it will be useful,
   11   //  but WITHOUT ANY WARRANTY; without even the implied warranty of
   12   //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13   //  GNU General Public License for more details.
   14   //
   15   //  You should have received a copy of the GNU General Public License
   16   //  along with this program; if not, write to the Free Software
   17   //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   18   //
   19   //  For information about Neuros Database Manipulator and its authors,
   20   //  please contact the Neuros Database Manipulator Web Site at
   21   //  http://neurosdbm.sourceforge.net
   22   //
   23   //
   24   
   25   package net.sourceforge.neurosdbm.db;
   26   
   27   
   28   import java.io.FileNotFoundException;
   29   import java.io.IOException;
   30   import java.util.ArrayList;
   31   import java.util.Iterator;
   32   import java.util.Comparator;
   33   import java.util.Collections;
   34   
   35   abstract class AudioDBBase {
   36   
   37     AudioMDB audioMDB;
   38     AudioSAI audioSAI;
   39     PlaylistMDB playlistMDB;
   40     PlaylistSAI playlistSAI;
   41     PAI playlistPAI;
   42     ArtistMDB artistMDB;
   43     ArtistSAI artistSAI;
   44     PAI artistPAI;
   45     AlbumsMDB albumsMDB;
   46     AlbumsSAI albumsSAI;
   47     PAI albumsPAI;
   48     GenreMDB genreMDB;
   49     GenreSAI genreSAI;
   50     PAI genrePAI;
   51     RecordingsMDB recordingsMDB;
   52     RecordingsSAI recordingsSAI;
   53     PAI recordingsPAI;
   54   
   55     
   56     abstract String getAudioDBName();
   57   
   58     
   59     static final int DBTYPE_AUDIO = 1;
   60     static final int DBTYPE_PCAUDIO = 2;
   61     abstract int getDBType();
   62   
   63   
   64     AudioDBBase(String path)
   65       throws FileNotFoundException, IOException, DatabaseCorruptException {
   66   
   67       audioMDB = new AudioMDB(this, path + "/" + getAudioDBName() + ".mdb",
   68                               getDBType());
   69       audioSAI = new AudioSAI(path + "/" + getAudioDBName() + ".sai",
   70                               getDBType(), audioMDB, null);
   71       playlistMDB = new PlaylistMDB(path + "/playlist.mdb", getDBType());
   72       playlistPAI = new PlaylistPAI(path + "/playlist.pai", getDBType(),
   73                                     audioMDB);
   74       playlistSAI = new PlaylistSAI(path + "/playlist.sai", getDBType(),
   75                                     playlistMDB, playlistPAI);
   76       artistMDB = new ArtistMDB(path + "/artist.mdb");
   77       artistPAI = new PAI(path + "/artist.pai", audioMDB);
   78       artistSAI = new ArtistSAI(path + "/artist.sai", artistMDB, artistPAI);
   79       albumsMDB = new AlbumsMDB(path + "/albums.mdb");
   80       albumsPAI = new PAI(path + "/albums.pai", audioMDB);
   81       albumsSAI = new AlbumsSAI(path + "/albums.sai", albumsMDB, albumsPAI);
   82       genreMDB = new GenreMDB(path + "/genre.mdb");
   83       genrePAI = new PAI(path + "/genre.pai", audioMDB);
   84       genreSAI = new GenreSAI(path + "/genre.sai", genreMDB, genrePAI);
   85       recordingsMDB = new RecordingsMDB(path + "/recordings.mdb");
   86       recordingsPAI = new PAI(path + "/recordings.pai", audioMDB);
   87       recordingsSAI = new RecordingsSAI(path + "/recordings.sai", recordingsMDB,
   88                                         recordingsPAI);
   89       audioMDB.linkPointers(playlistMDB, artistMDB, albumsMDB, genreMDB,
   90                             recordingsMDB);
   91     }
   92   
   93     AudioDBBase(String path, boolean createNewDB)
   94       throws FileNotFoundException, IOException, DatabaseCorruptException {
   95   
   96       audioMDB = new AudioMDB(this, path + "/" + getAudioDBName() + ".mdb",
   97                               getDBType(), true);
   98       audioSAI = new AudioSAI(path + "/" + getAudioDBName() + ".sai",
   99                               getDBType(), true, audioMDB, null);
  100       playlistMDB = new PlaylistMDB(path + "/playlist.mdb", getDBType(), true);
  101       playlistPAI = new PlaylistPAI(path + "/playlist.pai", getDBType(), true,
  102                                     audioMDB);
  103       playlistSAI = new PlaylistSAI(path + "/playlist.sai", getDBType(), true,
  104                                     playlistMDB, playlistPAI);
  105       artistMDB = new ArtistMDB(path + "/artist.mdb", true);
  106       artistPAI = new PAI(path + "/artist.pai", true, audioMDB);
  107       artistSAI = new ArtistSAI(path + "/artist.sai", true, artistMDB,
  108                                 artistPAI);
  109       albumsMDB = new AlbumsMDB(path + "/albums.mdb", true);
  110       albumsPAI = new PAI(path + "/albums.pai", true, audioMDB);
  111       albumsSAI = new AlbumsSAI(path + "/albums.sai", true, albumsMDB,
  112                                 albumsPAI);
  113       genreMDB = new GenreMDB(path + "/genre.mdb", true);
  114       genrePAI = new PAI(path + "/genre.pai", true, audioMDB);
  115       genreSAI = new GenreSAI(path + "/genre.sai", true, genreMDB, genrePAI);
  116       recordingsMDB = new RecordingsMDB(path + "/recordings.mdb", true);
  117       recordingsPAI = new PAI(path + "/recordings.pai", true, audioMDB);
  118       recordingsSAI = new RecordingsSAI(path + "/recordings.sai", true,
  119                                         recordingsMDB, recordingsPAI);
  120     }
  121   
  122   
  123     void write() throws IOException, DatabaseInternalCorruptionException {
  124       audioMDB.write();
  125       audioSAI.write();
  126       playlistMDB.write();
  127       playlistPAI.write();
  128       playlistSAI.write();
  129       artistMDB.write();
  130       artistPAI.write();
  131       artistSAI.write();
  132       albumsMDB.write();
  133       albumsPAI.write();
  134       albumsSAI.write();
  135       genreMDB.write();
  136       genrePAI.write();
  137       genreSAI.write();
  138       recordingsMDB.write();
  139       recordingsPAI.write();
  140       recordingsSAI.write();
  141     }
  142     
  143   
  144     ArrayList query(int database, int type) {
  145       ArrayList returnArrayList = null;
  146   
  147       switch (type) {
  148       case Database.DB_TRACKS:
  149         returnArrayList = new ArrayList();
  150         Iterator tracks = audioSAI.getEntriesIterator();
  151         for (; tracks.hasNext(); ) {
  152           SAIRecord record = (SAIRecord)tracks.next();
  153           if (record.getMDBKey() != 1) {
  154             DisplayIndex index = new DisplayIndex(database,
  155                                                   DisplayIndex.DB_TRACKS,
  156                                                   record.getMDBKey());
  157             returnArrayList.add(index);
  158           }
  159         }
  160         break;
  161   
  162       case Database.DB_PLAYLISTS:
  163         returnArrayList = new ArrayList();
  164         Iterator playlists = playlistSAI.getEntriesIterator();
  165         for (; playlists.hasNext(); ) {
  166           SAIRecord record = (SAIRecord)playlists.next();
  167           if (record.getMDBKey() != 1) {
  168             DisplayIndex index = new DisplayIndex(database,
  169                                                   DisplayIndex.DB_PLAYLISTS,
  170                                                   record.getMDBKey());
  171             returnArrayList.add(index);
  172           }
  173         }
  174         break;
  175         
  176       case Database.DB_ARTISTS:
  177         returnArrayList = new ArrayList();
  178         Iterator artists = artistSAI.getEntriesIterator();
  179         for (; artists.hasNext(); ) {
  180           SAIRecord record = (SAIRecord)artists.next();
  181           if (record.getMDBKey() != 1) {
  182             DisplayIndex index = new DisplayIndex(database,
  183                                                   DisplayIndex.DB_ARTISTS,
  184                                                   record.getMDBKey());
  185             returnArrayList.add(index);
  186           }
  187         }
  188         break;
  189         
  190       case Database.DB_ALBUMS:
  191         returnArrayList = new ArrayList();
  192         Iterator albums = albumsSAI.getEntriesIterator();
  193         for (; albums.hasNext(); ) {
  194           SAIRecord record = (SAIRecord)albums.next();
  195           if (record.getMDBKey() != 1) {
  196             DisplayIndex index = new DisplayIndex(database,
  197                                                   DisplayIndex.DB_ALBUMS,
  198                                                   record.getMDBKey());
  199             returnArrayList.add(index);
  200           }
  201         }
  202         break;
  203   
  204       case Database.DB_GENRES:
  205         returnArrayList = new ArrayList();
  206         Iterator genres = genreSAI.getEntriesIterator();
  207         for (; genres.hasNext(); ) {
  208           SAIRecord record = (SAIRecord)genres.next();
  209           if (record.getMDBKey() != 1) {
  210             DisplayIndex index = new DisplayIndex(database,
  211                                                   DisplayIndex.DB_GENRES,
  212                                                   record.getMDBKey());
  213             returnArrayList.add(index);
  214           }
  215         }
  216         break;
  217       }
  218        
  219       return returnArrayList;
  220     }
  221   
  222   
  223     ArrayList query(int database, int type, int key) {
  224       ArrayList returnArrayList = null;
  225       
  226       switch (type) {
  227       case Database.DB_PLAYLISTS: {
  228         returnArrayList = new ArrayList();
  229         int paiKey = playlistSAI.getPAIKey(key);
  230         PAIRecord paiRecord = playlistPAI.getRecord(paiKey);
  231         for (Iterator iter = paiRecord.iterator(); iter.hasNext(); ) {
  232           Integer trackKey = (Integer) iter.next();
  233           DisplayIndex index = new DisplayIndex(database,
  234                                                 Database.DB_TRACKS,
  235                                                 trackKey.intValue());
  236           returnArrayList.add(index);
  237         }
  238         break;
  239       }
  240         
  241       case Database.DB_ARTISTS: {
  242         returnArrayList = new ArrayList();
  243         int paiKey = artistSAI.getPAIKey(key);
  244         PAIRecord paiRecord = artistPAI.getRecord(paiKey);
  245         for (Iterator iter = paiRecord.iterator(); iter.hasNext(); ) {
  246           Integer trackKey = (Integer) iter.next();
  247           DisplayIndex index = new DisplayIndex(database,
  248                                                 Database.DB_TRACKS,
  249                                                 trackKey.intValue());
  250           returnArrayList.add(index);
  251         }
  252         break;
  253       }
  254         
  255       case Database.DB_ALBUMS: {
  256         returnArrayList = new ArrayList();
  257         int paiKey = albumsSAI.getPAIKey(key);
  258         PAIRecord paiRecord = albumsPAI.getRecord(paiKey);
  259         for (Iterator iter = paiRecord.iterator(); iter.hasNext(); ) {
  260           Integer trackKey = (Integer) iter.next();
  261           DisplayIndex index = new DisplayIndex(database,
  262                                                 Database.DB_TRACKS,
  263                                                 trackKey.intValue());
  264           returnArrayList.add(index);
  265         }
  266         break;
  267       }
  268         
  269       case Database.DB_GENRES: {
  270         returnArrayList = new ArrayList();
  271         int paiKey = genreSAI.getPAIKey(key);
  272         PAIRecord paiRecord = genrePAI.getRecord(paiKey);
  273         for (Iterator iter = paiRecord.iterator(); iter.hasNext(); ) {
  274           Integer trackKey = (Integer) iter.next();
  275           DisplayIndex index = new DisplayIndex(database,
  276                                                 Database.DB_TRACKS,
  277                                                 trackKey.intValue());
  278           returnArrayList.add(index);
  279         }
  280         break;
  281       }
  282       }
  283   
  284       return returnArrayList;
  285     }
  286   
  287     
  288     DisplayRecord query(DisplayIndex index) {
  289       switch (index.getSubdatabase()) {
  290       case Database.DB_TRACKS:
  291         return getAudioDisplayRecord(index.getKey());
  292         
  293       case Database.DB_PLAYLISTS:
  294         return getPlaylistDisplayRecord(index.getKey());
  295         
  296       case Database.DB_ARTISTS:
  297         return getArtistDisplayRecord(index.getKey());
  298         
  299       case Database.DB_ALBUMS:
  300         return getAlbumDisplayRecord(index.getKey());
  301         
  302       case Database.DB_GENRES:
  303         return getGenreDisplayRecord(index.getKey());
  304       }
  305       return null;
  306     }
  307   
  308   
  309     void sort(ArrayList list) {
  310       if (list.size() == 0)
  311         return;
  312       DisplayIndex index = (DisplayIndex)list.get(0);
  313       switch (index.getSubdatabase()) {
  314       case Database.DB_TRACKS:
  315         Collections.sort(list, new AudioTitleComparator());
  316         audioSAI.reorder(list);
  317         break;
  318         
  319       case Database.DB_PLAYLISTS:
  320         Collections.sort(list, new PlaylistComparator());
  321         playlistSAI.reorder(list);
  322         break;
  323         
  324       case Database.DB_ARTISTS:
  325         Collections.sort(list, new ArtistComparator());
  326         artistSAI.reorder(list);
  327         break;
  328         
  329       case Database.DB_ALBUMS:
  330         Collections.sort(list, new AlbumComparator());
  331         albumsSAI.reorder(list);
  332         break;
  333         
  334       case Database.DB_GENRES:
  335         Collections.sort(list, new GenreComparator());
  336         genreSAI.reorder(list);
  337         break;
  338       }
  339     }
  340   
  341     
  342     void sort(ArrayList list, int subDatabase, int mdbKey) {
  343       if (list.size() == 0)
  344         return;
  345       switch (subDatabase) {
  346       case Database.DB_ARTISTS: {
  347         int paiKey = artistSAI.getPAIKey(mdbKey);
  348         Collections.sort(list, new ArtistSubbaseComparator());
  349         artistPAI.reorder(list, paiKey);
  350         return;
  351       }
  352   
  353       case Database.DB_ALBUMS: {
  354         int paiKey = albumsSAI.getPAIKey(mdbKey);
  355         Collections.sort(list, new AlbumSubbaseComparator());
  356         albumsPAI.reorder(list, paiKey);
  357         return;
  358       }
  359   
  360       case Database.DB_GENRES: {
  361         int paiKey = genreSAI.getPAIKey(mdbKey);
  362         Collections.sort(list, new GenreSubbaseComparator());
  363         genrePAI.reorder(list, paiKey);
  364         return;
  365       }
  366       }
  367     }
  368   
  369     
  370     class AudioTitleComparator implements Comparator {
  371       public int compare(Object o1, Object o2) {
  372         if (!(o1 instanceof DisplayIndex))
  373           throw new ClassCastException();
  374         if (!(o2 instanceof DisplayIndex))
  375           throw new ClassCastException();
  376         AudioDisplayRecord rec1 =
  377           getAudioDisplayRecord(((DisplayIndex)o1).getKey());
  378         AudioDisplayRecord rec2 =
  379           getAudioDisplayRecord(((DisplayIndex)o2).getKey());
  380         String title1 = rec1.getTitle();
  381         String title2 = rec2.getTitle();
  382         return title1.compareToIgnoreCase(title2);
  383       }
  384   
  385       public boolean equals(Object o1, Object o2) {
  386         if (!(o1 instanceof DisplayIndex))
  387           throw new ClassCastException();
  388         if (!(o2 instanceof DisplayIndex))
  389           throw new ClassCastException();
  390         AudioDisplayRecord rec1 =
  391           getAudioDisplayRecord(((DisplayIndex)o1).getKey());
  392         AudioDisplayRecord rec2 =
  393           getAudioDisplayRecord(((DisplayIndex)o2).getKey());
  394         String title1 = rec1.getTitle();
  395         String title2 = rec2.getTitle();
  396         return title1.equals(title2);
  397       }
  398     }
  399     
  400     
  401     class PlaylistComparator implements Comparator {
  402       public int compare(Object o1, Object o2) {
  403         if (!(o1 instanceof DisplayIndex))
  404           throw new ClassCastException();
  405         if (!(o2 instanceof DisplayIndex))
  406           throw new ClassCastException();
  407         PlaylistDisplayRecord rec1 =
  408           getPlaylistDisplayRecord(((DisplayIndex)o1).getKey());
  409         PlaylistDisplayRecord rec2 =
  410           getPlaylistDisplayRecord(((DisplayIndex)o2).getKey());
  411         String playlist1 = rec1.getName();
  412         String playlist2 = rec2.getName();
  413         return playlist1.compareToIgnoreCase(playlist2);
  414       }
  415   
  416       public boolean equals(Object o1, Object o2) {
  417         if (!(o1 instanceof DisplayIndex))
  418           throw new ClassCastException();
  419         if (!(o2 instanceof DisplayIndex))
  420           throw new ClassCastException();
  421         PlaylistDisplayRecord rec1 =
  422           getPlaylistDisplayRecord(((DisplayIndex)o1).getKey());
  423         PlaylistDisplayRecord rec2 =
  424           getPlaylistDisplayRecord(((DisplayIndex)o2).getKey());
  425         String playlist1 = rec1.getName();
  426         String playlist2 = rec2.getName();
  427         return playlist1.equals(playlist2);
  428       }
  429     }
  430     
  431     
  432     class ArtistComparator implements Comparator {
  433       public int compare(Object o1, Object o2) {
  434         if (!(o1 instanceof DisplayIndex))
  435           throw new ClassCastException();
  436         if (!(o2 instanceof DisplayIndex))
  437           throw new ClassCastException();
  438         ArtistDisplayRecord rec1 =
  439           getArtistDisplayRecord(((DisplayIndex)o1).getKey());
  440         ArtistDisplayRecord rec2 =
  441           getArtistDisplayRecord(((DisplayIndex)o2).getKey());
  442         String artist1 = rec1.getArtist();
  443         String artist2 = rec2.getArtist();
  444         return artist1.compareToIgnoreCase(artist2);
  445       }
  446   
  447       public boolean equals(Object o1, Object o2) {
  448         if (!(o1 instanceof DisplayIndex))
  449           throw new ClassCastException();
  450         if (!(o2 instanceof DisplayIndex))
  451           throw new ClassCastException();
  452         ArtistDisplayRecord rec1 =
  453           getArtistDisplayRecord(((DisplayIndex)o1).getKey());
  454         ArtistDisplayRecord rec2 =
  455           getArtistDisplayRecord(((DisplayIndex)o2).getKey());
  456         String artist1 = rec1.getArtist();
  457         String artist2 = rec2.getArtist();
  458         return artist1.equals(artist2);
  459       }
  460     }
  461     
  462     
  463     class AlbumComparator implements Comparator {
  464       public int compare(Object o1, Object o2) {
  465         if (!(o1 instanceof DisplayIndex))
  466           throw new ClassCastException();
  467         if (!(o2 instanceof DisplayIndex))
  468           throw new ClassCastException();
  469         AlbumDisplayRecord rec1 =
  470           getAlbumDisplayRecord(((DisplayIndex)o1).getKey());
  471         AlbumDisplayRecord rec2 =
  472           getAlbumDisplayRecord(((DisplayIndex)o2).getKey());
  473         String album1 = rec1.getAlbum();
  474         String album2 = rec2.getAlbum();
  475         return album1.compareToIgnoreCase(album2);
  476       }
  477   
  478       public boolean equals(Object o1, Object o2) {
  479         if (!(o1 instanceof DisplayIndex))
  480           throw new ClassCastException();
  481         if (!(o2 instanceof DisplayIndex))
  482           throw new ClassCastException();
  483         AlbumDisplayRecord rec1 =
  484           getAlbumDisplayRecord(((DisplayIndex)o1).getKey());
  485         AlbumDisplayRecord rec2 =
  486           getAlbumDisplayRecord(((DisplayIndex)o2).getKey());
  487         String album1 = rec1.getAlbum();
  488         String album2 = rec2.getAlbum();
  489         return album1.equals(album2);
  490       }
  491     }
  492     
  493     
  494     class GenreComparator implements Comparator {
  495       public int compare(Object o1, Object o2) {
  496         if (!(o1 instanceof DisplayIndex))
  497           throw new ClassCastException();
  498         if (!(o2 instanceof DisplayIndex))
  499           throw new ClassCastException();
  500         GenreDisplayRecord rec1 =
  501           getGenreDisplayRecord(((DisplayIndex)o1).getKey());
  502         GenreDisplayRecord rec2 =
  503           getGenreDisplayRecord(((DisplayIndex)o2).getKey());
  504         String genre1 = rec1.getGenre();
  505         String genre2 = rec2.getGenre();
  506         return genre1.compareToIgnoreCase(genre2);
  507       }
  508   
  509       public boolean equals(Object o1, Object o2) {
  510         if (!(o1 instanceof DisplayIndex))
  511           throw new ClassCastException();
  512         if (!(o2 instanceof DisplayIndex))
  513           throw new ClassCastException();
  514         GenreDisplayRecord rec1 =
  515           getGenreDisplayRecord(((DisplayIndex)o1).getKey());
  516         GenreDisplayRecord rec2 =
  517           getGenreDisplayRecord(((DisplayIndex)o2).getKey());
  518         String genre1 = rec1.getGenre();
  519         String genre2 = rec2.getGenre();
  520         return genre1.equals(genre2);
  521       }
  522     }
  523     
  524     /**
  525      * Two key sort - album name then track number
  526      *
  527      */
  528     class ArtistSubbaseComparator implements Comparator {
  529       public int compare(Object o1, Object o2) {
  530         if (!(o1 instanceof DisplayIndex))
  531           throw new ClassCastException();
  532         if (!(o2 instanceof DisplayIndex))
  533           throw new ClassCastException();
  534         int key1 = ((DisplayIndex)o1).getKey();
  535         int key2 = ((DisplayIndex)o2).getKey();
  536         String album1 = FullTrackInfo.getAlbum(key1);
  537         String album2 = FullTrackInfo.getAlbum(key2);
  538         int compare = album1.compareToIgnoreCase(album2);
  539         if (compare != 0) {
  540           return compare;
  541         }
  542         int trackNumber1 = FullTrackInfo.getTrackNumber(key1);
  543         int trackNumber2 = FullTrackInfo.getTrackNumber(key2);
  544         if (trackNumber1 < trackNumber2) {
  545           return -1;
  546         }
  547         if (trackNumber1 > trackNumber2) {
  548           return 1;
  549         }
  550         return 0;
  551       }
  552   
  553       public boolean equals(Object o1, Object o2) {
  554         if (!(o1 instanceof DisplayIndex))
  555           throw new ClassCastException();
  556         if (!(o2 instanceof DisplayIndex))
  557           throw new ClassCastException();
  558         int key1 = ((DisplayIndex)o1).getKey();
  559         int key2 = ((DisplayIndex)o2).getKey();
  560         String album1 = FullTrackInfo.getAlbum(key1);
  561         String album2 = FullTrackInfo.getAlbum(key2);
  562         boolean compare = album1.equals(album2);
  563         if (!compare) {
  564           return false;
  565         }
  566         int trackNumber1 = FullTrackInfo.getTrackNumber(key1);
  567         int trackNumber2 = FullTrackInfo.getTrackNumber(key2);
  568         return trackNumber1 == trackNumber2;
  569       }
  570     }
  571     
  572   
  573     class AlbumSubbaseComparator implements Comparator {
  574       public int compare(Object o1, Object o2) {
  575         if (!(o1 instanceof DisplayIndex))
  576           throw new ClassCastException();
  577         if (!(o2 instanceof DisplayIndex))
  578           throw new ClassCastException();
  579         int key1 = ((DisplayIndex)o1).getKey();
  580         int key2 = ((DisplayIndex)o2).getKey();
  581         int trackNumber1 = FullTrackInfo.getTrackNumber(key1);
  582         int trackNumber2 = FullTrackInfo.getTrackNumber(key2);
  583         if (trackNumber1 < trackNumber2) {
  584           return -1;
  585         }
  586         if (trackNumber1 > trackNumber2) {
  587           return 1;
  588         }
  589         return 0;
  590       }
  591   
  592       public boolean equals(Object o1, Object o2) {
  593         if (!(o1 instanceof DisplayIndex))
  594           throw new ClassCastException();
  595         if (!(o2 instanceof DisplayIndex))
  596           throw new ClassCastException();
  597         int key1 = ((DisplayIndex)o1).getKey();
  598         int key2 = ((DisplayIndex)o2).getKey();
  599         int trackNumber1 = FullTrackInfo.getTrackNumber(key1);
  600         int trackNumber2 = FullTrackInfo.getTrackNumber(key2);
  601         return trackNumber1 == trackNumber2;
  602       }
  603     }
  604     
  605   
  606     class GenreSubbaseComparator implements Comparator {
  607       public int compare(Object o1, Object o2) {
  608         if (!(o1 instanceof DisplayIndex))
  609           throw new ClassCastException();
  610         if (!(o2 instanceof DisplayIndex))
  611           throw new ClassCastException();
  612         int key1 = ((DisplayIndex)o1).getKey();
  613         int key2 = ((DisplayIndex)o2).getKey();
  614         String title1 = FullTrackInfo.getTitle(key1);
  615         String title2 = FullTrackInfo.getTitle(key2);
  616         return title1.compareToIgnoreCase(title2);
  617       }
  618   
  619       public boolean equals(Object o1, Object o2) {
  620         if (!(o1 instanceof DisplayIndex))
  621           throw new ClassCastException();
  622         if (!(o2 instanceof DisplayIndex))
  623           throw new ClassCastException();
  624         int key1 = ((DisplayIndex)o1).getKey();
  625         int key2 = ((DisplayIndex)o2).getKey();
  626         String title1 = FullTrackInfo.getTitle(key1);
  627         String title2 = FullTrackInfo.getTitle(key2);
  628         return title1.equals(title2);
  629       }
  630     }
  631     
  632   
  633     void reorder(ArrayList list, int subDatabase, int mdbKey) {
  634       if (list.size() == 0)
  635         return;
  636       switch (subDatabase) {
  637       case Database.DB_PLAYLISTS: {
  638         int paiKey = playlistSAI.getPAIKey(mdbKey);
  639         playlistPAI.reorder(list, paiKey);
  640         return;
  641       }
  642   
  643       case Database.DB_ARTISTS: {
  644         int paiKey = artistSAI.getPAIKey(mdbKey);
  645         artistPAI.reorder(list, paiKey);
  646         return;
  647       }
  648   
  649       case Database.DB_ALBUMS: {
  650         int paiKey = albumsSAI.getPAIKey(mdbKey);
  651         albumsPAI.reorder(list, paiKey);
  652         return;
  653       }
  654   
  655       case Database.DB_GENRES: {
  656         int paiKey = genreSAI.getPAIKey(mdbKey);
  657         genrePAI.reorder(list, paiKey);
  658         return;
  659       }
  660       }
  661     }
  662   
  663     
  664     AudioDisplayRecord getAudioDisplayRecord(int key) {
  665       MDBRecord mdbRecord = audioMDB.getMDBRecord(key);
  666       if (mdbRecord instanceof AudioRecord) {
  667         AudioRecord audioRecord = (AudioRecord)mdbRecord;
  668       
  669         int artistKey = audioRecord.getArtistKey();
  670         ArtistRecord artistRecord = null;
  671         mdbRecord = artistMDB.getMDBRecord(artistKey);
  672         if (mdbRecord instanceof ArtistRecord) {
  673           artistRecord = (ArtistRecord)mdbRecord;
  674         }
  675       
  676         int albumKey = audioRecord.getAlbumKey();
  677         AlbumRecord albumRecord = null;
  678         mdbRecord = albumsMDB.getMDBRecord(albumKey);
  679         if (mdbRecord instanceof AlbumRecord) {
  680           albumRecord = (AlbumRecord)mdbRecord;
  681         }
  682       
  683         int genreKey = audioRecord.getGenreKey();
  684         GenreRecord genreRecord = null;
  685         mdbRecord = genreMDB.getMDBRecord(genreKey);
  686         if (mdbRecord instanceof GenreRecord) {
  687           genreRecord = (GenreRecord)mdbRecord;
  688         }
  689       
  690         return new AudioDisplayRecord(key, audioRecord, artistRecord,
  691                                       albumRecord, genreRecord);
  692       }
  693       return null;
  694     }
  695   
  696     PlaylistDisplayRecord getPlaylistDisplayRecord(int key) {
  697       MDBRecord mdbRecord = playlistMDB.getMDBRecord(key);
  698       if (mdbRecord instanceof PlaylistRecord) {
  699         PlaylistRecord playlistRecord = (PlaylistRecord)mdbRecord;
  700         if (playlistRecord == null)
  701           return null;
  702         return new PlaylistDisplayRecord(key, playlistRecord);
  703       }
  704       return null;
  705     }
  706     
  707     ArtistDisplayRecord getArtistDisplayRecord(int key) {
  708       MDBRecord mdbRecord = artistMDB.getMDBRecord(key);
  709       if (mdbRecord instanceof ArtistRecord) {
  710         ArtistRecord artistRecord = (ArtistRecord)mdbRecord;
  711         if (artistRecord == null)
  712           return null;
  713         return new ArtistDisplayRecord(key, artistRecord);
  714       }
  715       return null;
  716     }
  717     
  718     AlbumDisplayRecord getAlbumDisplayRecord(int key) {
  719       MDBRecord mdbRecord = albumsMDB.getMDBRecord(key);
  720       if (mdbRecord instanceof AlbumRecord) {
  721         AlbumRecord albumRecord = (AlbumRecord)mdbRecord;
  722         if (albumRecord == null)
  723           return null;
  724         return new AlbumDisplayRecord(key, albumRecord);
  725       }
  726       return null;
  727     }
  728     
  729     GenreDisplayRecord getGenreDisplayRecord(int key) {
  730       MDBRecord mdbRecord = genreMDB.getMDBRecord(key);
  731       if (mdbRecord instanceof GenreRecord) {
  732         GenreRecord genreRecord = (GenreRecord)mdbRecord;
  733         if (genreRecord == null)
  734           return null;
  735         return new GenreDisplayRecord(key, genreRecord);
  736       }
  737       return null;
  738     }
  739   
  740   
  741     void addNewTrack(NewTrack newTrack)
  742       throws DatabaseInternalCorruptionException {
  743   
  744       int audioKey = FullTrackInfo.addNewTrackInfo(newTrack);
  745       ArrayList playlistArrayList = new ArrayList();
  746       playlistArrayList.add(new Integer(1));
  747       int artistKey =
  748         artistMDB.addNewTrack(newTrack, artistSAI, artistPAI, audioKey);
  749       int albumKey =
  750         albumsMDB.addNewTrack(newTrack, albumsSAI, albumsPAI, audioKey);
  751       int genreKey =
  752         genreMDB.addNewTrack(newTrack, genreSAI, genrePAI, audioKey);
  753       int recordingsKey = 1;
  754       audioMDB.addNewTrack(newTrack, audioKey, playlistArrayList, artistKey,
  755                            albumKey,
  756                            genreKey, recordingsKey, playlistMDB, artistMDB,
  757                            albumsMDB, genreMDB, recordingsMDB, audioSAI);
  758     }
  759   
  760   
  761     void deleteTrack(int audioKey, int artistKey, int albumKey, int genreKey) {
  762       audioMDB.deleteRecord(audioKey);
  763       audioSAI.deleteRecord(audioKey);
  764       
  765       int artistPAIKey = artistSAI.getPAIKey(artistKey);
  766       boolean artistDeleted = artistPAI.deleteRecord(artistPAIKey, audioKey);
  767       if (artistDeleted) {
  768         artistMDB.deleteRecord(artistKey);
  769         artistSAI.deleteRecord(artistKey);
  770       }
  771       
  772       int albumsPAIKey = albumsSAI.getPAIKey(albumKey);
  773       boolean albumDeleted = albumsPAI.deleteRecord(albumsPAIKey, audioKey);
  774       if (albumDeleted) {
  775         albumsMDB.deleteRecord(albumKey);
  776         albumsSAI.deleteRecord(albumKey);
  777       }
  778       
  779       int genrePAIKey = genreSAI.getPAIKey(genreKey);
  780       boolean genreDeleted = genrePAI.deleteRecord(genrePAIKey, audioKey);
  781       if (genreDeleted) {
  782         genreMDB.deleteRecord(genreKey);
  783         genreSAI.deleteRecord(genreKey);
  784       }
  785     }
  786   
  787     
  788     public String toString() {
  789       String returnString = new String();
  790       returnString += audioMDB.toString() + "\n";
  791       returnString += audioSAI.toString() + "\n";
  792       returnString += playlistMDB.toString() + "\n";
  793       returnString += playlistSAI.toString() + "\n";
  794       returnString += playlistPAI.toString() + "\n";
  795       returnString += artistMDB.toString() + "\n";
  796       returnString += artistSAI.toString() + "\n";
  797       returnString += artistPAI.toString() + "\n";
  798       returnString += albumsMDB.toString() + "\n";
  799       returnString += albumsSAI.toString() + "\n";
  800       returnString += albumsPAI.toString() + "\n";
  801       returnString += genreMDB.toString() + "\n";
  802       returnString += genreSAI.toString() + "\n";
  803       returnString += genrePAI.toString() + "\n";
  804       returnString += recordingsMDB.toString() + "\n";
  805       returnString += recordingsSAI.toString() + "\n";
  806       returnString += recordingsPAI.toString() + "\n";
  807       return returnString;
  808     }
  809   
  810   
  811     /**
  812      * Determines if the new track is already in the database.
  813      *
  814      * @param newTrack The new track to check
  815      * @return Is it a duplicate - true means yes it is!
  816      */
  817     boolean isADuplicate(NewTrack newTrack) {
  818       return audioMDB.isADuplicate(newTrack);
  819     }
  820   
  821   
  822     /**
  823      * Creates a new playlist.  The key of the new playlist is returned.
  824      *
  825      * @param name The name of this playlist.
  826      * @return The key of the newly created playlist.
  827      */
  828     int createPlaylist(String name) {
  829       return playlistMDB.createPlaylist(name, playlistSAI, playlistPAI);
  830     }
  831   
  832   
  833     void removePlaylist(int playlistKey) {
  834       int paiKey = playlistSAI.getPAIKey(playlistKey);
  835       playlistSAI.deleteRecord(playlistKey);
  836       playlistMDB.deleteRecord(playlistKey);
  837       playlistPAI.deleteEntry(paiKey);
  838     }
  839   
  840     
  841     void addToPlaylist(int trackKey, int playlistKey)
  842       throws DatabaseInternalCorruptionException {
  843       
  844       int paiKey = playlistSAI.getPAIKey(playlistKey);
  845       playlistPAI.addNewRecord(trackKey, paiKey);
  846     }
  847   
  848   
  849     void deleteFromPlaylist(int trackKey, int playlistKey)
  850       throws DatabaseInternalCorruptionException {
  851   
  852       int paiKey = playlistSAI.getPAIKey(playlistKey);
  853       PAIRecord rec = playlistPAI.getRecord(paiKey);
  854       rec.deleteKey(trackKey);
  855     }
  856   
  857   
  858     void renamePlaylist(String newName, int playlistKey) {
  859       playlistMDB.renamePlaylist(newName, playlistKey);
  860     }
  861     
  862   
  863     String getArtist(int key) {
  864       if (artistMDB == null) {
  865         return null;
  866       }
  867       return artistMDB.getArtist(key);
  868     }
  869   
  870     String getAlbum(int key) {
  871       if (albumsMDB == null) {
  872         return null;
  873       }
  874       return albumsMDB.getAlbum(key);
  875     }
  876   
  877     String getGenre(int key) {
  878       if (genreMDB == null) {
  879         return null;
  880       }
  881       return genreMDB.getGenre(key);
  882     }
  883   }

Save This Page
Home » openjdk-7 » net.sourceforge » neurosdbm » db » [javadoc | source]