1
2
3
4
5
6
7
8
9
10 package com.sri.emo.dbobj;
11
12 import com.jcorporate.expresso.core.db.DBConnection;
13 import com.jcorporate.expresso.core.db.DBException;
14 import com.jcorporate.expresso.core.dbobj.DBField;
15 import com.jcorporate.expresso.core.dbobj.RowSecuredDBObject;
16 import com.jcorporate.expresso.core.security.ReadOnlyUser;
17 import com.jcorporate.expresso.core.security.SuperUser;
18
19
20 /***
21 * A setting for a variable.
22 *
23 * @author larry hamel
24 */
25 public class Setting extends RowSecuredDBObject implements ISetting {
26 /***
27 *
28 */
29 private static final long serialVersionUID = 1L;
30
31 /***
32 * TEMPLATE node id. PK with SETTING_NODE_ID, foreign key to node table
33 * Settings are attached to templates only
34 */
35 public static final String TEMPLATE_NODE_ID = "TEMPLATE_NODE_ID";
36
37 /***
38 * for this particular node, somehow associated with template, this setting applies
39 */
40 public static final String SETTING_NODE_ID = "SETTING_NODE_ID";
41
42 /***
43 * the chosen category
44 */
45 public static final String VALUE = "VALUE";
46 public static final String COMMENT = "COMMENT";
47 public static final String SETTING_DESCRIPTION = "Setting";
48 public static final String NOT_SPECIFIED_DISPLAY = PickList.NOT_SPECIFIED_DISPLAY;
49 public static final String NOT_SPECIFIED_ENTRY_INDEX = "0";
50
51 /***
52 * @throws DBException If the new object cannot be
53 * created
54 */
55 public Setting() throws DBException {
56 }
57
58 public Setting(String templateId, String settingParentNodeId) throws DBException {
59 super();
60 setTemplateId(templateId);
61 setSettingTargetId(settingParentNodeId);
62 }
63
64 public ISetting getInstance(String templateId, String settingParentNodeId) throws DBException {
65 Setting setting = new Setting();
66 setting.setTemplateId(templateId);
67 setting.setSettingTargetId(settingParentNodeId);
68 return setting;
69 }
70
71 /***
72 * Constructor
73 *
74 * @param theConnection DBConnection to be used to
75 * communicate with the database
76 * @param theUser User name attempting to access the
77 * object
78 * @throws DBException If the user cannot access this
79 * object or the object cannot be initialized
80 */
81 public Setting(DBConnection theConnection, int theUser)
82 throws DBException {
83 super(theConnection, theUser);
84 }
85
86 /***
87 * Constructor for security setup.
88 *
89 * @param userSecurity ReadOnlyUser security context.
90 * @throws DBException upon construction error.
91 */
92 public Setting(final ReadOnlyUser userSecurity) throws DBException {
93 super(userSecurity);
94 }
95
96
97 /***
98 * Defines the database table name and fields for this DB object
99 *
100 * @throws DBException if the operation cannot be performed
101 */
102 protected synchronized void setupFields() throws DBException {
103 setTargetTable("Setting");
104 setDescription(SETTING_DESCRIPTION);
105 addField(TEMPLATE_NODE_ID, DBField.INTEGER_TYPE, 0, false,
106 "foreign key to TEMPLATE node");
107 addField(SETTING_NODE_ID, DBField.INTEGER_TYPE, 0, false,
108 "foreign key to node to which setting applies (e.g., this setting is for TMV with Id=123");
109
110 addField(VALUE, DBField.VARCHAR_TYPE, 254, true, "value");
111 addField(COMMENT, DBField.LONGVARCHAR_TYPE, 0, true, "Comment");
112
113
114 addKey(TEMPLATE_NODE_ID);
115 addKey(SETTING_NODE_ID);
116
117 addIndex("parentID_idx", TEMPLATE_NODE_ID, false);
118 }
119
120 public String getValue() throws DBException {
121 return getField(VALUE);
122 }
123
124 /***
125 * @return ID for node which actually carries the setting value (i.e., not the template)
126 */
127 public String getSettingTargetId() throws DBException {
128 return getField(SETTING_NODE_ID);
129 }
130
131 /***
132 * will throw if not found
133 *
134 * @return the node, like task-model variable, that receives the setting
135 */
136 public Node getSettingTarget() throws DBException {
137 Node smv = new Node(getSettingTargetId());
138 smv.retrieve();
139
140 return smv;
141 }
142
143 public void setTemplateId(String nodeId) throws DBException {
144 setField(TEMPLATE_NODE_ID, nodeId);
145 }
146
147 public void setSettingTargetId(String nodeId) throws DBException {
148 setField(SETTING_NODE_ID, nodeId);
149 }
150
151 public void setValue(String val) throws DBException {
152 setField(VALUE, val);
153 }
154
155 /***
156 * @return if item is menued, we store menu id, so translate stored ID into string; otherwise if freeform, just return stored string, defaulting to (not specified)
157 */
158 public String getDisplayValue()
159 throws DBException {
160 String result = getValue();
161
162 if ((result == null) || (result.length() == 0)) {
163 result = NOT_SPECIFIED_DISPLAY;
164 }
165
166 return result;
167 }
168
169 public String getComment() throws DBException {
170 return getField(COMMENT);
171 }
172
173 public void setComment(String comment) throws DBException {
174 setField(COMMENT, comment);
175 }
176
177 public boolean canWrite(int uid, boolean valueIfNew)
178 throws DBException {
179 boolean result = valueIfNew;
180
181 try {
182 retrieve();
183 result = canRequesterWrite();
184 } catch (Exception e) {
185
186 result = valueIfNew;
187 }
188
189 return result;
190 }
191
192 public String getTemplateId() throws DBException {
193 return getField(TEMPLATE_NODE_ID);
194 }
195
196
197 /***
198 * override to use parent node perms for attrib.
199 *
200 * @param requestedFunction code for function -- Add, Update, Delete, Search (read)
201 * @return true if this function is allowed for this requesting user
202 * @throws SecurityException (unchecked) if not allowed
203 * @throws com.jcorporate.expresso.core.db.DBException
204 * for other data-related errors.
205 */
206 public boolean isRowAllowed(String requestedFunction) throws DBException {
207 if (SuperUser.SYSTEM_UID == getRequestingUid() || getRequestingUser().isAdmin()) {
208 return true;
209 }
210
211 boolean result = false;
212
213 try {
214
215 Node node = getTemplate();
216 result = node.isRowAllowed(requestedFunction);
217 } catch (DBException e) {
218
219 result = super.isRowAllowed(requestedFunction);
220 }
221
222 return result;
223 }
224
225 private Node getTemplate() throws DBException {
226 Node node = new Node(getTemplateId());
227 node.retrieve();
228 return node;
229 }
230
231 /***
232 * OVERRIDE to use parent node permissions
233 *
234 * @param requestedFunction The code of the requested function. The codes are:
235 * <ol><li>A: Add<li>
236 * <li>S: Search<li>
237 * <li>U: Update<li>
238 * <li>D: Delete<li>
239 * </ol>
240 * @throws com.jcorporate.expresso.core.db.DBException
241 * If the requested operation is not permitted to this user
242 * @throws SecurityException if the user is not allowed access to the object.
243 */
244 public void isAllowed(String requestedFunction) throws SecurityException, DBException {
245 if (getRequestingUser() == SuperUser.INSTANCE) {
246 return;
247 }
248
249 if (getRequestingUser().isAdmin()) {
250 return;
251 }
252
253
254 try {
255
256 Node node = getTemplate();
257 node.isAllowed(requestedFunction);
258 } catch (DBException e) {
259
260 super.isAllowed(requestedFunction);
261 }
262 }
263
264 }