KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > contineo > actions > rest > CheckoutDocumentRestAction


1 /* License: GNU General Public License (GPL) version 2 from June 1991; but not any newer version!
2  */

3 package org.contineo.actions.rest;
4
5 import java.io.FileNotFoundException JavaDoc;
6 import java.io.IOException JavaDoc;
7
8 import javax.servlet.http.HttpServletResponse JavaDoc;
9
10 import org.contineo.actions.documan.util.DownloadDocUtil;
11 import org.contineo.admin.Menu;
12 import org.contineo.admin.dao.MenuDAO;
13 import org.contineo.apis.rest.HttpStatusCodes;
14 import org.contineo.documan.Document;
15 import org.contineo.documan.dao.DocumentDAO;
16
17 /**
18  * answers a HTTP get request to checkout a document resource
19  * @author Sebastian Stein
20  */

21 public class CheckoutDocumentRestAction extends RESTAction {
22     
23     /** id of the document the client requests the meta data of */
24     private final int thisDocId;
25     
26     /** constructor sets some initial values and calls the processing of the request */
27     public CheckoutDocumentRestAction(String JavaDoc p_schemaName,
28             String JavaDoc requestedRESTUrl, String JavaDoc p_userName,
29             HttpServletResponse JavaDoc p_response, int p_docId) {
30         super(p_schemaName, requestedRESTUrl, p_userName, p_response, "GET REST/document/XX/checkout: checkout doc XX");
31         thisDocId = p_docId;
32         
33         processRequest();
34     }
35
36     @Override JavaDoc
37     protected void processRequest() {
38         // check if we can find the document
39
MenuDAO menuDao = new MenuDAO();
40         Menu thisMenu = menuDao.findByPrimaryKey(thisDocId);
41         DocumentDAO ddao = new DocumentDAO();
42         Document thisDoc = ddao.findByMenuId(thisDocId);
43         if (thisMenu == null || thisDoc == null) {
44             setHttpStatusCode(HttpStatusCodes.NOT_FOUND);
45             return;
46         }
47
48         // check, if the user has write access to the document
49
if (!menuDao.isWriteEnable(thisDocId, userName)) {
50             setHttpStatusCode(HttpStatusCodes.FORBIDDEN);
51             return;
52         }
53         
54         // the document must be checked in, otherwise we can not check it out
55
if (thisDoc.getDocStatus() == Document.DOC_CHECKED_OUT) {
56             setHttpStatusCode(HttpStatusCodes.FORBIDDEN);
57             return;
58         }
59         
60         // mark document as checked out by this user
61
thisDoc.setCheckoutUser(userName);
62         thisDoc.setDocStatus(Document.DOC_CHECKED_OUT);
63         thisDoc.setMenu(thisMenu);
64         if (ddao.store(thisDoc) == false) {
65             setHttpStatusCode(HttpStatusCodes.INTERNAL_SERVER_ERROR);
66             addLogMessage("Document can not be stored, so updating the status of the document to checked out was not possible.");
67             return;
68         }
69         
70         // send the document to the client; if something goes wrong an exception is thrown
71
try {
72             DownloadDocUtil.downloadDocument(response, thisDocId, null);
73         } catch (FileNotFoundException JavaDoc e) {
74             setHttpStatusCode(HttpStatusCodes.NOT_FOUND);
75             return;
76         } catch (IOException JavaDoc e) {
77             setHttpStatusCode(HttpStatusCodes.BAD_REQUEST);
78             return;
79         }
80     }
81 }
82
Popular Tags