KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > contineo > actions > documan > util > CheckoutAction


1 package org.contineo.actions.documan.util;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.io.OutputStream JavaDoc;
7
8 import javax.servlet.http.HttpServletRequest JavaDoc;
9 import javax.servlet.http.HttpServletResponse JavaDoc;
10 import javax.servlet.http.HttpSession JavaDoc;
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 /**
30  * Marks the document as checked out and returns it to the client.
31  * Created on 29. Oktober 2003, 00:05
32  * @author Michael Scholz
33  */

34 public class CheckoutAction extends Action {
35
36     /**
37      * @uml.property name="logger"
38      * @uml.associationEnd
39      */

40     private Logger logger;
41
42     
43     /** Creates a new instance of DownloadAction */
44     public CheckoutAction() {
45         logger = LoggingManager.getLogger(this.getClass());
46     }
47
48     public ActionForward execute(ActionMapping mapping,
49                     ActionForm form, HttpServletRequest JavaDoc request,
50                     HttpServletResponse JavaDoc response) {
51         ActionForward actionForward = new ActionForward();
52         HttpSession JavaDoc session = request.getSession();
53         if (SessionManagement.isValid(session)) {
54             String JavaDoc menuid = request.getParameter("menuid");
55             try {
56                 MenuDAO mdao = new MenuDAO();
57                 String JavaDoc username = (String JavaDoc)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 JavaDoc mimetype = "application/octet-stream";
65                         response.setContentType(mimetype);
66                         SettingConfigurator settings = new SettingConfigurator();
67                         String JavaDoc path = settings.getValue("docdir") + menu.getMenuPath() + "/" + menu.getMenuId();
68                         response.setHeader("Content-disposition", "attachment;filename=" + doc.getDocName().replaceAll(" ", "_")
69                                 + "." + doc.getDocType());
70                         String JavaDoc menuref = menu.getMenuRef();
71                         File JavaDoc file = new File JavaDoc(path + "/" + menuref);
72                         InputStream JavaDoc is = new FileInputStream JavaDoc(file);
73                         OutputStream JavaDoc 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 JavaDoc) session.getAttribute("authuser"));
83                         doc.setDocStatus(Document.DOC_CHECKED_OUT);
84                         doc.setMenu(menu);
85                         ddao.store(doc);
86
87                         /* create history entry */
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                         // do not forward, because we already sent something to the client
97
actionForward = null;
98                     } else {
99                         actionForward = mapping.findForward("noaccess");
100                     }
101                 } else
102                     actionForward = mapping.findForward("noaccess");
103             } catch (Exception JavaDoc 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