package com.ibtp.kontor.comics.entity;

import com.ibtp.kontor.Database;
import org.bson.types.ObjectId;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
import org.mongodb.morphia.annotations.Property;

import java.util.List;

/**
 * Created by thomas on 01.03.16.
 */
@Entity("publisher")
public class Publisher {

    public Publisher() {}

    public Publisher(String name) {
        setName(name);
    }

    @Id
    private ObjectId id;

    @Property
    private String name;

    public ObjectId getId() {
        return id;
    }

    public void setId(ObjectId id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }

    public static Publisher getById(ObjectId publisherId) {
        Datastore store = Database.init().getDatastore("comics");
        List<Publisher> results = store.createQuery(Publisher.class).field("id").equal(publisherId).asList();
        if (results.size() > 0) {
            return results.get(0);
        } else {
            return null;
        }
    }
}
