KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > view > admin > UserAction


1 /*
2  * Copyright (c) Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * This file creation date: Apr 19, 2003 / 9:13:16 PM
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.view.admin;
44
45 import java.util.ArrayList JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.List JavaDoc;
48
49 import net.jforum.SessionFacade;
50 import net.jforum.dao.DataAccessDriver;
51 import net.jforum.dao.GroupDAO;
52 import net.jforum.dao.UserDAO;
53 import net.jforum.entities.Group;
54 import net.jforum.entities.User;
55 import net.jforum.repository.SecurityRepository;
56 import net.jforum.util.I18n;
57 import net.jforum.util.TreeGroup;
58 import net.jforum.util.preferences.ConfigKeys;
59 import net.jforum.util.preferences.SystemGlobals;
60 import net.jforum.util.preferences.TemplateKeys;
61 import net.jforum.view.forum.common.UserCommon;
62 import net.jforum.view.forum.common.ViewCommon;
63
64 /**
65  * @author Rafael Steil
66  * @version $Id: UserAction.java,v 1.29 2005/09/30 23:19:20 rafaelsteil Exp $
67  */

68 public class UserAction extends AdminCommand
69 {
70     // Listing
71
public void list() throws Exception JavaDoc
72     {
73         int start = this.preparePagination(DataAccessDriver.getInstance().newUserDAO().getTotalUsers());
74         int usersPerPage = SystemGlobals.getIntValue(ConfigKeys.USERS_PER_PAGE);
75         
76         this.context.put("users", DataAccessDriver.getInstance().newUserDAO().selectAll(start ,usersPerPage));
77         this.commonData();
78     }
79     
80     private int preparePagination(int totalUsers)
81     {
82         int start = ViewCommon.getStartPage();
83         int usersPerPage = SystemGlobals.getIntValue(ConfigKeys.USERS_PER_PAGE);
84         
85         ViewCommon.contextToPagination(start, totalUsers, usersPerPage);
86         
87         return start;
88     }
89     
90     private void commonData() throws Exception JavaDoc
91     {
92         this.context.put("selectedList", new ArrayList JavaDoc());
93         this.context.put("groups", new TreeGroup().getNodes());
94         this.setTemplateName(TemplateKeys.USER_ADMIN_COMMON);
95         this.context.put("searchAction", "list");
96         this.context.put("searchId", new Integer JavaDoc(-1));
97     }
98     
99     public void groupSearch() throws Exception JavaDoc
100     {
101         final int groupId = this.request.getIntParameter("group_id");
102         if (groupId == 0) {
103             this.list();
104             return;
105         }
106         
107         UserDAO um = DataAccessDriver.getInstance().newUserDAO();
108         
109         int start = this.preparePagination(um.getTotalUsersByGroup(groupId));
110         int usersPerPage = SystemGlobals.getIntValue(ConfigKeys.USERS_PER_PAGE);
111         
112         this.commonData();
113         
114         List JavaDoc l = new ArrayList JavaDoc();
115         l.add(new Integer JavaDoc(groupId));
116         
117         this.context.put("selectedList", l);
118         this.context.put("searchAction", "groupSearch");
119         this.context.put("users", um.selectAllByGroup(groupId, start, usersPerPage));
120         this.context.put("searchId", new Integer JavaDoc(groupId));
121     }
122     
123     public void search() throws Exception JavaDoc
124     {
125         List JavaDoc users = new ArrayList JavaDoc();
126         String JavaDoc search = this.request.getParameter("username");
127         String JavaDoc group = this.request.getParameter("group");
128         
129         if (search != null && !"".equals(search)) {
130             users = DataAccessDriver.getInstance().newUserDAO().findByName(search, false);
131             
132             this.commonData();
133             
134             this.context.put("users", users);
135             this.context.put("search", search);
136             this.context.put("start", new Integer JavaDoc(1));
137         }
138         else if (!"0".equals(group)) {
139             this.groupSearch();
140             return;
141         }
142         else {
143             this.list();
144             return;
145         }
146     }
147     
148     public void edit() throws Exception JavaDoc
149     {
150         int userId = this.request.getIntParameter("id");
151         UserDAO um = DataAccessDriver.getInstance().newUserDAO();
152         User u = um.selectById(userId);
153         
154         this.context.put("u", u);
155         this.context.put("action", "editSave");
156         this.setTemplateName(TemplateKeys.USER_ADMIN_EDIT);
157         this.context.put("admin", true);
158     }
159     
160     public void editSave() throws Exception JavaDoc
161     {
162         int userId = this.request.getIntParameter("user_id");
163         UserCommon.saveUser(userId);
164
165         this.list();
166     }
167
168     // Delete
169
public void delete() throws Exception JavaDoc
170     {
171         String JavaDoc ids[] = this.request.getParameterValues("user_id");
172         UserDAO um = DataAccessDriver.getInstance().newUserDAO();
173         
174         if (ids != null) {
175             for (int i = 0; i < ids.length; i++) {
176                 
177                 int user = Integer.parseInt(ids[i]);
178                 
179                 if (um.isDeleted(user)){
180                     um.undelete(user);
181                 }
182                 else {
183                     String JavaDoc sessionId = SessionFacade.isUserInSession(user);
184                     
185                     if (sessionId != null) {
186                         SessionFacade.remove(sessionId);
187                     }
188                     
189                     um.delete(user);
190                 }
191             }
192         }
193         
194         this.list();
195     }
196     
197     // Groups
198
public void groups() throws Exception JavaDoc
199     {
200         int userId = this.request.getIntParameter("id");
201         
202         UserDAO um = DataAccessDriver.getInstance().newUserDAO();
203         User u = um.selectById(userId);
204         
205         List JavaDoc selectedList = new ArrayList JavaDoc();
206         for (Iterator JavaDoc iter = u.getGroupsList().iterator(); iter.hasNext(); ) {
207             selectedList.add(new Integer JavaDoc(((Group)iter.next()).getId()));
208         }
209         
210         this.context.put("selectedList", selectedList);
211         this.context.put("groups", new TreeGroup().getNodes());
212         this.context.put("user", u);
213         this.context.put("userId", new Integer JavaDoc(userId));
214         this.setTemplateName(TemplateKeys.USER_ADMIN_GROUPS);
215         this.context.put("groupFor", I18n.getMessage("User.GroupsFor", new String JavaDoc[] { u.getUsername() }));
216     }
217     
218     // Groups Save
219
public void groupsSave() throws Exception JavaDoc
220     {
221         int userId = this.request.getIntParameter("user_id");
222         
223         UserDAO um = DataAccessDriver.getInstance().newUserDAO();
224         GroupDAO gm = DataAccessDriver.getInstance().newGroupDAO();
225         
226         // Remove the old groups
227
List JavaDoc allGroupsList = gm.selectAll();
228         int[] allGroups = new int[allGroupsList.size()];
229         
230         int counter = 0;
231         for (Iterator JavaDoc iter = allGroupsList.iterator(); iter.hasNext(); counter++) {
232             Group g = (Group)iter.next();
233             
234             allGroups[counter] = g.getId();
235         }
236         
237         um.removeFromGroup(userId, allGroups);
238         
239         // Associate the user to the selected groups
240
String JavaDoc[] selectedGroups = this.request.getParameterValues("groups");
241         
242         if(selectedGroups == null) {
243             selectedGroups = new String JavaDoc[0];
244         }
245         
246         int[] newGroups = new int[selectedGroups.length];
247         
248         for (int i = 0; i < selectedGroups.length; i++) {
249             newGroups[i] = Integer.parseInt(selectedGroups[i]);
250         }
251         
252         um.addToGroup(userId, newGroups);
253         SecurityRepository.remove(userId);
254         
255         this.list();
256     }
257 }
258
Popular Tags