package com.ibtp.kontor;

import com.ibtp.kontor.comics.entity.Artist;
import com.ibtp.kontor.comics.entity.Comic;
import com.ibtp.kontor.comics.entity.Publisher;
import com.mongodb.MongoClient;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import org.mongodb.morphia.query.Query;

import java.util.Iterator;
import java.util.List;

/**
 * Created by thomas on 29.02.16.
 */
public class DumpComics {

    public static void main(String[] args) {
        final Database database = Database.init();
        database.registerDatastore("kontor");
        database.registerDatastore("comics");
        Datastore kontor = database.getDatastore("kontor");
        Datastore comics = database.getDatastore("comics");
        final Query<Artist> query = kontor.createQuery(Artist.class);
        final List<Artist> artists = query.asList();
        System.out.println(artists);
        for (Artist artist: artists) {
            String artistName = artist.getName();
            System.out.println("Artist(" + artist.getId() + ": " + artistName + ")");
            if (comics.createQuery(Artist.class).field("name").equal(artistName).asList().isEmpty()) {
                Artist comicsArtist = new Artist(artistName);
                comics.save(comicsArtist);
            }
        }
        final List<Publisher> publishers = kontor.createQuery(Publisher.class).asList();
        for (Publisher publisher: publishers) {
            String publisherName = publisher.getName();
            System.out.println("Publisher(" + publisher.getId() + ": " + publisherName + ")");
            if (comics.createQuery(Publisher.class).field("name").equal(publisherName).asList().isEmpty()) {
                Publisher comicsPublisher = new Publisher(publisherName);
                comics.save(comicsPublisher);
            }
        }
        final List<Comic> comicList = comics.createQuery(Comic.class).asList();
        System.out.println(comicList);
        for (Comic comic: comicList ) {
            System.out.println(comic);
        }
    }
}
