001: package dalma.webui;
002:
003: import dalma.Engine;
004: import dalma.Conversation;
005: import dalma.container.FailedOperationException;
006: import dalma.container.WorkflowApplication;
007: import dalma.container.WorkflowState;
008: import dalma.container.model.Model;
009: import org.kohsuke.stapler.StaplerRequest;
010: import org.kohsuke.stapler.StaplerResponse;
011: import org.apache.commons.fileupload.DiskFileUpload;
012: import org.apache.commons.fileupload.FileItem;
013: import org.apache.commons.fileupload.FileUploadException;
014:
015: import javax.servlet.ServletException;
016: import java.io.IOException;
017: import java.util.Map;
018: import java.util.Properties;
019: import java.util.List;
020: import java.util.Collection;
021: import java.util.Collections;
022: import java.util.ArrayList;
023: import java.util.Comparator;
024:
025: /**
026: * @author Kohsuke Kawaguchi
027: */
028: public class WWorkflow extends UIObject implements
029: Comparable<WWorkflow> {
030: private final WorkflowApplication core;
031:
032: public WWorkflow(WorkflowApplication core) {
033: this .core = core;
034: }
035:
036: public String getName() {
037: return core.getName();
038: }
039:
040: public String getDisplayName() {
041: return core.getName();
042: }
043:
044: public String getDescription() {
045: return core.getDescription();
046: }
047:
048: public boolean isRunning() {
049: return core.getState() == WorkflowState.RUNNING;
050: }
051:
052: public String getUrl() {
053: return "workflow/" + getName() + '/';
054: }
055:
056: public Model getModel() {
057: return core.getModel();
058: }
059:
060: public boolean isConfigured() {
061: return core.isConfigured();
062: }
063:
064: public String getConversationSize() {
065: Engine engine = core.getEngine();
066: return engine != null ? String.valueOf(engine
067: .getConversationsSize()) : "N/A";
068: }
069:
070: public String getLastActiveTime() {
071: Engine engine = core.getEngine();
072: if (engine == null)
073: return "N/A";
074: long t = engine.getLastActiveTime().getTime();
075: if (t == 0)
076: return "N/A";
077:
078: return Functions.getTimeSpanString(System.currentTimeMillis()
079: - t);
080: }
081:
082: /**
083: * Has to be named as "get" to make JSTL happy. Ugly.
084: */
085: public Properties getConfigProperties() throws IOException {
086: return core.loadConfigProperties();
087: }
088:
089: public static WWorkflow wrap(WorkflowApplication app) {
090: if (app == null)
091: return null;
092: return new WWorkflow(app);
093: }
094:
095: public WConversation getConversation(int id) {
096: Engine e = core.getEngine();
097: if (e != null) {
098: Conversation c = e.getConversation(id);
099: if (c != null)
100: return WConversation.wrap(this , c);
101: }
102:
103: return WConversation.wrap(this , core
104: .getCompletedConversations().get(id));
105: }
106:
107: public String getLogRotationDays() {
108: int d = core.getLogRotationDays();
109: if (d == -1)
110: return "";
111: return String.valueOf(d);
112: }
113:
114: public Collection<Conversation> getConversations() {
115: Engine e = core.getEngine();
116: if (e == null)
117: return Collections.emptyList();
118: return toSortedList(e.getConversations());
119: }
120:
121: public Collection<Conversation> getCompletedConversations() {
122: return toSortedList(core.getCompletedConversations().values());
123: }
124:
125: private List<Conversation> toSortedList(Collection<Conversation> c) {
126: List<Conversation> r = new ArrayList<Conversation>(c);
127: Collections.sort(r, REVERSE_CONVERSATION_SORTER);
128: return r;
129: }
130:
131: public void doStop(StaplerRequest req, StaplerResponse resp)
132: throws IOException {
133: core.stop();
134: resp.sendRedirect(".");
135: }
136:
137: public void doStart(StaplerRequest req, StaplerResponse resp)
138: throws IOException, ServletException {
139: try {
140: core.start();
141: resp.sendRedirect(".");
142: } catch (FailedOperationException e) {
143: sendError(req, e, resp);
144: }
145: }
146:
147: public void doDoDelete(StaplerRequest req, StaplerResponse resp)
148: throws IOException, ServletException {
149: try {
150: core.undeploy();
151: resp.sendRedirect(req.getContextPath());
152: } catch (FailedOperationException e) {
153: sendError(req, e, resp);
154: }
155: }
156:
157: /**
158: * Accepts the configuration page submission.
159: */
160: public void doPostConfigure(StaplerRequest req, StaplerResponse resp)
161: throws IOException, ServletException {
162: // TODO: report failed start operation correctly
163: Properties props = core.loadConfigProperties();
164: for (Map.Entry<String, String[]> e : ((Map<String, String[]>) req
165: .getParameterMap()).entrySet()) {
166: String name = e.getKey();
167: if (!name.startsWith("config-"))
168: continue;
169: name = name.substring(7);
170: props.put(name, e.getValue()[0]);
171: }
172: core.saveConfigProperties(props);
173:
174: String logRotateDays = req.getParameter("logrotate_days");
175: if (logRotateDays == null || logRotateDays.length() == 0)
176: logRotateDays = "-1";
177: try {
178: core.setLogRotationDays(Integer.valueOf(logRotateDays));
179: } catch (NumberFormatException e) {
180: sendError(req, logRotateDays + " is not an integer", resp);
181: }
182:
183: resp.sendRedirect(".");
184: }
185:
186: public void doSubmitNewBinary(StaplerRequest req,
187: StaplerResponse resp) throws IOException, ServletException {
188: byte[] contents = null;
189:
190: try {
191: DiskFileUpload fu = new DiskFileUpload();
192: for (FileItem fi : (List<FileItem>) fu.parseRequest(req)) {
193: if (fi.getFieldName().equals("file"))
194: contents = fi.get();
195: }
196: } catch (FileUploadException e) {
197: sendError(req, e, resp);
198: return;
199: }
200:
201: if (contents == null || contents.length == 0) {
202: sendError(req, "form data incomplete", resp);
203: return;
204: }
205:
206: try {
207: core.owner.deploy(core.getName(), contents);
208: resp.sendRedirect(".");
209: } catch (FailedOperationException e) {
210: sendError(req, e, resp);
211: } catch (InterruptedException e) {
212: sendError(req, e, resp);
213: }
214: }
215:
216: public int compareTo(WWorkflow that) {
217: return this .getName().compareTo(that.getName());
218: }
219:
220: private static final Comparator<Conversation> REVERSE_CONVERSATION_SORTER = new Comparator<Conversation>() {
221: public int compare(Conversation lhs, Conversation rhs) {
222: return rhs.getId() - lhs.getId();
223: }
224: };
225: }
|