KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Yasna > forum > ForumMessageProxy


1 /**
2  * Copyright (C) 2001 Yasna.com. All rights reserved.
3  *
4  * ===================================================================
5  * The Apache Software License, Version 1.1
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  * if any, must include the following acknowledgment:
21  * "This product includes software developed by
22  * Yasna.com (http://www.yasna.com)."
23  * Alternately, this acknowledgment may appear in the software itself,
24  * if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Yazd" and "Yasna.com" must not be used to
27  * endorse or promote products derived from this software without
28  * prior written permission. For written permission, please
29  * contact yazd@yasna.com.
30  *
31  * 5. Products derived from this software may not be called "Yazd",
32  * nor may "Yazd" appear in their name, without prior written
33  * permission of Yasna.com.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of Yasna.com. For more information
51  * on Yasna.com, please see <http://www.yasna.com>.
52  */

53
54
55 package com.Yasna.forum;
56
57 import java.util.*;
58
59 /**
60  * A protection proxy for ForumMessage objects.
61  */

62 public class ForumMessageProxy implements ForumMessage {
63
64     private ForumMessage message;
65     private Authorization authorization;
66     private ForumPermissions permissions;
67     private boolean canReadText;
68
69     /**
70      * Creates a new ForumMessageProxy to protect the supplied message with
71      * the specified permissions
72      */

73     public ForumMessageProxy(ForumMessage message, Authorization authorization,
74                              ForumPermissions permissions) {
75         this.message = message;
76         this.authorization = authorization;
77         this.permissions = permissions;
78         init();
79     }
80
81     /**
82      * Initialize some class members.
83      */

84     private void init() {
85         canReadText = false;
86         boolean isModerator = false;
87         boolean isSystemAdmin = permissions.get(ForumPermissions.SYSTEM_ADMIN);
88         boolean isUserAdmin = permissions.get(ForumPermissions.FORUM_ADMIN);
89         if (permissions != null) {
90             isModerator = permissions.get(ForumPermissions.MODERATOR);
91         }
92         boolean isAdmin = isSystemAdmin || isUserAdmin || isModerator;
93         if (isAdmin ||
94             (message.getUser().getID() == authorization.getUserID() &&
95              authorization.getUserID() != -1)) {
96             canReadText = true;
97         } else {
98             if (message.isApproved()) {
99                 //if private message
100
if (message.isPrivate()) {
101                     //if receiver
102
if (message.getReplyPrivateUserId() ==
103                         authorization.getUserID()) {
104                         canReadText = true;
105                     }
106                 } else { //not private message
107
canReadText = true;
108                 }
109             }
110         } //if
111
}
112
113     //FROM THE FORUMMESSAGE INTERFACE//
114

115     public int getID() {
116         return message.getID();
117     }
118
119     public Date getCreationDate() {
120         return message.getCreationDate();
121     }
122
123     public void setCreationDate(Date creationDate) throws UnauthorizedException {
124         if (permissions.isSystemOrForumAdmin()) {
125             this.message.setCreationDate(creationDate);
126         } else {
127             throw new UnauthorizedException();
128         }
129     }
130
131     public Date getModifiedDate() {
132         return message.getModifiedDate();
133     }
134
135     public void setModifiedDate(Date modifiedDate) throws UnauthorizedException {
136         if (permissions.isSystemOrForumAdmin()) {
137             this.message.setModifiedDate(modifiedDate);
138         } else {
139             throw new UnauthorizedException();
140         }
141     }
142
143     public String JavaDoc getSubject() {
144         return message.getSubject();
145     }
146
147     public int getReplyPrivateUserId() {
148         return message.getReplyPrivateUserId();
149     }
150
151     public boolean isApproved() {
152         return message.isApproved();
153     }
154
155     public String JavaDoc getUnfilteredSubject() {
156         return message.getUnfilteredSubject();
157     }
158
159     public void setSubject(String JavaDoc subject) throws UnauthorizedException {
160         if (permissions.isSystemOrForumAdmin() ||
161             getUser().hasPermission(ForumPermissions.USER_ADMIN)) {
162             this.message.setSubject(subject);
163         } else {
164             throw new UnauthorizedException();
165         }
166     }
167
168     public void setReplyPrivateUserId(int replyPrivateUserId) throws
169             UnauthorizedException {
170         if (permissions.isSystemOrForumAdmin() ||
171             getUser().hasPermission(ForumPermissions.USER_ADMIN)) {
172             this.message.setReplyPrivateUserId(replyPrivateUserId);
173         } else {
174             throw new UnauthorizedException();
175         }
176     }
177
178     public void setApprovment(boolean approved) throws UnauthorizedException {
179         if (permissions.isSystemOrForumAdmin() ||
180             permissions.get(ForumPermissions.MODERATOR)) {
181             this.message.setApprovment(approved);
182         } else {
183             throw new UnauthorizedException();
184         }
185     }
186
187     public String JavaDoc getBody() {
188         if (canReadText) {
189             return message.getBody();
190         } else {
191             return null;
192         }
193     }
194
195     public boolean isPrivate() {
196         return message.isPrivate();
197     }
198
199     public String JavaDoc getUnfilteredBody() {
200         if (canReadText) {
201             return message.getUnfilteredBody();
202         } else {
203             return null;
204         }
205     }
206
207     public void setBody(String JavaDoc body) throws UnauthorizedException {
208         if (permissions.isSystemOrForumAdmin() ||
209             getUser().hasPermission(ForumPermissions.USER_ADMIN)) {
210             this.message.setBody(body);
211         } else {
212             throw new UnauthorizedException();
213         }
214     }
215
216     public User getUser() {
217         User user = message.getUser();
218         ForumPermissions userPermissions = user.getPermissions(authorization);
219         ForumPermissions newPermissions =
220                 new ForumPermissions(permissions, userPermissions);
221         return new UserProxy(user, authorization, newPermissions);
222     }
223
224     public String JavaDoc getProperty(String JavaDoc name) {
225         return message.getProperty(name);
226     }
227
228     public String JavaDoc getUnfilteredProperty(String JavaDoc name) {
229         return message.getUnfilteredProperty(name);
230     }
231
232     public void setProperty(String JavaDoc name, String JavaDoc value) {
233         message.setProperty(name, value);
234     }
235
236     public Iterator propertyNames() {
237         return message.propertyNames();
238     }
239
240     public boolean isAnonymous() {
241         return message.isAnonymous();
242     }
243
244     public ForumThread getForumThread() {
245         return message.getForumThread();
246     }
247
248     public boolean hasPermission(int type) {
249         return permissions.get(type);
250     }
251
252     //OTHER METHODS//
253

254     /**
255      * Converts the object to a String by returning the subject of the message.
256      * This functionality is primarily for Java applications that might be
257      * accessing CoolForum objects through a GUI.
258      */

259     public String JavaDoc toString() {
260         return message.toString();
261     }
262
263     /**
264      * Small violation of our pluggable backend architecture so that database
265      * insertions can be made more efficiently and transactional. The fact
266      * that this violation is needed probably means that the proxy architecture
267      * needs to be adjusted a bit.
268      */

269     public void insertIntoDb(java.sql.Connection JavaDoc con, ForumThread thread) throws
270             java.sql.SQLException JavaDoc {
271         ((com.Yasna.forum.database.DbForumMessage) message).insertIntoDb(con,
272                 thread);
273     }
274     public MessageRanking getRanking(){
275         return message.getRanking();
276     }
277     public void setRanking(int para) throws UnauthorizedException{
278         if (!message.getForumThread().getRootMessage().getUser().isAnonymous() && authorization.getUserID() == message.getForumThread().getRootMessage().getUser().getID() && authorization.getUserID() != message.getUser().getID()){
279             message.setRanking(para);
280         } else {
281             throw new UnauthorizedException("Only the user who created the thread can rank other messages");
282         }
283     }
284
285
286 }
287
Popular Tags