001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018: package org.apache.roller.webservices.adminapi.sdk;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.util.Date;
023: import java.util.Locale;
024: import java.util.TimeZone;
025: import org.jdom.Document;
026: import org.jdom.Element;
027: import org.jdom.JDOMException;
028: import org.jdom.Text;
029: import org.jdom.input.SAXBuilder;
030: import org.apache.roller.webservices.adminapi.sdk.Entry.Attributes;
031: import org.apache.roller.webservices.adminapi.sdk.Entry.Types;
032:
033: /**
034: * This class describes a user entry; a user weblog resource.
035: * @author jtb
036: */
037: public class UserEntry extends Entry {
038: /** XML tags that define a user entry. */
039: static interface Tags {
040: public static final String USER = "user";
041: public static final String NAME = "name";
042: public static final String FULL_NAME = "full-name";
043: public static final String PASSWORD = "password";
044: public static final String EMAIL_ADDRESS = "email-address";
045: public static final String LOCALE = "locale";
046: public static final String TIMEZONE = "timezone";
047: public static final String DATE_CREATED = "date-created";
048: public static final String ENABLED = "enabled";
049: }
050:
051: private String name;
052: private String fullName;
053: private String password;
054: private Locale locale;
055: private TimeZone timezone;
056: private Date dateCreated;
057: private String emailAddress;
058: private Boolean enabled;
059:
060: /** Construct an empty user entry */
061: public UserEntry(String name, String urlPrefix) {
062: setName(name);
063: String href = urlPrefix + "/" + EntrySet.Types.USERS + "/"
064: + name;
065: setHref(href);
066: }
067:
068: /** Construct a user entry from a JDOM element. */
069: public UserEntry(Element e, String urlPrefix) {
070: populate(e, urlPrefix);
071: }
072:
073: public UserEntry(InputStream stream, String urlPrefix)
074: throws JDOMException, IOException {
075: SAXBuilder sb = new SAXBuilder();
076: Document d = sb.build(stream);
077: Element e = d.detachRootElement();
078:
079: populate(e, urlPrefix);
080: }
081:
082: private void populate(Element e, String urlPrefix) {
083: // name (required)
084: Element nameElement = e.getChild(Tags.NAME, NAMESPACE);
085: if (nameElement != null) {
086: setName(nameElement.getText());
087: }
088:
089: // href
090: String href = urlPrefix + "/" + EntrySet.Types.USERS + "/"
091: + getName();
092: setHref(href);
093:
094: // full name
095: Element fullNameElement = e.getChild(Tags.FULL_NAME, NAMESPACE);
096: if (fullNameElement != null) {
097: setFullName(fullNameElement.getText());
098: }
099:
100: // password
101: Element passwordElement = e.getChild(Tags.PASSWORD, NAMESPACE);
102: if (passwordElement != null) {
103: setPassword(passwordElement.getText());
104: }
105:
106: // locale
107: Element localeElement = e.getChild(Tags.LOCALE,
108: Service.NAMESPACE);
109: if (localeElement != null) {
110: setLocale(localeElement.getText());
111: }
112:
113: // timezone
114: Element tzElement = e
115: .getChild(Tags.TIMEZONE, Service.NAMESPACE);
116: if (tzElement != null) {
117: setTimezone(tzElement.getText());
118: }
119:
120: // email address
121: Element emailElement = e.getChild(Tags.EMAIL_ADDRESS,
122: Service.NAMESPACE);
123: if (emailElement != null) {
124: setEmailAddress(emailElement.getText());
125: }
126:
127: // created (optional)
128: Element createdElement = e.getChild(Tags.DATE_CREATED,
129: Service.NAMESPACE);
130: if (createdElement != null) {
131: setDateCreated(new Date(Long.valueOf(
132: createdElement.getText()).longValue()));
133: }
134:
135: // enabled
136: Element enabledElement = e.getChild(Tags.ENABLED,
137: Service.NAMESPACE);
138: if (enabledElement != null) {
139: setEnabled(Boolean.valueOf(enabledElement.getText()));
140: }
141: }
142:
143: public String getType() {
144: return Types.USER;
145: }
146:
147: public Document toDocument() {
148: Element userElement = new Element(Tags.USER, NAMESPACE);
149: Document doc = new Document(userElement);
150:
151: // href
152: String href = getHref();
153: if (href != null) {
154: userElement.setAttribute(Attributes.HREF, href);
155: }
156:
157: // name
158: String name = getName();
159: if (name != null) {
160: Element nameElement = new Element(Tags.NAME,
161: Service.NAMESPACE);
162: Text nameText = new Text(name);
163: nameElement.addContent(nameText);
164: userElement.addContent(nameElement);
165: }
166:
167: // full name
168: String fullName = getFullName();
169: if (fullName != null) {
170: Element fullNameElement = new Element(Tags.FULL_NAME,
171: NAMESPACE);
172: Text fullNameText = new Text(fullName);
173: fullNameElement.addContent(fullNameText);
174: userElement.addContent(fullNameElement);
175: }
176:
177: // password
178: String password = getPassword();
179: if (password != null) {
180: Element passwordElement = new Element(Tags.PASSWORD,
181: NAMESPACE);
182: Text passwordText = new Text(password);
183: passwordElement.addContent(passwordText);
184: userElement.addContent(passwordElement);
185: }
186:
187: // locale
188: Locale locale = getLocale();
189: if (locale != null) {
190: Element localeElement = new Element(Tags.LOCALE,
191: Service.NAMESPACE);
192: Text localeText = new Text(getLocale().toString());
193: localeElement.addContent(localeText);
194: userElement.addContent(localeElement);
195: }
196:
197: // timezone
198: TimeZone timezone = getTimezone();
199: if (timezone != null) {
200: Element timezoneElement = new Element(Tags.TIMEZONE,
201: Service.NAMESPACE);
202: Text timezoneText = new Text(timezone.getID());
203: timezoneElement.addContent(timezoneText);
204: userElement.addContent(timezoneElement);
205: }
206:
207: // email address
208: String emailAddress = getEmailAddress();
209: if (emailAddress != null) {
210: Element emailAddressElement = new Element(
211: Tags.EMAIL_ADDRESS, Service.NAMESPACE);
212: Text emailAddressText = new Text(emailAddress);
213: emailAddressElement.addContent(emailAddressText);
214: userElement.addContent(emailAddressElement);
215: }
216:
217: // creation date
218: Date datedCreated = getDateCreated();
219: if (dateCreated != null) {
220: Element dateCreatedElement = new Element(Tags.DATE_CREATED,
221: Service.NAMESPACE);
222: Text dateCreatedText = new Text(String.valueOf(dateCreated
223: .getTime()));
224: dateCreatedElement.addContent(dateCreatedText);
225: userElement.addContent(dateCreatedElement);
226: }
227:
228: // enabled
229: Boolean enabled = getEnabled();
230: if (enabled != null) {
231: Element enabledElement = new Element(Tags.ENABLED,
232: NAMESPACE);
233: Text enabledText = new Text(enabled.toString());
234: enabledElement.addContent(enabledText);
235: userElement.addContent(enabledElement);
236: }
237:
238: return doc;
239: }
240:
241: /** Get the user name of this user entry. */
242: public String getName() {
243: return name;
244: }
245:
246: /** Set of the user name of this user entry. */
247: public void setName(String name) {
248: this .name = name;
249: }
250:
251: /** Get the full name of this user entry. */
252: public String getFullName() {
253: return fullName;
254: }
255:
256: /** Set the full name of this user entry. */
257: public void setFullName(String fullName) {
258: this .fullName = fullName;
259: }
260:
261: /** Get the password of this user entry. */
262: public String getPassword() {
263: return password;
264: }
265:
266: /** Set the password of this user entry. */
267: public void setPassword(String password) {
268: this .password = password;
269: }
270:
271: /** Get the locale string of this user entry. */
272: public Locale getLocale() {
273: return locale;
274: }
275:
276: /** Set the locale of this user entry. */
277: public void setLocale(Locale locale) {
278: this .locale = locale;
279: }
280:
281: /** Set the locale string of this user entry. */
282: public void setLocale(String localeString) {
283: this .locale = new LocaleString(localeString).getLocale();
284: }
285:
286: /** Get the timezone string of this user entry. */
287: public TimeZone getTimezone() {
288: return timezone;
289: }
290:
291: /** Set the timezone of this user entry. */
292: public void setTimezone(TimeZone timezone) {
293: this .timezone = timezone;
294: }
295:
296: /** Set the timezone string of this user entry. */
297: public void setTimezone(String timezoneString) {
298: this .timezone = TimeZone.getTimeZone(timezoneString);
299: }
300:
301: /** Get the date created of this user entry. */
302: public Date getDateCreated() {
303: return dateCreated;
304: }
305:
306: /** Set the date created of this user entry. */
307: public void setDateCreated(Date dateCreated) {
308: this .dateCreated = dateCreated;
309: }
310:
311: /** Get the email address of this user entry. */
312: public String getEmailAddress() {
313: return emailAddress;
314: }
315:
316: /** Set the email address of this user entry. */
317: public void setEmailAddress(String emailAddress) {
318: this .emailAddress = emailAddress;
319: }
320:
321: /** Test if a user entry is equal to this user entry. */
322: public boolean equals(Object o) {
323: if (o == null || o.getClass() != this .getClass()) {
324: return false;
325: }
326:
327: UserEntry other = (UserEntry) o;
328:
329: if (!areEqual(getEmailAddress(), other.getEmailAddress())) {
330: return false;
331: }
332: if (!areEqual(getFullName(), other.getFullName())) {
333: return false;
334: }
335: if (!areEqual(getLocale(), other.getLocale())) {
336: return false;
337: }
338: if (!areEqual(getName(), other.getName())) {
339: return false;
340: }
341: if (!areEqual(getTimezone(), other.getTimezone())) {
342: return false;
343: }
344: if (!areEqual(getEnabled(), other.getEnabled())) {
345: return false;
346: }
347:
348: return super .equals(o);
349: }
350:
351: public Boolean getEnabled() {
352: return enabled;
353: }
354:
355: public void setEnabled(Boolean enabled) {
356: this.enabled = enabled;
357: }
358:
359: }
|