001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU Library General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package dlog4j.action;
017:
018: import java.sql.SQLException;
019: import java.util.List;
020:
021: import javax.servlet.http.HttpServletRequest;
022: import javax.servlet.http.HttpServletResponse;
023:
024: import net.sf.hibernate.HibernateException;
025: import net.sf.hibernate.Session;
026:
027: import org.apache.struts.action.ActionError;
028: import org.apache.struts.action.ActionErrors;
029: import org.apache.struts.action.ActionForm;
030: import org.apache.struts.action.ActionForward;
031: import org.apache.struts.action.ActionMapping;
032:
033: import dlog4j.CategoryManager;
034: import dlog4j.LogManager;
035: import dlog4j.SiteManager;
036: import dlog4j.formbean.CategoryForm;
037: import dlog4j.formbean.SiteForm;
038:
039: /**
040: * @author Liang.xf 2004-2-22 informix
041: *
042: */
043: public class DlogCategoryAction extends AdminActionBase {
044:
045: public final static String SUCCESS_LIST_PAGE = "success";
046: public final static String LIST_ERROR_KEY = "list";
047: public final static String EDIT_ERROR_KEY = "edit";
048:
049: /**
050: * 修改日记分类
051: * @param mapping
052: * @param form
053: * @param request
054: * @param response
055: * @param cat_id
056: * @return
057: * @throws Exception
058: */
059: public ActionForward doEditCategory(ActionMapping mapping,
060: ActionForm form, HttpServletRequest request,
061: HttpServletResponse response) throws Exception {
062: ActionErrors errors = new ActionErrors();
063:
064: CategoryForm cat = (CategoryForm) form;
065: Session ssn = null;
066: try {
067: ssn = getSession();
068: SiteForm site = SiteManager.getCurrentSite(request);
069: CategoryForm old = CategoryManager.getCategory(ssn, site,
070: cat.getId());
071: if (old != null) {
072: old.setName(cat.getName());
073: old.setType(cat.getType());
074: ssn.update(old);
075: }
076: } catch (SQLException e) {
077: errors.add(EDIT_ERROR_KEY, new ActionError(
078: "database_exception"));
079: } catch (HibernateException e) {
080: errors.add(EDIT_ERROR_KEY, new ActionError(
081: "hibernate_exception"));
082: } finally {
083: commitSession(ssn, true);
084: }
085: if (!errors.isEmpty())
086: saveErrors(request, errors);
087: ActionForward forward = mapping.getInputForward();
088: forward.setRedirect(true);
089: return forward;
090: }
091:
092: /**
093: * 创建日记分类
094: * @param mapping
095: * @param form
096: * @param request
097: * @param response
098: * @param cat_id
099: * @return
100: * @throws Exception
101: */
102: public ActionForward doCreateCategory(ActionMapping mapping,
103: ActionForm form, HttpServletRequest request,
104: HttpServletResponse response) throws Exception {
105: ActionErrors errors = new ActionErrors();
106:
107: CategoryForm cat = (CategoryForm) form;
108: Session ssn = null;
109: try {
110: ssn = getSession();
111: SiteForm site = SiteManager.getCurrentSite(request);
112: cat.setSite(site);
113: List categories = CategoryManager.listCategories(ssn, site);
114: int position = 1;
115: try {//默认插在最后
116: position = Integer.parseInt(request
117: .getParameter("position"));
118: } catch (Exception e) {
119: }
120: int catid = cat.getOrder();//插在该位置的之前或者之后
121: //智能顺序选择
122: if (cat.getOrder() == -1) {
123: if (categories.size() == 0)
124: cat.setOrder(1);
125: else
126: cat
127: .setOrder(((CategoryForm) categories
128: .get(categories.size() - 1))
129: .getOrder() + 1);
130: } else
131: for (int i = 0; i < categories.size(); i++) {
132: CategoryForm c = (CategoryForm) categories.get(i);
133: if (c.getId() == catid) {
134: if (position > 0) {//插在这之后
135: if (i == (categories.size() - 1)) {//当前已经是最后一个位置了
136: cat.setOrder(((CategoryForm) categories
137: .get(i)).getOrder() + 1);
138: break;
139: }
140: cat.setOrder(((CategoryForm) categories
141: .get(i)).getOrder() + 1);
142: if (cat.getOrder() == ((CategoryForm) categories
143: .get(i + 1)).getOrder()) {
144: for (int j = i + 1; j < categories
145: .size(); j++) {
146: CategoryForm cf = (CategoryForm) categories
147: .get(j);
148: cf.setOrder(cf.getOrder() + 1);
149: ssn.update(cf);
150: }
151: }
152: } else {//插在这之前
153: CategoryForm cur = (CategoryForm) categories
154: .get(i);
155: if (i > 0) {
156: if ((cur.getOrder() - 1) > ((CategoryForm) categories
157: .get(i - 1)).getOrder()) {
158: cat.setOrder(cur.getOrder() - 1);
159: break;
160: }
161: }
162: cat.setOrder((cur).getOrder());
163: for (int j = i; j < categories.size(); j++) {
164: CategoryForm cf = (CategoryForm) categories
165: .get(j);
166: cf.setOrder(cf.getOrder() + 1);
167: ssn.update(cf);
168: }
169: }
170: break;
171: }
172: }
173: ssn.save(cat);
174: } catch (SQLException e) {
175: errors.add(EDIT_ERROR_KEY, new ActionError(
176: "database_exception"));
177: } catch (HibernateException e) {
178: errors.add(EDIT_ERROR_KEY, new ActionError(
179: "hibernate_exception"));
180: } finally {
181: commitSession(ssn, true);
182: }
183:
184: if (!errors.isEmpty())
185: saveErrors(request, errors);
186: ActionForward forward = mapping.getInputForward();
187: forward.setRedirect(true);
188: return forward;
189: }
190:
191: /**
192: * 日记分类排序,向上一个位置
193: * @param mapping
194: * @param form
195: * @param request
196: * @param response
197: * @param cat_id
198: * @return
199: * @throws Exception
200: */
201: public ActionForward doMoveDown(ActionMapping mapping,
202: ActionForm form, HttpServletRequest request,
203: HttpServletResponse response, String cat_id)
204: throws Exception {
205: ActionErrors errors = new ActionErrors();
206: Session session = null;
207: try {
208: session = getSession();
209: int catid = Integer.parseInt(cat_id);
210: SiteForm site = SiteManager.getCurrentSite(request);
211: List categories = CategoryManager.listCategories(session,
212: site);
213: int i;
214: for (i = 0; i < categories.size(); i++) {
215: CategoryForm cat = (CategoryForm) categories.get(i);
216: if (cat.getId() == catid) {
217: break;
218: }
219: }
220: if (i == categories.size())
221: errors.add(LIST_ERROR_KEY, new ActionError(
222: "category_not_found"));
223: int next_idx = i + 1;
224: int me_idx = i;
225: if (next_idx < categories.size()) {
226: CategoryForm me = (CategoryForm) categories.get(me_idx);
227: CategoryForm front = (CategoryForm) categories
228: .get(next_idx);
229: //交换order值
230: int temp = me.getOrder();
231: me.setOrder(front.getOrder());
232: front.setOrder(temp);
233: session.update(me);
234: session.update(front);
235: }
236: } catch (SQLException e) {
237: errors.add(LIST_ERROR_KEY, new ActionError(
238: "database_exception"));
239: } catch (HibernateException e) {
240: errors.add(LIST_ERROR_KEY, new ActionError(
241: "hibernate_exception"));
242: } finally {
243: commitSession(session, true);
244: }
245: // Report any errors we have discovered back to the original form
246: ActionForward forward = mapping.getInputForward();
247: if (!errors.isEmpty())
248: saveErrors(request, errors);
249: else
250: forward.setRedirect(true);
251: return forward;
252: }
253:
254: /**
255: * 日记分类排序,向下一个位置
256: * @param mapping
257: * @param form
258: * @param request
259: * @param response
260: * @param cat_id
261: * @return
262: * @throws Exception
263: */
264: public ActionForward doMoveUp(ActionMapping mapping,
265: ActionForm form, HttpServletRequest request,
266: HttpServletResponse response, String cat_id)
267: throws Exception {
268: ActionErrors errors = new ActionErrors();
269: Session session = null;
270: try {
271: session = getSession();
272: int catid = Integer.parseInt(cat_id);
273: SiteForm site = SiteManager.getCurrentSite(request);
274: List categories = CategoryManager.listCategories(session,
275: site);
276: int i;
277: for (i = 0; i < categories.size(); i++) {
278: CategoryForm cat = (CategoryForm) categories.get(i);
279: if (cat.getId() == catid) {
280: break;
281: }
282: }
283: if (i == categories.size())
284: errors.add(LIST_ERROR_KEY, new ActionError(
285: "category_not_found"));
286: int front_idx = i - 1;
287: int me_idx = i;
288: if (front_idx >= 0) {
289: CategoryForm me = (CategoryForm) categories.get(me_idx);
290: CategoryForm front = (CategoryForm) categories
291: .get(front_idx);
292: //交换order值
293: int temp = me.getOrder();
294: me.setOrder(front.getOrder());
295: front.setOrder(temp);
296: session.update(me);
297: session.update(front);
298: }
299: } catch (SQLException e) {
300: errors.add(LIST_ERROR_KEY, new ActionError(
301: "database_exception"));
302: } catch (HibernateException e) {
303: errors.add(LIST_ERROR_KEY, new ActionError(
304: "hibernate_exception"));
305: } finally {
306: commitSession(session, true);
307: }
308: // Report any errors we have discovered back to the original form
309: ActionForward forward = mapping.getInputForward();
310: if (!errors.isEmpty())
311: saveErrors(request, errors);
312: else
313: forward.setRedirect(true);
314: return forward;
315: }
316:
317: /**
318: * 删除日记分类
319: * @param mapping
320: * @param form
321: * @param request
322: * @param response
323: * @param cat_id
324: * @return
325: * @throws Exception
326: */
327: public ActionForward doDeleteCategory(ActionMapping mapping,
328: ActionForm form, HttpServletRequest request,
329: HttpServletResponse response, String cat_id)
330: throws Exception {
331:
332: ActionErrors errors = new ActionErrors();
333: Session session = null;
334: try {
335: session = getSession();
336: int catid = Integer.parseInt(cat_id);
337: SiteForm site = SiteManager.getCurrentSite(request);
338: CategoryForm cat = CategoryManager.getCategory(session,
339: site, catid);
340: if (cat != null) {
341: int logcount = LogManager.getLogCount(session, catid);
342: if (logcount > 0)//还有日记不允许删除
343: errors.add(LIST_ERROR_KEY, new ActionError(
344: "logs_not_empty"));
345: else {
346: session.delete(cat);
347: commitSession(session, false);
348: }
349: } else
350: errors.add(LIST_ERROR_KEY, new ActionError(
351: "category_not_found"));
352: } catch (HibernateException e) {
353: errors.add(LIST_ERROR_KEY, new ActionError(
354: "hibernate_exception"));
355: } catch (SQLException e) {
356: errors.add(LIST_ERROR_KEY, new ActionError(
357: "database_exception"));
358: } finally {
359: closeSession(session);
360: }
361: // Report any errors we have discovered back to the original form
362: if (!errors.isEmpty())
363: saveErrors(request, errors);
364: ActionForward forward = mapping.getInputForward();
365: if (errors.isEmpty())
366: forward.setRedirect(true);
367: return forward;
368: }
369:
370: /**
371: * 增加Category
372: *
373: * @param mapping
374: * @param form
375: * @param request
376: * @param response
377: * @return @throws
378: * Exception
379: */
380: public ActionForward doAddCat(ActionMapping mapping,
381: ActionForm form, HttpServletRequest request,
382: HttpServletResponse response) throws Exception {
383:
384: Session session = getSession();
385: CategoryForm cat = (CategoryForm) form;
386: try {
387: cat.setSite(SiteManager.getCurrentSite(request));
388: session.save(cat);
389: } catch (Exception e) {
390: System.out.println("error when save Category: " + e);
391: } finally {
392: commitSession(session, true);
393: }
394: return mapping.findForward(SUCCESS_LIST_PAGE);
395: }
396:
397: /**
398: * 获取某个分类的详细资料信息
399: *
400: * @param ssn
401: * @param userid
402: * @param withDetails
403: * @return @throws
404: * HibernateException
405: */
406: public static CategoryForm getUser(Session ssn, int catid,
407: boolean withDetails) throws HibernateException {
408: CategoryForm cat = (CategoryForm) ssn.load(CategoryForm.class,
409: new Integer(catid));
410:
411: return cat;
412: }
413: }
|