1
2 package inklings.jgatms.command;
3
4 import java.util.List;
5
6 import net.sf.hibernate.Session;
7 import org.apache.commons.chain.Context;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11
12 import inklings.jgatms.ContextKeys;
13 import inklings.jgatms.bean.Question;
14
15 /***
16 * Gets a single question from the database
17 *
18 * @author Greg Reddin
19 */
20 public class GetQuestion extends BaseCommand {
21
22 private static Log log = LogFactory.getLog(GetQuestion.class);
23
24 public String getQuestionKey() {
25 return ContextKeys.QUESTION_KEY;
26 }
27
28 public String getQuestionIdKey() {
29 return ContextKeys.QUESTION_ID_KEY;
30 }
31
32 public boolean execute(Context context) throws Exception {
33
34 Session session = (Session) context.get(getHibernateSessionKey());
35 if (session == null) {
36 log.error("No Hibernate Session object found.");
37 context.put(ContextKeys.DISPATCH_KEY, ContextKeys.FATAL_DISPATCH);
38 return true;
39 }
40
41 Long questionId = (Long) context.get(getQuestionIdKey());
42 if (questionId == null) {
43 log.error("Can't get question. No questionId present.");
44 context.put(ContextKeys.DISPATCH_KEY, ContextKeys.FATAL_DISPATCH);
45 return true;
46 }
47
48 try {
49 Question question = (Question) session.load(
50 Question.class, questionId);
51 context.put(getQuestionKey(), question);
52 context.put(ContextKeys.DISPATCH_KEY, ContextKeys.SUCCESS_DISPATCH);
53 } catch (Exception e) {
54 log.fatal("Error getting question.", e);
55 context.put(ContextKeys.DISPATCH_KEY, ContextKeys.FATAL_DISPATCH);
56 return true;
57 } finally {
58 if (session != null)
59 session.close();
60 }
61
62 return false;
63 }
64
65 }
This page was automatically generated by Maven