2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

Coverage Report - org.apache.shale.usecases.rolodex.RolodexDao
 
Classes in this File Line Coverage Branch Coverage Complexity
RolodexDao
100%
89/89
N/A
1.833
RolodexDao$1
100%
18/18
N/A
1.833
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to you under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 package org.apache.shale.usecases.rolodex;
 19  
 
 20  
 import java.io.IOException;
 21  
 import java.util.ArrayList;
 22  
 import java.util.Collection;
 23  
 import java.util.Iterator;
 24  
 import java.util.List;
 25  
 import java.util.SortedSet;
 26  
 import java.util.TreeSet;
 27  
 
 28  
 import javax.faces.model.SelectItem;
 29  
 
 30  
 import org.apache.commons.digester.Digester;
 31  
 import org.xml.sax.InputSource;
 32  
 import org.xml.sax.SAXException;
 33  
 
 34  
 /**
 35  
  * <p>
 36  
  * Data Access Object for the roledex use case.
 37  
  * </p>
 38  
  */
 39  935
 public class RolodexDao {
 40  
 
 41  
     /**
 42  
      * <p>
 43  
      * Mock datastore.
 44  
      * </p>
 45  
      */
 46  5
     private Collection entityDataStore = null;
 47  
 
 48  5
     private Collection stateDataStore = null;
 49  
 
 50  5
     private Collection countryDataStore = null;
 51  
 
 52  
     /**
 53  
      * <p>
 54  
      * The constructor loads some default data.
 55  
      * </p>
 56  
      */
 57  5
     public RolodexDao() {
 58  5
         GenericComparator comparator = new GenericComparator();
 59  5
         comparator.setSortBy("sortName, name");
 60  
 
 61  5
         entityDataStore = new TreeSet(comparator);
 62  
 
 63  5
         comparator = new GenericComparator();
 64  5
         comparator.setSortBy("value");
 65  
 
 66  5
         stateDataStore = new TreeSet(comparator);
 67  5
         countryDataStore = new TreeSet(comparator);
 68  5
         loadStates();
 69  5
     }
 70  
 
 71  
     /**
 72  
      * <p>
 73  
      * Tab indexes for the rolodex. Each sub array represents the starting and
 74  
      * ending character index.
 75  
      * </p>
 76  
      */
 77  1
     public final static char[][] TAB_INDEX = { { 'A', 'B' }, { 'C', 'D' },
 78  
             { 'E', 'F' }, { 'G', 'H' }, { 'I', 'J' }, { 'K', 'L' },
 79  
             { 'M', 'N' }, { 'O', 'P' }, { 'Q', 'Z' } };
 80  
 
 81  
     /**
 82  
      * <p>
 83  
      * Returns a list of <code>SelectItem</code> that will be used to build
 84  
      * the links.
 85  
      * </p>
 86  
      */
 87  
     public List getTabs() {
 88  1
         List tabs = new ArrayList();
 89  1
         SelectItem tab = null;
 90  
 
 91  10
         for (int i = 0; i < TAB_INDEX.length; i++) {
 92  9
             tab = new SelectItem();
 93  9
             tab.setLabel(TAB_INDEX[i][0] + "-" + TAB_INDEX[i][1]);
 94  9
             tab.setValue(new Integer(i));
 95  9
             tabs.add(tab);
 96  
         }
 97  
 
 98  1
         return tabs;
 99  
     }
 100  
 
 101  
     /**
 102  
      * <p>
 103  
      * Saves a {@link Contact} to the mock data store.
 104  
      * </p>
 105  
      */
 106  
     public int saveContact(Contact entity) {
 107  66
         entityDataStore.add(entity);
 108  66
         return findTabForContact(entity);
 109  
     }
 110  
 
 111  
     /**
 112  
      * <p>This function will find the tabIndex that the {@link Contact}
 113  
      * will be located on.  It will default to the first page.</p>
 114  
      */
 115  
     public int findTabForContact(Contact contact) {
 116  79
         for (int i = 0; i < TAB_INDEX.length; i++) {
 117  79
            if ((contact.getTabIndex() >= TAB_INDEX[i][0]) && 
 118  
                (contact.getTabIndex() <= TAB_INDEX[i][1]))  
 119  66
               return i;    
 120  
         }
 121  
         
 122  
         return 0;
 123  
     }
 124  
     
 125  
     
 126  
     /**
 127  
      * <p>
 128  
      * Retuns a subset of contacts within the <code>index</code> of a a tab
 129  
      * defined by <code>TAB_INDEX</code> from the mock data store. If this was
 130  
      * a RDBMS data store, the contacts returned might be a "ghost" object
 131  
      * meaning that maybe only the <code>name</code> attribute would be
 132  
      * populated.
 133  
      * </p>
 134  
      */
 135  
     public List findContactsForTab(int index) {
 136  12
         List contacts = new ArrayList();
 137  
 
 138  12
         Contact low = new Contact();
 139  12
         Contact high = new Contact();
 140  
 
 141  12
         StringBuffer key = new StringBuffer();
 142  
         
 143  12
         key.append(TAB_INDEX[index][0]);
 144  12
         low.setName(key.toString());
 145  
 
 146  12
         key.setLength(0);
 147  12
         key.append(TAB_INDEX[index][1]);
 148  612
         for (int i = 0; i < 50; i++)
 149  600
            key.append('z');
 150  
 
 151  12
         high.setName(key.toString());
 152  
 
 153  12
         SortedSet subSet = ((SortedSet) entityDataStore).subSet(low, high);
 154  12
         Iterator si = subSet.iterator();
 155  38
         while (si.hasNext())
 156  26
             contacts.add(si.next());
 157  
 
 158  12
         return contacts;
 159  
     }
 160  
 
 161  
     /**
 162  
      * <p>
 163  
      * Removes the contact from the mock data store.
 164  
      * </p>
 165  
      */
 166  
     public void deleteContact(Contact entity) {
 167  1
         entityDataStore.remove(entity);
 168  1
     }
 169  
 
 170  
     /**
 171  
      * <p>
 172  
      * Loads the {@link State} codes and contacts from an XML data source. The
 173  
      * <code>stateDataStore</code> set will hold the states where the
 174  
      * <code>countryDataStore</code> set will hold the countries. The target type of
 175  
      * these collections will be SelectItem.  The contacts are held in the 
 176  
      * <code>entityDataStore</code> set.
 177  
      * </p>
 178  
      */
 179  
     public void loadStates() {
 180  
 
 181  5
         Object config = new Object() {
 182  
             public void addState(State state) {
 183  310
                 SelectItem item = null;
 184  
                 // add a blank option
 185  310
                 if (stateDataStore.size() == 0) {
 186  5
                     item = new SelectItem();
 187  5
                     item.setLabel("");
 188  5
                     item.setValue("");
 189  5
                     stateDataStore.add(item);
 190  
                 }
 191  
                 
 192  310
                 item = new SelectItem();
 193  310
                 item.setLabel(state.getState());
 194  310
                 item.setValue(state.getAbbrState());
 195  310
                 stateDataStore.add(item);
 196  
 
 197  310
                 item = new SelectItem();
 198  310
                 item.setLabel(state.getCountry());
 199  310
                 item.setValue(state.getAbbrCountry());
 200  310
                 countryDataStore.add(item);
 201  310
             }
 202  
 
 203  5
             public void addContact(Contact contact) {
 204  65
                 saveContact(contact);
 205  65
             }
 206  
         };
 207  
 
 208  5
         Digester digester = new Digester();
 209  5
         digester.setValidating(false);
 210  5
         digester.setUseContextClassLoader(true);
 211  
 
 212  5
         digester.addObjectCreate("dex/states/state",
 213  3
                 org.apache.shale.usecases.rolodex.State.class);
 214  5
         digester.addSetProperties("dex/states/state");
 215  5
         digester.addSetNext("dex/states/state", "addState",
 216  
                 "org.apache.shale.usecases.rolodex.State");
 217  
 
 218  5
         digester.addObjectCreate("dex/contacts/contact/residentialAddress",
 219  
                 org.apache.shale.usecases.rolodex.Address.class);
 220  5
         digester.addCallMethod(
 221  
                 "dex/contacts/contact/residentialAddress/street1",
 222  
                 "setStreet1", 0);
 223  5
         digester.addCallMethod(
 224  
                 "dex/contacts/contact/residentialAddress/street2",
 225  
                 "setStreet2", 0);
 226  5
         digester.addCallMethod("dex/contacts/contact/residentialAddress/city",
 227  
                 "setCity", 0);
 228  5
         digester.addCallMethod("dex/contacts/contact/residentialAddress/state",
 229  
                 "setState", 0);
 230  5
         digester.addCallMethod("dex/contacts/contact/residentialAddress/zip",
 231  
                 "setZipAsString", 0);
 232  5
         digester.addCallMethod(
 233  
                 "dex/contacts/contact/residentialAddress/province",
 234  
                 "setProvince", 0);
 235  5
         digester.addCallMethod(
 236  
                 "dex/contacts/contact/residentialAddress/country",
 237  
                 "setCountry", 0);
 238  5
         digester.addSetNext("dex/contacts/contact/residentialAddress",
 239  
                 "setResidentialAddress",
 240  
                 "org.apache.shale.usecases.rolodex.Address");
 241  
 
 242  5
         digester.addObjectCreate("dex/contacts/contact/businessAddress",
 243  
                 org.apache.shale.usecases.rolodex.Address.class);
 244  5
         digester.addCallMethod("dex/contacts/contact/businessAddress/street1",
 245  
                 "setStreet1", 0);
 246  5
         digester.addCallMethod("dex/contacts/contact/businessAddress/street2",
 247  
                 "setStreet2", 0);
 248  5
         digester.addCallMethod("dex/contacts/contact/businessAddress/city",
 249  
                 "setCity", 0);
 250  5
         digester.addCallMethod("dex/contacts/contact/businessAddress/state",
 251  
                 "setState", 0);
 252  5
         digester.addCallMethod("dex/contacts/contact/businessAddress/zip",
 253  
                 "setZipAsString", 0);
 254  5
         digester.addCallMethod("dex/contacts/contact/businessAddress/province",
 255  
                 "setProvince", 0);
 256  5
         digester.addCallMethod("dex/contacts/contact/businessAddress/country",
 257  
                 "setCountry", 0);
 258  5
         digester.addSetNext("dex/contacts/contact/businessAddress",
 259  
                 "setBusinessAddress",
 260  
                 "org.apache.shale.usecases.rolodex.Address");
 261  
 
 262  5
         digester.addObjectCreate("dex/contacts/contact",
 263  
                 org.apache.shale.usecases.rolodex.Contact.class);
 264  5
         digester.addCallMethod("dex/contacts/contact/name", "setName", 0);
 265  5
         digester.addCallMethod("dex/contacts/contact/email", "setEmail", 0);
 266  5
         digester.addCallMethod("dex/contacts/contact/residentialPhone",
 267  
                 "setResidentialPhone", 0);
 268  5
         digester.addCallMethod("dex/contacts/contact/businessPhone",
 269  
                 "setBusinessPhone", 0);
 270  5
         digester.addSetNext("dex/contacts/contact", "addContact",
 271  
                 "org.apache.shale.usecases.rolodex.Contact");
 272  
 
 273  5
         digester.push(config);
 274  
 
 275  5
         ClassLoader classloader = Thread.currentThread()
 276  
                 .getContextClassLoader();
 277  5
         if (classloader == null)
 278  
             classloader = config.getClass().getClassLoader();
 279  
 
 280  5
         InputSource in = new InputSource(
 281  
                 classloader
 282  
                         .getResourceAsStream("org/apache/shale/usecases/rolodex/dex.xml"));
 283  
 
 284  
         try {
 285  5
             digester.parse(in);
 286  
         } catch (IOException e) {
 287  
             e.printStackTrace();
 288  
         } catch (SAXException e) {
 289  
             e.printStackTrace();
 290  5
         }
 291  
 
 292  5
     }
 293  
 
 294  
     /**
 295  
      * <p>
 296  
      * Returns an array of states used to populate a select list. The target
 297  
      * type is an array of SelectItem.
 298  
      * </p>
 299  
      */
 300  
     public SelectItem[] getStates() {
 301  
         SelectItem[] states = new SelectItem[stateDataStore.size()];
 302  
         stateDataStore.toArray(states);
 303  
         return states;
 304  
     }
 305  
 
 306  
     /**
 307  
      * <p>
 308  
      * Returns an array of countries used to populate a select list. The target
 309  
      * type is an array of SelectItem.
 310  
      * </p>
 311  
      */
 312  
     public SelectItem[] getCountries() {
 313  
         SelectItem[] countries = new SelectItem[countryDataStore.size()];
 314  
         countryDataStore.toArray(countries);
 315  
         return countries;
 316  
     }
 317  
 
 318  
     /**
 319  
      * <p>
 320  
      * Returns the latest copy of the {@link Contact} by primary key.
 321  
      * </p>
 322  
      * 
 323  
      * @param name
 324  
      *            contact name that uniquely identifies a Contact.
 325  
      * @return the target, fully populated {@link Contact}
 326  
      */
 327  
     public Contact findContact(String name) {
 328  13
         Contact sarg = new Contact();
 329  13
         sarg.setName(name);
 330  13
         SortedSet subSet = ((TreeSet) entityDataStore).tailSet(sarg);
 331  13
         return (Contact) subSet.first();
 332  
     }
 333  
 
 334  
 }