KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > efs > openreports > actions > admin > EditGroupAction


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

19
20 package org.efs.openreports.actions.admin;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import com.opensymphony.xwork.ActionContext;
26 import com.opensymphony.xwork.ActionSupport;
27
28 import org.efs.openreports.ORStatics;
29 import org.efs.openreports.objects.Report;
30 import org.efs.openreports.objects.ReportGroup;
31 import org.efs.openreports.objects.ReportUser;
32 import org.efs.openreports.providers.*;
33
34 public class EditGroupAction extends ActionSupport implements ReportProviderAware, GroupProviderAware, UserProviderAware
35 {
36     private String JavaDoc command;
37     private String JavaDoc submitType;
38
39     private int id;
40     private String JavaDoc name;
41     private String JavaDoc description;
42
43     private ReportGroup reportGroup;
44     private int[] reportIds;
45
46     private GroupProvider groupProvider;
47     private ReportProvider reportProvider;
48     private UserProvider userProvider;
49
50     public String JavaDoc execute()
51     {
52         try
53         {
54             if (command.equals("edit"))
55             {
56                 reportGroup = groupProvider.getReportGroup(new Integer JavaDoc(id));
57             }
58             else
59             {
60                 reportGroup = new ReportGroup();
61             }
62
63             if (command.equals("edit") && submitType == null)
64             {
65                 name = reportGroup.getName();
66                 description = reportGroup.getDescription();
67                 reportIds = null;
68             }
69
70             if (submitType == null)
71                 return INPUT;
72
73             reportGroup.setName(name);
74             reportGroup.setDescription(description);
75             reportGroup.setReports(convertIdsToReports(reportIds));
76
77             if (command.equals("edit"))
78             {
79                 groupProvider.updateReportGroup(reportGroup);
80             }
81
82             if (command.equals("add"))
83             {
84                 groupProvider.insertReportGroup(reportGroup);
85             }
86             
87             // refresh current user
88
ReportUser user = (ReportUser) ActionContext.getContext().getSession().get(ORStatics.REPORT_USER);
89             if (user != null)
90             {
91                 user = userProvider.getUser(user.getId());
92                 ActionContext.getContext().getSession().put(ORStatics.REPORT_USER, user);
93             }
94
95             return SUCCESS;
96         }
97         catch (Exception JavaDoc e)
98         {
99             addActionError(e.toString());
100             return INPUT;
101         }
102     }
103
104     public String JavaDoc getCommand()
105     {
106         return command;
107     }
108
109     public String JavaDoc getDescription()
110     {
111         return description;
112     }
113
114     public String JavaDoc getName()
115     {
116         return name;
117     }
118
119     public String JavaDoc getSubmitType()
120     {
121         return submitType;
122     }
123
124     public void setCommand(String JavaDoc command)
125     {
126         this.command = command;
127     }
128
129     public void setDescription(String JavaDoc description)
130     {
131         this.description = description;
132     }
133
134     public void setName(String JavaDoc name)
135     {
136         this.name = name;
137     }
138
139     public void setSubmitType(String JavaDoc submitType)
140     {
141         this.submitType = submitType;
142     }
143
144     public List JavaDoc getReports()
145     {
146         try
147         {
148             return reportProvider.getReports();
149         }
150         catch (Exception JavaDoc e)
151         {
152             addActionError(e.getMessage());
153             return null;
154         }
155     }
156
157     public List JavaDoc getReportsInGroup()
158     {
159         return reportGroup.getReports();
160     }
161
162     public int getId()
163     {
164         return id;
165     }
166
167     public void setId(int id)
168     {
169         this.id = id;
170     }
171
172     public int[] getReportIds()
173     {
174         return reportIds;
175     }
176
177     public void setReportIds(int[] reportIds)
178     {
179         this.reportIds = reportIds;
180     }
181
182     private List JavaDoc convertIdsToReports(int[] ids)
183     {
184         if (ids == null)
185             return null;
186
187         List JavaDoc reports = new ArrayList JavaDoc();
188
189         try
190         {
191             for (int i = 0; i < ids.length; i++)
192             {
193                 Report report = reportProvider.getReport(new Integer JavaDoc(ids[i]));
194                 reports.add(report);
195             }
196         }
197         catch (Exception JavaDoc e)
198         {
199             addActionError(e.toString());
200         }
201
202         return reports;
203     }
204
205     public void setGroupProvider(GroupProvider groupProvider)
206     {
207         this.groupProvider = groupProvider;
208     }
209
210     public void setReportProvider(ReportProvider reportProvider)
211     {
212         this.reportProvider = reportProvider;
213     }
214
215     public void setUserProvider(UserProvider userProvider)
216     {
217         this.userProvider = userProvider;
218     }
219
220 }
Popular Tags