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.util.Collection;
31  import java.util.List;
32  import java.util.StringTokenizer;
33  import java.util.Vector;
34  
35  import javax.ejb.EJB;
36  import javax.ejb.Stateless;
37  import javax.ws.rs.Consumes;
38  import javax.ws.rs.DELETE;
39  import javax.ws.rs.DefaultValue;
40  import javax.ws.rs.GET;
41  import javax.ws.rs.POST;
42  import javax.ws.rs.PUT;
43  import javax.ws.rs.Path;
44  import javax.ws.rs.PathParam;
45  import javax.ws.rs.Produces;
46  import javax.ws.rs.QueryParam;
47  
48  import org.imixs.workflow.ItemCollection;
49  import org.imixs.workflow.xml.EntityCollection;
50  import org.imixs.workflow.xml.XMLItemCollection;
51  import org.imixs.workflow.xml.XMLItemCollectionAdapter;
52  
53  /**
54   * The WorkflowService Handler supports methods to process different kind of
55   * request URIs
56   * 
57   * @author rsoika
58   * 
59   */
60  @Path("/model")
61  @Produces({ "text/html", "application/xml", "application/json" })
62  @Stateless
63  public class ModelRestService {
64  
65  	@EJB
66  	org.imixs.workflow.jee.ejb.EntityService entityService;
67  
68  	@EJB
69  	org.imixs.workflow.jee.ejb.ModelService modelService;
70  
71  	@GET
72  	@Produces("application/xml")
73  	public String getAllVersions() {
74  		List<String> col = null;
75  		StringBuffer sb = new StringBuffer();
76  		sb.append("<model>");
77  		try {
78  			col = modelService.getAllModelVersions();
79  
80  			for (String aversion : col) {
81  				sb.append("<version>" + aversion + "</version>");
82  			}
83  		} catch (Exception e) {
84  			e.printStackTrace();
85  		}
86  		sb.append("</model>");
87  		return sb.toString();
88  	}
89  
90  	@GET
91  	@Path("/{version}")
92  	public EntityCollection getProcessList(
93  			@PathParam("version") String version,
94  			@QueryParam("items") String items) {
95  		Collection<ItemCollection> col = null;
96  		try {
97  
98  			col = modelService.getProcessEntityListByVersion(version);
99  			return XMLItemCollectionAdapter.putCollection(col,
100 					getItemList(items));
101 
102 		} catch (Exception e) {
103 			e.printStackTrace();
104 		}
105 		return new EntityCollection();
106 	}
107 
108 	@GET
109 	@Path("/{version}.xml")
110 	@Produces("application/xml")
111 	public EntityCollection getProcessListXML(
112 			@PathParam("version") String version,
113 			@QueryParam("items") String items) {
114 		return getProcessList(version, items);
115 	}
116 
117 	@GET
118 	@Path("/{version}.json")
119 	@Produces("application/json")
120 	public EntityCollection getProcessListJSON(
121 			@PathParam("version") String version,
122 			@QueryParam("items") String items) {
123 		return getProcessList(version, items);
124 	}
125 
126 	
127 	
128 	@GET
129 	@Path("/{version}/process/{processid}")
130 	public XMLItemCollection getProcessEntity(
131 			@PathParam("version") String version,
132 			@PathParam("processid") int processid,
133 			@QueryParam("items") String items) {
134 		ItemCollection process= null;
135 		try {
136 
137 			process = modelService.getProcessEntityByVersion(processid,version);
138 			return XMLItemCollectionAdapter.putItemCollection(process,
139 					getItemList(items));
140 
141 		} catch (Exception e) {
142 			e.printStackTrace();
143 		}
144 		return new XMLItemCollection();
145 	}
146 
147 	
148 	@GET
149 	@Path("/{version}/process/{processid}.xml")
150 	@Produces("application/xml")
151 	public XMLItemCollection getProcessEntityXML(
152 			@PathParam("version") String version,
153 			@PathParam("processid") int processid,
154 			@QueryParam("items") String items) {
155 		
156 		return  getProcessEntity(version,processid,items);
157 	}
158 
159 	
160 	@GET
161 	@Path("/{version}/process/{processid}.json")
162 	@Produces("application/json")
163 	public XMLItemCollection getProcessEntityJSON(
164 			@PathParam("version") String version,
165 			@PathParam("processid") int processid,
166 			@QueryParam("items") String items) {
167 		
168 		return  getProcessEntity(version,processid,items);
169 	}
170 
171 	
172 	
173 	/**
174 	 * Retuns a list of all Start Entities from each workflowgroup
175 	 * 
176 	 * @param version
177 	 * @return
178 	 */
179 	@GET
180 	@Path("/groups/{version}")
181 	public EntityCollection getStartProcessList(
182 			@PathParam("version") String version,
183 			@QueryParam("items") String items) {
184 		Collection<ItemCollection> col = null;
185 		try {
186 
187 			col = modelService.getAllStartProcessEntitiesByVersion(version);
188 			return XMLItemCollectionAdapter.putCollection(col,
189 					getItemList(items));
190 
191 		} catch (Exception e) {
192 			e.printStackTrace();
193 		}
194 		return new EntityCollection();
195 	}
196 
197 	@GET
198 	@Path("/groups/{version}.xml")
199 	@Produces("application/xml")
200 	public EntityCollection getStartProcessListXML(
201 			@PathParam("version") String version,
202 			@QueryParam("items") String items) {
203 		return getStartProcessList(version, items);
204 	}
205 
206 	@GET
207 	@Path("/groups/{version}.json")
208 	@Produces("application/json")
209 	public EntityCollection getStartProcessListJSON(
210 			@PathParam("version") String version,
211 			@QueryParam("items") String items) {
212 		return getStartProcessList(version, items);
213 	}
214 
215 	@GET
216 	@Path("/{version}/activities/{processid}")
217 	public EntityCollection getActivityList(
218 			@PathParam("version") String version,
219 			@PathParam("processid") int processid,
220 			@QueryParam("items") String items) {
221 		Collection<ItemCollection> col = null;
222 		try {
223 			col = modelService.getActivityEntityListByVersion(processid,
224 					version);
225 			return XMLItemCollectionAdapter.putCollection(col,
226 					getItemList(items));
227 		} catch (Exception e) {
228 			e.printStackTrace();
229 		}
230 		return new EntityCollection();
231 	}
232 
233 	@GET
234 	@Path("/{version}/activities/{processid}.xml")
235 	@Produces("application/xml")
236 	public EntityCollection getActivityListXML(
237 			@PathParam("version") String version,
238 			@PathParam("processid") int processid,
239 			@QueryParam("items") String items) {
240 		return getActivityList(version, processid,items);
241 	}
242 	
243 	@GET
244 	@Path("/{version}/activities/{processid}.json")
245 	@Produces("application/json")
246 	public EntityCollection getActivityListJSON(
247 			@PathParam("version") String version,
248 			@PathParam("processid") int processid,
249 			@QueryParam("items") String items) {
250 		return getActivityList(version, processid,items);
251 	}
252 	
253 
254 	@DELETE
255 	@Path("/{version}")
256 	public void deleteModel(@PathParam("version") String version) {
257 		try {
258 			modelService.removeModelVersion(version);
259 		} catch (Exception e) {
260 			e.printStackTrace();
261 		}
262 
263 	}
264 
265 	/**
266 	 * This method updates a Model provided in a EntityCollection object for a
267 	 * provided model version. The Method expects a subresource with a
268 	 * ModelVersion. Next the method updates each Entity object with the
269 	 * property $ModelVersion. An old version will be automatically removed
270 	 * before update.
271 	 * 
272 	 * @param version
273 	 *            - $modelversion
274 	 * @param ecol
275 	 *            - model data
276 	 */
277 	@PUT
278 	@Path("/{version}")
279 	@Consumes({ "application/xml", "text/xml" })
280 	public void putModelByVersion(@PathParam("version") String sModelVersion,
281 			EntityCollection ecol) {
282 
283 		XMLItemCollection entity;
284 		ItemCollection itemCollection;
285 		try {
286 			if (ecol.getEntity().length > 0) {
287 				/*
288 				 * first we need to delete the old model if available.
289 				 */
290 				if (sModelVersion == null)
291 					sModelVersion = "";
292 
293 				// delete old model if a modelversion is available
294 				if (!"".equals(sModelVersion))
295 					modelService.removeModelVersion(sModelVersion);
296 
297 				// save new entities into database and update modelversion.....
298 				for (int i = 0; i < ecol.getEntity().length; i++) {
299 					entity = ecol.getEntity()[i];
300 					itemCollection = XMLItemCollectionAdapter
301 							.getItemCollection(entity);
302 					// update model version
303 					itemCollection.replaceItemValue("$modelVersion",
304 							sModelVersion);
305 					// save entity
306 					entityService.save(itemCollection);
307 				}
308 			}
309 
310 		} catch (Exception e) {
311 			e.printStackTrace();
312 		}
313 
314 	}
315 
316 	/**
317 	 * This method updates a Model provided in a EntityCollection object.
318 	 * 
319 	 * The method takes the first entity to get the provided $modelVersion. An
320 	 * old version will be automatically removed before update.
321 	 * 
322 	 * @param ecol
323 	 */
324 	@PUT
325 	@Consumes({ "application/xml", "text/xml" })
326 	public void putModel(EntityCollection ecol) {
327 		String sModelVersion = null;
328 		XMLItemCollection entity;
329 		ItemCollection itemCollection;
330 		try {
331 			if (ecol.getEntity().length > 0) {
332 				/*
333 				 * first we need get model version from first entity
334 				 */
335 				entity = ecol.getEntity()[0];
336 				itemCollection = XMLItemCollectionAdapter
337 						.getItemCollection(entity);
338 				sModelVersion = itemCollection
339 						.getItemValueString("$ModelVersion");
340 
341 				putModelByVersion(sModelVersion, ecol);
342 
343 			}
344 
345 		} catch (Exception e) {
346 			e.printStackTrace();
347 		}
348 
349 	}
350 
351 	@POST
352 	@Path("/{version}")
353 	@Consumes({ "application/xml", "text/xml" })
354 	public void postModelByVersion(@PathParam("version") String sModelVersion,
355 			EntityCollection ecol) {
356 		putModelByVersion(sModelVersion, ecol);
357 	}
358 
359 	@POST
360 	@Consumes({ "application/xml", "text/xml" })
361 	public void postModel(EntityCollection ecol) {
362 		putModel(ecol);
363 	}
364 
365 	/**
366 	 * This method returns a List object from a given comma separated string.
367 	 * The method returns null if no elements are found. The provided parameter
368 	 * looks typical like this: <code>
369 	 *   txtWorkflowStatus,numProcessID,txtName
370 	 * </code>
371 	 * 
372 	 * @param items
373 	 * @return
374 	 */
375 	private List<String> getItemList(String items) {
376 		if (items == null || "".equals(items))
377 			return null;
378 		Vector<String> v = new Vector<String>();
379 		StringTokenizer st = new StringTokenizer(items, ",");
380 		while (st.hasMoreTokens())
381 			v.add(st.nextToken());
382 		return v;
383 	}
384 }