KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > proxy > ForumThreadProxy


1 /*
2  * NEMESIS-FORUM.
3  * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
6  *
7  * Copyright (C) 2001 Yasna.com. All rights reserved.
8  *
9  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
10  *
11  * NEMESIS-FORUM. is free software; you can redistribute it and/or
12  * modify it under the terms of the Apache Software License, Version 1.1,
13  * or (at your option) any later version.
14  *
15  * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
16  * application are parts of NEMESIS-FORUM and are distributed under
17  * same terms of licence.
18  *
19  *
20  * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
21  * and software developed by CoolServlets.com (http://www.coolservlets.com).
22  * and software developed by Yasna.com (http://www.yasna.com).
23  *
24  */

25 package org.nemesis.forum.proxy;
26
27 import java.sql.SQLException JavaDoc;
28 import java.util.Date JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import org.nemesis.forum.Authorization;
32 import org.nemesis.forum.Forum;
33 import org.nemesis.forum.ForumPermissions;
34 import org.nemesis.forum.ForumThread;
35 import org.nemesis.forum.Message;
36 import org.nemesis.forum.TreeWalker;
37 import org.nemesis.forum.impl.DbForumThread;
38 import org.nemesis.forum.config.Constants;
39 import org.nemesis.forum.exception.ForumMessageNotFoundException;
40 import org.nemesis.forum.exception.UnauthorizedException;
41
42 /**
43  * Protection proxy for ForumThread objects. It restricts access to protected
44  * methods by throwing UnauthorizedExceptions if the user does not have
45  * permission to access the class.
46  */

47 public class ForumThreadProxy implements ForumThread {
48
49     private ForumThread thread;
50     private Authorization authorization;
51     private ForumPermissions permissions;
52
53     /**
54      * Creates a new proxy.
55      */

56     public ForumThreadProxy(ForumThread thread, Authorization authorization, ForumPermissions permissions) {
57         this.thread = thread;
58         this.authorization = authorization;
59         this.permissions = permissions;
60     }
61
62     public int getID() {
63         return thread.getID();
64     }
65
66     public String JavaDoc getName() {
67         return thread.getName();
68     }
69     
70     public boolean isApproved() {
71             return thread.isApproved();
72     }
73     
74     public void setApproved(boolean approved) throws UnauthorizedException {
75         if (permissions.isSystemOrForumAdmin() || permissions.get(Constants.MODERATOR)) {
76             thread.setApproved(approved);
77         } else {
78             throw new UnauthorizedException();
79         }
80     }
81
82     public Date JavaDoc getCreationDate() {
83         return thread.getCreationDate();
84     }
85
86     public void setCreationDate(Date JavaDoc creationDate) throws UnauthorizedException {
87         if (permissions.isSystemOrForumAdmin()) {
88             thread.setCreationDate(creationDate);
89         } else
90             throw new UnauthorizedException();
91     }
92
93     public Date JavaDoc getModifiedDate() {
94         return thread.getModifiedDate();
95     }
96
97     public void setModifiedDate(Date JavaDoc modifiedDate) throws UnauthorizedException {
98         if (permissions.isSystemOrForumAdmin()) {
99             thread.setModifiedDate(modifiedDate);
100         } else
101             throw new UnauthorizedException();
102     }
103
104     public Forum getForum() {
105         Forum forum = thread.getForum();
106         return new ForumProxy(forum, authorization, permissions);
107     }
108
109     public int getMessageCount() {
110         return thread.getMessageCount();
111     }
112     
113     public int getMessageCount(boolean approved) {
114             return thread.getMessageCount(approved);
115     }
116
117
118     public Message getRootMessage() {
119         Message message = thread.getRootMessage();
120         return new MessageProxy(message, authorization, permissions);
121     }
122
123     public void addMessage(Message parentMessage, Message newMessage) {
124         thread.addMessage(parentMessage, newMessage);
125     }
126
127     public void deleteMessage(Message message) throws UnauthorizedException {
128         if (permissions.isSystemOrForumAdmin() || permissions.get(Constants.MODERATOR)) {
129             thread.deleteMessage(message);
130         } else {
131             throw new UnauthorizedException();
132         }
133     }
134
135     public void moveMessage(Message message, ForumThread newThread, Message parentMessage)
136         throws UnauthorizedException {
137         //If the user is an amdin of both forums
138
if (permissions.isSystemOrForumAdmin()
139             && (newThread.hasPermission(Constants.SYSTEM_ADMIN) || newThread.hasPermission(Constants.FORUM_ADMIN))) {
140             thread.moveMessage(message, newThread, parentMessage);
141         } else {
142             throw new UnauthorizedException();
143         }
144     }
145
146     public Message getMessage(int messageID) throws ForumMessageNotFoundException {
147         Message message = thread.getMessage(messageID);
148         //Apply the protection proxy and return message.
149
return new MessageProxy(message, authorization, permissions);
150     }
151
152     public TreeWalker treeWalker() {
153         return new TreeWalkerProxy(thread.treeWalker(), authorization, permissions);
154     }
155     
156     public TreeWalker treeWalker(boolean approved) {
157             return new TreeWalkerProxy(thread.treeWalker(approved), authorization, permissions);
158     }
159
160     public Iterator JavaDoc messages() {
161         Iterator JavaDoc iterator = thread.messages();
162         return new MessageIteratorProxy(iterator, authorization, permissions);
163     }
164
165     public Iterator JavaDoc messages(int startIndex, int numResults) {
166         Iterator JavaDoc iterator = thread.messages(startIndex, numResults);
167         return new MessageIteratorProxy(iterator, authorization, permissions);
168     }
169     
170     public Iterator JavaDoc messages(boolean approved) {
171         Iterator JavaDoc iterator = thread.messages(approved);
172         return new MessageIteratorProxy(iterator, authorization, permissions);
173     }
174
175     public Iterator JavaDoc messages(boolean approved,int startIndex, int numResults) {
176         Iterator JavaDoc iterator = thread.messages(approved,startIndex, numResults);
177         return new MessageIteratorProxy(iterator, authorization, permissions);
178     }
179
180     public boolean hasPermission(int type) {
181         return permissions.get(type);
182     }
183
184     public String JavaDoc toString() {
185         return thread.toString();
186     }
187
188     /**
189      * Small violation of our pluggable backend architecture so that database
190      * insertions can be made more efficiently and transactional. The fact
191      * that this violation is needed probably means that the proxy architecture
192      * needs to be adjusted a bit.
193      */

194     public void insertIntoDb(java.sql.Connection JavaDoc con) throws SQLException JavaDoc {
195         ((DbForumThread) thread).insertIntoDb(con);
196     }
197 }
198
Popular Tags