KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > snip > storage > UserSerializer


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002,2003 Fraunhofer Gesellschaft
5  * Fraunhofer Institut for Computer Architecture and Software Technology
6  * All Rights Reserved.
7  *
8  * Please visit http://snipsnap.org/ for updates and contact.
9  *
10  * --LICENSE NOTICE--
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  * --LICENSE NOTICE--
25  */

26 package org.snipsnap.snip.storage;
27
28 import org.dom4j.DocumentHelper;
29 import org.dom4j.Element;
30 import org.radeox.util.logging.Logger;
31 import org.snipsnap.user.Roles;
32 import org.snipsnap.user.User;
33
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.Map JavaDoc;
37
38 /**
39  * A user serializer that can store and load users in XML format.
40  *
41  * @author Matthias L. Jugel
42  * @version $Id: UserSerializer.java 1606 2004-05-17 10:56:18Z leo $
43  */

44 public class UserSerializer extends SerializerSupport {
45   public final static String JavaDoc USER = "user";
46
47   public final static String JavaDoc USER_NAME = "login";
48   public final static String JavaDoc USER_PASSWORD = "passwd";
49   public final static String JavaDoc USER_EMAIL = "email";
50   public final static String JavaDoc USER_ROLES = "roles";
51   public final static String JavaDoc USER_STATUS = "status";
52   public final static String JavaDoc USER_CTIME = "cTime";
53   public final static String JavaDoc USER_MTIME = "mTime";
54   public final static String JavaDoc USER_LAST_ACCESS = "lastAccess";
55   public final static String JavaDoc USER_LAST_LOGIN = "lastLogin";
56   public final static String JavaDoc USER_LAST_LOGOUT = "lastLogout";
57   public final static String JavaDoc USER_APPLICATION = "application";
58
59   private static UserSerializer serializer = null;
60
61   /**
62    * Get an instance of the user serializer.
63    * @return the serializer
64    */

65   public synchronized static UserSerializer getInstance() {
66     if (null == serializer) {
67       serializer = new UserSerializer();
68     }
69     return serializer;
70   }
71
72   protected UserSerializer() {
73   }
74
75   /**
76    * Store a snip in an XML node.
77    * @param user the user to store
78    * @return the serialized user as XML
79    */

80   public Element serialize(User user) {
81     Element userElement = DocumentHelper.createElement(USER);
82     userElement.addElement(USER_NAME).addText(user.getLogin());
83     userElement.addElement(USER_PASSWORD).addText(notNull(user.getPasswd()));
84     userElement.addElement(USER_EMAIL).addText(notNull(user.getEmail()));
85     userElement.addElement(USER_ROLES).addText(user.getRoles().toString());
86     userElement.addElement(USER_STATUS).addText(notNull(user.getStatus()));
87     userElement.addElement(USER_CTIME).addText(getStringTimestamp(user.getCTime()));
88     userElement.addElement(USER_MTIME).addText(getStringTimestamp(user.getMTime()));
89     userElement.addElement(USER_LAST_ACCESS).addText(getStringTimestamp(user.getLastAccess()));
90     userElement.addElement(USER_LAST_LOGIN).addText(getStringTimestamp(user.getLastLogin()));
91     userElement.addElement(USER_LAST_LOGOUT).addText(getStringTimestamp(user.getLastLogout()));
92     userElement.addElement(USER_APPLICATION).addText(notNull(user.getApplication()));
93
94     return userElement;
95   }
96
97   /**
98    * Load user from XML serialized file.
99    * @param userEl the XML node containing the user
100    * @return the modified user
101    */

102   public User deserialize(Element userEl, User user) {
103     Map JavaDoc snipMap = getElementMap(userEl);
104     return deserialize(snipMap, user);
105   }
106
107   public User deserialize(Map JavaDoc userMap, User user) {
108     Iterator JavaDoc elementIt = userMap.keySet().iterator();
109     while (elementIt.hasNext()) {
110       String JavaDoc element = (String JavaDoc) elementIt.next();
111       String JavaDoc value = "";
112       Object JavaDoc elementValue = userMap.get(element);
113       if (elementValue instanceof String JavaDoc) {
114         value = notNull((String JavaDoc) userMap.get(element));
115       }
116
117       if(USER_NAME.equals(element)) {
118         user.setLogin(value);
119       } else if(USER_PASSWORD.equals(element)) {
120         user.setPasswd(notNull(value));
121       } else if(USER_EMAIL.equals(element)) {
122         user.setEmail(notNull(value));
123       } else if(USER_ROLES.equals(element)) {
124         user.setRoles(new Roles(value));
125       } else if(USER_STATUS.equals(element)) {
126         user.setStatus(notNull(value));
127       } else if(USER_CTIME.equals(element)) {
128         user.setCTime(getTimestamp(value));
129       } else if(USER_MTIME.equals(element)) {
130         user.setMTime(getTimestamp(value));
131       } else if(USER_LAST_ACCESS.equals(element)) {
132         user.setLastAccess(getTimestamp(value));
133       } else if(USER_LAST_LOGIN.equals(element)) {
134         user.setLastLogin(getTimestamp(value));
135       } else if(USER_LAST_LOGOUT.equals(element)) {
136         user.setLastLogout(getTimestamp(value));
137       } else if(USER_APPLICATION.equals(element)) {
138         user.setApplication(value);
139       } else {
140         Logger.warn("unknown entry in serialized user: " + element + "='" + value + "'");
141       }
142     }
143
144     return user;
145   }
146
147   public Map JavaDoc createUserMap(User user) {
148     Map JavaDoc userMap = new HashMap JavaDoc();
149     userMap.put(USER_NAME, user.getLogin());
150     userMap.put(USER_PASSWORD, notNull(user.getPasswd()));
151     userMap.put(USER_EMAIL, notNull(user.getEmail()));
152     userMap.put(USER_STATUS, notNull(user.getStatus()));
153     userMap.put(USER_ROLES, user.getRoles().toString());
154     userMap.put(USER_CTIME, getStringTimestamp(user.getCTime()));
155     userMap.put(USER_MTIME, getStringTimestamp(user.getMTime()));
156     userMap.put(USER_LAST_LOGIN, getStringTimestamp(user.getLastLogin()));
157     userMap.put(USER_LAST_ACCESS, getStringTimestamp(user.getLastAccess()));
158     userMap.put(USER_LAST_LOGOUT, getStringTimestamp(user.getLastLogout()));
159     userMap.put(USER_APPLICATION, notNull(user.getApplication()));
160     return userMap;
161   }
162 }
163
Popular Tags