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.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
55
56
57
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
175
176
177
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
267
268
269
270
271
272
273
274
275
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
289
290 if (sModelVersion == null)
291 sModelVersion = "";
292
293
294 if (!"".equals(sModelVersion))
295 modelService.removeModelVersion(sModelVersion);
296
297
298 for (int i = 0; i < ecol.getEntity().length; i++) {
299 entity = ecol.getEntity()[i];
300 itemCollection = XMLItemCollectionAdapter
301 .getItemCollection(entity);
302
303 itemCollection.replaceItemValue("$modelVersion",
304 sModelVersion);
305
306 entityService.save(itemCollection);
307 }
308 }
309
310 } catch (Exception e) {
311 e.printStackTrace();
312 }
313
314 }
315
316
317
318
319
320
321
322
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
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
367
368
369
370
371
372
373
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 }