001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.web.statemachine;
024:
025: import java.io.ByteArrayOutputStream;
026: import java.io.IOException;
027: import java.io.ObjectInputStream;
028: import java.io.ObjectOutputStream;
029: import java.sql.Connection;
030: import java.sql.Date;
031: import java.sql.SQLException;
032: import java.util.ArrayList;
033: import java.util.Collection;
034: import java.util.Iterator;
035: import java.util.zip.GZIPInputStream;
036:
037: import javax.servlet.ServletInputStream;
038: import javax.servlet.ServletOutputStream;
039: import javax.servlet.http.HttpServletRequest;
040: import javax.servlet.http.HttpServletResponse;
041:
042: import org.apache.log4j.Logger;
043:
044: import biz.hammurapi.sql.IDatabaseObject;
045: import biz.hammurapi.sql.IdentityGenerator;
046: import biz.hammurapi.sql.IdentityManager;
047: import biz.hammurapi.sql.IdentityRetriever;
048: import biz.hammurapi.sql.SQLProcessor;
049: import biz.hammurapi.sql.Transaction;
050: import biz.hammurapi.util.Attributable;
051: import biz.hammurapi.web.ActionsBase;
052: import biz.hammurapi.web.HttpError;
053: import biz.hammurapi.web.statemachine.sql.SmState;
054: import biz.hammurapi.web.statemachine.sql.SmStateImpl;
055: import biz.hammurapi.web.statemachine.sql.SmTransitionImpl;
056: import biz.hammurapi.web.statemachine.sql.StateMachineEngine;
057: import biz.hammurapi.web.statemachine.sql.StateMachineImpl;
058:
059: public class StateMachineActions extends ActionsBase {
060: private static final Logger logger = Logger
061: .getLogger(StateMachineActions.class);
062:
063: protected static StateMachineEngine getEngine(
064: HttpServletRequest request) {
065: return new StateMachineEngine((SQLProcessor) getGlobal(request,
066: "sql-stateMachineor"));
067: }
068:
069: /**
070: * Stores stateMachine in the database.
071: * @param request
072: * @param response
073: */
074: public void save(HttpServletRequest request,
075: HttpServletResponse response) throws IOException {
076: try {
077: ServletInputStream in = request.getInputStream();
078: GZIPInputStream gzis = new GZIPInputStream(in);
079: ObjectInputStream ois = new ObjectInputStream(gzis);
080: biz.hammurapi.web.statemachine.sql.StateMachine data = (biz.hammurapi.web.statemachine.sql.StateMachine) ois
081: .readObject();
082: Transaction transaction = Boolean.TRUE
083: .equals(((Attributable) data).getAttribute("new")) ? create(
084: data, request)
085: : update(data, request);
086: SQLProcessor stateMachineor = (SQLProcessor) getGlobal(
087: request, "sql-stateMachineor");
088: stateMachineor.executeTransaction(transaction);
089: ois.close();
090: gzis.close();
091: in.close();
092: } catch (Exception e) {
093: logger.error("Could not create stateMachine", e);
094: response.sendError(500, e.toString());
095: }
096: }
097:
098: private Transaction update(
099: final biz.hammurapi.web.statemachine.sql.StateMachine data,
100: final HttpServletRequest request) {
101: return new Transaction() {
102:
103: public boolean execute(SQLProcessor stateMachineor)
104: throws SQLException {
105: Connection con = stateMachineor.getConnection();
106: StateMachineEngine engine = new StateMachineEngine(
107: new SQLProcessor(con, null));
108: data.setLastModified(new Date(System
109: .currentTimeMillis()));
110: data.setVersion(data.getVersion() + 1);
111: engine.deleteSmStateByStateMachine(data.getId());
112: engine.updateStateMachine(data);
113: insertTasksAndTransitions(data,
114: (IdentityManager) getGlobal(request,
115: "db/IdentityManager"), con, engine);
116: stateMachineor.releaseConnection(con);
117: return true;
118: }
119:
120: };
121: }
122:
123: private Transaction create(
124: final biz.hammurapi.web.statemachine.sql.StateMachine data,
125: final HttpServletRequest request) {
126: return new Transaction() {
127:
128: public boolean execute(SQLProcessor stateMachineor)
129: throws SQLException {
130: IdentityManager identityManager = (IdentityManager) getGlobal(
131: request, "db/IdentityManager");
132: Connection con = stateMachineor.getConnection();
133: StateMachineEngine engine = new StateMachineEngine(
134: new SQLProcessor(con, null));
135: data.setLastModified(new Date(System
136: .currentTimeMillis()));
137: data.setVersion(1);
138: // User user = (User) request.getSession().getAttribute(AuthFilter.USER);
139: // data.setOwner(user==null ? null : user.getLoginName());
140: String menuId = request.getParameter("MENU_ID");
141: if (!isBlank(menuId)) {
142: data.setOwnerMenu(Integer.parseInt(menuId));
143: }
144: if (identityManager instanceof IdentityGenerator) {
145: data.setId(((IdentityGenerator) identityManager)
146: .generate(con, "PROCESS_TASK_DEF"));
147: }
148: engine.insertStateMachine(data);
149: if (identityManager instanceof IdentityRetriever) {
150: data.setId(((IdentityRetriever) identityManager)
151: .retrieve(con));
152: }
153:
154: insertTasksAndTransitions(data, identityManager, con,
155: engine);
156:
157: stateMachineor.releaseConnection(con);
158: return true;
159: }
160:
161: };
162: }
163:
164: private void insertTasksAndTransitions(
165: final biz.hammurapi.web.statemachine.sql.StateMachine data,
166: IdentityManager identityManager, Connection con,
167: StateMachineEngine engine) throws SQLException {
168: Collection steps = (Collection) ((Attributable) data)
169: .getAttribute("states");
170: if (steps != null) {
171: Iterator it = steps.iterator();
172: while (it.hasNext()) {
173: SmStateImpl originalTask = (SmStateImpl) it.next();
174: class PtdiEx extends SmStateImpl {
175: PtdiEx() {
176: super (true);
177: }
178:
179: void crearId() {
180: getColumn("ID").clear();
181: }
182: }
183:
184: PtdiEx state = new PtdiEx();
185: state.copy(originalTask);
186: state.crearId();
187:
188: state.setStateMachineId(data.getId());
189: if (identityManager instanceof IdentityGenerator) {
190: state.setId(((IdentityGenerator) identityManager)
191: .generate(con, "PROCESS_TASK_DEF"));
192: }
193: engine.insertSmState(state);
194: if (identityManager instanceof IdentityRetriever) {
195: state.setId(((IdentityRetriever) identityManager)
196: .retrieve(con));
197: }
198: originalTask.setId(state.getId()); // To propagate to transition.
199: }
200: }
201:
202: Collection transitions = (Collection) ((Attributable) data)
203: .getAttribute("transitions");
204: if (transitions != null) {
205: Iterator it = transitions.iterator();
206: while (it.hasNext()) {
207: SmTransitionImpl originalTransition = (SmTransitionImpl) it
208: .next();
209:
210: class PTEx extends SmTransitionImpl {
211: PTEx() {
212: super (true);
213: }
214:
215: void crearId() {
216: getColumn("ID").clear();
217: }
218: }
219:
220: PTEx transition = new PTEx();
221: transition.copy(originalTransition);
222: transition.crearId();
223:
224: SmState transitionSource = (SmState) ((IDatabaseObject) originalTransition)
225: .getColumnAttribute("FROM_STATE", "state");
226: transition.setFromState(transitionSource.getId());
227: SmState transitionTarget = (SmState) ((IDatabaseObject) originalTransition)
228: .getColumnAttribute("TO_STATE", "state");
229: transition.setToState(transitionTarget.getId());
230: engine.insertSmTransition(transition);
231: }
232: }
233: }
234:
235: public void get(HttpServletRequest request,
236: HttpServletResponse response) throws IOException {
237: try {
238: StateMachineEngine engine = getEngine(request);
239: StateMachineImpl stateMachine = (StateMachineImpl) engine
240: .getStateMachine(Integer.parseInt(request
241: .getParameter("ID")));
242: if (stateMachine == null) {
243: response.sendError(404, "Invalid stateMachine id");
244: return;
245: }
246:
247: stateMachine.setStateMachineImage(null); // No need to transfer image over the wire.
248: Collection states = engine.getSmStateByStateMachine(
249: stateMachine.getId(), new ArrayList());
250: stateMachine.setAttribute("states", states);
251: Collection transitions = engine.getStateTransitions(
252: stateMachine.getId(), stateMachine.getId(),
253: new ArrayList());
254: stateMachine.setAttribute("transitions", transitions);
255: Iterator it = transitions.iterator();
256: while (it.hasNext()) {
257: SmTransitionImpl transition = (SmTransitionImpl) it
258: .next();
259: Iterator tit = states.iterator();
260: while (tit.hasNext()) {
261: SmStateImpl state = (SmStateImpl) tit.next();
262: if (transition.getFromState() == state.getId()) {
263: transition.setColumnAttribute("FROM_STATE",
264: "state", state);
265: }
266: if (transition.getToState() == state.getId()) {
267: transition.setColumnAttribute("TO_STATE",
268: "state", state);
269: }
270: }
271: }
272: ByteArrayOutputStream baos = new ByteArrayOutputStream();
273: //GZIPOutputStream gzos = new GZIPOutputStream(baos);
274: ObjectOutputStream oos = new ObjectOutputStream(baos /* gzos */);
275: oos.writeObject(stateMachine);
276: oos.close();
277: //gzos.close();
278: baos.close();
279: byte[] bytes = baos.toByteArray();
280: response.setContentType("application/binary");
281: response.setContentLength(bytes.length);
282: //System.out.println("Content length:"+bytes.length);
283: ServletOutputStream out = response.getOutputStream();
284: out.write(bytes);
285: out.close();
286: } catch (Exception e) {
287: logger
288: .error(
289: "Could not retrieve stateMachine from the database",
290: e);
291: response.sendError(500, e.toString());
292: }
293: }
294:
295: /**
296: * Retrieves stateMachine with steps and transitions to be XML-ized and rendered for viewing
297: * @param request
298: * @param response
299: * @return
300: * @throws SQLException
301: * @throws NumberFormatException
302: * @throws SQLException
303: * @throws NumberFormatException
304: */
305: public Object getXml(HttpServletRequest request,
306: HttpServletResponse response) throws NumberFormatException,
307: SQLException {
308: StateMachineEngine engine = getEngine(request);
309: StateMachineImpl ret = (StateMachineImpl) engine
310: .getStateMachine(Integer.parseInt(request
311: .getParameter("ID")));
312: if (ret == null) {
313: return new HttpError(404, "Invalid stateMachine id");
314: }
315: ret.setStateMachineImage(null); // No need to transfer image over the wire.
316: Collection states = engine.getSmStateByStateMachine(
317: ret.getId(), new ArrayList());
318: ret.setAttribute("states", states);
319: Collection transitions = engine.getStateTransitions(
320: ret.getId(), ret.getId(), new ArrayList());
321: ret.setAttribute("transitions", transitions);
322: return ret;
323: }
324:
325: public void getImage(HttpServletRequest request,
326: HttpServletResponse response) throws NumberFormatException,
327: SQLException, IOException {
328: biz.hammurapi.web.statemachine.sql.StateMachine stateMachine = getEngine(
329: request).getStateMachine(
330: Integer.parseInt(request.getParameter("ID")));
331: if (stateMachine == null) {
332: response.sendError(404, "Invalid stateMachine id");
333: } else {
334: byte[] content = stateMachine.getStateMachineImage();
335: if (content == null) {
336: response.sendError(404, "StateMachine image not found");
337: } else {
338: response.setContentType(stateMachine.getImageType());
339: response.setContentLength(content.length);
340: ServletOutputStream out = response.getOutputStream();
341: out.write(content);
342: out.close();
343: }
344: }
345: }
346:
347: }
|