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 java.util.ArrayList;
9 import java.util.Calendar;
10 import java.util.Collections;
11 import java.util.Comparator;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Random;
15 import java.util.logging.Level;
16 import java.util.logging.Logger;
17 import net.korabo.app.vaadin01.ent.Contact;
18 import net.korabo.lib.beans.InstanceUtil;
19
20 /**
21  *
22  * @author cintake
23  */
24 public class ContactService {
25  
26   // Create dummy data by randomly combining first and last names
27     static String[] fnames = { "Peter", "Alice", "John", "Mike", "Olivia",
28             "Nina", "Alex", "Rita", "Dan", "Umberto", "Henrik", "Rene", "Lisa",
29             "Linda", "Timothy", "Daniel", "Brian", "George", "Scott",
30             "Jennifer" };
31     static String[] lnames = { "Smith", "Johnson", "Williams", "Jones",
32             "Brown", "Davis", "Miller", "Wilson", "Moore", "Taylor",
33             "Anderson", "Thomas", "Jackson", "White", "Harris", "Martin",
34             "Thompson", "Young", "King", "Robinson" };
35
36     private static ContactService instance;
37
38     public static ContactService createDemoService() {
39         if (instance == null) {
40
41             final ContactService contactService = new ContactService();
42
43             Random r = new Random(0);
44             Calendar cal = Calendar.getInstance();
45             for (int i = 0; i < 100; i++) {
46                 Contact contact = new Contact();
47                 contact.setFirstName(fnames[r.nextInt(fnames.length)]);
48                 contact.setLastName(lnames[r.nextInt(fnames.length)]);
49                 contact.setEmail(contact.getFirstName().toLowerCase() + "@"
50                         + contact.getLastName().toLowerCase() + ".com");
51                 contact.setPhone("+ 358 555 " + (100 + r.nextInt(900)));
52                 cal.set(1930 + r.nextInt(70),
53                         r.nextInt(11), r.nextInt(28));
54                 contact.setBirthDate(cal.getTime());
55                 contactService.save(contact);
56             }
57             instance = contactService;
58         }
59
60         return instance;
61     }
62
63     private HashMap<Long, Contact> contacts = new HashMap<>();
64     private long nextId = 0;
65
66     public synchronized List<Contact> findAll(String stringFilter) {
67         ArrayList arrayList = new ArrayList();
68         for (Contact contact : contacts.values()) {
69             try {
70                 boolean passesFilter = (stringFilter == null || stringFilter.isEmpty())
71                         || contact.toString().toLowerCase()
72                                 .contains(stringFilter.toLowerCase());
73                 if (passesFilter) {
74                     arrayList.add(contact.clone());
75                 }
76             } catch (CloneNotSupportedException ex) {
77                 Logger.getLogger(ContactService.class.getName()).log(
78                         Level.SEVERE, null, ex);
79             }
80         }
81         Collections.sort(arrayList, new Comparator<Contact>() {
82
83             @Override
84             public int compare(Contact o1, Contact o2) {
85                 return (int) (o2.getId() - o1.getId());
86             }
87         });
88         return arrayList;
89     }
90
91     public synchronized long count() {
92         return contacts.size();
93     }
94
95     public synchronized void delete(Contact value) {
96         contacts.remove(value.getId());
97     }
98
99     public synchronized void save(Contact entry) {
100         if (entry.getId() == null) {
101             entry.setId(nextId++);
102         }
103         try {
104             entry = (Contact) InstanceUtil.dupObj(entry);
105         } catch (Exception ex) {
106             throw new RuntimeException(ex);
107         }
108         contacts.put(entry.getId(), entry);
109     }
110
111   
112 }