001    /*
002     * Copyright 2002-2006 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.samples.simple.domain;
017    
018    import java.util.ArrayList;
019    import java.util.Date;
020    import java.util.HashSet;
021    import java.util.List;
022    
023    /**
024     * This class provides a trivial in-memory datastore to hold all the contacts. In a real application, this would
025     * probably be a server-side object that is accessed via an interface using the typical Spring wiring and remoting.
026     * @author Larry Streepy
027     */
028    public class ContactDataStore {
029    
030            /** Simple Id generator. */
031            private static int nextId = 1;
032    
033            /** Our contacts. */
034            private HashSet contacts = new HashSet();
035    
036            /**
037             * Default constructor - "load" our initial data.
038             */
039            public ContactDataStore() {
040                    loadData();
041            }
042    
043            /**
044             * Get all the contacts.
045             * @return Array of all contact objects
046             */
047            public Contact[] getAllContacts() {
048                    return (Contact[]) contacts.toArray(new Contact[0]);
049            }
050    
051            /**
052             * Update a contact.
053             */
054            public void update(Contact contact) {
055                    contacts.add(contact);
056            }
057    
058            /**
059             * Delete a contact.
060             */
061            public void delete(Contact contact) {
062                    contacts.remove(contact);
063            }
064    
065            /**
066             * Add a new contact.
067             */
068            public void add(Contact contact) {
069                    contact.setId(nextId++); // Give it a unique id
070                    contacts.add(contact);
071            }
072    
073            /**
074             * Load our initial data.
075             */
076            private void loadData() {
077                    contacts.add(makeContact("Larry", "Streepy", "123 Some St.", "Apt. #26C", "New York", "NY", "10010",
078                                    ContactType.BUSINESS, "Lorem ipsum..."));
079                    contacts.add(makeContact("Keith", "Donald", "456 WebFlow Rd.", "2", "Cooltown", "NY", "10001",
080                                    ContactType.BUSINESS, "Lorem ipsum..."));
081                    contacts.add(makeContact("Steve", "Brothers", "10921 The Other Street", "", "Denver", "CO", "81234-2121",
082                                    ContactType.PERSONAL, "Lorem ipsum..."));
083                    contacts.add(makeContact("Carlos", "Mencia", "4321 Comedy Central", "", "Hollywood", "CA", "91020",
084                                    ContactType.PERSONAL, "Lorem ipsum..."));
085                    contacts.add(makeContact("Jim", "Jones", "1001 Another Place", "", "Dallas", "TX", "71212",
086                                    ContactType.PERSONAL, "Lorem ipsum..."));
087                    contacts.add(makeContact("Jenny", "Jones", "1001 Another Place", "", "Dallas", "TX", "75201",
088                                    ContactType.PERSONAL, "Lorem ipsum..."));
089                    contacts.add(makeContact("Greg", "Jones", "9 Some Other Place", "Apt. 12D", "Chicago", "IL", "60601",
090                                    ContactType.PERSONAL, "Lorem ipsum..."));
091            }
092    
093        private List<TodoItem> getTodoItemList()
094        {
095            List<TodoItem> l = new ArrayList<TodoItem>();
096            l.add(new TodoItem("test", "test", new Date()));
097            return l;
098        }
099    
100            /**
101             * Make a Contact object with the given data.
102             * @return Contact object
103             */
104            private Contact makeContact(String first, String last, String address1, String address2, String city, String state,
105                            String zip, ContactType contactType, String memo) {
106                    Contact contact = new Contact();
107                    contact.setId(nextId++);
108                    contact.setContactType(contactType);
109                    contact.setFirstName(first);
110                    contact.setLastName(last);
111            contact.setMemo(memo);
112    
113                    Address address = contact.getAddress();
114                    address.setAddress1(address1);
115                    address.setAddress2(address2);
116                    address.setCity(city);
117                    address.setState(state);
118                    address.setZip(zip);
119    
120            contact.setTodoItems(getTodoItemList());
121    
122                    return contact;
123            }
124    }