1 18 23 24 package org.apache.roller.webservices.adminapi.sdk; 25 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.util.Date ; 29 import java.util.Locale ; 30 import java.util.TimeZone ; 31 import org.jdom.Document; 32 import org.jdom.Element; 33 import org.jdom.JDOMException; 34 import org.jdom.Text; 35 import org.jdom.input.SAXBuilder; 36 import org.apache.roller.webservices.adminapi.sdk.Entry.Attributes; 37 import org.apache.roller.webservices.adminapi.sdk.Entry.Types; 38 39 43 public class UserEntry extends Entry { 44 45 static interface Tags { 46 public static final String USER = "user"; 47 public static final String NAME = "name"; 48 public static final String FULL_NAME = "full-name"; 49 public static final String PASSWORD = "password"; 50 public static final String EMAIL_ADDRESS = "email-address"; 51 public static final String LOCALE = "locale"; 52 public static final String TIMEZONE = "timezone"; 53 public static final String DATE_CREATED = "date-created"; 54 } 55 56 private String name; 57 private String fullName; 58 private String password; 59 private Locale locale; 60 private TimeZone timezone; 61 private Date dateCreated; 62 private String emailAddress; 63 64 65 public UserEntry(String name, String urlPrefix) { 66 setName(name); 67 String href = urlPrefix + "/" + EntrySet.Types.USERS + "/" + name; 68 setHref(href); 69 } 70 71 72 public UserEntry(Element e, String urlPrefix) throws MissingElementException { 73 populate(e, urlPrefix); 74 } 75 76 public UserEntry(InputStream stream, String urlPrefix) throws JDOMException, IOException , MissingElementException { 77 SAXBuilder sb = new SAXBuilder(); 78 Document d = sb.build(stream); 79 Element e = d.detachRootElement(); 80 81 populate(e, urlPrefix); 82 } 83 84 private void populate(Element e, String urlPrefix) throws MissingElementException { 85 Element nameElement = e.getChild(Tags.NAME, NAMESPACE); 87 if (nameElement == null) { 88 throw new MissingElementException("ERROR: Missing element", e.getName(), Tags.NAME); 89 } 90 setName(nameElement.getText()); 91 92 String href = urlPrefix + "/" + EntrySet.Types.USERS + "/" + getName(); 94 setHref(href); 95 96 Element fullNameElement = e.getChild(Tags.FULL_NAME, NAMESPACE); 98 if (fullNameElement == null) { 99 throw new MissingElementException("ERROR: Missing element", e.getName(), Tags.FULL_NAME); 100 } 101 setFullName(fullNameElement.getText()); 102 103 Element passwordElement = e.getChild(Tags.PASSWORD, NAMESPACE); 106 if (passwordElement != null) { 107 setPassword(passwordElement.getText()); 108 } 109 110 Element localeElement = e.getChild(Tags.LOCALE, Service.NAMESPACE); 112 if (localeElement == null) { 113 throw new MissingElementException("ERROR: Missing element", e.getName(), Tags.LOCALE); 114 } 115 setLocale(localeElement.getText()); 116 117 Element tzElement = e.getChild(Tags.TIMEZONE, Service.NAMESPACE); 119 if (tzElement == null) { 120 throw new MissingElementException("ERROR: Missing element", e.getName(), Tags.TIMEZONE); 121 } 122 setTimezone(tzElement.getText()); 123 124 Element emailElement = e.getChild(Tags.EMAIL_ADDRESS, Service.NAMESPACE); 126 if (emailElement == null) { 127 throw new MissingElementException("ERROR: Missing element", e.getName(), Tags.EMAIL_ADDRESS); 128 } 129 setEmailAddress(emailElement.getText()); 130 131 Element createdElement = e.getChild(Tags.DATE_CREATED, Service.NAMESPACE); 133 if (createdElement != null) { 134 setDateCreated(new Date (Long.valueOf(createdElement.getText()).longValue())); 135 } 136 } 137 138 139 public String getType() { 140 return Types.USER; 141 } 142 143 public Document toDocument() { 144 Element userElement = new Element(Tags.USER, NAMESPACE); 145 Document doc = new Document(userElement); 146 147 String href = getHref(); 149 if (href != null) { 150 userElement.setAttribute(Attributes.HREF, href); 151 } 152 153 String name = getName(); 155 if (name != null) { 156 Element nameElement = new Element(Tags.NAME, Service.NAMESPACE); 157 Text nameText = new Text(getName()); 158 nameElement.addContent(nameText); 159 userElement.addContent(nameElement); 160 } 161 162 String fullName = getFullName(); 164 if (fullName != null) { 165 Element fullNameElement = new Element(Tags.FULL_NAME, NAMESPACE); 166 Text fullNameText = new Text(getFullName()); 167 fullNameElement.addContent(fullNameText); 168 userElement.addContent(fullNameElement); 169 } 170 171 String password = getPassword(); 173 if (password != null) { 174 Element passwordElement = new Element(Tags.PASSWORD, NAMESPACE); 175 Text passwordText = new Text(getPassword()); 176 passwordElement.addContent(passwordText); 177 userElement.addContent(passwordElement); 178 } 179 180 Locale locale = getLocale(); 182 if (locale != null ) { 183 Element localeElement = new Element(Tags.LOCALE, Service.NAMESPACE); 184 Text localeText = new Text(getLocale().toString()); 185 localeElement.addContent(localeText); 186 userElement.addContent(localeElement); 187 } 188 189 TimeZone timezone = getTimezone(); 191 if (timezone != null) { 192 Element timezoneElement = new Element(Tags.TIMEZONE, Service.NAMESPACE); 193 Text timezoneText = new Text(timezone.getID()); 194 timezoneElement.addContent(timezoneText); 195 userElement.addContent(timezoneElement); 196 } 197 198 String emailAddress = getEmailAddress(); 200 if (emailAddress != null) { 201 Element emailAddressElement = new Element(Tags.EMAIL_ADDRESS, Service.NAMESPACE); 202 Text emailAddressText = new Text(String.valueOf(emailAddress)); 203 emailAddressElement.addContent(emailAddressText); 204 userElement.addContent(emailAddressElement); 205 } 206 207 Date datedCreated = getDateCreated(); 209 if (dateCreated != null) { 210 Element dateCreatedElement = new Element(Tags.DATE_CREATED, Service.NAMESPACE); 211 Text dateCreatedText = new Text(String.valueOf(dateCreated.getTime())); 212 dateCreatedElement.addContent(dateCreatedText); 213 userElement.addContent(dateCreatedElement); 214 } 215 216 return doc; 217 } 218 219 220 public String getName() { 221 return name; 222 } 223 224 225 public void setName(String name) { 226 this.name = name; 227 } 228 229 230 public String getFullName() { 231 return fullName; 232 } 233 234 235 public void setFullName(String fullName) { 236 this.fullName = fullName; 237 } 238 239 240 public String getPassword() { 241 return password; 242 } 243 244 245 public void setPassword(String password) { 246 this.password = password; 247 } 248 249 250 public Locale getLocale() { 251 return locale; 252 } 253 254 255 public void setLocale(Locale locale) { 256 this.locale = locale; 257 } 258 259 260 public void setLocale(String localeString) { 261 this.locale = new LocaleString(localeString).getLocale(); 262 } 263 264 265 public TimeZone getTimezone() { 266 return timezone; 267 } 268 269 270 public void setTimezone(TimeZone timezone) { 271 this.timezone = timezone; 272 } 273 274 275 public void setTimezone(String timezoneString) { 276 this.timezone = TimeZone.getTimeZone(timezoneString); 277 } 278 279 280 public Date getDateCreated() { 281 return dateCreated; 282 } 283 284 285 public void setDateCreated(Date dateCreated) { 286 this.dateCreated = dateCreated; 287 } 288 289 290 public String getEmailAddress() { 291 return emailAddress; 292 } 293 294 295 public void setEmailAddress(String emailAddress) { 296 this.emailAddress = emailAddress; 297 } 298 299 300 public boolean equals(Object o) { 301 if ( o == null || o.getClass() != this.getClass()) { 302 return false; 303 } 304 305 UserEntry other = (UserEntry)o; 306 307 if (!areEqual(getEmailAddress(), other.getEmailAddress())) { 308 return false; 309 } 310 if (!areEqual(getFullName(), other.getFullName())) { 311 return false; 312 } 313 if (!areEqual(getLocale(), other.getLocale())) { 314 return false; 315 } 316 if (!areEqual(getName(), other.getName())) { 317 return false; 318 } 319 if (!areEqual(getTimezone(), other.getTimezone())) { 320 return false; 321 } 322 323 return super.equals(o); 324 } 325 326 } 327 | Popular Tags |