1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.imixs.workflow.jaxrs;
29
30 import java.io.BufferedWriter;
31 import java.io.IOException;
32 import java.io.OutputStream;
33 import java.io.OutputStreamWriter;
34 import java.lang.annotation.Annotation;
35 import java.lang.reflect.Type;
36 import java.text.DateFormat;
37 import java.util.Calendar;
38 import java.util.Date;
39 import java.util.Iterator;
40 import java.util.Map;
41 import java.util.Vector;
42
43 import javax.ws.rs.Produces;
44 import javax.ws.rs.WebApplicationException;
45 import javax.ws.rs.core.MediaType;
46 import javax.ws.rs.core.MultivaluedMap;
47 import javax.ws.rs.ext.MessageBodyWriter;
48 import javax.ws.rs.ext.Provider;
49
50 import org.imixs.workflow.ItemCollection;
51 import org.imixs.workflow.xml.EntityCollection;
52 import org.imixs.workflow.xml.XMLItemCollection;
53 import org.imixs.workflow.xml.XMLItemCollectionAdapter;
54
55 @Provider
56 @Produces("text/html")
57 public class XMLItemCollectionWriter implements MessageBodyWriter<XMLItemCollection> {
58
59
60 public boolean isWriteable(Class<?> type, Type genericType,
61 Annotation[] annotations, MediaType mediaType) {
62
63 return XMLItemCollection.class.isAssignableFrom(type);
64 }
65
66 public void writeTo(XMLItemCollection xmlItemCollection, Class<?> type, Type genericType,
67 Annotation[] annotations, MediaType mediaType,
68 MultivaluedMap<String, Object> httpHeaders,
69 OutputStream entityStream) throws IOException,
70 WebApplicationException {
71 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
72 entityStream));
73
74
75
76 bw.write("<html>");
77 printHead(bw);
78 bw.write("<body>");
79 try {
80 bw.write("<h1>Entity</h1>");
81
82 printXMLItemCollectionHTML(bw, xmlItemCollection);
83
84
85 } catch (Exception e) {
86 bw.write("ERROR<br>");
87
88 }
89
90
91 bw.write("</body>");
92 bw.write("</html>");
93
94 bw.flush();
95 }
96
97 public long getSize(XMLItemCollection arg0, Class<?> arg1, Type arg2,
98 Annotation[] arg3, MediaType arg4) {
99 return -1;
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 public static void printXMLItemCollectionHTML(BufferedWriter bw, XMLItemCollection xmlworkItem) throws IOException {
117 boolean trClass = false;
118
119 ItemCollection workItem = XMLItemCollectionAdapter.getItemCollection(xmlworkItem);
120 bw.write("<table><tbody>");
121
122 bw.write("<tr class=\"a\">");
123
124
125
126
127
128
129
130 bw.write("<th colspan=\"2\"><b>UniqueID: "
131 + workItem.getItemValueString("$uniqueid") + "</b></th></tr>");
132
133 bw.write("<tr class=\"a\">");
134
135 bw.write("<th>Property</th><th>Value</th></tr>");
136
137 Iterator iter = workItem.getAllItems().entrySet().iterator();
138 while (iter.hasNext()) {
139
140
141 Map.Entry mapEntry = (Map.Entry) iter.next();
142 String sName = mapEntry.getKey().toString();
143 Vector value = (Vector) mapEntry.getValue();
144
145 if (trClass)
146 bw.write("<tr class=\"a\">");
147 else
148 bw.write("<tr class=\"b\">");
149 trClass = !trClass;
150
151 bw.write("<td>" + sName + "</td><td>"
152 + convertValuesToString(value) + "</td></tr>");
153
154 }
155 bw.write("</tbody></table>");
156
157 bw.write("<br/><br/>");
158 }
159
160
161
162
163
164
165
166
167
168
169
170
171 public static String convertValuesToString(Vector values) {
172 String convertedValue = "";
173
174 if (values == null)
175 return convertedValue;
176
177 boolean bFirstValue = true;
178
179 Iterator iter = values.iterator();
180 while (iter.hasNext()) {
181 Object o = iter.next();
182 Date dateValue = null;
183
184 if (o instanceof Date) {
185 dateValue = (Date) o;
186 }
187 if (o instanceof Calendar) {
188 Calendar cal = (Calendar) o;
189 dateValue = cal.getTime();
190 }
191
192
193 String singleValue = "";
194 if (!bFirstValue)
195 convertedValue += "~";
196
197
198
199 if (dateValue != null) {
200 singleValue = DateFormat.getDateTimeInstance(DateFormat.SHORT,
201 DateFormat.SHORT).format(dateValue);
202 } else {
203
204 if (o != null)
205 singleValue = singleValue + o.toString();
206 }
207
208 convertedValue += singleValue;
209 bFirstValue = false;
210 }
211
212
213 return convertedValue;
214 }
215
216
217
218
219
220
221
222
223 public static void printHead(BufferedWriter bw) throws IOException {
224 bw.write("<head>");
225 bw.write("<style>");
226 bw.write("table {padding:0px;width: 100%;margin-left: -2px;margin-right: -2px;}");
227 bw.write("body,td,select,input,li {font-family: Verdana, Helvetica, Arial, sans-serif;font-size: 13px;}");
228 bw.write("table th {color: white;background-color: #bbb;text-align: left;font-weight: bold;}");
229
230 bw.write("table th,table td {font-size: 12px;}");
231
232 bw.write("table tr.a {background-color: #ddd;}");
233
234 bw.write("table tr.b {background-color: #eee;}");
235
236 bw.write("</style>");
237 bw.write("</head>");
238 }
239
240 }