1
2 package inklings.jgatms.command;
3
4 import java.util.List;
5
6 import net.sf.hibernate.Session;
7 import net.sf.hibernate.Transaction;
8 import org.apache.commons.chain.Context;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12
13 import inklings.jgatms.ContextKeys;
14 import inklings.jgatms.bean.Question;
15 import inklings.jgatms.bean.Response;
16
17 /***
18 * Adds a response to the list of possible responses for a question.
19 *
20 * @author Greg Reddin
21 */
22 public class AddResponseToQuestion extends BaseCommand {
23
24 private static Log log = LogFactory.getLog(AddResponseToQuestion.class);
25
26 public boolean execute(Context context) throws Exception {
27
28 Session session = (Session) context.get(getHibernateSessionKey());
29 if (session == null) {
30 log.error("No Hibernate Session object found.");
31 context.put(ContextKeys.DISPATCH_KEY, ContextKeys.FATAL_DISPATCH);
32 return true;
33 }
34
35 Question question = (Question) context.get(ContextKeys.QUESTION_KEY);
36 if (question == null) {
37 log.error("Cannot add response. No question object found.");
38 context.put(ContextKeys.DISPATCH_KEY, ContextKeys.FATAL_DISPATCH);
39 return true;
40 }
41
42 Response response = (Response) context.get(ContextKeys.RESPONSE_KEY);
43 if (response == null) {
44 log.error("Cannot add response. No response object found.");
45 context.put(ContextKeys.DISPATCH_KEY, ContextKeys.FATAL_DISPATCH);
46 return true;
47 }
48
49 Transaction tx = null;
50 try {
51 tx = session.beginTransaction();
52 question.getPossibleResponses().add(response);
53 session.update(question);
54 tx.commit();
55 context.put(ContextKeys.DISPATCH_KEY, ContextKeys.SUCCESS_DISPATCH);
56 } catch (Exception e) {
57 if (tx != null) {
58 tx.rollback();
59 }
60
61 log.fatal("Could not add response to question.", e);
62 context.put(ContextKeys.DISPATCH_KEY, ContextKeys.FATAL_DISPATCH);
63 return true;
64 } finally {
65 if (session != null)
66 session.close();
67 }
68
69 return false;
70 }
71
72 }
This page was automatically generated by Maven