KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > pojo > User


1 /*
2  * $Id: User.java,v 1.7 2004/12/30 19:11:19 keyboardsamurai Exp $
3  *
4  * Copyright (c) 2004 j2biz Group, http://www.j2biz.com
5  * Koeln / Duesseldorf , Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.pojo;
27
28 import java.io.Serializable JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Locale JavaDoc;
32 import java.util.Set JavaDoc;
33 import java.util.TreeSet JavaDoc;
34
35 import net.sf.hibernate.Validatable;
36 import net.sf.hibernate.ValidationFailure;
37
38 import com.j2biz.blogunity.util.CommentComparator;
39 import com.j2biz.blogunity.util.BlogComparator;
40 import com.j2biz.blogunity.util.UserComparator;
41 import com.j2biz.blogunity.util.BlogUtils;
42 import com.j2biz.blogunity.util.UserpicComparator;
43
44 /**
45  * @hibernate.class table="USERS" dynamic-insert = "true" dynamic-update =
46  * "true"
47  *
48  * @author michelson
49  * @version $$
50  * @since 0.1
51  *
52  *
53  */

54 public class User implements Validatable, Serializable JavaDoc {
55
56    /**
57      * Comment for <code>serialVersionUID</code>
58      */

59     private static final long serialVersionUID = 3258697602278439221L;
60
61      public static final int MALE = 0;
62
63     public static final int FEMALE = 1;
64
65     private Long JavaDoc id;
66
67     private String JavaDoc nickname;
68
69     private String JavaDoc firstname;
70
71     private String JavaDoc lastname;
72
73     private String JavaDoc bioRaw;
74
75     private String JavaDoc bio;
76
77     private String JavaDoc password;
78
79     private String JavaDoc email;
80
81     private String JavaDoc homepage;
82
83     private Date JavaDoc birthday;
84
85     private String JavaDoc icq;
86
87     private String JavaDoc msn;
88
89     private String JavaDoc yahoo;
90
91     private String JavaDoc jabber;
92
93     private int sex = MALE;
94
95     private Set JavaDoc /* <Userpic> */userpics = new TreeSet JavaDoc(new UserpicComparator());
96
97     private Set JavaDoc/* <User> */friends = new TreeSet JavaDoc(new UserComparator());
98
99     private Set JavaDoc /* <Blog> */foundedBlogs = new TreeSet JavaDoc(new BlogComparator());
100
101     private Set JavaDoc /* <Blog> */contributedBlogs = new TreeSet JavaDoc(new BlogComparator());
102
103     private Set JavaDoc /* <Blog> */favoriteBlogs = new TreeSet JavaDoc(new BlogComparator());
104
105     private Set JavaDoc /* <Blog> */waitingForAcceptanceBlogs = new TreeSet JavaDoc(new BlogComparator());
106
107     private Set JavaDoc /* <Comment> */comments = new TreeSet JavaDoc(new CommentComparator());
108
109     private boolean isActive = true;
110
111     private boolean showEmail = true;
112
113     private Date JavaDoc registerTime = new Date JavaDoc();
114
115     private Date JavaDoc lastUpdateTime = new Date JavaDoc();
116
117     private boolean isAdministrator = false;
118
119     private Locale JavaDoc language = Locale.ENGLISH;
120
121     /**
122      *
123      */

124     public User() {
125
126     }
127
128     public User(String JavaDoc name, String JavaDoc password, String JavaDoc email, String JavaDoc firstname, String JavaDoc lastname) {
129
130         this.nickname = name;
131         this.password = password;
132         this.email = email;
133         this.firstname = firstname;
134         this.lastname = lastname;
135
136     }
137
138     /**
139      * @hibernate.id generator-class="increment" type="long" column="ID"
140      * unsaved-value="null"
141      *
142      * @return Returns the id.
143      */

144     public Long JavaDoc getId() {
145         return id;
146     }
147
148     /**
149      * @param id
150      * The id to set.
151      */

152     public void setId(Long JavaDoc id) {
153         this.id = id;
154     }
155
156     /**
157      * @hibernate.property name="language" column="LANGUAGE" type="locale"
158      * not-null="true" unique="false"
159      *
160      * @return locale
161      * @return
162      */

163     public Locale JavaDoc getLanguage() {
164         return language;
165     }
166
167     /**
168      * @param language
169      */

170     public void setLanguage(Locale JavaDoc language) {
171         this.language = language;
172     }
173
174     /**
175      * @hibernate.set name="comments"
176      * sort="com.j2biz.blogunity.util.CommentComparator"
177      * inverse="true" cascade="delete" lazy="true"
178      *
179      * @hibernate.collection-key column="AUTHOR_ID"
180      * @hibernate.collection-one-to-many class="com.j2biz.blogunity.pojo.Comment"
181      *
182      *
183      * @return Returns the comments.
184      */

185     public Set JavaDoc getComments() {
186         return comments;
187     }
188
189     /**
190      * @param comments
191      * The comments to set.
192      */

193     public void setComments(Set JavaDoc comments) {
194         this.comments = comments;
195     }
196
197     /**
198      * @hibernate.property name="email" column="EMAIL" type="string"
199      * not-null="true" unique="false"
200      *
201      * @return string
202      * @return Returns the email.
203      */

204     public String JavaDoc getEmail() {
205         return email;
206     }
207
208     /**
209      * @param email
210      * The email to set.
211      */

212     public void setEmail(String JavaDoc email) {
213         this.email = email;
214     }
215
216     /**
217      * @hibernate.property name="name" column="NICKNAME" type="string"
218      * not-null="true" unique="true"
219      *
220      * @return string
221      * @return Returns the name.
222      */

223     public String JavaDoc getNickname() {
224         return nickname;
225     }
226
227     /**
228      * @param name
229      * The name to set.
230      */

231     public void setNickname(String JavaDoc name) {
232         this.nickname = name;
233     }
234
235     /**
236      * @hibernate.property name="password" column="PASSWORD" type="string"
237      * not-null="true" unique="false"
238      *
239      * @return Returns the password.
240      */

241     public String JavaDoc getPassword() {
242         return password;
243     }
244
245     /**
246      * @param password
247      * The password to set.
248      */

249     public void setPassword(String JavaDoc password) {
250         this.password = password;
251     }
252
253     /**
254      * @hibernate.set name="userpics"
255      * sort="com.j2biz.blogunity.util.UserpicComparator"
256      * inverse="true" cascade="delete" lazy="true"
257      * @hibernate.collection-key column="USER_ID"
258      * @hibernate.collection-one-to-many class="com.j2biz.blogunity.pojo.Userpic"
259      *
260      * @return Returns the userpics.
261      */

262     public Set JavaDoc getUserpics() {
263         return userpics;
264     }
265
266     /**
267      * @param userpics
268      * The userpics to set.
269      */

270     public void setUserpics(Set JavaDoc userpics) {
271         this.userpics = userpics;
272     }
273
274     public void addUserpic(Userpic pic) {
275         userpics.add(pic);
276     }
277
278     /**
279      * Checks, if userpic with given url is already assigned to user.
280      *
281      * @param url
282      * @return
283      */

284     public boolean containsUserpicWithUrl(String JavaDoc url) {
285         for (Iterator JavaDoc it = getUserpics().iterator(); it.hasNext();) {
286             Userpic e = (Userpic) it.next();
287             if (e.getUrl().equals(url)) return true;
288         }
289
290         return false;
291     }
292
293     public boolean containsUserpicWithName(String JavaDoc name) {
294         for (Iterator JavaDoc it = getUserpics().iterator(); it.hasNext();) {
295             Userpic e = (Userpic) it.next();
296             if (e.getName().equals(name)) return true;
297         }
298         return false;
299     }
300
301     public boolean containsUserpic(Userpic pic) {
302         for (Iterator JavaDoc it = getUserpics().iterator(); it.hasNext();) {
303             Userpic e = (Userpic) it.next();
304             if (e.getName().equals(pic.getName()) && e.getUrl().equals(pic.getUrl())) return true;
305         }
306         return false;
307     }
308
309     /**
310      * @hibernate.set name="friends" table="FRIENDS" sort =
311      * "com.j2biz.blogunity.util.UserComparator" cascade="none"
312      * inverse="false" lazy="true"
313      * @hibernate.collection-key column="USER_ID"
314      * @hibernate.collection-many-to-many class="com.j2biz.blogunity.pojo.User"
315      * column="FRIEND_ID"
316      *
317      * @return Returns the friends.
318      */

319     public Set JavaDoc/* <User> */getFriends() {
320         return friends;
321     }
322
323     public void addFriend(User friend) {
324         friends.add(friend);
325     }
326
327     /**
328      * @param friends
329      * The friends to set.
330      */

331     public void setFriends(Set JavaDoc/* <User> */friends) {
332         this.friends = friends;
333     }
334
335     /**
336      * @hibernate.set name="contributedBlogs"
337      * sort="com.j2biz.blogunity.util.BlogComparator"
338      * table="BLOG_CONTRIBUTORS" cascade="none" inverse="false"
339      * lazy="true"
340      * @hibernate.collection-key column="CONTRIBUTOR_ID"
341      * @hibernate.collection-many-to-many class="com.j2biz.blogunity.pojo.Blog"
342      * column="BLOG_ID"
343      *
344      *
345      * @return Returns the contributedBlogs.
346      */

347     public Set JavaDoc getContributedBlogs() {
348         return contributedBlogs;
349     }
350
351     public void addContributedBlog(Blog blog) {
352         contributedBlogs.add(blog);
353     }
354
355     /**
356      * @param contributedBlogs
357      * The contributedBlogs to set.
358      */

359     public void setContributedBlogs(Set JavaDoc/* <Blog> */contributedBlogs) {
360         this.contributedBlogs = contributedBlogs;
361     }
362
363     /**
364      * @hibernate.set name="waitingForAcceptanceBlogs"
365      * sort="com.j2biz.blogunity.util.BlogComparator"
366      * table="BLOG_WAITING_ACCEPTANCE_USERS" cascade="none"
367      * inverse="false" lazy="true"
368      * @hibernate.collection-key column="CONTRIBUTOR_ID"
369      * @hibernate.collection-many-to-many class="com.j2biz.blogunity.pojo.Blog"
370      * column="BLOG_ID"
371      *
372      *
373      * @return Returns the waitingForAcceptanceBlogs.
374      */

375     public Set JavaDoc getWaitingForAcceptanceBlogs() {
376         return waitingForAcceptanceBlogs;
377     }
378
379     /**
380      * @param waitingForAcceptanceBlogs
381      * The waitingForAcceptanceBlogs to set.
382      */

383     public void setWaitingForAcceptanceBlogs(Set JavaDoc waitingForAcceptanceBlogs) {
384         this.waitingForAcceptanceBlogs = waitingForAcceptanceBlogs;
385     }
386
387     public void addWaitingForAcceptanceBlog(Blog blog) {
388         waitingForAcceptanceBlogs.add(blog);
389     }
390
391     /**
392      * @hibernate.set name="foundedBlogs"
393      * sort="com.j2biz.blogunity.util.BlogComparator"
394      * inverse="true" cascade="delete" lazy="true"
395      * @hibernate.collection-key column="FOUNDER_ID"
396      * @hibernate.collection-one-to-many class="com.j2biz.blogunity.pojo.Blog"
397      *
398      * @return Returns the foundedBlogs.
399      */

400     public Set JavaDoc getFoundedBlogs() {
401         return foundedBlogs;
402     }
403
404     public int getNumberOfIndividualBlogsFounded() {
405         int counter = 0;
406         for (Iterator JavaDoc i = getFoundedBlogs().iterator(); i.hasNext();) {
407             Blog b = (Blog) i.next();
408             if (b.getType() == Blog.INDIVIDUAL_BLOG) counter++;
409
410         }
411         return counter;
412     }
413
414     public int getNumberOfCommunityBlogsFounded() {
415         int counter = 0;
416         for (Iterator JavaDoc i = getFoundedBlogs().iterator(); i.hasNext();) {
417             Blog b = (Blog) i.next();
418             if (b.getType() == Blog.COMMUNITY_BLOG) counter++;
419
420         }
421         return counter;
422     }
423
424     /**
425      * @param foundedBlogs
426      * The foundedBlogs to set.
427      */

428     public void setFoundedBlogs(Set JavaDoc/* <Blog> */foundedBlogs) {
429         this.foundedBlogs = foundedBlogs;
430     }
431
432     public void addFoundedBlog(Blog blog) {
433         foundedBlogs.add(blog);
434     }
435
436     /**
437      * @hibernate.set name="favoriteBlogs"
438      * sort="com.j2biz.blogunity.util.BlogComparator"
439      * table="USER_FAVORITE_BLOGS" cascade="none" inverse="false"
440      * lazy="true"
441      * @hibernate.collection-key column="USER_ID"
442      * @hibernate.collection-many-to-many class="com.j2biz.blogunity.pojo.Blog"
443      * column="BLOG_ID"
444      *
445      * @return Returns the favoriteBlogs.
446      */

447     public Set JavaDoc getFavoriteBlogs() {
448         return favoriteBlogs;
449     }
450
451     /**
452      * @param favoriteBlogs
453      * The favoriteBlogs to set.
454      */

455     public void setFavoriteBlogs(Set JavaDoc favoriteBlogs) {
456         this.favoriteBlogs = favoriteBlogs;
457     }
458
459     public void addFavoriteBlog(Blog blog) {
460         favoriteBlogs.add(blog);
461     }
462
463     /**
464      * @hibernate.property name="active" column="ACTIVE" type="boolean"
465      * not-null="true" unique="false"
466      *
467      * @return Returns the isActive.
468      */

469     public boolean getActive() {
470         return isActive;
471     }
472
473     /**
474      * @return Returns the isActive.
475      */

476     public boolean isActive() {
477         return isActive;
478     }
479
480     /**
481      * @param isActive
482      * The isActive to set.
483      */

484     public void setActive(boolean isActive) {
485         this.isActive = isActive;
486     }
487
488     /**
489      * @hibernate.property name="administrator" column="IS_ADMINISTRATOR"
490      * type="boolean" not-null="true" unique="false"
491      *
492      * @return Returns the isAdministrator.
493      */

494     public boolean getAdministrator() {
495         return isAdministrator;
496     }
497
498     /**
499      * @return Returns the isAdministrator.
500      */

501     public boolean isAdministrator() {
502         return isAdministrator;
503     }
504
505     /**
506      * @param isAdministrator
507      * The isAdministrator to set.
508      */

509     public void setAdministrator(boolean isAdministrator) {
510         this.isAdministrator = isAdministrator;
511     }
512
513     /**
514      * @hibernate.property name="bio" column="BIO" type="text" not-null="false"
515      * unique="false"
516      *
517      * @return text
518      * @return Returns the bio.
519      */

520     public String JavaDoc getBio() {
521         return bio;
522     }
523
524     /**
525      * @param bio
526      * The bio to set.
527      */

528     public void setBio(String JavaDoc bio) {
529         this.bio = bio;
530     }
531
532     /**
533      * @hibernate.property name="bioRaw" column="BIO_RAW" type="text"
534      * not-null="false" unique="false"
535      *
536      * @return Returns the bioRaw.
537      */

538     public String JavaDoc getBioRaw() {
539         return bioRaw;
540     }
541
542     /**
543      * @param bioRaw
544      * The bioRaw to set.
545      */

546     public void setBioRaw(String JavaDoc bioRaw) {
547         this.bioRaw = bioRaw;
548     }
549
550     /**
551      * @hibernate.property name="firstname" column="FIRSTNAME" type="string"
552      * not-null="true" unique="false"
553      *
554      * @return Returns the firstName.
555      */

556     public String JavaDoc getFirstname() {
557         return firstname;
558     }
559
560     /**
561      * @param firstName
562      * The firstName to set.
563      */

564     public void setFirstname(String JavaDoc firstName) {
565         this.firstname = firstName;
566     }
567
568     /**
569      * @hibernate.property name="lastName" column="LASTNAME" type="string"
570      * not-null="true" unique="false"
571      *
572      * @return Returns the lastName.
573      */

574     public String JavaDoc getLastname() {
575         return lastname;
576     }
577
578     /**
579      * @param lastName
580      * The lastName to set.
581      */

582     public void setLastname(String JavaDoc lastName) {
583         this.lastname = lastName;
584     }
585
586     /**
587      * @hibernate.property name="showEmail" column="SHOW_EMAIL" type="boolean"
588      * not-null="true" unique="false"
589      *
590      * @return Returns the showEmail.
591      */

592     public boolean getShowEmail() {
593         return showEmail;
594     }
595
596     /**
597      * @return Returns the showEmail.
598      */

599     public boolean isShowEmail() {
600         return showEmail;
601     }
602
603     /**
604      * @param showEmail
605      * The showEmail to set.
606      */

607     public void setShowEmail(boolean showEmail) {
608         this.showEmail = showEmail;
609     }
610
611     /**
612      * @hibernate.property name="registerTime" column="REGISTER_TIME"
613      * type="timestamp" not-null="true" unique="false"
614      *
615      * @return Returns the registerTime.
616      */

617     public Date JavaDoc getRegisterTime() {
618         return registerTime;
619     }
620
621     /**
622      * @param registerTime
623      * The registerTime to set.
624      */

625     public void setRegisterTime(Date JavaDoc regiterTime) {
626         this.registerTime = regiterTime;
627     }
628
629     /**
630      * @hibernate.property name="homepage" column="HOMEPAGE" type="string"
631      * not-null="false" unique="false"
632      *
633      * @return Returns the homepage.
634      */

635     public String JavaDoc getHomepage() {
636         return homepage;
637     }
638
639     /**
640      * @param homepage
641      * The homepage to set.
642      */

643     public void setHomepage(String JavaDoc homepage) {
644         this.homepage = homepage;
645     }
646
647     /**
648      * @hibernate.property name="icq" column="ICQ" type="string"
649      * not-null="false" unique="false"
650      * @return Returns the icq.
651      */

652     public String JavaDoc getIcq() {
653         return icq;
654     }
655
656     /**
657      * @param icq
658      * The icq to set.
659      */

660     public void setIcq(String JavaDoc icq) {
661         this.icq = icq;
662     }
663
664     /**
665      * @hibernate.property name="jabber" column="JABBER" type="string"
666      * not-null="false" unique="false"
667      * @return Returns the jabber.
668      */

669     public String JavaDoc getJabber() {
670         return jabber;
671     }
672
673     /**
674      * @param jabber
675      * The jabber to set.
676      */

677     public void setJabber(String JavaDoc jabber) {
678         this.jabber = jabber;
679     }
680
681     /**
682      * @hibernate.property name="msn" column="MSN" type="string"
683      * not-null="false" unique="false"
684      * @return Returns the msn.
685      */

686     public String JavaDoc getMsn() {
687         return msn;
688     }
689
690     /**
691      * @param msn
692      * The msn to set.
693      */

694     public void setMsn(String JavaDoc msn) {
695         this.msn = msn;
696     }
697
698     /**
699      * @hibernate.property name="yahoo" column="YAHOO" type="string"
700      * not-null="false" unique="false"
701      * @return Returns the yahoo.
702      *
703      */

704     public String JavaDoc getYahoo() {
705         return yahoo;
706     }
707
708     /**
709      * @param yahoo
710      * The yahoo to set.
711      */

712     public void setYahoo(String JavaDoc yahoo) {
713         this.yahoo = yahoo;
714     }
715
716     /**
717      * @hibernate.property name="birthday" column="BIRTHDAY" type="date"
718      * not-null="false" unique="false"
719      *
720      * @return Returns the birthday.
721      */

722     public Date JavaDoc getBirthday() {
723         return birthday;
724     }
725
726     /**
727      * @param birthday
728      * The birthday to set.
729      */

730     public void setBirthday(Date JavaDoc birthday) {
731         this.birthday = birthday;
732     }
733
734     /**
735      * @hibernate.property name="lastUpdateTime" column="LAST_UPDATE_TIME"
736      * type="timestamp" not-null="true" unique="false"
737      *
738      * @return Returns the lastUpdateTime.
739      */

740     public Date JavaDoc getLastUpdateTime() {
741         return lastUpdateTime;
742     }
743
744     /**
745      * @hibernate.property name="sex" column="SEX" type="integer"
746      * not-null="true" unique="false"
747      *
748      * @return Returns the sex.
749      */

750     public int getSex() {
751         return sex;
752     }
753
754     /**
755      * @param sex
756      * The sex to set.
757      */

758     public void setSex(int sex) {
759         this.sex = sex;
760     }
761
762     /**
763      * @param lastUpdateTime
764      * The lastUpdateTime to set.
765      */

766     public void setLastUpdateTime(Date JavaDoc lastUpdateTime) {
767         this.lastUpdateTime = lastUpdateTime;
768     }
769
770     /**
771      * Returns true if this user can post new entries to requested blog. This is
772      * true if:
773      * <ul>
774      * <li>user is a blog founder;</li>
775      * <li>user is a blog countributor;</li>
776      * <li>user is a system administrator;</li>
777      * </ul>
778      *
779      * @param blog
780      * @return
781      */

782     public boolean canPostToBlog(Blog blog) {
783         if (isFounder(blog) || isContributor(blog) || isAdministrator()) return true;
784         return false;
785     }
786
787     /**
788      * @param blog
789      * @return
790      */

791     public boolean isContributor(Blog blog) {
792
793         if (blog.getType() == Blog.INDIVIDUAL_BLOG) {
794             return false;
795         } else {
796             Iterator JavaDoc it = blog.getContributors().iterator();
797             while (it.hasNext()) {
798                 User u = (User) it.next();
799                 if (u.getId().longValue() == this.id.longValue()) return true;
800
801             }
802         }
803         return false;
804     }
805
806     /**
807      * @param blog
808      * @return
809      */

810     public boolean isFounder(Blog blog) {
811         return (blog.getFounder().getId().longValue() == this.id.longValue());
812     }
813
814     /**
815      * Returns true, if the given user is one of the friends of the user <i>u
816      * </i>. Otherwise returns false.
817      *
818      * @param user
819      * <i>u </i> to test for friendship with given user.
820      * @return true, if the given user is one of the friends of the user <i>u
821      * </i>.
822      */

823     public boolean containsAsFriend(User u) {
824         Set JavaDoc fr = getFriends();
825         Iterator JavaDoc it = fr.iterator();
826         while (it.hasNext()) {
827             User f = (User) it.next();
828             if (f.getId().longValue() == u.getId().longValue()) return true;
829         }
830
831         return false;
832     }
833
834     /*
835      * (non-Javadoc)
836      *
837      * @see net.sf.hibernate.Validatable#validate()
838      */

839     public void validate() throws ValidationFailure {
840
841         if (!BlogUtils.getInstance().isValidNickname(nickname))
842                 throw new ValidationFailure("Nickname is not valid!");
843
844         if (!BlogUtils.getInstance().isValidEmailAddress(email))
845                 throw new ValidationFailure("Email is not valid!");
846
847     }
848
849 }
Popular Tags