1 16 package dlog4j.action; 17 18 import java.sql.SQLException ; 19 import java.util.List ; 20 21 import javax.servlet.http.HttpServletRequest ; 22 import javax.servlet.http.HttpServletResponse ; 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 43 public class DlogCategoryAction extends AdminActionBase { 44 45 public final static String SUCCESS_LIST_PAGE = "success"; 46 public final static String LIST_ERROR_KEY = "list"; 47 public final static String EDIT_ERROR_KEY = "edit"; 48 58 public ActionForward doEditCategory( 59 ActionMapping mapping, 60 ActionForm form, 61 HttpServletRequest request, 62 HttpServletResponse response) 63 throws Exception 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 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 101 public ActionForward doCreateCategory( 102 ActionMapping mapping, 103 ActionForm form, 104 HttpServletRequest request, 105 HttpServletResponse response) 106 throws Exception 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 categories = CategoryManager.listCategories(ssn,site); 117 int position = 1; 118 try { position = Integer.parseInt(request.getParameter("position")); 120 }catch(Exception e) {} 121 int catid = cat.getOrder(); 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) { if(i==(categories.size()-1)) { 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 { 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 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 190 public ActionForward doMoveDown( 191 ActionMapping mapping, 192 ActionForm form, 193 HttpServletRequest request, 194 HttpServletResponse response, 195 String cat_id) 196 throws Exception { 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 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 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 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 ActionForward forward = mapping.getInputForward(); 234 if (!errors.isEmpty()) 235 saveErrors(request, errors); 236 else 237 forward.setRedirect(true); 238 return forward; 239 } 240 250 public ActionForward doMoveUp( 251 ActionMapping mapping, 252 ActionForm form, 253 HttpServletRequest request, 254 HttpServletResponse response, 255 String cat_id) 256 throws Exception 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 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 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 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 ActionForward forward = mapping.getInputForward(); 295 if (!errors.isEmpty()) 296 saveErrors(request, errors); 297 else 298 forward.setRedirect(true); 299 return forward; 300 } 301 311 public ActionForward doDeleteCategory( 312 ActionMapping mapping, 313 ActionForm form, 314 HttpServletRequest request, 315 HttpServletResponse response, 316 String cat_id) 317 throws Exception { 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) 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 e) { 340 errors.add(LIST_ERROR_KEY,new ActionError("database_exception")); 341 } finally { 342 closeSession(session); 343 } 344 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 362 public ActionForward doAddCat( 363 ActionMapping mapping, 364 ActionForm form, 365 HttpServletRequest request, 366 HttpServletResponse response) 367 throws Exception { 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 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 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 (catid)); 398 399 return cat; 400 } 401 } 402 | Popular Tags |