KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Yasna > forum > tags > PerformAdminActionTag


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 package com.Yasna.forum.tags;
55
56 import javax.servlet.jsp.tagext.*;
57 import com.Yasna.forum.*;
58 import javax.servlet.jsp.*;
59 import java.net.*;
60 import java.util.*;
61
62 /**
63  * Jsp tag evaluates its body if curent logged in user is admin or moderator.
64  * Must be nested in Thread or Forum tag. Adds 'delete' and 'approve' functionality
65  * to current parent container. Type of parent container must be specified in
66  * the [id] parameter. Two values are available - "thread" and "forum".
67  */

68 public class PerformAdminActionTag extends TagSupport {
69
70     private Action target;
71
72     public int doStartTag() throws JspException {
73         try {
74             Authorization authToken = getAuthToken();
75             ForumFactory forumFactory = ForumFactory.getInstance(authToken);
76             ForumPermissions permissions = forumFactory.getPermissions(authToken);
77             boolean isSystemAdmin = permissions.get(ForumPermissions.SYSTEM_ADMIN);
78             boolean isUserAdmin = permissions.get(ForumPermissions.FORUM_ADMIN);
79             boolean isModerator = false;
80             Forum forum = getCurrentForum();
81             if (forum != null) {
82                 isModerator = forum.getPermissions(authToken).get(ForumPermissions.MODERATOR);
83             }
84             boolean isAdmin = isUserAdmin || isSystemAdmin || isModerator;
85             if (isAdmin) {
86                 Enumeration en = pageContext.getRequest().getParameterNames();
87                 while (en.hasMoreElements()) {
88                     String JavaDoc p = (String JavaDoc) en.nextElement();
89                     if (p.startsWith("approve_")) {
90                         try {
91                             String JavaDoc idStr = p.substring(8, p.indexOf("."));
92                             int id = Integer.parseInt(idStr);
93                             target.setApprovement(id, true);
94                             break;
95                         } catch(Exception JavaDoc e) {
96                             e.printStackTrace();
97                         }
98                     } else if (p.startsWith("delete_")) {
99                         try {
100                             String JavaDoc idStr = p.substring(7, p.indexOf("."));
101                             int id = Integer.parseInt(idStr);
102                             target.delete(id);
103                             break;
104                         } catch(Exception JavaDoc e) {
105                             e.printStackTrace();
106                         }
107                     }
108                 }
109
110             }//if isAdmin
111
} catch(Exception JavaDoc e) {
112         }
113         return SKIP_BODY;
114     }
115
116     /**
117      * Sets the parent type of parent container.
118      * @param t String containing one of both values "thread" and "forum".
119      * @throws Exception
120      */

121     public void setTarget(String JavaDoc t) throws Exception JavaDoc {
122         if ("thread".equals(t)) {
123             target = new TargetThread();
124         } else if ("forum".equals(t)) {
125             target = new TargetForum();
126         } else {
127             throw new Exception JavaDoc("Unknown 'target' = [" + target + "] set for attribute on PerformAdminAction tag!");
128         }
129     }
130
131     /**
132      * Retrieve Authorization object from pageContext which give the information
133      * for currently logged user.
134      *
135      * @return Authorization object.
136      */

137     private Authorization getAuthToken() {
138         YazdState js = (YazdState) pageContext.getAttribute("yazdUserState",PageContext.SESSION_SCOPE);
139         return js.getAuthorization();
140     }
141
142     private ForumThread getCurrentThread() {
143         ThreadTag tt = null;
144         try {
145             tt = (ThreadTag)this.findAncestorWithClass(this,
146                 Class.forName("com.Yasna.forum.tags.ThreadTag"));
147         } catch(Exception JavaDoc e) {
148         }
149         return tt.getThread();
150     }
151
152     private Forum getCurrentForum() {
153         ForumTag tt = null;
154         try {
155             tt = (ForumTag)this.findAncestorWithClass(this,
156                 Class.forName("com.Yasna.forum.tags.ForumTag"));
157         } catch(Exception JavaDoc e) {
158         }
159         return tt.getForum();
160     }
161
162     interface Action {
163         public void delete(int id) throws Exception JavaDoc;
164         public void setApprovement(int id, boolean approvement) throws Exception JavaDoc;
165     }
166     class TargetThread implements Action {
167         public void delete(int id) throws Exception JavaDoc {
168             ForumThread thread = getCurrentThread();
169             ForumMessage toDelete = thread.getMessage(id);
170             thread.deleteMessage(toDelete);
171         }
172         public void setApprovement(int id, boolean approvement) throws Exception JavaDoc {
173             ForumThread thread = getCurrentThread();
174             thread.getMessage(id).setApprovment(approvement);
175         }
176     }
177     class TargetForum implements Action {
178         public void delete(int id) throws Exception JavaDoc {
179             Forum forum = getCurrentForum();
180             ForumThread toDelete = forum.getThread(id);
181             forum.deleteThread(toDelete);
182         }
183         public void setApprovement(int id, boolean approvement) throws Exception JavaDoc {
184             Forum forum = getCurrentForum();
185             forum.getThread(id).setApprovment(approvement);
186         }
187     }
188
189 }
Popular Tags