View Javadoc

1   /*******************************************************************************
2    *  Imixs Workflow 
3    *  Copyright (C) 2001, 2011 Imixs Software Solutions GmbH,  
4    *  http://www.imixs.com
5    *  
6    *  This program is free software; you can redistribute it and/or 
7    *  modify it under the terms of the GNU General Public License 
8    *  as published by the Free Software Foundation; either version 2 
9    *  of the License, or (at your option) any later version.
10   *  
11   *  This program is distributed in the hope that it will be useful, 
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
14   *  General Public License for more details.
15   *  
16   *  You can receive a copy of the GNU General Public
17   *  License at http://www.gnu.org/licenses/gpl.html
18   *  
19   *  Project: 
20   *  	http://www.imixs.org
21   *  	http://java.net/projects/imixs-workflow
22   *  
23   *  Contributors:  
24   *  	Imixs Software Solutions GmbH - initial API and implementation
25   *  	Ralph Soika - Software Developer
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  			//e.printStackTrace(bw.);
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 	 * This Method prints a single XMLItemCollection in html format
111 	 * 
112 	 * @param out
113 	 * @param workItem
114 	 * @throws IOException 
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 		bw.write("<th colspan=\"2\"><b>UniqueID: </b><a href=\""
126 				+workItem.getItemValueString("$uniqueid") + 
127 				"\">"
128 				+ workItem.getItemValueString("$uniqueid") + "</a></th></tr>");
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 			// WorkItemAttribute da = new WorkItemAttribute();
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 	 * This method converts the Values of a vector into a string representation.
163 	 * 
164 	 * Multivalues will be separated with '~' characters. Date Objects will be
165 	 * converted into a short String representation taking the server locale
166 	 * 
167 	 * 
168 	 * @param values
169 	 * @return
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 		// Iterate over vector list
179 		Iterator iter = values.iterator();
180 		while (iter.hasNext()) {
181 			Object o = iter.next();
182 			Date dateValue = null;
183 			// now test the objct type to date
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 			// if it is not the first value then add the delimiter ~
193 			String singleValue = "";
194 			if (!bFirstValue)
195 				convertedValue += "~";
196 
197 			// if value is a date object format date into a string
198 			// otherwise take the value as it is
199 			if (dateValue != null) {
200 				singleValue = DateFormat.getDateTimeInstance(DateFormat.SHORT,
201 						DateFormat.SHORT).format(dateValue);
202 			} else {
203 				// take value as it is
204 				if (o != null)
205 					singleValue = singleValue + o.toString();
206 			}
207 
208 			convertedValue += singleValue;
209 			bFirstValue = false;
210 		}
211 
212 		// return values as string
213 		return convertedValue;
214 	}
215 	
216 	/**
217 	 * prints the generic HTML Head for HTML output.
218 	 * The method genereates a default CSS definition for table output
219 	 * 
220 	 * @param bw
221 	 * @throws IOException 
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 }