KickJava   Java API By Example, From Geeks To Geeks.

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


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.ForumPermissions;
33 import org.nemesis.forum.ForumThread;
34 import org.nemesis.forum.Message;
35 import org.nemesis.forum.User;
36 import org.nemesis.forum.config.Constants;
37 import org.nemesis.forum.exception.UnauthorizedException;
38
39 /**
40  * A protection proxy for ForumMessage objects.
41  */

42 public class MessageProxy implements Message {
43
44     private Message message;
45     private Authorization authorization;
46     private ForumPermissions permissions;
47
48     /**
49      * Creates a new ForumMessageProxy to protect the supplied message with
50      * the specified permissions
51      */

52     public MessageProxy(Message message, Authorization authorization, ForumPermissions permissions) {
53         this.message = message;
54         this.authorization = authorization;
55         this.permissions = permissions;
56     }
57
58     //FROM THE FORUMMESSAGE INTERFACE//
59

60     public int getID() {
61         return message.getID();
62     }
63     
64     public boolean isApproved() {
65             return message.isApproved();
66     }
67     
68     
69     public void setApproved(boolean approved) throws UnauthorizedException {
70         if (permissions.isSystemOrForumAdmin() || permissions.get(Constants.MODERATOR)) {
71             message.setApproved(approved);
72         } else {
73             throw new UnauthorizedException();
74         }
75     }
76
77     public Date JavaDoc getCreationDate() {
78         return message.getCreationDate();
79     }
80
81     public void setCreationDate(Date JavaDoc creationDate) throws UnauthorizedException {
82         if (permissions.isSystemOrForumAdmin()) {
83             this.message.setCreationDate(creationDate);
84         } else {
85             throw new UnauthorizedException();
86         }
87     }
88
89     public Date JavaDoc getModifiedDate() {
90         return message.getModifiedDate();
91     }
92
93     public void setModifiedDate(Date JavaDoc modifiedDate) throws UnauthorizedException {
94         if (permissions.isSystemOrForumAdmin()) {
95             this.message.setModifiedDate(modifiedDate);
96         } else {
97             throw new UnauthorizedException();
98         }
99     }
100
101     public String JavaDoc getSubject() {
102         return message.getSubject();
103     }
104
105     public String JavaDoc getUnfilteredSubject() {
106         return message.getUnfilteredSubject();
107     }
108
109     public void setSubject(String JavaDoc subject) throws UnauthorizedException {
110         if (permissions.isSystemOrForumAdmin() || getUser().hasPermission(Constants.USER_ADMIN)) {
111             this.message.setSubject(subject);
112         } else {
113             throw new UnauthorizedException();
114         }
115     }
116
117     public String JavaDoc getBody() {
118         return message.getBody();
119     }
120
121     public String JavaDoc getUnfilteredBody() {
122         return message.getUnfilteredBody();
123     }
124
125     public void setBody(String JavaDoc body) throws UnauthorizedException {
126         if (permissions.isSystemOrForumAdmin() || getUser().hasPermission(Constants.USER_ADMIN)) {
127             this.message.setBody(body);
128         } else {
129             throw new UnauthorizedException();
130         }
131     }
132
133     public User getUser() {
134         User user = message.getUser();
135         ForumPermissions userPermissions = user.getPermissions(authorization);
136         ForumPermissions newPermissions = new ForumPermissions(permissions, userPermissions);
137         return new UserProxy(user, authorization, newPermissions);
138     }
139
140     public String JavaDoc getProperty(String JavaDoc name) {
141         return message.getProperty(name);
142     }
143
144     public String JavaDoc getUnfilteredProperty(String JavaDoc name) {
145         return message.getUnfilteredProperty(name);
146     }
147
148     public void setProperty(String JavaDoc name, String JavaDoc value) {
149         message.setProperty(name, value);
150     }
151
152     public Iterator JavaDoc propertyNames() {
153         return message.propertyNames();
154     }
155
156     public boolean isAnonymous() {
157         return message.isAnonymous();
158     }
159
160     public ForumThread getForumThread() {
161         return message.getForumThread();
162     }
163
164     public boolean hasPermission(int type) {
165         return permissions.get(type);
166     }
167
168     //OTHER METHODS//
169

170     /**
171      * Converts the object to a String by returning the subject of the message.
172      * This functionality is primarily for Java applications that might be
173      * accessing CoolForum objects through a GUI.
174      */

175     public String JavaDoc toString() {
176         return message.toString();
177     }
178
179     /**
180      * Small violation of our pluggable backend architecture so that database
181      * insertions can be made more efficiently and transactional. The fact
182      * that this violation is needed probably means that the proxy architecture
183      * needs to be adjusted a bit.
184      *
185      */

186     public void insertIntoDb(java.sql.Connection JavaDoc con, ForumThread thread) throws SQLException JavaDoc {
187         ((org.nemesis.forum.impl.DbForumMessage) message).insertIntoDb(con, thread);
188     }
189     
190     /**
191      *
192      * @author dlaurent
193      *
194      * another violation, need uml diagram ....
195      */

196     public void setApproved2(boolean approved) throws UnauthorizedException {
197                 message.setApproved(approved);
198     }
199
200 }
201
Popular Tags