Source code: org/mitre/cvw/CVWUser.java
1 /*
2 * Copyright (c) 1996-2000. The MITRE Corporation (http://www.mitre.org/).
3 * All rights reserved.
4 * CVW comes with ABSOLUTELY NO WARRANTY. See license for details.
5 */
6
7 package org.mitre.cvw;
8
9 import java.awt.Image;
10 import java.util.*;
11
12 import java.awt.Frame;
13
14 import org.mitre.tjt.FrameClient;
15 import org.mitre.tjt.NoSuchAttributeException;
16
17 /**
18 * This class represents the user on the CVW server.
19 * @version
20 * @author Deb Ercolini
21 */
22 public class CVWUser extends CVWObject implements FrameClient {
23 String fullName;
24 String empID;
25 String office;
26 String phone;
27 String email;
28 String gender;
29 private CVWServer server;
30
31 private int quota = 10; //some number other than 0
32
33 static Image unknownImage;
34 private static CVWUser currentUser;
35
36 static void setCurrentUser(CVWUser user) {
37 currentUser = user;
38 debug("Set current user to: " + user.name);
39 }
40
41 public static CVWUser getCurrentUser() {
42 return currentUser;
43 }
44
45 public static String getFedUserName() {
46 if (currentUser != null) {
47 return currentUser.getName() + "@" + CVWServerList.getLocalServer();
48 } else {
49 return "unknown@" + CVWServerList.getLocalServer();
50 }
51 }
52
53 public static Image getUnknownImage() {
54 if (unknownImage == null) {
55 unknownImage = CVWCoordinator.getInstance().getImageFromFile("images/UnknownUser.gif");
56 }
57 return unknownImage;
58 }
59
60 /**
61 * Converts seconds to the standard CVW way of displaying user idle time with
62 * the default level of high detail.
63 *
64 * @param secs the secons the user has been idle
65 * @see #convIdleSecsToEnglish(int secs, boolean detail)
66 */
67 public static String convIdleSecsToEnglish(int secs) {
68 return convIdleSecsToEnglish(secs, true);
69 }
70
71 /**
72 * Converts seconds to the standard CVW way of displaying user idle time
73 * detail depending on the boolean value passed in.
74 *
75 * @param secs the secons the user has been idle
76 * @param detail <code>true</code> return detail idle time, <code>false</code> return
77 * abbreviated time
78 * @see #convIdleSecsToEnglish(int secs)
79 */
80 public static String convIdleSecsToEnglish(int secs, boolean detail) {
81 int mins, hours, days;
82 String secsv, daysv, hoursv, minsv, timestr;
83
84 if (secs == 0)
85 return "active";
86
87 timestr = "";
88
89 mins = secs/60;
90 hours = mins/60;
91 days = hours/24;
92
93 hours = hours-(days*24);
94 mins = mins-(hours*60);
95 mins = mins-(days*24*60);
96
97 if (secs != 1) {secsv = " secs ";} else {secsv = " sec ";}
98 if (days > 1) {daysv = " days ";} else {daysv = " day ";}
99 if (hours > 1) {hoursv = " hrs ";} else {hoursv = " hr ";}
100 if (mins > 1) {minsv = " mins ";} else {minsv = " min ";}
101
102 if (days > 0) {
103 timestr = new String(timestr + days + daysv);
104 if (detail && hours > 0)
105 timestr = new String(timestr + hours + hoursv);
106 } else {
107 if (hours > 0)
108 timestr = new String(timestr + hours + hoursv);
109 if (mins > 0 && (detail || timestr.length() == 0))
110 timestr = new String(timestr + mins + minsv);
111 }
112
113 if (timestr.equals(""))
114 timestr = new String(secs + secsv);
115
116 return timestr.trim();
117 }
118
119 /**
120 * Constructor
121 */
122 CVWUser() {
123 super(CVWObject.USER);
124 server = CVWServerList.getLocalServer();
125 if (server != null)
126 debug("1Made user: " + this.name + " for server: " + server.toString() + " obj: " + server.toString());
127 else
128 debug("1Made user: " + this.name + " with null server");
129 }
130
131 /**
132 * Constructor
133 */
134 CVWUser(CVWServer serv) {
135 super(CVWObject.USER);
136 server = serv;
137 debug("2Made user: " + this.name + " for server: " + server.toString() + " obj: " + server.toString());
138 }
139
140 /**
141 * Constructor
142 *
143 * @param type value, either USER or PROXY
144 */
145 CVWUser(int type) {
146 super((type == USER || type == PROXY) ? type : PROXY);
147 server = CVWServerList.getLocalServer();
148 debug("3Made user: " + this.name + " for server: " + server.toString() + " obj: " + server.toString());
149 }
150
151 /**
152 * Constructor
153 *
154 * @param type the type of this object as a String as defined by the CVW server
155 */
156 CVWUser(String type) {
157 super(USER);
158 server = CVWServerList.getLocalServer();
159 debug("4Made user: " + this.name + " for server: " + server.toString() + " obj: " + server.toString());
160 }
161
162
163 /**
164 * prints debugging lines if debugging is on
165 * @param message the line to print
166 */
167 private static void debug(String message) {
168 if(CVWCoordinator.DEBUG) System.err.println(message);
169 }
170
171
172 /**
173 * Sets the raw detail name which is the full name of the user
174 * @param detailName the full name of the user
175 */
176 public void setRawDetailName(String detailName) {
177 fullName = detailName;
178 }
179
180 /**
181 * Returns the full name of the user
182 * @return the full name of the user
183 */
184 public String getRawDetailName() {
185 return fullName;
186 }
187
188 /**
189 * Returns the full name of the user in the following format <br>
190 * Lastname Suffix, Prefix Firstname Middle.
191 * 7/1/97
192 * @return
193 */
194 public String getDetailName() {
195 //System.err.println("in getDetailName");
196 //System.err.println("fullName is "+fullName);
197 if (fullName == null || fullName == "") {
198 return "null";
199 }
200
201 StringTokenizer st = new StringTokenizer(fullName);
202 int tot = st.countTokens();
203
204 if (tot == 0) {
205 return "Full Name Unknown";
206 }
207
208 Vector v = new Vector(tot);
209 while (st.hasMoreTokens())
210 v.addElement(st.nextToken());
211 String lastName = (String)v.lastElement();
212 if (lastName.equals("II") || lastName.equals("III") ||
213 lastName.equals("IV") || lastName.equals("V") ||
214 lastName.equals("Jr") || lastName.equals("Jr.") ||
215 lastName.equals("Sr") || lastName.equals("Sr.")) {
216 lastName = (String)v.lastElement();
217 v.removeElementAt(v.size()-1);
218 lastName = (String)v.lastElement() + " " + lastName;
219 }
220 else {
221 lastName = (String)v.lastElement();
222 }
223 v.removeElementAt(v.size()-1);
224
225 String firstName = new String();;
226 for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {
227 firstName = firstName + e.nextElement() + " ";
228 }
229 //System.err.println("full " + fullName);
230 //System.err.println("new " + lastName + ", " + firstName);
231 return lastName + ", " + firstName;
232 }
233
234 /**
235 * Returns the gif image filename for this user.
236 * The CVW server sends "user/empNo", strip off user and append .gif
237 * 9/28/98 dage
238 * @return the gif filename for this user
239 */
240 public String getIcon() {
241 if (icon == null)
242 return null;
243 String tmp;
244 int ind = icon.indexOf("user/");
245 //System.err.println(ind);
246 if (ind == -1)
247 tmp = icon;
248 else
249 tmp = icon.substring(5);
250 //System.err.println(tmp);
251 if (tmp.length() == 0) return null;
252 return tmp + ".gif";
253 }
254
255 /**
256 * Returns the image of this object.
257 * @return the image of this object
258 */
259 public Image getImage() {
260 String i = getIcon();
261 if (i != null) {
262 Image img = CVWCoordinator.getInstance().getUserImage(i);
263 if (img != null)
264 return img;
265 }
266 return getUnknownImage();
267 }
268
269 /**
270 * Returns the email address of the user.
271 * 8/19/98 - dage
272 * @return the email address of the user
273 */
274 public String getEmail() {
275 return email;
276 }
277
278 /**
279 * Sets the email address of the user
280 * @param e the email address of the user
281 */
282 public void setEmail(String e) {
283 email = e;
284 }
285
286 /**
287 * Returns the quota of this user
288 * @return the quota of this user
289 */
290 public int getQuota() {
291 return quota;
292 }
293
294 /**
295 * Get the server this user resides on
296 *
297 * @return The user's server
298 */
299 public CVWServer getServer() {
300 return server;
301 }
302
303 /**
304 * Set the user's server
305 *
306 * @param server The server the user resides on
307 */
308 public void setServer(CVWServer server) {
309 this.server = server;
310 }
311
312 /**
313 * Sets the quota of this user
314 * @param e the quota of this user
315 */
316 public void setQuota(String num) {
317 if (num == null) return;
318 try {
319 quota = Integer.parseInt(num);
320 } catch (Exception e) {
321 System.err.println("Can't parse quota value: " + num + "(" + e + ")");
322 }
323 }
324
325 /**
326 * Starts opening the get info on user by requesting detail info.
327 * <br> MCP send cvw-object-detail-request
328 * @param type not applicable to users
329 */
330 public void startInfoDialog(int type) {
331 if (WindowMgr.getWindowMgr().objectWindowExists("org.mitre.cvw.UserDescDialog", objNum))
332 return;
333
334 // determine if we are looking at someone on the local server or remote
335 CVWServerList sl = CVWServerList.getCVWServerList();
336 String commandSuffix = "";
337 System.out.println("User's server is " + server);
338
339 //users in list all users are not in the cache ;-)
340 CVWObject obj = CVWCache.getInstance().get(objNum);
341 if (obj == null)
342 CVWServerComm.sendMCPCmdToServer("#$#cvw-object-info-request",
343 "object: " + objNum.strValue() + commandSuffix);
344
345 if(CVWCoordinator.DEBUG) System.err.println("GAB - get detail info.");
346 CVWServerComm.sendMCPCmdToServer("#$#cvw-object-detail-request",
347 "object: " + objNum.strValue() + commandSuffix);
348 }
349
350
351 /**
352 * Opens the Information dialog box for this user.
353 * <br> MCP receive cvw-object-detail
354 *
355 * @see org.mitre.jcvw.UserDescDialog
356 */
357 public void getInfo() {
358 Image image = getImage(); //jcvw.getUserImage(getIcon());
359
360 boolean editable = (jcvw.currentUser == this);
361 // in future check to see if current user is admin
362 if (desc == null) {
363 //System.err.println("desc" + desc + "end");
364 desc = "";
365 }
366
367 UserDescDialog diag = new UserDescDialog(this, name, image, desc, fullName,
368 empID, office, phone, email, editable);
369 diag.set("objNumber", objNum.strValue());
370 WindowMgr.getWindowMgr().addObjectWindow(WindowMgr.OBJECT, diag, objNum);
371
372 java.awt.Point p = CVWCoordinator.getJCVWCenter();
373 java.awt.Rectangle r = new java.awt.Rectangle(p.x+20, p.y+40, diag.getSize().width, diag.getSize().width);
374 r = CVWCoordinator.checkWindowMeasurements(r);
375 diag.setLocation(r.x, r.y);
376
377 diag.setVisible(true);
378 diag.requestFocus();
379 }
380
381 /* 3/25/98 dage - call this.objectModify rather than changeUser in CVWCoordinator
382 */
383 /**
384 * Process the closing of the UserDescDialog, if the user pushed Save, send the
385 * new info to the CVW server
386 * <br> MCP send cvw-modify-metadata
387 *
388 * @param f the instance of UserDescDialog
389 * @see org.mitre.tjt.NotePad
390 */
391 public void frameDismissed(Frame f) {
392 if (f instanceof UserDescDialog) {
393 UserDescDialog user = (UserDescDialog)f;
394 if(!user.wasDismissed()) // need to save info
395 try {
396 //user.get("objNumber");
397 objectModify((String)user.get("fullName"),
398 (String)user.get("phone"),
399 (String)user.get("description"),
400 (String)user.get("email"),
401 (String)user.get("office"),
402 (String)user.get("employeeId"));
403 }
404 catch(NoSuchAttributeException e) {
405 System.err.println("Invalid Attribute: " + e);
406 }
407 catch(Exception gene) {
408 System.err.println("gen : " + gene);
409 }
410 }
411 else
412 if (CVWCoordinator.DEBUG) System.err.println("Not instance of UserDescDialog " + f);
413 }
414
415 /**
416 * Starts building the MCP for notifying the server of any change to the information
417 * of this object. Only sends the data of the attributes that have changed.
418 * 3/25/98 dage
419 *
420 * @param newName name from the get info dialog box
421 * @param newFName full name from the get info dialog box
422 * @param newPhone phone from the get info dialog box
423 * @param newDesc description from the get info dialog box
424 * @param newEmail email address from the get info dialog box
425 * @param newOffice office from the get info dialog box
426 * @param newEmpID employee id from the get info dialog box
427 */
428 public void objectModify(String newFName, String newPhone, String newDesc, String newEmail, String newOffice, String newEmpID ) {
429 String param = "";
430
431 if (!desc.equals(newDesc)) {
432 param = " description: " + prepTextForServer(newDesc);
433 desc = newDesc;
434 }
435 if (!newFName.equals(fullName)) {
436 param = param + " full_name: " + prepTextForServer(newFName);
437 fullName = newFName;
438 }
439 if (!newOffice.equals(office)) {
440 param = param + " office: " + prepTextForServer(newOffice);
441 office = newOffice;
442 }
443 if (!phone.equals(newPhone)) {
444 param = param + " phone: " + prepTextForServer(newPhone);
445 phone = newPhone;
446 }
447 if (!newEmpID.equals(empID)) {
448 param = param + " badge: " + prepTextForServer(newEmpID);
449 empID = newEmpID;
450 }
451 if (!newEmail.equals(email)) {
452 param = param + " email_address: " + prepTextForServer(newEmail);
453 email = newEmail;
454 }
455 objectModify(param);
456 }
457
458 /**
459 * Sets the full name associated with a user.
460 * <br> MCP receive cvw-object-detail
461 *
462 * @param fName full name for this user
463 */
464 public void setDetailInfo(String fName) {
465 fullName = fName;
466 }
467
468 /**
469 * Sets the detail information associated with a user.
470 * <br> MCP receive cvw-object-detail
471 *
472 * @param fName full name for this user
473 * @param emp employee id for this user
474 * @param o office for this user
475 * @param ph phone for this user
476 * @param d description for this user
477 * @param em email address for this user
478 * @param g gender for this user
479 */
480 public void setDetailInfo(String fName, String emp, String o, String ph,
481 String d, String em, String g) {
482 fullName = fName;
483 empID = emp;
484 office = o;
485 phone = ph;
486 desc = d;
487 email = em;
488 gender = g;
489 }
490
491 /**
492 * Returns a string representing the state of this component.
493 *
494 * @return a string representation of this component's state.
495 */
496 public String paramString() {
497 return (super.paramString() +
498 ",fullName=" + fullName +
499 ",empID=" + empID +
500 ",office=" + office +
501 ",phone=" + phone +
502 ",email=" + email +
503 ",gender=" + gender +
504 ",server=" + server);
505 }
506
507 /**
508 * Returns a string representation of this component and its values.
509 * @return a string representation of this component.
510 */
511 public String toString() {
512 return(getClass().getName() + "[" + paramString() + "]" );
513 }
514
515 }
516