KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > forumadmin > ForumAdminService


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2002 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.forumadmin;
20
21 import org.lucane.common.*;
22 import org.lucane.common.acl.AclInfo;
23 import org.lucane.common.acl.AclProducer;
24 import org.lucane.common.concepts.GroupConcept;
25 import org.lucane.common.concepts.UserConcept;
26 import org.lucane.common.net.ObjectConnection;
27 import org.lucane.server.*;
28 import org.lucane.server.acl.AccessController;
29 import java.sql.*;
30 import java.util.*;
31
32
33 public class ForumAdminService
34 extends Service
35 implements AclProducer
36 {
37     private static final String JavaDoc FORUM_APP = "org.lucane.applications.forum";
38     
39     public ForumAdminService()
40     {
41     }
42     
43     public void process(ObjectConnection oc, Message message)
44     {
45         ForumAdminAction faa = (ForumAdminAction)message.getData();
46         
47         try {
48             switch(faa.action)
49             {
50             case ForumAdminAction.LIST_FORUMS:
51                 ArrayList forums = listForums();
52                 oc.write("OK");
53                 oc.write(forums);
54                 break;
55             
56             case ForumAdminAction.DELETE_FORUM:
57                 deleteForum(faa.forum);
58                 oc.write("OK");
59                 break;
60                 
61             case ForumAdminAction.CREATE_FORUM:
62                 createForum(faa.forum);
63                 oc.write("OK");
64                 break;
65             
66             case ForumAdminAction.GET_ACLS:
67                 AclInfo[] acl = getAcls(faa.forum);
68                 oc.write("OK");
69                 oc.write(acl);
70                 break;
71                 
72             case ForumAdminAction.GET_GROUPS:
73                 ArrayList groups = getAllGroups();
74                 oc.write("OK");
75                 oc.write(groups);
76                 break;
77                 
78             case ForumAdminAction.GET_USERS:
79                 ArrayList users = getAllUsers();
80                 oc.write("OK");
81                 oc.write(users);
82                 break;
83                 
84             case ForumAdminAction.ADD_ACL:
85                 addAcl(faa.forum, (AclInfo)faa.param);
86                 oc.write("OK");
87                 break;
88                 
89             case ForumAdminAction.REMOVE_ACL:
90                 removeAcl(faa.forum, (AclInfo)faa.param);
91                 oc.write("OK");
92                 break;
93             }
94         } catch(Exception JavaDoc e) {
95             try {
96                 oc.write("FAILED " + e);
97             } catch(Exception JavaDoc e2) {}
98             
99             e.printStackTrace();
100         }
101     }
102     
103     public ArrayList listForums()
104     throws Exception JavaDoc
105     {
106         ArrayList forums = new ArrayList();
107         
108         Connection connection = Server.getInstance().getDBLayer().getConnection();
109         PreparedStatement select = connection.prepareStatement("SELECT name FROM forum");
110         ResultSet res = select.executeQuery();
111         
112         while(res.next())
113             forums.add(res.getString(1));
114         
115         res.close();
116         select.close();
117         connection.close();
118         
119         return forums;
120     }
121     
122     public void deleteForum(String JavaDoc forum)
123     throws Exception JavaDoc
124     {
125         Connection connection = Server.getInstance().getDBLayer().getConnection();
126         PreparedStatement delete = connection.prepareStatement("DELETE FROM forum WHERE name= ?");
127         delete.setString(1, forum);
128         delete.execute();
129         delete.close();
130         connection.close();
131         
132         //remove acls
133
Server.getInstance().getAccessController().removeItem(FORUM_APP, forum);
134     }
135     
136     public void createForum(String JavaDoc forum)
137     throws Exception JavaDoc
138     {
139         Connection connection = Server.getInstance().getDBLayer().getConnection();
140         PreparedStatement insert = connection.prepareStatement("INSERT INTO forum VALUES(?)");
141         insert.setString(1, forum);
142         insert.execute();
143         insert.close();
144         connection.close();
145         
146         //set default acls.
147
Server.getInstance().getAccessController().allowGroup(FORUM_APP, forum, "Moderate", "Admins");
148         Server.getInstance().getAccessController().allowGroup(FORUM_APP, forum, "Write", "AllUsers");
149         Server.getInstance().getAccessController().allowGroup(FORUM_APP, forum, "Read", "AllUsers");
150     }
151     
152     public AclInfo[] getAcls(String JavaDoc forum)
153     throws Exception JavaDoc
154     {
155         AccessController acl = Server.getInstance().getAccessController();
156         return acl.getAcls(FORUM_APP, forum);
157     }
158     
159     public ArrayList getAllGroups()
160     throws Exception JavaDoc
161     {
162         ArrayList groups = new ArrayList();
163         Iterator i = Server.getInstance().getStore().getGroupStore().getAllGroups();
164         while(i.hasNext())
165         {
166             GroupConcept concept = (GroupConcept)i.next();
167             groups.add(concept.getName());
168         }
169         
170         return groups;
171     }
172     
173     public ArrayList getAllUsers()
174     throws Exception JavaDoc
175     {
176         ArrayList users = new ArrayList();
177         Iterator i = Server.getInstance().getStore().getUserStore().getAllUsers();
178         while(i.hasNext())
179         {
180             UserConcept concept = (UserConcept)i.next();
181             users.add(concept.getName());
182         }
183         
184         return users;
185     }
186     
187     public void addAcl(String JavaDoc forum, AclInfo aclInfo)
188     throws Exception JavaDoc
189     {
190         AccessController acl = Server.getInstance().getAccessController();
191         if(aclInfo.getGroup() != null)
192         {
193             if(aclInfo.isAllow())
194                 acl.allowGroup(FORUM_APP, forum, aclInfo.getAccess(), aclInfo.getGroup());
195             else
196                 acl.denyGroup(FORUM_APP, forum, aclInfo.getAccess(), aclInfo.getGroup());
197         }
198         else if(aclInfo.getUser() != null)
199         {
200             if(aclInfo.isAllow())
201                 acl.allowUser(FORUM_APP, forum, aclInfo.getAccess(), aclInfo.getUser());
202             else
203                 acl.denyUser(FORUM_APP, forum, aclInfo.getAccess(), aclInfo.getUser());
204         }
205     }
206     
207     public void removeAcl(String JavaDoc forum, AclInfo aclInfo)
208     throws Exception JavaDoc
209     {
210         AccessController acl = Server.getInstance().getAccessController();
211         if(aclInfo.getGroup() != null)
212             acl.removeAclForGroup(FORUM_APP, forum, aclInfo.getAccess(), aclInfo.getGroup());
213         else if(aclInfo.getUser() != null)
214             acl.removeAclForUser(FORUM_APP, forum, aclInfo.getAccess(), aclInfo.getUser());
215     }
216
217     //introduced for acl webapp
218
public String JavaDoc[] getAccesses()
219     {
220         return new String JavaDoc[] {"Moderate", "Write", "Read"};
221     }
222
223     public String JavaDoc getItemName(String JavaDoc itemId)
224     {
225         return itemId;
226     }
227 }
228
Popular Tags