KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > action > DlogFavoriteAction


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.Connection JavaDoc;
19 import java.sql.PreparedStatement JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.List JavaDoc;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27 import net.sf.hibernate.Criteria;
28 import net.sf.hibernate.HibernateException;
29 import net.sf.hibernate.Query;
30 import net.sf.hibernate.Session;
31 import net.sf.hibernate.expression.Order;
32
33 import org.apache.struts.action.ActionError;
34 import org.apache.struts.action.ActionErrors;
35 import org.apache.struts.action.ActionForm;
36 import org.apache.struts.action.ActionForward;
37 import org.apache.struts.action.ActionMapping;
38
39 import dlog4j.SiteManager;
40 import dlog4j.formbean.FavoriteForm;
41 import dlog4j.formbean.SiteForm;
42 import dlog4j.formbean.UserForm;
43
44 /**
45  * 网站链接的操作类
46  * @author Winter Lau
47  */

48 public class DlogFavoriteAction extends AdminActionBase {
49
50     public ActionForward doDefault(
51         ActionMapping mapping,
52         ActionForm form,
53         HttpServletRequest JavaDoc request,
54         HttpServletResponse JavaDoc response)
55         throws Exception JavaDoc {
56         return mapping.findForward("links");
57     }
58     /**
59      * 添加链接
60      * @param mapping
61      * @param form
62      * @param request
63      * @param response
64      * @return
65      * @throws Exception
66      */

67     public ActionForward doCreate(
68             ActionMapping mapping,
69             ActionForm form,
70             HttpServletRequest JavaDoc request,
71             HttpServletResponse JavaDoc response)
72             throws Exception JavaDoc
73     {
74         ActionErrors errors = new ActionErrors();
75         FavoriteForm link = (FavoriteForm)form;
76         Session ssn = null;
77
78         int position = 1;
79         try {//默认插在最后
80
position = Integer.parseInt(request.getParameter("position"));
81         }catch(Exception JavaDoc e) {}
82         
83         int fav_id = link.getOrder();
84         
85         try {
86             ssn = getSession();
87             FavoriteForm fav = null;
88             int order = 1;
89             String JavaDoc hql = "FROM "+FavoriteForm.class.getName()+" AS f WHERE f.id=? ORDER BY f.order";
90             Query q = ssn.createQuery(hql);
91             q.setInteger(0, fav_id);
92             List JavaDoc res = q.list();
93             int pos = (position>0)?1:0;
94             for(int i=0;i<res.size();i++){
95                 FavoriteForm tmp_fav = (FavoriteForm)res.get(i);
96                 if(tmp_fav.getId()==fav_id){
97                     if((i+position)<res.size()){
98                         FavoriteForm tmp_fav2 = (FavoriteForm)res.get(i+pos);
99                         order = tmp_fav2.getOrder();
100                     }
101                     else
102                         order = tmp_fav.getOrder()+ pos;
103                     break;
104                 }
105                 order = tmp_fav.getOrder() + 1;
106             }
107             
108             //UPDATE the order then insert the new link
109
String JavaDoc update_sql = "UPDATE dlog_favorite SET sortOrder=sortOrder+1 WHERE sortOrder>=?";
110             Connection JavaDoc conn = null;
111             PreparedStatement JavaDoc ps = null;
112             try{
113                 conn = getConnection();
114                 ps = conn.prepareStatement(update_sql);
115                 ps.setInt(1, order);
116                 ps.executeUpdate();
117             }finally{
118                 close(null,ps,conn);
119             }
120             //insert the new link
121
link.setCreateTime(new Date JavaDoc());
122             link.setSite(SiteManager.getCurrentSite(request));
123             link.setOrder(order);
124             ssn.save(link);
125         } catch(SQLException JavaDoc e) {
126             servlet.log("DlogFavoriteAction.doCreate",e);
127             errors.add("create",new ActionError("database_exception"));
128         } catch(HibernateException e) {
129             servlet.log("DlogFavoriteAction.doCreate",e);
130             errors.add("create",new ActionError("hibernate_exception"));
131         } finally {
132             commitSession(ssn, true);
133         }
134         ActionForward forward = mapping.getInputForward();
135         if (!errors.isEmpty())
136             saveErrors(request, errors);
137         else
138             forward.setRedirect(true);
139         return forward;
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(
151             ActionMapping mapping,
152             ActionForm form,
153             HttpServletRequest JavaDoc request,
154             HttpServletResponse JavaDoc response)
155             throws Exception JavaDoc
156     {
157         ActionErrors errors = new ActionErrors();
158         FavoriteForm link = (FavoriteForm)form;
159         Session ssn = null;
160         try {
161             ssn = getSession();
162             SiteForm site = SiteManager.getCurrentSite(request);
163             FavoriteForm old = (FavoriteForm)ssn.load(FavoriteForm.class, new Integer JavaDoc(link.getId()));
164             if(old!=null) {
165                 old.setUrl(link.getUrl());
166                 old.setTitle(link.getTitle());
167                 old.setOpenInNewWindow(link.getOpenInNewWindow());
168                 old.setMode(link.getMode());
169                 ssn.update(old);
170             }
171         } catch(SQLException JavaDoc e) {
172             errors.add("edit",new ActionError("database_exception"));
173         } catch(HibernateException e) {
174             errors.add("edit",new ActionError("hibernate_exception"));
175         } finally {
176             commitSession(ssn, true);
177         }
178         ActionForward forward = mapping.getInputForward();
179         if (!errors.isEmpty())
180             saveErrors(request, errors);
181         else
182             forward.setRedirect(true);
183         return forward;
184     }
185     /**
186      * 删除链接
187      */

188     public ActionForward doDelete(
189         ActionMapping mapping,
190         ActionForm form,
191         HttpServletRequest JavaDoc request,
192         HttpServletResponse JavaDoc response,
193         String JavaDoc favorite_id)
194         throws Exception JavaDoc {
195         Session ssn = null;
196         try {
197             ssn = getSession();
198             FavoriteForm link = (FavoriteForm) ssn.load(FavoriteForm.class, new Integer JavaDoc(favorite_id));
199             ssn.delete(link);
200         } finally {
201             if(ssn!=null)
202                 commitSession(ssn, true);
203         }
204         return mapping.findForward("links");
205     }
206
207     /**
208      * 链接排序,向上一个位置
209      * @param mapping
210      * @param form
211      * @param request
212      * @param response
213      * @param cat_id
214      * @return
215      * @throws Exception
216      */

217     public ActionForward doMoveDown(
218             ActionMapping mapping,
219             ActionForm form,
220             HttpServletRequest JavaDoc request,
221             HttpServletResponse JavaDoc response,
222             String JavaDoc lnkid)
223             throws Exception JavaDoc {
224         ActionErrors errors = new ActionErrors();
225         Session session = null;
226         //判断用户是否登陆
227
UserForm user = getLoginUser(request);
228         if(user==null || !user.isLogin())
229             errors.add("links",new ActionError("operation_need_login"));
230         else if(!user.isAdmin())
231             errors.add("links",new ActionError("only_owner_allow"));
232         else{
233             try {
234                 session = getSession();
235                 int linkid = Integer.parseInt(lnkid);
236                 SiteForm site = SiteManager.getCurrentSite(request);
237                 Criteria crit = session.createCriteria(FavoriteForm.class);
238                 crit.addOrder(Order.desc("order"));
239                 List JavaDoc links = crit.list();
240                 int i;
241                 for(i=0;i<links.size();i++) {
242                     FavoriteForm link = (FavoriteForm)links.get(i);
243                     if(link.getId()==linkid) {
244                         break;
245                     }
246                 }
247                 if(i==links.size())
248                     errors.add("links",new ActionError("category_not_found"));
249                 int next_idx = i-1;
250                 int me_idx = i;
251                 if(next_idx<links.size()) {
252                     FavoriteForm me = (FavoriteForm)links.get(me_idx);
253                     FavoriteForm front = (FavoriteForm)links.get(next_idx);
254                     //交换order值
255
int temp = me.getOrder();
256                     me.setOrder(front.getOrder());
257                     front.setOrder(temp);
258                     session.update(me);
259                     session.update(front);
260                 }
261             } catch(SQLException JavaDoc e) {
262                 errors.add("links",new ActionError("database_exception"));
263             } catch(HibernateException e) {
264                 errors.add("links",new ActionError("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      * @param mapping
280      * @param form
281      * @param request
282      * @param response
283      * @param cat_id
284      * @return
285      * @throws Exception
286      */

287     public ActionForward doMoveUp(
288             ActionMapping mapping,
289             ActionForm form,
290             HttpServletRequest JavaDoc request,
291             HttpServletResponse JavaDoc response,
292             String JavaDoc lnkid)
293             throws Exception JavaDoc
294     {
295         ActionErrors errors = new ActionErrors();
296         Session session = null;
297         //判断用户是否登陆
298
UserForm user = getLoginUser(request);
299         if(user==null || !user.isLogin())
300             errors.add("links",new ActionError("operation_need_login"));
301         else if(!user.isAdmin())
302             errors.add("links",new ActionError("only_owner_allow"));
303         else{
304             try {
305                 session = getSession();
306                 int linkid = Integer.parseInt(lnkid);
307                 SiteForm site = SiteManager.getCurrentSite(request);
308                 Criteria crit = session.createCriteria(FavoriteForm.class);
309                 crit.addOrder(Order.desc("order"));
310                 List JavaDoc 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.get(me_idx);
323                         FavoriteForm front = (FavoriteForm) links
324                                 .get(front_idx);
325                         //交换order值
326
int temp = me.getOrder();
327                         me.setOrder(front.getOrder());
328                         front.setOrder(temp);
329                         session.update(me);
330                         session.update(front);
331                     }
332                 }
333             } catch(SQLException JavaDoc e) {
334                 errors.add("links",new ActionError("database_exception"));
335             } catch(HibernateException e) {
336                 errors.add("links",new ActionError("hibernate_exception"));
337             } finally {
338                 commitSession(session, true);
339             }
340         }
341         // Report any errors we have discovered back to the original form
342
ActionForward forward = mapping.getInputForward();
343         if (!errors.isEmpty())
344             saveErrors(request, errors);
345         else
346             forward.setRedirect(true);
347         return forward;
348     }
349 }
350
Popular Tags