KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > action > DlogCategoryAction


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

16 package dlog4j.action;
17
18 import java.sql.SQLException JavaDoc;
19 import java.util.List JavaDoc;
20
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23
24 import net.sf.hibernate.HibernateException;
25 import net.sf.hibernate.Session;
26
27 import org.apache.struts.action.ActionError;
28 import org.apache.struts.action.ActionErrors;
29 import org.apache.struts.action.ActionForm;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32
33 import dlog4j.CategoryManager;
34 import dlog4j.LogManager;
35 import dlog4j.SiteManager;
36 import dlog4j.formbean.CategoryForm;
37 import dlog4j.formbean.SiteForm;
38
39 /**
40  * @author Liang.xf 2004-2-22 informix
41  *
42  */

43 public class DlogCategoryAction extends AdminActionBase {
44
45     public final static String JavaDoc SUCCESS_LIST_PAGE = "success";
46     public final static String JavaDoc LIST_ERROR_KEY = "list";
47     public final static String JavaDoc EDIT_ERROR_KEY = "edit";
48     /**
49      * 修改日记分类
50      * @param mapping
51      * @param form
52      * @param request
53      * @param response
54      * @param cat_id
55      * @return
56      * @throws Exception
57      */

58     public ActionForward doEditCategory(
59             ActionMapping mapping,
60             ActionForm form,
61             HttpServletRequest JavaDoc request,
62             HttpServletResponse JavaDoc response)
63             throws Exception JavaDoc
64     {
65         ActionErrors errors = new ActionErrors();
66         
67         CategoryForm cat = (CategoryForm)form;
68         Session ssn = null;
69         try {
70             ssn = getSession();
71             SiteForm site = SiteManager.getCurrentSite(request);
72             CategoryForm old = CategoryManager.getCategory(ssn,site,cat.getId());
73             if(old!=null) {
74                 old.setName(cat.getName());
75                 old.setType(cat.getType());
76                 ssn.update(old);
77             }
78         } catch(SQLException JavaDoc e) {
79             errors.add(EDIT_ERROR_KEY,new ActionError("database_exception"));
80         } catch(HibernateException e) {
81             errors.add(EDIT_ERROR_KEY,new ActionError("hibernate_exception"));
82         } finally {
83             commitSession(ssn, true);
84         }
85         if (!errors.isEmpty())
86             saveErrors(request, errors);
87         ActionForward forward = mapping.getInputForward();
88         forward.setRedirect(true);
89         return forward;
90     }
91     /**
92      * 创建日记分类
93      * @param mapping
94      * @param form
95      * @param request
96      * @param response
97      * @param cat_id
98      * @return
99      * @throws Exception
100      */

101     public ActionForward doCreateCategory(
102             ActionMapping mapping,
103             ActionForm form,
104             HttpServletRequest JavaDoc request,
105             HttpServletResponse JavaDoc response)
106             throws Exception JavaDoc
107     {
108         ActionErrors errors = new ActionErrors();
109         
110         CategoryForm cat = (CategoryForm)form;
111         Session ssn = null;
112         try {
113             ssn = getSession();
114             SiteForm site = SiteManager.getCurrentSite(request);
115             cat.setSite(site);
116             List JavaDoc categories = CategoryManager.listCategories(ssn,site);
117             int position = 1;
118             try {//默认插在最后
119
position = Integer.parseInt(request.getParameter("position"));
120             }catch(Exception JavaDoc e) {}
121             int catid = cat.getOrder();//插在该位置的之前或者之后
122
//智能顺序选择
123
if(cat.getOrder()==-1) {
124                 if(categories.size()==0)
125                     cat.setOrder(1);
126                 else
127                     cat.setOrder(((CategoryForm)categories.get(categories.size()-1)).getOrder()+1);
128             }
129             else
130             for(int i=0;i<categories.size();i++) {
131                 CategoryForm c = (CategoryForm)categories.get(i);
132                 if(c.getId()==catid) {
133                     if(position>0) {//插在这之后
134
if(i==(categories.size()-1)) {//当前已经是最后一个位置了
135
cat.setOrder(((CategoryForm)categories.get(i)).getOrder()+1);
136                             break;
137                         }
138                         cat.setOrder(((CategoryForm)categories.get(i)).getOrder()+1);
139                         if(cat.getOrder()==((CategoryForm)categories.get(i+1)).getOrder()) {
140                             for(int j=i+1;j<categories.size();j++) {
141                                 CategoryForm cf = (CategoryForm)categories.get(j);
142                                 cf.setOrder(cf.getOrder()+1);
143                                 ssn.update(cf);
144                             }
145                         }
146                     }
147                     else {//插在这之前
148
CategoryForm cur = (CategoryForm)categories.get(i);
149                         if(i>0) {
150                             if((cur.getOrder()-1)>((CategoryForm)categories.get(i-1)).getOrder()) {
151                                 cat.setOrder(cur.getOrder()-1);
152                                 break;
153                             }
154                         }
155                         cat.setOrder((cur).getOrder());
156                         for(int j=i;j<categories.size();j++) {
157                             CategoryForm cf = (CategoryForm)categories.get(j);
158                             cf.setOrder(cf.getOrder()+1);
159                             ssn.update(cf);
160                         }
161                     }
162                     break;
163                 }
164             }
165             ssn.save(cat);
166         } catch(SQLException JavaDoc e) {
167             errors.add(EDIT_ERROR_KEY,new ActionError("database_exception"));
168         } catch(HibernateException e) {
169             errors.add(EDIT_ERROR_KEY,new ActionError("hibernate_exception"));
170         } finally {
171             commitSession(ssn, true);
172         }
173         
174         if (!errors.isEmpty())
175             saveErrors(request, errors);
176         ActionForward forward = mapping.getInputForward();
177         forward.setRedirect(true);
178         return forward;
179     }
180     /**
181      * 日记分类排序,向上一个位置
182      * @param mapping
183      * @param form
184      * @param request
185      * @param response
186      * @param cat_id
187      * @return
188      * @throws Exception
189      */

190     public ActionForward doMoveDown(
191             ActionMapping mapping,
192             ActionForm form,
193             HttpServletRequest JavaDoc request,
194             HttpServletResponse JavaDoc response,
195             String JavaDoc cat_id)
196             throws Exception JavaDoc {
197         ActionErrors errors = new ActionErrors();
198         Session session = null;
199         try {
200             session = getSession();
201             int catid = Integer.parseInt(cat_id);
202             SiteForm site = SiteManager.getCurrentSite(request);
203             List JavaDoc categories = CategoryManager.listCategories(session,site);
204             int i;
205             for(i=0;i<categories.size();i++) {
206                 CategoryForm cat = (CategoryForm)categories.get(i);
207                 if(cat.getId()==catid) {
208                     break;
209                 }
210             }
211             if(i==categories.size())
212                 errors.add(LIST_ERROR_KEY,new ActionError("category_not_found"));
213             int next_idx = i+1;
214             int me_idx = i;
215             if(next_idx<categories.size()) {
216                 CategoryForm me = (CategoryForm)categories.get(me_idx);
217                 CategoryForm front = (CategoryForm)categories.get(next_idx);
218                 //交换order值
219
int temp = me.getOrder();
220                 me.setOrder(front.getOrder());
221                 front.setOrder(temp);
222                 session.update(me);
223                 session.update(front);
224             }
225         } catch(SQLException JavaDoc e) {
226             errors.add(LIST_ERROR_KEY,new ActionError("database_exception"));
227         } catch(HibernateException e) {
228             errors.add(LIST_ERROR_KEY,new ActionError("hibernate_exception"));
229         } finally {
230             commitSession(session, true);
231         }
232         // Report any errors we have discovered back to the original form
233
ActionForward forward = mapping.getInputForward();
234         if (!errors.isEmpty())
235             saveErrors(request, errors);
236         else
237             forward.setRedirect(true);
238         return forward;
239     }
240     /**
241      * 日记分类排序,向下一个位置
242      * @param mapping
243      * @param form
244      * @param request
245      * @param response
246      * @param cat_id
247      * @return
248      * @throws Exception
249      */

250     public ActionForward doMoveUp(
251             ActionMapping mapping,
252             ActionForm form,
253             HttpServletRequest JavaDoc request,
254             HttpServletResponse JavaDoc response,
255             String JavaDoc cat_id)
256             throws Exception JavaDoc
257     {
258         ActionErrors errors = new ActionErrors();
259         Session session = null;
260         try {
261             session = getSession();
262             int catid = Integer.parseInt(cat_id);
263             SiteForm site = SiteManager.getCurrentSite(request);
264             List JavaDoc categories = CategoryManager.listCategories(session,site);
265             int i;
266             for(i=0;i<categories.size();i++) {
267                 CategoryForm cat = (CategoryForm)categories.get(i);
268                 if(cat.getId()==catid) {
269                     break;
270                 }
271             }
272             if(i==categories.size())
273                 errors.add(LIST_ERROR_KEY,new ActionError("category_not_found"));
274             int front_idx = i-1;
275             int me_idx = i;
276             if(front_idx>=0) {
277                 CategoryForm me = (CategoryForm)categories.get(me_idx);
278                 CategoryForm front = (CategoryForm)categories.get(front_idx);
279                 //交换order值
280
int temp = me.getOrder();
281                 me.setOrder(front.getOrder());
282                 front.setOrder(temp);
283                 session.update(me);
284                 session.update(front);
285             }
286         } catch(SQLException JavaDoc e) {
287             errors.add(LIST_ERROR_KEY,new ActionError("database_exception"));
288         } catch(HibernateException e) {
289             errors.add(LIST_ERROR_KEY,new ActionError("hibernate_exception"));
290         } finally {
291             commitSession(session, true);
292         }
293         // Report any errors we have discovered back to the original form
294
ActionForward forward = mapping.getInputForward();
295         if (!errors.isEmpty())
296             saveErrors(request, errors);
297         else
298             forward.setRedirect(true);
299         return forward;
300     }
301     /**
302      * 删除日记分类
303      * @param mapping
304      * @param form
305      * @param request
306      * @param response
307      * @param cat_id
308      * @return
309      * @throws Exception
310      */

311     public ActionForward doDeleteCategory(
312             ActionMapping mapping,
313             ActionForm form,
314             HttpServletRequest JavaDoc request,
315             HttpServletResponse JavaDoc response,
316             String JavaDoc cat_id)
317             throws Exception JavaDoc {
318
319             ActionErrors errors = new ActionErrors();
320             Session session = null;
321             try {
322                 session = getSession();
323                 int catid = Integer.parseInt(cat_id);
324                 SiteForm site = SiteManager.getCurrentSite(request);
325                 CategoryForm cat = CategoryManager.getCategory(session,site,catid);
326                 if(cat!=null) {
327                     int logcount = LogManager.getLogCount(session,catid);
328                     if(logcount>0)//还有日记不允许删除
329
errors.add(LIST_ERROR_KEY,new ActionError("logs_not_empty"));
330                     else{
331                         session.delete(cat);
332                         commitSession(session, false);
333                     }
334                 }
335                 else
336                     errors.add(LIST_ERROR_KEY,new ActionError("category_not_found"));
337             } catch(HibernateException e) {
338                 errors.add(LIST_ERROR_KEY,new ActionError("hibernate_exception"));
339             } catch(SQLException JavaDoc e) {
340                 errors.add(LIST_ERROR_KEY,new ActionError("database_exception"));
341             } finally {
342                 closeSession(session);
343             }
344             // Report any errors we have discovered back to the original form
345
if (!errors.isEmpty())
346                 saveErrors(request, errors);
347             ActionForward forward = mapping.getInputForward();
348             if(errors.isEmpty())
349                 forward.setRedirect(true);
350             return forward;
351         }
352     /**
353      * 增加Category
354      *
355      * @param mapping
356      * @param form
357      * @param request
358      * @param response
359      * @return @throws
360      * Exception
361      */

362     public ActionForward doAddCat(
363         ActionMapping mapping,
364         ActionForm form,
365         HttpServletRequest JavaDoc request,
366         HttpServletResponse JavaDoc response)
367         throws Exception JavaDoc {
368
369         Session session = getSession();
370         CategoryForm cat = (CategoryForm) form;
371         try {
372             cat.setSite(SiteManager.getCurrentSite(request));
373             session.save(cat);
374         } catch (Exception JavaDoc e) {
375             System.out.println("error when save Category: " + e);
376         } finally {
377             commitSession(session, true);
378         }
379         return mapping.findForward(SUCCESS_LIST_PAGE);
380     }
381
382     /**
383      * 获取某个分类的详细资料信息
384      *
385      * @param ssn
386      * @param userid
387      * @param withDetails
388      * @return @throws
389      * HibernateException
390      */

391     public static CategoryForm getUser(
392         Session ssn,
393         int catid,
394         boolean withDetails)
395         throws HibernateException {
396         CategoryForm cat =
397             (CategoryForm) ssn.load(CategoryForm.class, new Integer JavaDoc(catid));
398
399         return cat;
400     }
401 }
402
Popular Tags