KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > webservices > adminapi > sdk > UserEntry


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18 /*
19  * UserEntry.java
20  *
21  * Created on January 17, 2006, 12:44 PM
22  */

23
24 package org.apache.roller.webservices.adminapi.sdk;
25
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.util.Date JavaDoc;
29 import java.util.Locale JavaDoc;
30 import java.util.TimeZone JavaDoc;
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 /**
40  * This class describes a user entry; a user weblog resource.
41  * @author jtb
42  */

43 public class UserEntry extends Entry {
44     /** XML tags that define a user entry. */
45     static interface Tags {
46         public static final String JavaDoc USER = "user";
47         public static final String JavaDoc NAME = "name";
48         public static final String JavaDoc FULL_NAME = "full-name";
49         public static final String JavaDoc PASSWORD = "password";
50         public static final String JavaDoc EMAIL_ADDRESS = "email-address";
51         public static final String JavaDoc LOCALE = "locale";
52         public static final String JavaDoc TIMEZONE = "timezone";
53         public static final String JavaDoc DATE_CREATED = "date-created";
54     }
55     
56     private String JavaDoc name;
57     private String JavaDoc fullName;
58     private String JavaDoc password;
59     private Locale JavaDoc locale;
60     private TimeZone JavaDoc timezone;
61     private Date JavaDoc dateCreated;
62     private String JavaDoc emailAddress;
63     
64     /** Construct an empty user entry */
65     public UserEntry(String JavaDoc name, String JavaDoc urlPrefix) {
66         setName(name);
67         String JavaDoc href = urlPrefix + "/" + EntrySet.Types.USERS + "/" + name;
68         setHref(href);
69     }
70     
71     /** Construct a user entry from a JDOM element. */
72     public UserEntry(Element e, String JavaDoc urlPrefix) throws MissingElementException {
73         populate(e, urlPrefix);
74     }
75     
76     public UserEntry(InputStream JavaDoc stream, String JavaDoc urlPrefix) throws JDOMException, IOException JavaDoc, 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 JavaDoc urlPrefix) throws MissingElementException {
85         // name
86
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         // href
93
String JavaDoc href = urlPrefix + "/" + EntrySet.Types.USERS + "/" + getName();
94         setHref(href);
95         
96         // full name
97
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         // password
104
// this is optional
105
Element passwordElement = e.getChild(Tags.PASSWORD, NAMESPACE);
106         if (passwordElement != null) {
107             setPassword(passwordElement.getText());
108         }
109         
110         // locale
111
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         // timezone
118
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         // email address
125
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         // created (optional)
132
Element createdElement = e.getChild(Tags.DATE_CREATED, Service.NAMESPACE);
133         if (createdElement != null) {
134             setDateCreated(new Date JavaDoc(Long.valueOf(createdElement.getText()).longValue()));
135         }
136     }
137     
138         
139     public String JavaDoc 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         // href
148
String JavaDoc href = getHref();
149         if (href != null) {
150             userElement.setAttribute(Attributes.HREF, href);
151         }
152         
153         // name
154
String JavaDoc 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         // full name
163
String JavaDoc 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         // password
172
String JavaDoc 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
181
Locale JavaDoc 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
190
TimeZone JavaDoc 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         // email address
199
String JavaDoc 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         // creation date (optional)
208
Date JavaDoc 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     /** Get the user name of this user entry. */
220     public String JavaDoc getName() {
221         return name;
222     }
223     
224     /** Set of the user name of this user entry. */
225     public void setName(String JavaDoc name) {
226         this.name = name;
227     }
228     
229     /** Get the full name of this user entry. */
230     public String JavaDoc getFullName() {
231         return fullName;
232     }
233     
234     /** Set the full name of this user entry. */
235     public void setFullName(String JavaDoc fullName) {
236         this.fullName = fullName;
237     }
238     
239     /** Get the password of this user entry. */
240     public String JavaDoc getPassword() {
241         return password;
242     }
243     
244     /** Set the password of this user entry. */
245     public void setPassword(String JavaDoc password) {
246         this.password = password;
247     }
248     
249     /** Get the locale string of this user entry. */
250     public Locale JavaDoc getLocale() {
251         return locale;
252     }
253     
254     /** Set the locale of this user entry. */
255     public void setLocale(Locale JavaDoc locale) {
256         this.locale = locale;
257     }
258     
259     /** Set the locale string of this user entry. */
260     public void setLocale(String JavaDoc localeString) {
261         this.locale = new LocaleString(localeString).getLocale();
262     }
263     
264     /** Get the timezone string of this user entry. */
265     public TimeZone JavaDoc getTimezone() {
266         return timezone;
267     }
268     
269     /** Set the timezone of this user entry. */
270     public void setTimezone(TimeZone JavaDoc timezone) {
271         this.timezone = timezone;
272     }
273
274     /** Set the timezone string of this user entry. */
275     public void setTimezone(String JavaDoc timezoneString) {
276         this.timezone = TimeZone.getTimeZone(timezoneString);
277     }
278     
279     /** Get the date created of this user entry. */
280     public Date JavaDoc getDateCreated() {
281         return dateCreated;
282     }
283     
284     /** Set the date created of this user entry. */
285     public void setDateCreated(Date JavaDoc dateCreated) {
286         this.dateCreated = dateCreated;
287     }
288     
289     /** Get the email address of this user entry. */
290     public String JavaDoc getEmailAddress() {
291         return emailAddress;
292     }
293     
294     /** Set the email address of this user entry. */
295     public void setEmailAddress(String JavaDoc emailAddress) {
296         this.emailAddress = emailAddress;
297     }
298     
299     /** Test if a user entry is equal to this user entry. */
300     public boolean equals(Object JavaDoc 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