1 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 ; 35 import java.util.Iterator ; 36 import java.util.Map ; 37 38 44 public class UserSerializer extends SerializerSupport { 45 public final static String USER = "user"; 46 47 public final static String USER_NAME = "login"; 48 public final static String USER_PASSWORD = "passwd"; 49 public final static String USER_EMAIL = "email"; 50 public final static String USER_ROLES = "roles"; 51 public final static String USER_STATUS = "status"; 52 public final static String USER_CTIME = "cTime"; 53 public final static String USER_MTIME = "mTime"; 54 public final static String USER_LAST_ACCESS = "lastAccess"; 55 public final static String USER_LAST_LOGIN = "lastLogin"; 56 public final static String USER_LAST_LOGOUT = "lastLogout"; 57 public final static String USER_APPLICATION = "application"; 58 59 private static UserSerializer serializer = null; 60 61 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 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 102 public User deserialize(Element userEl, User user) { 103 Map snipMap = getElementMap(userEl); 104 return deserialize(snipMap, user); 105 } 106 107 public User deserialize(Map userMap, User user) { 108 Iterator elementIt = userMap.keySet().iterator(); 109 while (elementIt.hasNext()) { 110 String element = (String ) elementIt.next(); 111 String value = ""; 112 Object elementValue = userMap.get(element); 113 if (elementValue instanceof String ) { 114 value = notNull((String ) 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 createUserMap(User user) { 148 Map userMap = new HashMap (); 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 |