1 16 package dlog4j.action; 17 18 import java.sql.Connection ; 19 import java.sql.PreparedStatement ; 20 import java.sql.SQLException ; 21 import java.util.Date ; 22 import java.util.List ; 23 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 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 48 public class DlogFavoriteAction extends AdminActionBase { 49 50 public ActionForward doDefault( 51 ActionMapping mapping, 52 ActionForm form, 53 HttpServletRequest request, 54 HttpServletResponse response) 55 throws Exception { 56 return mapping.findForward("links"); 57 } 58 67 public ActionForward doCreate( 68 ActionMapping mapping, 69 ActionForm form, 70 HttpServletRequest request, 71 HttpServletResponse response) 72 throws Exception 73 { 74 ActionErrors errors = new ActionErrors(); 75 FavoriteForm link = (FavoriteForm)form; 76 Session ssn = null; 77 78 int position = 1; 79 try { position = Integer.parseInt(request.getParameter("position")); 81 }catch(Exception e) {} 82 83 int fav_id = link.getOrder(); 84 85 try { 86 ssn = getSession(); 87 FavoriteForm fav = null; 88 int order = 1; 89 String 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 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 String update_sql = "UPDATE dlog_favorite SET sortOrder=sortOrder+1 WHERE sortOrder>=?"; 110 Connection conn = null; 111 PreparedStatement 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 link.setCreateTime(new Date ()); 122 link.setSite(SiteManager.getCurrentSite(request)); 123 link.setOrder(order); 124 ssn.save(link); 125 } catch(SQLException 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 150 public ActionForward doUpdate( 151 ActionMapping mapping, 152 ActionForm form, 153 HttpServletRequest request, 154 HttpServletResponse response) 155 throws Exception 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 (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 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 188 public ActionForward doDelete( 189 ActionMapping mapping, 190 ActionForm form, 191 HttpServletRequest request, 192 HttpServletResponse response, 193 String favorite_id) 194 throws Exception { 195 Session ssn = null; 196 try { 197 ssn = getSession(); 198 FavoriteForm link = (FavoriteForm) ssn.load(FavoriteForm.class, new Integer (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 217 public ActionForward doMoveDown( 218 ActionMapping mapping, 219 ActionForm form, 220 HttpServletRequest request, 221 HttpServletResponse response, 222 String lnkid) 223 throws Exception { 224 ActionErrors errors = new ActionErrors(); 225 Session session = null; 226 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 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 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 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 ActionForward forward = mapping.getInputForward(); 271 if (!errors.isEmpty()) 272 saveErrors(request, errors); 273 else 274 forward.setRedirect(true); 275 return forward; 276 } 277 287 public ActionForward doMoveUp( 288 ActionMapping mapping, 289 ActionForm form, 290 HttpServletRequest request, 291 HttpServletResponse response, 292 String lnkid) 293 throws Exception 294 { 295 ActionErrors errors = new ActionErrors(); 296 Session session = null; 297 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 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 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 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 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 |