2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
6 package net.korabo.app.vaadin01.srv;
8 import com.orientechnologies.orient.client.remote.OServerAdmin;
9 import com.orientechnologies.orient.core.db.OPartitionedDatabasePool;
10 import com.orientechnologies.orient.object.db.OObjectDatabaseTx;
11 import java.io.IOException;
12 import java.util.ArrayList;
13 import java.util.Calendar;
14 import java.util.Collections;
15 import java.util.Comparator;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Random;
19 import java.util.logging.Level;
20 import java.util.logging.Logger;
21 import net.korabo.app.vaadin01.ent.Contact;
22 import net.korabo.lib.beans.InstanceUtil;
28 public class ContactService {
30 // Create dummy data by randomly combining first and last names
31 static String[] fnames = {"Peter", "Alice", "John", "Mike", "Olivia",
32 "Nina", "Alex", "Rita", "Dan", "Umberto", "Henrik", "Rene", "Lisa",
33 "Linda", "Timothy", "Daniel", "Brian", "George", "Scott",
35 static String[] lnames = {"Smith", "Johnson", "Williams", "Jones",
36 "Brown", "Davis", "Miller", "Wilson", "Moore", "Taylor",
37 "Anderson", "Thomas", "Jackson", "White", "Harris", "Martin",
38 "Thompson", "Young", "King", "Robinson"};
40 private static ContactService instance;
41 private static OPartitionedDatabasePool pool;
43 public static ContactService createDemoService(){
45 return createService0();
46 } catch (IOException ex) {
47 Logger.getLogger(ContactService.class.getName()).log(Level.SEVERE, null, ex);
52 private static ContactService createService0() throws IOException {
53 if (instance == null) {
55 final ContactService contactService = new ContactService();
57 // ODatabaseDocumentTx db =
58 // new ODatabaseDocumentTx("remote:127.0.0.1/DocumentTest")
59 // .open("root", "korabo");
60 // OPartitionedDatabasePool pool = new OPartitionedDatabasePool("remote:127.0.0.1/dbtest","root","korabo");
62 // OObjectDatabaseTx db = new OObjectDatabaseTx("remote:127.0.0.1/dbtest");
63 String remote = "remote:127.0.0.1/";
64 String nameDB = "TestPartitioned2";
65 String url = remote + nameDB;
67 OServerAdmin serverAdmin = new OServerAdmin(url).connect("root", "korabo");
68 if (!serverAdmin.listDatabases().containsKey(nameDB)){
69 serverAdmin.createDatabase(nameDB, "object", "plocal");
70 System.out.println(" Database '" + nameDB + "' created!..");
72 // OPartitionedDatabasePool pool = new OPartitionedDatabasePool(url, "admin", "admin");
73 pool = new OPartitionedDatabasePool(url, "admin", "admin");
76 OObjectDatabaseTx db = new OObjectDatabaseTx(pool.acquire())) {
77 db.getEntityManager().registerEntityClass(Contact.class);
78 if (db.countClass(Contact.class) == 0) {
80 Random r = new Random(0);
81 Calendar cal = Calendar.getInstance();
82 for (int i = 0; i < 100; i++) {
83 Contact contact = db.newInstance(Contact.class);
84 contact.setFirstName(fnames[r.nextInt(fnames.length)]);
85 contact.setLastName(lnames[r.nextInt(fnames.length)]);
86 contact.setEmail(contact.getFirstName().toLowerCase() + "@"
87 + contact.getLastName().toLowerCase() + ".com");
88 contact.setPhone("+ 358 555 " + (100 + r.nextInt(900)));
89 cal.set(1930 + r.nextInt(70),
90 r.nextInt(11), r.nextInt(28));
91 contact.setBirthDate(cal.getTime());
97 instance = contactService;
103 // private HashMap<Long, Contact> contacts = new HashMap<>();
104 // private long nextId = 0;
105 public synchronized List<Contact> findAll(String stringFilter) {
107 ArrayList arrayList = new ArrayList();
109 OObjectDatabaseTx db = new OObjectDatabaseTx(pool.acquire())) {
110 db.getEntityManager().registerEntityClass(Contact.class);
111 for (Contact contact : db.browseClass(Contact.class)) {
113 boolean passesFilter = (stringFilter == null || stringFilter.isEmpty())
114 || contact.toString().toLowerCase()
115 .contains(stringFilter.toLowerCase());
117 arrayList.add(contact.clone());
119 } catch (CloneNotSupportedException ex) {
120 Logger.getLogger(ContactService.class.getName()).log(
121 Level.SEVERE, null, ex);
126 // for (Contact contact : contacts.values()) {
128 // boolean passesFilter = (stringFilter == null || stringFilter.isEmpty())
129 // || contact.toString().toLowerCase()
130 // .contains(stringFilter.toLowerCase());
131 // if (passesFilter) {
132 // arrayList.add(contact.clone());
134 // } catch (CloneNotSupportedException ex) {
135 // Logger.getLogger(ContactService.class.getName()).log(
136 // Level.SEVERE, null, ex);
139 // Collections.sort(arrayList, new Comparator<Contact>() {
142 // public int compare(Contact o1, Contact o2) {
143 // return (int) (o2.getId() - o1.getId());
149 public synchronized long count() {
150 // return contacts.size();
152 OObjectDatabaseTx db = new OObjectDatabaseTx(pool.acquire())) {
153 db.getEntityManager().registerEntityClass(Contact.class);
154 return db.countClass(Contact.class);
158 public synchronized void delete(Contact value) {
159 // contacts.remove(value.getId());
162 public synchronized void save(Contact entry) {
163 // if (entry.getId() == null) {
164 // entry.setId(nextId++);
167 // entry = (Contact) InstanceUtil.dupObj(entry);
168 // } catch (Exception ex) {
169 // throw new RuntimeException(ex);
171 // contacts.put(entry.getId(), entry);