001    package market;
002    
003    import java.util.Iterator;
004    
005    import market.statistics.CICustomerStats;
006    import sale.SalesPoint;
007    import sale.Shop;
008    import users.User;
009    import users.UserManager;
010    import users.UserManagerFilter;
011    import users.swing.UserFilter;
012    import data.events.VetoException;
013    
014    /**
015     * The market's UserManager.
016     */
017    public class UMUserBase extends UserManager {
018    
019        /**
020             * ID for serialization.
021             */
022            private static final long serialVersionUID = 8476169105291116432L;
023            
024            public static final int CUSTOMER = 1;
025        public static final int WAREHOUSE_WORKER = 2;
026        public static final int SELLER = 3;
027        public static final int MANAGER = 4;
028    
029        /**
030         * Filters market's customers.
031         */
032        private UserFilter ufCustomers = new UserFilter(){
033                    private static final long serialVersionUID = -190142240082722787L;
034    
035                    public boolean match(User user) {
036                return UCustomer.class.isInstance(user);
037            }
038        };
039    
040        /**
041         * Filters market's warehouse workers.
042         */
043        private UserFilter ufWarehouseWorker = new UserFilter(){
044                    private static final long serialVersionUID = -7344603246615314557L;
045    
046                    public boolean match(User user) {
047                if(UStaffer.class.isInstance(user) && ((UStaffer)user).getQualification().
048                        equals(UStaffer.WAREHOUSE_WORKER)){
049                    return true;
050                }
051                return false;
052            }
053        };
054    
055        /**
056         * Filters market's sellers.
057         */
058        private UserFilter ufSellers = new UserFilter(){
059                    private static final long serialVersionUID = -2081936239256484026L;
060    
061                    public boolean match(User user) {
062                if(UStaffer.class.isInstance(user) && ((UStaffer)user).getQualification().
063                        equals(UStaffer.SELLER)){
064                    return true;
065                }
066                return false;
067            }
068        };
069    
070        /**
071         * Filters the market's manager(s)
072         */
073        private UserFilter ufManager = new UserFilter(){
074                    private static final long serialVersionUID = 545767415085896502L;
075    
076                    public boolean match(User user) {
077                if(UStaffer.class.isInstance(user) && ((UStaffer)user).getQualification().equals(UStaffer.MANAGER)){
078                    return true;
079                }
080                return false;
081            }
082        };
083    
084        /**
085         * Filters the market's staff (warehouse-worker, seller, manager)
086         */
087        private UserFilter ufStaff = new UserFilter() {
088                    private static final long serialVersionUID = -7102053036151507300L;
089                    public boolean match(User user) {
090                return user instanceof UStaffer;
091            }
092        };
093    
094    
095        /**
096         * A UserManagerFilter containing all managers of the market.
097         */
098        private UserManagerFilter umfManager = new UserManagerFilter(this, ufManager);
099    
100    
101        /**
102         * Static method that gets the {@link UserManager#getGlobalUM() global user manager}.
103         * @return the global user manager.
104         */
105        public static UMUserBase getGlobalBase() {
106            return (UMUserBase)getGlobalUM();
107        }
108    
109        /**
110         * creates a new User with given name, type and qualification
111         *
112         * @param sName the User's ID
113         * @param type the User's type: 1 for customer, other numbers for employees.
114         * @param qualification the employee's qualification (manager, seller, warehouse worker)
115         */
116        public static User createUser(String sName, int type, String qualification) {
117            if(isUser(sName)) return null;
118            User user;
119            switch(type){
120                case CUSTOMER: user = new UCustomer(sName);
121                    //add to customer catalog (base for queues)
122                    SMarket.getCustomers().add(Conversions.customerToCI((UCustomer)user), null);
123                    //add to customer statistics catalog
124                    SMarket.getCustomerStats().add(new CICustomerStats(user.getName()), null);
125                    break;
126                default: user = new UStaffer(sName, qualification);
127            }
128            UserManager.getGlobalUM().addUser(user);
129            return user;
130        }
131    
132        /**
133         * Checks the global UserManager for a String.
134         *
135         * @param userName the name of the user
136         * @return <code>true</code> if a User could be identified by <code>userName</code>, otherwise
137         * <code>false</code>.
138         */
139        public static boolean isUser(String userName) {
140            if(UserManager.getGlobalUM().getUser(userName)==null){
141                return false;
142            }
143            return true;
144        }
145    
146        /**
147         * Gets the {@link #ufCustomers customer filter}.
148         * @return the customer filter.
149         */
150        public UserFilter getCustomers() {
151            return ufCustomers;
152        }
153    
154        /**
155         * Gets the {@link #ufWarehouseWorker warehouse worker filter}.
156         * @return the warehouse worker filter.
157         */
158        public UserFilter getWarehouseWorker() {
159            return ufWarehouseWorker;
160        }
161    
162        /**
163         * Gets the {@link #ufSellers sellers filter}.
164         * @return the sellers filter.
165         */
166        public UserFilter getSeller() {
167            return ufSellers;
168        }
169    
170        /**
171         * Gets the {@link #ufManager mangaer filter}.
172         * @return the manager filter.
173         */
174        public UserFilter getManager() {
175            return ufManager;
176        }
177    
178        /**
179         * Gets the {@link #ufStaff staff filter}.
180         * @return the staff filter.
181         */
182        public UserFilter getStaff() {
183            return ufStaff;
184        }
185    
186        /**
187         * Counts and returns the number of Users that are managers.
188         * @return the number of the market's managers.
189         */
190        public static int getNumberOfManagers() {
191            return ((UMUserBase)getGlobalUM()).umfManager.getUsers().size();
192        }
193    
194        /**
195         * Checks whether there is a SalesPoint with the searched User logged on.
196         *
197         * @param user the searched user.
198         * @return <code>true</code> if the user is logged on on any open SalesPoint,
199         * otherwise <code>false</code>.
200         */
201        public static boolean isLoggedOn(User user){
202            Iterator it = Shop.getTheShop().getSalesPoints().iterator();
203            while(it.hasNext()){
204                if(((SalesPoint)it.next()).getUser()==user) return true;
205            }
206            return false;
207        }
208    
209        /**
210         * Removes a User from market.
211         *
212         * @param user the user to be removed.
213         * @throws VetoException if user to be removed is currently logged on.
214         */
215        public static void deleteUser(UPerson user) throws VetoException{
216            if(!isLoggedOn(user)){
217                String key = user.getName();
218                if(user instanceof UCustomer) {
219                    SMarket.getOffer().addStock(((UCustomer)user).getShoppingBasket(), null, true);
220                    SSListenable orderQueue = SMarket.getWarehouseQueue();
221                    SSListenable tillQueue = SMarket.getTillQueue();
222                    if(orderQueue.contains(key, null)) orderQueue.remove(key, null);
223                    Iterator it = tillQueue.get(key, null, false);
224                    while(it.hasNext()){
225                        if(((SICustomer)it.next()).getOrderCount(false)==0){
226                            tillQueue.remove(key, null);
227                        }
228                    }
229                    if(!tillQueue.contains(key, null)) SMarket.getCustomers().remove(key, null);
230                    SMarket.getCustomerStats().remove(key);
231                }
232                UserManager.getGlobalUM().deleteUser(key);
233            }
234            else {
235                throw new VetoException("Nutzer " + user.getFullName() + " ist noch am System angemeldet.");
236            }
237        }
238    
239        /**
240         * Sums up and returns wages of all currently employed persons.
241         * @return the sum of all employee's wages.
242         */
243        public int getCurrentWages() {
244            UserFilter staff = UMUserBase.getGlobalBase().getStaff();
245            Iterator it = new UserManagerFilter(staff).getUsers().iterator();
246            int sum = 0;
247            while (it.hasNext()) {
248                UStaffer us = (UStaffer)it.next();
249                sum += Conversions.valueToInt(us.getSalary());
250            }
251            return sum;
252        }
253    }
254