001    package org.springframework.richclient.samples.dataeditor.domain;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    import java.util.Map;
006    import java.util.HashMap;
007    
008    public class SupplierService
009    {
010        private static final Map<Integer, Supplier> REPOSITORY = new HashMap<Integer, Supplier>();
011    
012        static
013        {
014            REPOSITORY.put(1, new Supplier("Jake Johnson","jake@springcource.com","555-5236","SpringCource","555-3636"));
015            REPOSITORY.put(2, new Supplier("Tim Sears","tim@microshoft.com","555-3634","Microshoft","555-8978"));
016            REPOSITORY.put(3, new Supplier("Peter Deloye","peter@ibmn.com","525-6636","IBMN","556-5654"));
017        }
018    
019        @SuppressWarnings("unchecked")
020        public List<Supplier> findSuppliers(final SupplierFilter filter)
021        {
022            List<Supplier> filtered = new ArrayList<Supplier>();
023            for (Supplier supplier : REPOSITORY.values())
024            {
025                if (checkFilter(supplier, filter))
026                {
027                    filtered.add(supplier);
028                }
029            }
030            return filtered;
031        }
032    
033        public Supplier getSupplier(Integer id)
034        {
035            return REPOSITORY.get(id);
036        }
037    
038        public boolean checkFilter(Supplier supplier, SupplierFilter filter)
039        {
040            boolean nameOk = true;
041            boolean contactNameOk = true;
042            if (filter.getNameContains() != null)
043                nameOk = supplier.getName().contains(filter.getNameContains());
044            if (filter.getContactNameContains() != null)
045                contactNameOk = supplier.getContactName().contains(filter.getContactNameContains());
046            return nameOk && contactNameOk;
047        }
048    }