KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > sam > examples > data > User


1 /*
2  * Copyright (C) 2003 Stefan Armbruster [stefan@armbruster-it.de]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: User.java,v 1.2 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.contrib.sam.examples.data;
21
22 import org.enhydra.barracuda.contrib.sam.data.*;
23 import org.enhydra.barracuda.core.comp.*;
24 import java.util.*;
25 import org.apache.log4j.*;
26
27 /** User is a sample class for some data object. For demo purposes it stores all
28  * instances of itself in a list, normally we would use a database for this.
29  */

30 public class User implements DataObject {
31     protected static Map USERS = new HashMap();
32     protected static int maxOid = 0;
33
34     protected static Logger logger = Logger.getLogger(User.class.getName());
35
36     protected String JavaDoc oid;
37     protected String JavaDoc login;
38     protected String JavaDoc password;
39     protected String JavaDoc email;
40     protected List roles;
41
42     public User()
43     throws DataObjectException {
44         oid = Integer.toString(maxOid++);
45         logger.debug("consturcting new USER");
46     }
47
48     public Object JavaDoc get(String JavaDoc field)
49     throws DataObjectException {
50         field = field.toLowerCase();
51         if (field.equals("login")) return login;
52         if (field.equals("password")) return password;
53         if (field.equals("email")) return email;
54         if (field.equals("oid")) return oid;
55         if (field.equals("fk_roles")) return roles;
56         //if (field.equals("fk_serviceprovider")) return getFk_serviceprovider();
57
throw new DataObjectException("invalid field " + field);
58     }
59
60     public void set(String JavaDoc field, Object JavaDoc value)
61     throws DataObjectException {
62         field = field.toLowerCase();
63         if (field.equals("login")) { login = (String JavaDoc)value; return; }
64         if (field.equals("password")) { password = (String JavaDoc)value; return; }
65         if (field.equals("email")) { email=(String JavaDoc)value; return; }
66         if (field.equals("fk_roles")) {
67             if (value instanceof List) {
68                 roles = (List)value;
69             } else if (value instanceof String JavaDoc) {
70                 roles = new ArrayList();
71                 roles.add(value);
72             }
73             return;
74         }
75         if (field.equals("oid")) {
76             // oid is only set during construction, so we ignore this one
77
return;
78         }
79         throw new DataObjectException("invalid (oid is " + this.oid + ") field " + field + " " + value);
80     }
81
82
83     /**
84      * save a user
85      */

86     public void save(Object JavaDoc dbt)
87     throws DataObjectException {
88         USERS.put(this.oid, this);
89     }
90
91     /**
92      * Löscht den Benutzer aus der Datenbank
93      */

94     public void delete(Object JavaDoc dbt)
95     throws DataObjectException {
96         USERS.remove(this.oid);
97     }
98
99     public boolean equals(User cmp) {
100         return (this.oid == cmp.oid);
101     }
102
103     public static User getUser(String JavaDoc oid) throws DataObjectException {
104         User ret = (User) USERS.get(oid);
105         if (ret == null) {
106             ret = new User();
107         }
108         return ret;
109     }
110
111     public static Collection getAll() {
112         return USERS.values();
113     }
114
115     public static List getFormMapRoles() {
116         List result = new ArrayList();
117         result.add(new DefaultItemMap("1","Admin"));
118         result.add(new DefaultItemMap("2", "Poweruser"));
119         result.add(new DefaultItemMap("3", "regular User"));
120         result.add(new DefaultItemMap("4", "DAU"));
121         return result;
122     }
123
124
125 }
126
Popular Tags