001: package com.ice.jcvsweb.bean;
002:
003: import java.io.File;
004: import java.io.FileWriter;
005: import java.io.IOException;
006: import java.io.PrintWriter;
007: import java.util.HashMap;
008: import java.util.Iterator;
009:
010: import org.w3c.dom.Document;
011: import org.w3c.dom.DocumentType;
012: import org.w3c.dom.Element;
013: import org.w3c.dom.Node;
014: import org.w3c.dom.NodeList;
015: import org.w3c.dom.Text;
016: import org.xml.sax.Attributes;
017: import org.xml.sax.SAXException;
018:
019: import com.ice.jcvsweb.helper.JavaHelper;
020: import com.ice.jcvsweb.helper.XMLHelper;
021:
022: /**
023: *
024: * @author Tim Endres, <a href="mailto:time@jcvs.org">time@jcvs.org</a>
025: * @see com.ice.cvsc
026: */
027:
028: public class JCVSUser implements Comparable {
029: /**
030: * The "typees" of users we recognize.
031: */
032: public static final String USER_TYPE_ADMIN_NM = "admin";
033: public static final String USER_TYPE_USER_NM = "user";
034: public static final String USER_TYPE_ANON_NM = "anon";
035:
036: public static final int USER_TYPE_ADMIN = 99;
037: public static final int USER_TYPE_USER = 10;
038: public static final int USER_TYPE_ANON = 1;
039:
040: /**
041: * We recognize several types of users.
042: */
043: private int type = USER_TYPE_ANON;
044:
045: /**
046: * The UNIQUE login name of the user.
047: */
048: private String name = null;
049:
050: /**
051: * The crypted user password.
052: */
053: private String password = null;
054:
055: /**
056: * The full name of the user.
057: */
058: private String fullName = null;
059:
060: /**
061: * The user's email address.
062: */
063: private String email = null;
064:
065: /**
066: * The user's IM service alias.
067: */
068: private String imService = null;
069:
070: /**
071: * The user's IM identity alias.
072: */
073: private String imAlias = null;
074:
075: /**
076: * The user's homepage URL.
077: */
078: private String homePage = null;
079:
080: /**
081: * The user's favorite webpage URL.
082: */
083: private String favPage = null;
084:
085: /**
086: * The user's favorite webpage URL.
087: */
088: private boolean isBlocked = false;
089:
090: /**
091: * The number of projects this user owns.
092: * NOTE This is strictly a temporary value filled in by certain
093: * commands to pass the value to the presentation layer. It
094: * it not normally filled in, and NEVER stored.
095: */
096: private int projectCount = 0;
097:
098: public JCVSUser() {
099: }
100:
101: public int compareTo(Object o) {
102: if (o instanceof JCVSUser) {
103: return this .getName().toLowerCase().compareTo(
104: ((JCVSUser) o).getName().toLowerCase());
105: }
106:
107: return 0;
108: }
109:
110: public boolean getIsAdmin() {
111: return (type == USER_TYPE_ADMIN);
112: }
113:
114: public boolean getIsGuest() {
115: return (type == USER_TYPE_ANON);
116: }
117:
118: public int getType() {
119: return this .type;
120: }
121:
122: public void setType(int type) {
123: this .type = type;
124: }
125:
126: public String getTypeName() {
127: return (this .type == USER_TYPE_ADMIN ? USER_TYPE_ADMIN_NM
128: : (this .type == USER_TYPE_USER ? USER_TYPE_USER_NM
129: : USER_TYPE_ANON_NM));
130: }
131:
132: public String getName() {
133: return this .name;
134: }
135:
136: public void setName(String name) {
137: this .name = name;
138: }
139:
140: public String getPassword() {
141: return this .password;
142: }
143:
144: public void setPassword(String password) {
145: this .password = password;
146: }
147:
148: public String getFullName() {
149: return this .fullName;
150: }
151:
152: public void setFullName(String fullName) {
153: this .fullName = fullName;
154: }
155:
156: public String getEmail() {
157: return this .email;
158: }
159:
160: public void setEmail(String email) {
161: this .email = email;
162: }
163:
164: public String getImService() {
165: return this .imService;
166: }
167:
168: public void setImService(String imService) {
169: this .imService = imService;
170: }
171:
172: public String getImAlias() {
173: return this .imAlias;
174: }
175:
176: public void setImAlias(String imAlias) {
177: this .imAlias = imAlias;
178: }
179:
180: public String getHomePage() {
181: return this .homePage;
182: }
183:
184: public void setHomePage(String homePage) {
185: this .homePage = homePage;
186: }
187:
188: public String getFavoritePage() {
189: return this .favPage;
190: }
191:
192: public void setFavoritePage(String favPage) {
193: this .favPage = favPage;
194: }
195:
196: public boolean getIsBlocked() {
197: return this .isBlocked;
198: }
199:
200: public void setIsBlocked(boolean isBlocked) {
201: this .isBlocked = isBlocked;
202: }
203:
204: public int getProjectCount() {
205: return this .projectCount;
206: }
207:
208: public void setProjectCount(int projectCount) {
209: this .projectCount = projectCount;
210: }
211:
212: public String toString() {
213: StringBuffer sBuf = new StringBuffer(128);
214:
215: sBuf.append("[ ");
216: sBuf.append(JavaHelper.getHashedName(this ));
217: sBuf.append(" ");
218: sBuf.append("type=").append(this .type).append(", ");
219: sBuf.append("name=").append(this .name).append(", ");
220: sBuf.append("isBlocked=").append(this .isBlocked).append(", ");
221: sBuf.append("fullName=").append(this .fullName).append(", ");
222: sBuf.append("email=").append(this .email).append(" ");
223: sBuf.append("imService=").append(this .imService).append(", ");
224: sBuf.append("imAlias=").append(this .imAlias).append(", ");
225: sBuf.append("homePage=").append(this .homePage).append(" ");
226: sBuf.append("favPage=").append(this .favPage).append(" ");
227: sBuf.append(" ]");
228:
229: return sBuf.toString();
230: }
231:
232: public void loadConfiguration(Node defNode) throws SAXException {
233: NodeList children = defNode.getChildNodes();
234: for (int ni = 0; ni < children.getLength(); ++ni) {
235: Node child = children.item(ni);
236: if (child != null
237: && child.getNodeType() == Node.ELEMENT_NODE) {
238: String name = child.getNodeName();
239: String value = XMLHelper
240: .getElementValue((Element) child);
241:
242: if ("type".equals(name)) {
243: if (USER_TYPE_ADMIN_NM.equals(value))
244: this .setType(USER_TYPE_ADMIN);
245: else if (USER_TYPE_USER_NM.equals(value))
246: this .setType(USER_TYPE_USER);
247: else if (USER_TYPE_ANON_NM.equals(value))
248: this .setType(USER_TYPE_ANON);
249: } else if ("name".equals(name)) {
250: this .setName(value);
251: } else if ("password".equals(name)) {
252: this .setPassword(value);
253: } else if ("email".equals(name)) {
254: this .setEmail(value);
255: } else if ("fullname".equals(name)) {
256: this .setFullName(value);
257: } else if ("imservice".equals(name)) {
258: this .setImService(value);
259: } else if ("imalias".equals(name)) {
260: this .setImAlias(value);
261: } else if ("homepage".equals(name)) {
262: this .setHomePage(value);
263: } else if ("favoritepage".equals(name)) {
264: this .setFavoritePage(value);
265: } else if ("blocked".equals(name)) {
266: this .setIsBlocked(value.equals("true"));
267: }
268: } else {
269: if (child.getNodeType() != Node.TEXT_NODE)
270: System.err.println("SKIP node type"
271: + " ["
272: + child.getNodeType()
273: + "] '"
274: + XMLHelper.getNodeTypeName(child
275: .getNodeType()) + "' name = '"
276: + child.getNodeName() + "'");
277: }
278: }
279:
280: // UNDONE HIGH
281: // if ( ! this.validate() )
282: // {
283: // Validate the localdir, cvsroot, etc...
284: // }
285: }
286:
287: public void saveConfiguration(File userFile) throws IOException {
288: // UNDONE MED
289: // This should really be DOM-based, but for now we cheat...
290: //
291: FileWriter fW = null;
292: PrintWriter pW = null;
293: try {
294: fW = new FileWriter(userFile);
295: pW = new PrintWriter(fW);
296:
297: pW.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
298: pW.println("<jcvs-user>");
299: pW.println("");
300:
301: pW.print(" <type>");
302: pW.print((this .type == USER_TYPE_ADMIN ? USER_TYPE_ADMIN_NM
303: : (this .type == USER_TYPE_USER ? USER_TYPE_USER_NM
304: : USER_TYPE_ANON_NM)));
305: pW.println("</type>");
306:
307: pW.print(" <blocked>");
308: pW.print(this .isBlocked);
309: pW.println("</blocked>");
310:
311: pW.print(" <name>");
312: pW.print(this .name);
313: pW.println("</name>");
314:
315: pW.print(" <password>");
316: pW.print(this .password);
317: pW.println("</password>");
318:
319: pW.print(" <fullname>");
320: pW.print(this .fullName);
321: pW.println("</fullname>");
322:
323: pW.print(" <email>");
324: pW.print(this .email);
325: pW.println("</email>");
326:
327: pW.print(" <imservice>");
328: pW.print(this .imService);
329: pW.println("</imservice>");
330:
331: pW.print(" <imalias>");
332: pW.print(this .imAlias);
333: pW.println("</imalias>");
334:
335: pW.print(" <homepage>");
336: pW.print(this .homePage);
337: pW.println("</homepage>");
338:
339: pW.print(" <favoritepage>");
340: pW.print(this .favPage);
341: pW.println("</favoritepage>");
342:
343: pW.println("");
344: pW.println("</jcvs-user>");
345: } finally {
346: if (pW != null)
347: pW.close();
348: }
349: }
350:
351: }
|