package doc.walkthru;

import java.net.URL;
import java.util.List;

import org.benow.repository.mapping.JSQLQuery;
import org.benow.service.LocalServices;
import org.benow.service.Services;

public class ServiceRunner {

  /**
   * @param args
   */
  public static void main(String[] args) {
    // clean up from previous run
    JSQLQuery.deleteObjects(new Class<?>[] { AlbumImpl.class, TrackImpl.class, AlbumImpl.class });

    // get services, as we'll be using them
    PerformerService performerService = (PerformerService) Services.takeAService(PerformerService.class);
    AlbumService albumService=(AlbumService) Services.takeAService(AlbumService.class);
    try {
      // create a new artist
      ArtistImpl aphex = (ArtistImpl) performerService.createArtist("Aphex Twin");
      aphex.addLink(new URL("http://www.rephlex.com/"));
      aphex.addLink(new URL("http://en.wikipedia.org/wiki/Aphex_Twin"));
      // create a new album
      AlbumImpl saw8592=(AlbumImpl) albumService.createAlbum("Selected Ambient Works 85-92", aphex);
      Track[] tracks=new Track[]{
          new TrackImpl("Xtal", aphex, "4:51"),
          new TrackImpl("Tha", aphex, "9:01"),
          new TrackImpl("Pulsewidth", aphex, "3:47"),
          new TrackImpl("Ageispolis", aphex, "5:21"),
          new TrackImpl("i", aphex, "1:13"),
          new TrackImpl("Green Calx", aphex, "6:02"),
          new TrackImpl("Heliosphan", aphex, "4:51"),
          new TrackImpl("We Are The Music Makers", aphex, "7:42"),
          new TrackImpl("Schottkey 7th Path", aphex, "5:07"),
          new TrackImpl("Ptolemy", aphex, "7:12"),
          new TrackImpl("Hedpheylm", aphex, "6:02"),
          new TrackImpl("Delphium", aphex, "5:36"),
          new TrackImpl("Actium", aphex, "7:35")
      };
      saw8592.setTracks(tracks);
      // update the album as it's been updated
      saw8592.update();
      
      GroupImpl datacide=new GroupImpl("DATacide");
      datacide.addMember(performerService.createArtist("Atom Heart"));
      datacide.addMember(performerService.createArtist("Tetsu Inoue"));
      datacide.addLink(new URL("http://www.discogs.com/DATacide-Flowerhead/release/5"));
      AlbumImpl flowerhead=(AlbumImpl) albumService.createAlbum("Flowerhead", datacide);
      tracks=new Track[]{
          new TrackImpl("Flashback Signal",datacide,"15:54"),  
          new TrackImpl("Flowerhead",datacide,"9:33"),  
          new TrackImpl("Deep Chair",datacide,"14:05"),  
          new TrackImpl("So Much Light",datacide,"12:02"),  
          new TrackImpl("Sixties Out Of Tune",datacide,"13:05"),  
      };
      flowerhead.setTracks(tracks);
      flowerhead.update();
     
      List<Album> albums = albumService.getAlbums();
      for (Album a : albums)
        System.out.println(((AlbumImpl) a).toFullString());

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      // return the services, so they can be used again.
      LocalServices.returnAService(performerService);
      LocalServices.returnAService(albumService);
    }

    System.exit(0);
  }

}