1 package org.contineo.actions.documan.util; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.io.InputStream ; 6 import java.io.OutputStream ; 7 8 import javax.servlet.http.HttpServletRequest ; 9 import javax.servlet.http.HttpServletResponse ; 10 import javax.servlet.http.HttpSession ; 11 12 import org.apache.log4j.Level; 13 import org.apache.log4j.Logger; 14 import org.apache.struts.action.Action; 15 import org.apache.struts.action.ActionForm; 16 import org.apache.struts.action.ActionForward; 17 import org.apache.struts.action.ActionMapping; 18 import org.contineo.admin.Menu; 19 import org.contineo.admin.dao.MenuDAO; 20 import org.contineo.core.DateBean; 21 import org.contineo.core.LoggingManager; 22 import org.contineo.core.SessionManagement; 23 import org.contineo.core.config.SettingConfigurator; 24 import org.contineo.documan.Document; 25 import org.contineo.documan.History; 26 import org.contineo.documan.dao.DocumentDAO; 27 import org.contineo.documan.dao.HistoryDAO; 28 29 34 public class CheckoutAction extends Action { 35 36 40 private Logger logger; 41 42 43 44 public CheckoutAction() { 45 logger = LoggingManager.getLogger(this.getClass()); 46 } 47 48 public ActionForward execute(ActionMapping mapping, 49 ActionForm form, HttpServletRequest request, 50 HttpServletResponse response) { 51 ActionForward actionForward = new ActionForward(); 52 HttpSession session = request.getSession(); 53 if (SessionManagement.isValid(session)) { 54 String menuid = request.getParameter("menuid"); 55 try { 56 MenuDAO mdao = new MenuDAO(); 57 String username = (String )session.getAttribute("authuser"); 58 if (mdao.isWriteEnable(Integer.parseInt(menuid), username)) { 59 Menu menu = mdao.findByPrimaryKey(Integer.parseInt(menuid)); 60 DocumentDAO ddao = new DocumentDAO(); 61 Document doc = ddao.findByMenuId(Integer.parseInt(menuid)); 62 63 if (doc.getDocStatus() == Document.DOC_CHECKED_IN) { 64 String mimetype = "application/octet-stream"; 65 response.setContentType(mimetype); 66 SettingConfigurator settings = new SettingConfigurator(); 67 String path = settings.getValue("docdir") + menu.getMenuPath() + "/" + menu.getMenuId(); 68 response.setHeader("Content-disposition", "attachment;filename=" + doc.getDocName().replaceAll(" ", "_") 69 + "." + doc.getDocType()); 70 String menuref = menu.getMenuRef(); 71 File file = new File (path + "/" + menuref); 72 InputStream is = new FileInputStream (file); 73 OutputStream os; 74 os = response.getOutputStream(); 75 int letter = 0; 76 while ((letter = is.read()) != -1) { 77 os.write(letter); 78 } 79 os.flush(); 80 os.close(); 81 is.close(); 82 doc.setCheckoutUser((String ) session.getAttribute("authuser")); 83 doc.setDocStatus(Document.DOC_CHECKED_OUT); 84 doc.setMenu(menu); 85 ddao.store(doc); 86 87 88 History history = new History(); 89 history.setDocId(doc.getDocId()); 90 history.setDate(DateBean.toCompactString()); 91 history.setUsername(username); 92 history.setEvent(History.CHECKOUT); 93 HistoryDAO historyDao = new HistoryDAO(); 94 historyDao.store(history); 95 96 actionForward = null; 98 } else { 99 actionForward = mapping.findForward("noaccess"); 100 } 101 } else 102 actionForward = mapping.findForward("noaccess"); 103 } catch (Exception e) { 104 if (logger.isEnabledFor(Level.ERROR)) 105 logger.error(e.getMessage()); 106 actionForward = mapping.findForward("error"); 107 } 108 } else 109 actionForward = mapping.findForward("invalid"); 110 return actionForward; 111 } 112 } 113 | Popular Tags |