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.util.Vector;
37  
38  import javax.ws.rs.Produces;
39  import javax.ws.rs.WebApplicationException;
40  import javax.ws.rs.core.MediaType;
41  import javax.ws.rs.core.MultivaluedMap;
42  import javax.ws.rs.ext.MessageBodyWriter;
43  import javax.ws.rs.ext.Provider;
44  
45  import org.imixs.workflow.ItemCollection;
46  import org.imixs.workflow.xml.EntityTable;
47  import org.imixs.workflow.xml.XMLItemCollection;
48  import org.imixs.workflow.xml.XMLItemCollectionAdapter;
49  
50  /**
51   * This MessageBodyWriter generates an HTML representation from a EntityTable.
52   * The output is a table format where each entity has the same columns.
53   * 
54   * @author rsoika
55   * 
56   */
57  @Provider
58  @Produces("text/html")
59  public class EntityTableWriter implements MessageBodyWriter<EntityTable> {
60  
61  	public boolean isWriteable(Class<?> type, Type genericType,
62  			Annotation[] annotations, MediaType mediaType) {
63  		return EntityTable.class.isAssignableFrom(type);
64  	}
65  
66  	public void writeTo(EntityTable entityCollection, Class<?> type,
67  			Type genericType, Annotation[] annotations, MediaType mediaType,
68  			MultivaluedMap<String, Object> httpHeaders,
69  			OutputStream entityStream) throws IOException,
70  			WebApplicationException {
71  
72  		boolean trClass = true;
73  
74  		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
75  				entityStream));
76  
77  		bw.write("<html>");
78  		XMLItemCollectionWriter.printHead(bw);
79  
80  		bw.write("<body>");
81  
82  		/* Print table header */
83  		bw.write("<table><tbody>");
84  		if (trClass)
85  			bw.write("<tr class=\"a\">");
86  		else
87  			bw.write("<tr class=\"b\">");
88  		trClass = !trClass;
89  
90  		for (String attr : entityCollection.getAttributeList()) {
91  			bw.write("<th>" + attr + "</th>");
92  		}
93  		bw.write("</tr>");
94  
95  		// print table body
96  		try {
97  
98  			for (XMLItemCollection xmlworkItem : entityCollection.getEntity()) {
99  				/* Print row */
100 				if (trClass)
101 					bw.write("<tr class=\"a\">");
102 				else
103 					bw.write("<tr class=\"b\">");
104 				trClass = !trClass;
105 
106 				for (String attr : entityCollection.getAttributeList()) {
107 					ItemCollection itemCol = XMLItemCollectionAdapter
108 							.getItemCollection(xmlworkItem);
109 					Vector vValues = itemCol.getItemValue(attr);
110 					/*
111 					 * String sValue = vValues.toString(); sValue =
112 					 * sValue.substring(1, sValue.length() - 1);
113 					 */
114 					bw.write("<td>"
115 							+ XMLItemCollectionWriter
116 									.convertValuesToString(vValues) + "</td>");
117 				}
118 				bw.write("</tr>");
119 
120 			}
121 
122 			bw.write("</tbody></table>");
123 		} catch (Exception e) {
124 			bw.write("ERROR<br>");
125 			// e.printStackTrace(bw.);
126 		}
127 
128 		bw.write("</body>");
129 		bw.write("</html>");
130 
131 		bw.flush();
132 	}
133 
134 	public long getSize(EntityTable arg0, Class<?> arg1, Type arg2,
135 			Annotation[] arg3, MediaType arg4) {
136 		return -1;
137 	}
138 
139 }