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.Connection;
019: import java.sql.PreparedStatement;
020: import java.sql.SQLException;
021: import java.util.Date;
022: import java.util.List;
023:
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026:
027: import net.sf.hibernate.Criteria;
028: import net.sf.hibernate.HibernateException;
029: import net.sf.hibernate.Query;
030: import net.sf.hibernate.Session;
031: import net.sf.hibernate.expression.Order;
032:
033: import org.apache.struts.action.ActionError;
034: import org.apache.struts.action.ActionErrors;
035: import org.apache.struts.action.ActionForm;
036: import org.apache.struts.action.ActionForward;
037: import org.apache.struts.action.ActionMapping;
038:
039: import dlog4j.SiteManager;
040: import dlog4j.formbean.FavoriteForm;
041: import dlog4j.formbean.SiteForm;
042: import dlog4j.formbean.UserForm;
043:
044: /**
045: * 网站链接的操作类
046: * @author Winter Lau
047: */
048: public class DlogFavoriteAction extends AdminActionBase {
049:
050: public ActionForward doDefault(ActionMapping mapping,
051: ActionForm form, HttpServletRequest request,
052: HttpServletResponse response) throws Exception {
053: return mapping.findForward("links");
054: }
055:
056: /**
057: * 添加链接
058: * @param mapping
059: * @param form
060: * @param request
061: * @param response
062: * @return
063: * @throws Exception
064: */
065: public ActionForward doCreate(ActionMapping mapping,
066: ActionForm form, HttpServletRequest request,
067: HttpServletResponse response) throws Exception {
068: ActionErrors errors = new ActionErrors();
069: FavoriteForm link = (FavoriteForm) form;
070: Session ssn = null;
071:
072: int position = 1;
073: try {//默认插在最后
074: position = Integer.parseInt(request
075: .getParameter("position"));
076: } catch (Exception e) {
077: }
078:
079: int fav_id = link.getOrder();
080:
081: try {
082: ssn = getSession();
083: FavoriteForm fav = null;
084: int order = 1;
085: String hql = "FROM " + FavoriteForm.class.getName()
086: + " AS f WHERE f.id=? ORDER BY f.order";
087: Query q = ssn.createQuery(hql);
088: q.setInteger(0, fav_id);
089: List res = q.list();
090: int pos = (position > 0) ? 1 : 0;
091: for (int i = 0; i < res.size(); i++) {
092: FavoriteForm tmp_fav = (FavoriteForm) res.get(i);
093: if (tmp_fav.getId() == fav_id) {
094: if ((i + position) < res.size()) {
095: FavoriteForm tmp_fav2 = (FavoriteForm) res
096: .get(i + pos);
097: order = tmp_fav2.getOrder();
098: } else
099: order = tmp_fav.getOrder() + pos;
100: break;
101: }
102: order = tmp_fav.getOrder() + 1;
103: }
104:
105: //UPDATE the order then insert the new link
106: String update_sql = "UPDATE dlog_favorite SET sortOrder=sortOrder+1 WHERE sortOrder>=?";
107: Connection conn = null;
108: PreparedStatement ps = null;
109: try {
110: conn = getConnection();
111: ps = conn.prepareStatement(update_sql);
112: ps.setInt(1, order);
113: ps.executeUpdate();
114: } finally {
115: close(null, ps, conn);
116: }
117: //insert the new link
118: link.setCreateTime(new Date());
119: link.setSite(SiteManager.getCurrentSite(request));
120: link.setOrder(order);
121: ssn.save(link);
122: } catch (SQLException e) {
123: servlet.log("DlogFavoriteAction.doCreate", e);
124: errors.add("create", new ActionError("database_exception"));
125: } catch (HibernateException e) {
126: servlet.log("DlogFavoriteAction.doCreate", e);
127: errors
128: .add("create", new ActionError(
129: "hibernate_exception"));
130: } finally {
131: commitSession(ssn, true);
132: }
133: ActionForward forward = mapping.getInputForward();
134: if (!errors.isEmpty())
135: saveErrors(request, errors);
136: else
137: forward.setRedirect(true);
138: return forward;
139: }
140:
141: /**
142: * 修改链接
143: * @param mapping
144: * @param form
145: * @param request
146: * @param response
147: * @return
148: * @throws Exception
149: */
150: public ActionForward doUpdate(ActionMapping mapping,
151: ActionForm form, HttpServletRequest request,
152: HttpServletResponse response) throws Exception {
153: ActionErrors errors = new ActionErrors();
154: FavoriteForm link = (FavoriteForm) form;
155: Session ssn = null;
156: try {
157: ssn = getSession();
158: SiteForm site = SiteManager.getCurrentSite(request);
159: FavoriteForm old = (FavoriteForm) ssn.load(
160: FavoriteForm.class, new Integer(link.getId()));
161: if (old != null) {
162: old.setUrl(link.getUrl());
163: old.setTitle(link.getTitle());
164: old.setOpenInNewWindow(link.getOpenInNewWindow());
165: old.setMode(link.getMode());
166: ssn.update(old);
167: }
168: } catch (SQLException e) {
169: errors.add("edit", new ActionError("database_exception"));
170: } catch (HibernateException e) {
171: errors.add("edit", new ActionError("hibernate_exception"));
172: } finally {
173: commitSession(ssn, true);
174: }
175: ActionForward forward = mapping.getInputForward();
176: if (!errors.isEmpty())
177: saveErrors(request, errors);
178: else
179: forward.setRedirect(true);
180: return forward;
181: }
182:
183: /**
184: * 删除链接
185: */
186: public ActionForward doDelete(ActionMapping mapping,
187: ActionForm form, HttpServletRequest request,
188: HttpServletResponse response, String favorite_id)
189: throws Exception {
190: Session ssn = null;
191: try {
192: ssn = getSession();
193: FavoriteForm link = (FavoriteForm) ssn.load(
194: FavoriteForm.class, new Integer(favorite_id));
195: ssn.delete(link);
196: } finally {
197: if (ssn != null)
198: commitSession(ssn, true);
199: }
200: return mapping.findForward("links");
201: }
202:
203: /**
204: * 链接排序,向上一个位置
205: * @param mapping
206: * @param form
207: * @param request
208: * @param response
209: * @param cat_id
210: * @return
211: * @throws Exception
212: */
213: public ActionForward doMoveDown(ActionMapping mapping,
214: ActionForm form, HttpServletRequest request,
215: HttpServletResponse response, String lnkid)
216: throws Exception {
217: ActionErrors errors = new ActionErrors();
218: Session session = null;
219: //判断用户是否登陆
220: UserForm user = getLoginUser(request);
221: if (user == null || !user.isLogin())
222: errors
223: .add("links", new ActionError(
224: "operation_need_login"));
225: else if (!user.isAdmin())
226: errors.add("links", new ActionError("only_owner_allow"));
227: else {
228: try {
229: session = getSession();
230: int linkid = Integer.parseInt(lnkid);
231: SiteForm site = SiteManager.getCurrentSite(request);
232: Criteria crit = session
233: .createCriteria(FavoriteForm.class);
234: crit.addOrder(Order.desc("order"));
235: List links = crit.list();
236: int i;
237: for (i = 0; i < links.size(); i++) {
238: FavoriteForm link = (FavoriteForm) links.get(i);
239: if (link.getId() == linkid) {
240: break;
241: }
242: }
243: if (i == links.size())
244: errors.add("links", new ActionError(
245: "category_not_found"));
246: int next_idx = i - 1;
247: int me_idx = i;
248: if (next_idx < links.size()) {
249: FavoriteForm me = (FavoriteForm) links.get(me_idx);
250: FavoriteForm front = (FavoriteForm) links
251: .get(next_idx);
252: //交换order值
253: int temp = me.getOrder();
254: me.setOrder(front.getOrder());
255: front.setOrder(temp);
256: session.update(me);
257: session.update(front);
258: }
259: } catch (SQLException e) {
260: errors.add("links", new ActionError(
261: "database_exception"));
262: } catch (HibernateException e) {
263: errors.add("links", new ActionError(
264: "hibernate_exception"));
265: } finally {
266: commitSession(session, true);
267: }
268: }
269: // Report any errors we have discovered back to the original form
270: ActionForward forward = mapping.getInputForward();
271: if (!errors.isEmpty())
272: saveErrors(request, errors);
273: else
274: forward.setRedirect(true);
275: return forward;
276: }
277:
278: /**
279: * 链接排序,向下一个位置
280: * @param mapping
281: * @param form
282: * @param request
283: * @param response
284: * @param cat_id
285: * @return
286: * @throws Exception
287: */
288: public ActionForward doMoveUp(ActionMapping mapping,
289: ActionForm form, HttpServletRequest request,
290: HttpServletResponse response, String lnkid)
291: throws Exception {
292: ActionErrors errors = new ActionErrors();
293: Session session = null;
294: //判断用户是否登陆
295: UserForm user = getLoginUser(request);
296: if (user == null || !user.isLogin())
297: errors
298: .add("links", new ActionError(
299: "operation_need_login"));
300: else if (!user.isAdmin())
301: errors.add("links", new ActionError("only_owner_allow"));
302: else {
303: try {
304: session = getSession();
305: int linkid = Integer.parseInt(lnkid);
306: SiteForm site = SiteManager.getCurrentSite(request);
307: Criteria crit = session
308: .createCriteria(FavoriteForm.class);
309: crit.addOrder(Order.desc("order"));
310: List links = crit.list();
311: int i;
312: for (i = 0; i < links.size(); i++) {
313: FavoriteForm cat = (FavoriteForm) links.get(i);
314: if (cat.getId() == linkid) {
315: break;
316: }
317: }
318: if (i < links.size()) {
319: int front_idx = i + 1;
320: int me_idx = i;
321: if (front_idx >= 0) {
322: FavoriteForm me = (FavoriteForm) links
323: .get(me_idx);
324: FavoriteForm front = (FavoriteForm) links
325: .get(front_idx);
326: //交换order值
327: int temp = me.getOrder();
328: me.setOrder(front.getOrder());
329: front.setOrder(temp);
330: session.update(me);
331: session.update(front);
332: }
333: }
334: } catch (SQLException e) {
335: errors.add("links", new ActionError(
336: "database_exception"));
337: } catch (HibernateException e) {
338: errors.add("links", new ActionError(
339: "hibernate_exception"));
340: } finally {
341: commitSession(session, true);
342: }
343: }
344: // Report any errors we have discovered back to the original form
345: ActionForward forward = mapping.getInputForward();
346: if (!errors.isEmpty())
347: saveErrors(request, errors);
348: else
349: forward.setRedirect(true);
350: return forward;
351: }
352: }
|