Package de.linwave.music

Source Code of de.linwave.music.AddMusic

package de.linwave.music;

import java.util.Arrays;

import org.odbms.ObjectContainer;
import org.odbms.ObjectSet;

import de.linwave.gtm.GTM;
import de.linwave.gtm.IndexUtils;
import de.linwave.junit.inheritance.Adress;

public class AddMusic
{
  private static ObjectContainer db = GTM.getInstance();

  Genre rock = new Genre("Rock");
  Genre classic = new Genre("Classic");

  void clear() throws Exception
  {
    db.deleteClass(Band.class);
    db.deleteClass(Adress.class);
    db.deleteClass(Genre.class);
    db.deleteClass(Artist.class);
    db.deleteClass(Album.class);
    db.deleteClass(Track.class);
    db.deleteClass(GTM.REFCNT);

    IndexUtils.addIndex(Artist.class, "name");
  }

  private void loadArtist()
  {
    Band theBeatles = new Band("THE BEATLES");
    Artist ringo = new Artist("Ringo Starr");
    Artist paul = new Artist("Paul Mc.Cartney");

    Artist john = new Artist("John Lennon");
    Adress adress = new Adress("Kings Road 1", "London", 12345);
    john.setAdress(adress);

    Artist george = new Artist("George Harrison");
    theBeatles.getAritsts().addAll(Arrays.asList(ringo, paul, john, george));
    //
    Band stones = new Band("Rolling Stones");
    stones.getAritsts().add(new Artist("Mick Jagger"));

    //
    Album album = new Album("Seargent Peppers lonely hearts club", 1969, theBeatles);
    album.getTracks().add(new Track("Yellow submarine", 120, rock, paul));
    album.getTracks().add(new Track("Abbey road", 96, classic, john));
    album.getTracks().add(new Track("Hello and good bye", 132, rock, ringo));
    album.getTracks().add(new Track("Sag zum Abschied leise Servus", 112, rock, george));
    theBeatles.getAlbums().add(album);

    Album letItBe = new Album("Let it be", 1970, john);
    letItBe.getTracks().add(new Track("Two of us", 243, rock));
    letItBe.getTracks().add(new Track("Dig it", 145, rock));
    letItBe.getTracks().add(new Track("Let it be", 121, rock));
    letItBe.getTracks().add(new Track("Goodby yellow brick road", 121, rock));

    Album sunrise = new Album("Yellow Submarine", 1970, john);
    sunrise.getTracks().add(new Track("Two of us", 243, rock));
    sunrise.getTracks().add(new Track("Dig it", 145, rock));
    sunrise.getTracks().add(new Track("Let it be", 121, rock));
    sunrise.getTracks().add(new Track("Goodby yellow brick road", 121, rock));
    john.setPlayed(2);

    Album clock = new Album("Clockwork Orange", 1970, george);
    clock.getTracks().add(new Track("Track1", 243, rock));
    clock.getTracks().add(new Track("Track2", 145, rock));
    clock.getTracks().add(new Track("Track3", 121, rock));
    clock.getTracks().add(new Track("Track4", 121, rock));

    try {
      db.store(theBeatles);
      db.store(john);
      db.store(stones);
      db.store(clock);
    } catch (Exception e) {
      e.printStackTrace();
    }

    // List<Artist> artists = db.query(new Predicate<Artist>() {
    // public boolean match(Artist artist) {
    // return artist.getName().equals("John Lennon");
    // }
    // });
    // if (!artists.isEmpty()) {
    // Artist artist = artists.get(0);
    // System.out.println("Artist=" + artist.name);
    // Album clockwork = new Album("Clockwork Orange", 1978, artist);
    // clockwork.getTracks().add(new Track("Track1", 243, rock));
    // clockwork.getTracks().add(new Track("Track2", 145, rock));
    // clockwork.getTracks().add(new Track("Track3", 121, rock));
    // clockwork.getTracks().add(new Track("Track4", 121, rock));
    // try {
    // db.store(clockwork);
    // } catch (Exception e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    // }

  }

  private void showBand()
  {
    System.out.println("*** BANDS ***");
    try {
      ObjectSet<Band> items = db.query(Band.class).execute();
      int cnt = 0;
      for (Band band : items) {
        cnt++;
        System.out.println(cnt + " [BAND  oid=" + band.OID + "] " + band.name);
        for (Artist artist : band.getAritsts()) {
          System.out.println("  [ARTIST  oid=" + artist.OID + "] " + artist.name);
        }
        for (Album album : band.getAlbums()) {
          System.out.println("  [ALBUM  oid=" + album.OID + "] " + album.name + " (" + album.year + ")");
          String albumArtist = "          Artist=";
          if (album.artist != null)
            albumArtist += album.artist.getName();
          else
            albumArtist = "<NULL>";
          System.out.println(albumArtist);

          for (Track track : album.getTracks()) {
            System.out.println("      [TRACK  oid=" + track.OID + "]  title=" + track.title + " duration=" + track.duration);
            if (track.artist != null) {
              System.out.println("         Artist=" + track.artist.name);
            }
          }
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }

  }

  private void showArtist()
  {
    try {
      ObjectSet<Artist> items = db.query(Artist.class).execute();
      int cnt = 0;
      for (Artist artist : items) {
        System.out.println(" [ARTIST] " + artist.name + "  alb=" + artist.albums);
        for (Album album : artist.getAlbums()) {
          System.out.println("  [ALBUM " + album.OID + "] " + album.name + " (" + album.year + ")");
          String albumArtist = "          Artist=";
          if (album.artist != null)
            albumArtist += album.artist.getName();
          else
            albumArtist = "<NULL>";
          System.out.println(albumArtist);

          for (Track track : album.getTracks()) {
            System.out.println("      [TRACK " + track.OID + "] " + track.title);
            if (track.artist != null) {
              System.out.println("         Artist=" + track.artist.name);
            }
          }
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  private void showAlbum()
  {
    try {
      ObjectSet<Album> items = db.query(Album.class).execute();
      int cnt = 0;
      for (Album album : items) {
        System.out.println("  [ALBUM " + album.OID + "] " + album.name + " (" + album.year + ")");
        String albumArtist = "          Artist=";
        if (album.artist != null)
          albumArtist += album.artist.getName();
        else
          albumArtist = "<NULL>";
        System.out.println(albumArtist);

        for (Track track : album.getTracks()) {
          System.out.println("      [TRACK " + track.OID + "] " + track.title);
          if (track.artist != null) {
            System.out.println("         Artist=" + track.artist.name);
          }
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }

  }

  public static void main(String[] args)
  {
    try {
      AddMusic music = new AddMusic();
      music.clear();

      // Add some Bands
      for (int i = 0; i < 10; i++) {
        music.loadArtist();
        if (i > 0 && i % 100 == 0)
          System.out.println(i);
      }

      // Show the bands
      music.showBand();
      music.showArtist();
      music.showAlbum();

      db.printCacheStatistics();

    } catch (Exception ex) {
      ex.printStackTrace();
    }
    System.out.println("-done-");
  }
}
TOP

Related Classes of de.linwave.music.AddMusic

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.