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 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;
24 public class ContactService {
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",
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" };
36 private static ContactService instance;
38 public static ContactService createDemoService() {
39 if (instance == null) {
41 final ContactService contactService = new ContactService();
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);
57 instance = contactService;
63 private HashMap<Long, Contact> contacts = new HashMap<>();
64 private long nextId = 0;
66 public synchronized List<Contact> findAll(String stringFilter) {
67 ArrayList arrayList = new ArrayList();
68 for (Contact contact : contacts.values()) {
70 boolean passesFilter = (stringFilter == null || stringFilter.isEmpty())
71 || contact.toString().toLowerCase()
72 .contains(stringFilter.toLowerCase());
74 arrayList.add(contact.clone());
76 } catch (CloneNotSupportedException ex) {
77 Logger.getLogger(ContactService.class.getName()).log(
78 Level.SEVERE, null, ex);
81 Collections.sort(arrayList, new Comparator<Contact>() {
84 public int compare(Contact o1, Contact o2) {
85 return (int) (o2.getId() - o1.getId());
91 public synchronized long count() {
92 return contacts.size();
95 public synchronized void delete(Contact value) {
96 contacts.remove(value.getId());
99 public synchronized void save(Contact entry) {
100 if (entry.getId() == null) {
101 entry.setId(nextId++);
104 entry = (Contact) InstanceUtil.dupObj(entry);
105 } catch (Exception ex) {
106 throw new RuntimeException(ex);
108 contacts.put(entry.getId(), entry);