1 package com.sri.emo.annotations;
2
3 import com.jcorporate.expresso.core.controller.*;
4 import com.jcorporate.expresso.core.db.DBConnection;
5 import com.jcorporate.expresso.core.db.DBException;
6 import com.jcorporate.expresso.core.security.SuperUser;
7 import com.sri.common.controller.StateHandler;
8 import com.sri.emo.controller.AddNodeAction;
9 import com.sri.emo.dbobj.Node;
10 import org.apache.log4j.Logger;
11
12 import java.util.StringTokenizer;
13
14 /***
15 * Adds the tags defined in the tag.
16 *
17 * @author Michael Rimov
18 * @version 1.0
19 */
20 public class DoAddTag implements StateHandler {
21
22
23 public static final String NAME = "doAddTag";
24
25 public static final String DESCRIPTION = "Add Tag";
26
27 public static final String PARAM_NODE_ID = "nodeId";
28
29 public static final String PARAM_TAG_VALUE = "tagValue";
30
31 public final Controller parentController;
32
33 public DoAddTag(final Controller parent) {
34 this.parentController = parent;
35 }
36
37
38 /***
39 * Adds the tags defined.
40 *
41 * @param request ExpressoRequest
42 * @param response ExpressoResponse
43 * @throws DBException
44 * @throws ControllerException
45 */
46 public void handleRequest(final ExpressoRequest request, final ExpressoResponse response) throws DBException,
47 ControllerException {
48 String result = request.getParameter(PARAM_TAG_VALUE);
49 if (result == null || result.trim().length() == 0) {
50 response.addError("You need to enter a value for the tags.");
51 Transition toTransition = new Transition(PromptAddTag.NAME, parentController);
52 toTransition.addParam(PARAM_NODE_ID, request.getParameter(PARAM_NODE_ID));
53 toTransition.addParam(PARAM_TAG_VALUE, request.getParameter(PARAM_TAG_VALUE));
54 toTransition.execute(request, response, false);
55 return;
56 }
57
58 result = result.trim();
59
60 Integer nodeId = new Integer(request.getParameter(PARAM_NODE_ID));
61
62 Node n = new Node();
63 n.setNodeId(nodeId.toString());
64 try {
65 n.retrieve();
66 } catch (DBException ex) {
67 Logger.getLogger(getClass()).error("Error getting node " + request.getParameter(PARAM_NODE_ID), ex);
68 response.addError("Could not find node ID" + request.getParameter(PARAM_NODE_ID)
69 + " perhaps somebody else deleted it?");
70 Transition toTransition = new Transition(PromptAddTag.NAME, parentController);
71 toTransition.addParam(PARAM_NODE_ID, request.getParameter(PARAM_NODE_ID));
72 toTransition.addParam(PARAM_TAG_VALUE, request.getParameter(PARAM_TAG_VALUE));
73 toTransition.execute(request, response, false);
74 return;
75 }
76
77 NodeTag nodeTags = new NodeTag(SuperUser.INSTANCE);
78 DBConnection c = null;
79 try {
80 c = nodeTags.getConnectionPool().getConnection("Add Tags");
81 c.setAutoCommit(false);
82 StringTokenizer stok = new StringTokenizer(result);
83 while (stok.hasMoreTokens()) {
84 String eachTag = stok.nextToken();
85 if (eachTag.length() > 0) {
86 NodeTag newTag = new NodeTag(c);
87 newTag.setRequestingUser(SuperUser.INSTANCE);
88 newTag.setNodeId(nodeId);
89 newTag.setTagValue(eachTag);
90 newTag.add(request.getRequestingUser().getLoginName());
91 }
92 }
93 c.commit();
94 } catch (Exception ex) {
95 Logger.getLogger(getClass()).error("Error adding tags for " + request.getParameter(PARAM_NODE_ID)
96 + " Tag Values: " + result, ex);
97
98 if (c != null) {
99 c.rollback();
100 }
101
102 if (ex instanceof DBException) {
103 throw (DBException) ex;
104 } else if (ex instanceof ControllerException) {
105 throw (ControllerException) ex;
106 } else if (ex instanceof RuntimeException) {
107 throw (RuntimeException) ex;
108 } else {
109 throw new RuntimeException("Error adding tags", ex);
110 }
111 } finally {
112 if (c != null) {
113 c.release();
114 }
115 }
116 Transition redirect = new Transition("cancel", "Cancel", AddNodeAction.class, AddNodeAction.VIEW_NODE);
117 redirect.addParam(Node.NODE_ID, request.getParameter("nodeId"));
118 redirect.redirectTransition(request, response);
119 }
120 }