comparison software/mpdl-services-new/mpiwg-mpdl-cms-web/src/de/mpg/mpiwg/berlin/mpdl/servlets/cms/GetDocumentJobs.java @ 25:e9fe3186670c default tip

letzter Stand eingecheckt
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Tue, 21 May 2013 10:19:32 +0200
parents
children
comparison
equal deleted inserted replaced
23:e845310098ba 25:e9fe3186670c
1 package de.mpg.mpiwg.berlin.mpdl.servlets.cms;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.ArrayList;
6 import java.util.Date;
7
8 import javax.servlet.ServletConfig;
9 import javax.servlet.ServletException;
10 import javax.servlet.http.HttpServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13
14 import de.mpg.mpiwg.berlin.mpdl.cms.scheduler.CmsChainScheduler;
15 import de.mpg.mpiwg.berlin.mpdl.cms.scheduler.CmsDocOperation;
16
17 public class GetDocumentJobs extends HttpServlet {
18 private static final long serialVersionUID = 1L;
19 public GetDocumentJobs() {
20 super();
21 }
22
23 public void init(ServletConfig config) throws ServletException {
24 super.init(config);
25 }
26
27 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
28 request.setCharacterEncoding("utf-8");
29 response.setCharacterEncoding("utf-8");
30 response.setContentType("text/xml");
31 String jobIdStr = request.getParameter("id");
32 String outputFormat = request.getParameter("outputFormat");
33 if (outputFormat == null)
34 outputFormat = "xml";
35 try {
36 boolean getAllJobs = false;
37 if (jobIdStr == null)
38 getAllJobs = true;
39 CmsChainScheduler scheduler = CmsChainScheduler.getInstance();
40 ArrayList<CmsDocOperation> docOperations = new ArrayList<CmsDocOperation>();
41 if (getAllJobs) {
42 docOperations = scheduler.getDocOperations();
43 } else {
44 int jobId = Integer.parseInt(jobIdStr);
45 CmsDocOperation docOperation = scheduler.getDocOperation(jobId);
46 if (docOperation != null)
47 docOperations.add(docOperation);
48 }
49 PrintWriter out = response.getWriter();
50 String resultStr = "";
51 if (outputFormat.equals("xml")) {
52 response.setContentType("text/xml");
53 resultStr = createXmlString(docOperations);
54 } else if (outputFormat.equals("html")) {
55 response.setContentType("text/html");
56 resultStr = createHtmlString(docOperations);
57 } else if (outputFormat.equals("error")) {
58 response.setContentType("text/html");
59 resultStr = createErrorString(docOperations);
60 }
61 out.print(resultStr);
62 out.close();
63 } catch (Exception e) {
64 throw new ServletException(e);
65 }
66 }
67
68 String createXmlString(ArrayList<CmsDocOperation> docOperations) {
69 StringBuilder result = new StringBuilder();
70 if (docOperations != null && ! docOperations.isEmpty()) {
71 result.append("<docJobs>");
72 for (int i=0; i<docOperations.size(); i++) {
73 CmsDocOperation docOperation = docOperations.get(i);
74 result.append("<job>");
75 int jobId = docOperation.getOrderId();
76 result.append("<id>" + jobId + "</id>");
77 result.append("<name>" + docOperation.getName() + "</name>");
78 result.append("<status>");
79 Date start = docOperation.getStart();
80 String startStr = "No start time available because job is scheduled into server queue where other jobs have been started earlier";
81 if (start != null)
82 startStr = start.toString();
83 result.append("<started>" + startStr + "</started>");
84 Date end = docOperation.getEnd();
85 String endStr = "No end time available because job is not finished yet";
86 if (end != null)
87 endStr = end.toString();
88 result.append("<finished>" + endStr + "</finished>");
89 String status = docOperation.getStatus();
90 result.append("<description>" + status + "</description>");
91 String errorMessage = docOperation.getErrorMessage();
92 if (errorMessage == null)
93 errorMessage = "no error";
94 result.append("<error>" + errorMessage + "</error>");
95 result.append("</status>");
96 if (docOperation.getName().equals("delete")) {
97 result.append("<destination>");
98 result.append("<docId>" + docOperation.getDocIdentifier() + "</docId>");
99 result.append("</destination>");
100 } else if (docOperation.getName().equals("create")) {
101 result.append("<source>");
102 result.append("<url>" + docOperation.getSrcUrl() + "</url>");
103 result.append("<uploadFileName>" + docOperation.getUploadFileName() + "</uploadFileName>");
104 result.append("</source>");
105 result.append("<destination>");
106 result.append("<docId>" + docOperation.getDocIdentifier() + "</docId>");
107 result.append("</destination>");
108 }
109 String desc = "Document operations are maintained on server asychronously. Each operation is scheduled into a server job queue " +
110 "and is executed when all previous started jobs in the queue are worked off. Each operation needs some execution time dependent " +
111 "on the size and the number of pages of the document, the speed of the network connection and the performance of the " +
112 "server.";
113 result.append("<description>" + desc + "</description>");
114 result.append("</job>");
115 }
116 result.append("</docJobs>");
117 } else {
118 String message = "there are no scheduled jobs (neither finished, queued or executed)";
119 result.append("<message>" + message + "</message>");
120 }
121 return result.toString();
122 }
123
124 String createHtmlString(ArrayList<CmsDocOperation> docOperations) {
125 // TODO all
126 StringBuilder result = new StringBuilder();
127 result.append("<html>");
128 result.append("<head>");
129 result.append("<title>" + "Document operation status" + "</title>");
130 result.append("</head>");
131 result.append("<body>");
132 result.append("<table>");
133 result.append("<h1>" + "Document operation status" + "</h1>");
134 if (docOperations != null && ! docOperations.isEmpty()) {
135 for (int i=0; i<docOperations.size(); i++) {
136 result.append("<tr>");
137 result.append("<td>");
138 CmsDocOperation docOperation = docOperations.get(i);
139 result.append("<b>Operation: </b>" + docOperation.getName());
140 int jobId = docOperation.getOrderId();
141 result.append("<p/>");
142 result.append("<b>Job id: </b>" + jobId);
143 result.append("<p/>");
144 result.append("<b>Job status: </b>");
145 result.append("<ul>");
146 Date start = docOperation.getStart();
147 String startStr = "No start time available because job is scheduled into server queue where other jobs have been started earlier";
148 if (start != null)
149 startStr = start.toString();
150 result.append("<li>");
151 result.append("<b>Started: </b>");
152 result.append("<started>" + startStr + "</started>");
153 result.append("</li>");
154 Date end = docOperation.getEnd();
155 String endStr = "No end time available because job is not finished yet";
156 if (end != null)
157 endStr = end.toString();
158 result.append("<li>");
159 result.append("<b>Finished: </b>");
160 result.append("<finished>" + endStr + "</finished>");
161 result.append("</li>");
162 String status = docOperation.getStatus();
163 result.append("<li>");
164 result.append("<b>Description: </b>");
165 result.append("<description>" + status + "</description>");
166 result.append("</li>");
167 String errorMessage = docOperation.getErrorMessage();
168 result.append("<li>");
169 if (errorMessage != null) {
170 result.append("<font color=\"#FF0000\"><b>Error: </b></font>");
171 result.append("<error>" + errorMessage + "</error>");
172 } else {
173 result.append("<b>Error: </b>");
174 result.append("<error>" + "no error" + "</error>");
175 }
176 result.append("</li>");
177 result.append("</ul>");
178 if (docOperation.getName().equals("delete")) {
179 result.append("<b>Destination</b>");
180 result.append("<ul>");
181 result.append("<li>");
182 result.append("<b>Document identifier: </b>");
183 result.append("<docId>" + docOperation.getDocIdentifier() + "</docId>");
184 result.append("</li>");
185 result.append("</ul>");
186 } else if (docOperation.getName().equals("create")) {
187 result.append("<b>Source</b>");
188 result.append("<ul>");
189 result.append("<li>");
190 result.append("<url>" + docOperation.getSrcUrl() + "</url>");
191 String uploadFileName = docOperation.getUploadFileName();
192 if (uploadFileName != null) {
193 result.append("<li>");
194 result.append("<uploadFileName>" + uploadFileName + "</uploadFileName>");
195 result.append("</li>");
196 }
197 result.append("</li>");
198 result.append("</ul>");
199 result.append("<b>Destination</b>");
200 result.append("<ul>");
201 result.append("<li>");
202 result.append("<b>Document identifier: </b>");
203 result.append("<docId>" + docOperation.getDocIdentifier() + "</docId>");
204 result.append("</li>");
205 result.append("</ul>");
206 }
207 String desc = "Document operations are maintained on server asychronously. Each operation is scheduled into a server job queue " +
208 "and is executed when all previous started jobs in the queue are worked off. Each operation needs some execution time dependent " +
209 "on the size and the number of pages of the document, the speed of the network connection and the performance of the " +
210 "server.";
211 result.append("<b>Description: </b>" + desc);
212 result.append("<p/>");
213 result.append("<hr/>");
214 }
215 result.append("</td>");
216 result.append("</tr>");
217 } else {
218 String message = "there are no scheduled jobs (neither finished, queued or executed)";
219 result.append("<message>" + message + "</message>");
220 }
221 result.append("</table>");
222 result.append("</body>");
223 result.append("</html>");
224 return result.toString();
225 }
226
227 String createErrorString(ArrayList<CmsDocOperation> docOperations) {
228 String error = "";
229 if (docOperations != null && ! docOperations.isEmpty()) {
230 for (int i=0; i<docOperations.size(); i++) {
231 CmsDocOperation docOperation = docOperations.get(i);
232 String errorMessage = docOperation.getErrorMessage();
233 if (errorMessage != null)
234 error = errorMessage;
235 }
236 }
237 return error;
238 }
239
240 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
241 doGet(request, response);
242 }
243
244 }