OSDN Git Service

(no commit message)
[spiga-app/vaadin.git] / src / main / java / net / korabo / app / vaadin01 / srv / ContactService.java
1 /*
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.
5  */
6 package net.korabo.app.vaadin01.srv;
7
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;
23
24 /**
25  *
26  * @author cintake
27  */
28 public class ContactService {
29
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",
34         "Jennifer"};
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"};
39
40     private static ContactService instance;
41     private static OPartitionedDatabasePool pool;
42
43     public static ContactService createDemoService(){
44         try {
45             return createService0();
46         } catch (IOException ex) {
47             Logger.getLogger(ContactService.class.getName()).log(Level.SEVERE, null, ex);
48         }
49         return null;
50     }
51     
52     private static ContactService createService0() throws IOException {
53         if (instance == null) {
54
55             final ContactService contactService = new ContactService();
56             // OrientDB
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");
61 //            pool.
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;
66
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!..");
71             }
72 //            OPartitionedDatabasePool pool = new OPartitionedDatabasePool(url, "admin", "admin");
73             pool = new OPartitionedDatabasePool(url, "admin", "admin");
74
75             try (
76                     OObjectDatabaseTx db = new OObjectDatabaseTx(pool.acquire())) {
77                 db.getEntityManager().registerEntityClass(Contact.class);
78                 if (db.countClass(Contact.class) == 0) {
79                     // init
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());
92                         db.save(contact);
93                     }
94                     db.commit();
95                 }
96             }
97             instance = contactService;
98         }
99
100         return instance;
101     }
102
103 //    private HashMap<Long, Contact> contacts = new HashMap<>();
104 //    private long nextId = 0;
105     public synchronized List<Contact> findAll(String stringFilter) {
106
107         ArrayList arrayList = new ArrayList();
108         try (
109                 OObjectDatabaseTx db = new OObjectDatabaseTx(pool.acquire())) {
110             db.getEntityManager().registerEntityClass(Contact.class);
111             for (Contact contact : db.browseClass(Contact.class)) {
112                 try {
113                     boolean passesFilter = (stringFilter == null || stringFilter.isEmpty())
114                             || contact.toString().toLowerCase()
115                             .contains(stringFilter.toLowerCase());
116                     if (passesFilter) {
117                         arrayList.add(contact.clone());
118                     }
119                 } catch (CloneNotSupportedException ex) {
120                     Logger.getLogger(ContactService.class.getName()).log(
121                             Level.SEVERE, null, ex);
122                 }
123             }
124
125         }
126 //        for (Contact contact : contacts.values()) {
127 //            try {
128 //                boolean passesFilter = (stringFilter == null || stringFilter.isEmpty())
129 //                        || contact.toString().toLowerCase()
130 //                        .contains(stringFilter.toLowerCase());
131 //                if (passesFilter) {
132 //                    arrayList.add(contact.clone());
133 //                }
134 //            } catch (CloneNotSupportedException ex) {
135 //                Logger.getLogger(ContactService.class.getName()).log(
136 //                        Level.SEVERE, null, ex);
137 //            }
138 //        }
139 //        Collections.sort(arrayList, new Comparator<Contact>() {
140 //
141 //            @Override
142 //            public int compare(Contact o1, Contact o2) {
143 //                return (int) (o2.getId() - o1.getId());
144 //            }
145 //        });
146         return arrayList;
147     }
148
149     public synchronized long count() {
150 //        return contacts.size();
151         try (
152                 OObjectDatabaseTx db = new OObjectDatabaseTx(pool.acquire())) {
153             db.getEntityManager().registerEntityClass(Contact.class);
154             return db.countClass(Contact.class);
155         }
156     }
157
158     public synchronized void delete(Contact value) {
159 //        contacts.remove(value.getId());
160     }
161
162     public synchronized void save(Contact entry) {
163 //        if (entry.getId() == null) {
164 //            entry.setId(nextId++);
165 //        }
166 //        try {
167 //            entry = (Contact) InstanceUtil.dupObj(entry);
168 //        } catch (Exception ex) {
169 //            throw new RuntimeException(ex);
170 //        }
171 //        contacts.put(entry.getId(), entry);
172     }
173
174 }