Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package dipu.familytree.models; import com.mongodb.client.MongoCollection; import static com.mongodb.client.model.Filters.and; import static com.mongodb.client.model.Filters.eq; import static com.mongodb.client.model.Filters.or; import dipu.familytree.App; import dipu.familytree.dbmodel.PersonModel; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.function.Consumer; import javax.naming.InvalidNameException; import org.bson.Document; import org.bson.conversions.Bson; /** * * @author dipu */ public class Person extends PersonModel { public static final String COLLECTION = "persons"; private static final HashMap<String, Person> PERSONS = new HashMap<>(); /*==========================================================+ | - - - - - - PUBLIC METHODS - - - - - - - | +==========================================================*/ /** * Get BSon document for database. * * @return */ public Document getDocument() { return Person.toDocument(this); } /** * Populate all field with proper objects. */ public void populate() { if (isPopulated()) { return; } setFather(Person.findOne(getFatherName())); setMother(Person.findOne(getMotherName())); setSpouse(Person.findOne(getSpouseName())); getExSpouses().addAll(Person.findAll(getExSpouseNames())); setPopulated(true); } /*==========================================================+ | - - - - - - STATIC METHODS - - - - - - - | +==========================================================*/ /** * Get BSon document for database. * * @param person * @return */ public static Document toDocument(Person person) { return App.instance.mongo().document().append("name", person.getName()).append("gender", person.getGender()) .append("birthday", person.getBirthday()).append("father", person.getFatherName()) .append("mother", person.getMotherName()).append("spouse", person.getSpouseName()) .append("exs", person.getExSpouseNames()); } public static Person fromDocument(Document doc) { if (doc == null) { return null; } String name = (String) doc.get("name"); // check if cached if (PERSONS.containsKey(name)) { return PERSONS.get(name); } Person person = new Person(); PERSONS.put(name, person); // cache person person.setName(name); person.setGender((String) doc.get("gender")); person.setBirthday((Date) doc.get("birthday")); person.setFatherName((String) doc.get("father")); person.setMotherName((String) doc.get("mother")); person.setSpouseName((String) doc.get("spouse")); List<String> exList = (List<String>) doc.get("exs"); person.getExSpouseNames().addAll(exList); return person; } /*----------------------------------------------------------+ | READ from database | +----------------------------------------------------------*/ public static Person findOne(Bson filter) { // get collection MongoCollection<Document> col = App.instance.mongo().collection(COLLECTION); // search existing person Document doc = col.find(filter).first(); // return person return Person.fromDocument(doc); } public static Person findOne(String name) { // return a person return findOne(eq("name", name)); } public static List<Person> find(Bson filter, boolean populate) { // get collection MongoCollection<Document> col = App.instance.mongo().collection(COLLECTION); // build person list List<Person> persons = new ArrayList<>(); // search existing person col.find(filter).forEach((Consumer<? super Document>) (Document doc) -> { Person person = Person.fromDocument(doc); if (populate) { person.populate(); } persons.add(person); }); // return person list return persons; } public static List<Person> find(Bson filter) { return find(filter, false); } public static List<Person> find() { return find(null); } public static List<Person> find(boolean populate) { return find(null, populate); } public static List<Person> findAll(List<String> name, boolean populate) { // get collection Bson[] filters = new Bson[name.size()]; for (int i = 0; i < name.size(); i++) { filters[i] = eq("name", name.get(i)); } return find(or(filters), populate); } public static List<Person> findAll(List<String> name) { return findAll(name, false); } /*----------------------------------------------------------+ | CREATE in database | +----------------------------------------------------------*/ public static void create(Person person) { if (Person.findOne(person.getName())) { throw new InvalidNameException("Person with similar name already exists"); } } /*----------------------------------------------------------+ | UPDATE in database | +----------------------------------------------------------*/ public static void update(Person person) { } /*----------------------------------------------------------+ | DELETE from database | +----------------------------------------------------------*/ public static void delete(Person person) { } }