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