KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > communicator > admin > controller > AccountManagementAction


1 /*
2  * AccountManagementAction.java
3  *
4  * Created on May 4, 2003, 11:10 AM
5  */

6
7 package com.quikj.application.communicator.admin.controller;
8
9 import javax.servlet.http.*;
10 import org.apache.struts.action.*;
11 import java.sql.*;
12
13 import com.quikj.application.communicator.admin.model.*;
14 import com.quikj.server.framework.*;
15
16 /**
17  *
18  * @author bhm
19  */

20 public class AccountManagementAction extends Action
21 {
22     
23     /** Creates a new instance of AccountManagementAction */
24     public AccountManagementAction()
25     {
26     }
27     
28     public ActionForward execute(ActionMapping mapping,
29     ActionForm form,
30     HttpServletRequest request,
31     HttpServletResponse response)
32     {
33         AccountManagementForm aform = (AccountManagementForm)form;
34         
35         ActionErrors errors = new ActionErrors();
36         
37         Connection c = (Connection)request.getSession().getAttribute("connection");
38         if (c == null)
39         {
40             errors.add(ActionErrors.GLOBAL_ERROR,
41             new ActionError("error.not.logged.in"));
42             saveErrors(request, errors);
43             return mapping.findForward("logon");
44         }
45         
46         AccountElement element = (AccountElement)request.getSession().getAttribute("userInfo");
47         if (element.isAdminLevel() == false)
48         {
49             errors.add(ActionErrors.GLOBAL_ERROR,
50             new ActionError("error.insufficient.privilege"));
51             saveErrors(request, errors);
52             
53             return mapping.findForward("main_menu");
54         }
55                 
56         if (aform.getSubmit().equals("Find") == true)
57         {
58             AccountsTable accounts = new AccountsTable(AdminConfig.getInstance().getDBParams().getAdminDb());
59             accounts.setConnection(c);
60             
61             AccountElement e = accounts.query(aform.getName());
62             
63             if (e == null)
64             {
65                 errors.add(ActionErrors.GLOBAL_ERROR,
66                 new ActionError("error.no.such.user"));
67                 
68                 if (accounts.getErrorMessage() != null)
69                 {
70                     AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
71                     "AccountManagementAction.execute()/Find/by-"
72                     + element.getName()
73                     + ": "
74                     + accounts.getErrorMessage());
75                 }
76             }
77             else
78             {
79                 aform.setAdditionalInfo(e.getAdditionalInfo());
80                 aform.setAssignedFeatures(e.getFeatureList());
81                 aform.setLevel(e.getLevel());
82                 aform.setDomain(e.getDomain());
83             }
84         }
85         else if (aform.getSubmit().equals("Modify") == true)
86         {
87             AccountsTable accounts = new AccountsTable(AdminConfig.getInstance().getDBParams().getAdminDb());
88             accounts.setConnection(c);
89             
90             AccountElement e = new AccountElement();
91             e.setAdditionalInfo(aform.getAdditionalInfo());
92             e.setLevel(aform.getLevel());
93             e.setName(aform.getName());
94             e.setDomain(aform.getDomain());
95             
96             String JavaDoc password = aform.getPassword();
97             if ((password != null) && (password.length() > 0))
98             {
99                 e.setPassword(password);
100             }
101             
102             Object JavaDoc[] features = aform.getAssignedFeatures();
103             for (int i = 0; i < features.length; i++)
104             {
105                 if (features[i] != null)
106                 {
107                     e.addFeature(features[i].toString());
108                 }
109             }
110             
111             if (aform.getName().equals(element.getName()) == true)
112             {
113                 errors.add(ActionErrors.GLOBAL_ERROR,
114                 new ActionError("error.account.modify.self"));
115             }
116             else if (accounts.modify(e) == false)
117             {
118                 errors.add(ActionErrors.GLOBAL_ERROR,
119                 new ActionError("error.no.such.user"));
120                 
121                 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
122                     "AccountManagementAction.execute()/Modify/by-"
123                     + element.getName()
124                     + ": "
125                     + accounts.getErrorMessage());
126             }
127             else
128             {
129                 AceLogger.Instance().log(AceLogger.INFORMATIONAL, AceLogger.USER_LOG,
130                 "User " + element.getName() + " modified account for user " +
131                 aform.getName());
132
133                 // forward control to the main menu
134
ActionMessages messages = new ActionMessages();
135                 messages.add(ActionMessages.GLOBAL_MESSAGE,
136                 new ActionMessage("message.account.modified"));
137                 saveMessages(request, messages);
138                 return mapping.findForward("main_menu");
139             }
140         }
141         else if (aform.getSubmit().equals("Create") == true)
142         {
143             AccountsTable accounts = new AccountsTable(AdminConfig.getInstance().getDBParams().getAdminDb());
144             accounts.setConnection(c);
145             
146             AccountElement e = new AccountElement();
147             e.setAdditionalInfo(aform.getAdditionalInfo());
148             e.setLevel(aform.getLevel());
149             e.setName(aform.getName());
150             e.setPassword(aform.getPassword());
151             e.setDomain(aform.getDomain());
152             
153             Object JavaDoc[] features = aform.getAssignedFeatures();
154             for (int i = 0; i < features.length; i++)
155             {
156                 if (features[i] != null)
157                 {
158                     e.addFeature(features[i].toString());
159                 }
160             }
161
162             if (accounts.create(e) == false)
163             {
164                 errors.add(ActionErrors.GLOBAL_ERROR,
165                 new ActionError("error.account.create.failure"));
166                 
167                 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
168                 "AccountManagementAction.execute()/Create/by-"
169                 + element.getName()
170                 + ": "
171                 + accounts.getErrorMessage());
172             }
173          else
174             {
175                 AceLogger.Instance().log(AceLogger.INFORMATIONAL, AceLogger.USER_LOG,
176                 "User " + element.getName() + " created account for user " +
177                 aform.getName());
178                 
179                 // forward control to the main menu
180
ActionMessages messages = new ActionMessages();
181                 messages.add(ActionMessages.GLOBAL_MESSAGE,
182                 new ActionMessage("message.account.created"));
183                 saveMessages(request, messages);
184                 return mapping.findForward("main_menu");
185             }
186         }
187         else if (aform.getSubmit().equals("Delete") == true)
188         {
189             AccountsTable accounts = new AccountsTable(AdminConfig.getInstance().getDBParams().getAdminDb());
190             accounts.setConnection(c);
191             
192             if (aform.getName().equals(element.getName()) == true)
193             {
194                 errors.add(ActionErrors.GLOBAL_ERROR,
195                 new ActionError("error.account.delete.self"));
196             }
197             else if (accounts.delete(aform.getName()) == false)
198             {
199                 if (accounts.getErrorMessage() == null)
200                 {
201                     errors.add(ActionErrors.GLOBAL_ERROR,
202                     new ActionError("error.no.such.user"));
203                 }
204                 else
205                 {
206                     errors.add(ActionErrors.GLOBAL_ERROR,
207                     new ActionError("error.db.failure"));
208                     
209                     AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
210                     "AccountManagementAction.execute()/Delete/by-"
211                     + element.getName()
212                     + ": "
213                     + accounts.getErrorMessage());
214                 }
215             }
216             else
217             {
218                 AceLogger.Instance().log(AceLogger.INFORMATIONAL, AceLogger.USER_LOG,
219                 "User " + element.getName() + " deleted account for user " +
220                 aform.getName());
221                 
222                 // forward control to the main menu
223
ActionMessages messages = new ActionMessages();
224                 messages.add(ActionMessages.GLOBAL_MESSAGE,
225                 new ActionMessage("message.account.deleted"));
226                 saveMessages(request, messages);
227                 return mapping.findForward("main_menu");
228             }
229         }
230         
231         if (errors.isEmpty() == false)
232         {
233             saveErrors(request, errors);
234         }
235         
236         // add related tasks to the navigation bar
237
/*
238         MenuLinks menu = new MenuLinks();
239         LinkAttribute attr = new LinkAttribute();
240         attr.setName("View users");
241         attr.setLink("list_accounts");
242         menu.addLink(attr);;
243         request.setAttribute("menu", menu);
244          **/

245         
246         return mapping.getInputForward();
247     }
248 }
249
Popular Tags