001: package org.bpmscript.web;
002:
003: import java.beans.PropertyEditor;
004: import java.util.HashMap;
005: import java.util.List;
006: import java.util.Map;
007:
008: import javax.servlet.http.HttpServletRequest;
009: import javax.servlet.http.HttpServletResponse;
010:
011: import org.bpmscript.jbi.tasklist.IAllowableResponse;
012: import org.bpmscript.jbi.tasklist.IComplexType;
013: import org.bpmscript.jbi.tasklist.IField;
014: import org.bpmscript.jbi.tasklist.IRemoteTaskListManager;
015: import org.bpmscript.jbi.tasklist.ISimpleType;
016: import org.bpmscript.jbi.tasklist.ITask;
017: import org.bpmscript.jbi.tasklist.IType;
018: import org.bpmscript.jbi.tasklist.TaskListException;
019: import org.springframework.web.servlet.ModelAndView;
020: import org.springframework.web.servlet.mvc.AbstractController;
021:
022: public class TaskController extends AbstractController {
023:
024: private IRemoteTaskListManager remoteTaskListManager;
025: private Map<String, PropertyEditor> propertyEditors;
026: private String formView;
027:
028: public void setRemoteTaskListManager(
029: IRemoteTaskListManager remoteTaskListManager) {
030: this .remoteTaskListManager = remoteTaskListManager;
031: }
032:
033: protected Map<String, Object> referenceData(
034: HttpServletRequest request, HttpServletResponse response)
035: throws Exception {
036: Map<String, Object> model = new HashMap<String, Object>();
037: return model;
038: }
039:
040: @Override
041: protected ModelAndView handleRequestInternal(
042: HttpServletRequest request, HttpServletResponse response)
043: throws Exception {
044: Map<String, Object> model = referenceData(request, response);
045: if ("POST".equals(request.getMethod())) {
046: return onSubmit(request, response, model);
047: } else {
048: return onView(request, response, model);
049: }
050: }
051:
052: protected ModelAndView onView(HttpServletRequest request,
053: HttpServletResponse response, Map<String, Object> model)
054: throws TaskListException {
055: model.put("task", remoteTaskListManager.getTask(request
056: .getParameter("taskId")));
057: return new ModelAndView(formView, model);
058: }
059:
060: protected ModelAndView onSubmit(HttpServletRequest request,
061: HttpServletResponse response, Map<String, Object> model)
062: throws TaskListException {
063: String taskId = request.getParameter("taskId");
064: String path = request.getParameter("path");
065: ITask task = remoteTaskListManager.getTask(taskId);
066: List<IAllowableResponse> allowableResponses = task
067: .getTaskRequest().getAllowableResponses();
068: IType type = null;
069: for (IAllowableResponse allowableResponse : allowableResponses) {
070: if (allowableResponse.getPath().equals(path)) {
071: type = allowableResponse.getType();
072: }
073: }
074: Object result = null;
075: if (type == null) {
076: // add error
077: }
078: if (type instanceof ISimpleType) {
079: ISimpleType simpleType = (ISimpleType) type;
080: Class simpleTypeClass = simpleType.getSimpleType();
081: String value = request.getParameter("value");
082: if (!(simpleTypeClass == String.class)) {
083: PropertyEditor editor = propertyEditors
084: .get(simpleTypeClass.getName());
085: editor.setAsText(value);
086: result = editor.getValue();
087: } else {
088: result = value;
089: }
090: } else if (type instanceof IComplexType) {
091: Map<String, Object> map = new HashMap<String, Object>();
092: IComplexType complexType = (IComplexType) type;
093: for (IField field : complexType.getFields()) {
094: String value = request.getParameter(field.getName());
095: if (field.getType() instanceof ISimpleType) {
096: ISimpleType simpleType = (ISimpleType) field
097: .getType();
098: Class simpleTypeClass = simpleType.getSimpleType();
099: if (!(simpleTypeClass == String.class)) {
100: PropertyEditor editor = propertyEditors
101: .get(simpleTypeClass.getName());
102: editor.setAsText(value);
103: map.put(field.getName(), editor.getValue());
104: } else {
105: map.put(field.getName(), value);
106: }
107: }
108: }
109: result = map;
110: }
111: remoteTaskListManager.respondToTask(taskId, path, result,
112: request.getRemoteUser());
113: model.put("task", remoteTaskListManager.getTask(taskId));
114: return new ModelAndView(formView, model);
115: }
116:
117: public void setPropertyEditors(
118: Map<String, PropertyEditor> propertyEditors) {
119: this .propertyEditors = propertyEditors;
120: }
121:
122: public void setFormView(String formView) {
123: this.formView = formView;
124: }
125:
126: }
|