1
2
3
4
5
6
7
8
9
10 package com.sri.emo.dbobj;
11
12 import com.jcorporate.expresso.core.controller.ControllerRequest;
13 import com.jcorporate.expresso.core.controller.Transition;
14 import com.jcorporate.expresso.core.db.DBException;
15 import com.jcorporate.expresso.core.dbobj.DBObject;
16 import com.jcorporate.expresso.core.security.User;
17 import com.sri.emo.commandline.defaults.AbstractNodeExportSupport;
18 import com.sri.emo.controller.AddNodeAction;
19 import org.dom4j.Element;
20
21 import java.io.IOException;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Map;
26
27
28 /***
29 * base class for custom handler for settings
30 *
31 * @author Larry Hamel
32 */
33 public abstract class AbstractSettingHandler extends AbstractAttributeHandler {
34 public static final String SETTING = "SETTING";
35
36 public AbstractSettingHandler(String attribName, Class editorClass,
37 String viewState, String promptEditState) {
38 super(attribName, editorClass, viewState, promptEditState);
39 }
40
41 /***
42 * clone data from existing to clone
43 */
44 public void clone(Attribute existing, Attribute clone)
45 throws DBException {
46
47 Setting setting = new Setting(User.getAdmin(existing.getDataContext()));
48 setting.setDataContext(existing.getDataContext());
49 setting.setTemplateId(existing.getParentNodeId());
50
51
52
53
54
55
56
57 List settingList = setting.searchAndRetrieveList();
58
59 for (Iterator iterator = settingList.iterator(); iterator.hasNext();) {
60 Setting aSetting = (Setting) iterator.next();
61 aSetting.setRequestingUser(clone.getRequestingUser());
62
63 aSetting.setTemplateId(clone.getParentNodeId());
64
65 aSetting.addIfNeeded();
66 }
67 }
68
69 public void delete(Attribute attribute) throws DBException {
70
71 Setting setting = new Setting(attribute.getRequestingUser());
72 setting.setDataContext(attribute.getDataContext());
73 setting.setTemplateId(attribute.getParentNodeId());
74
75 List settingList = setting.searchAndRetrieveList();
76
77 for (Iterator iterator = settingList.iterator(); iterator.hasNext();) {
78 Setting aSetting = (Setting) iterator.next();
79 aSetting.delete();
80 }
81 }
82
83 public Element addXML(Attribute attrib, Element root, String typeName,
84 ControllerRequest request) throws DBException {
85 Setting setting = new Setting(User.getAdmin(attrib.getDataContext()));
86 setting.setDataContext(attrib.getDataContext());
87 setting.setTemplateId(attrib.getParentNodeId());
88
89 List settingList = setting.searchAndRetrieveList();
90
91 for (Iterator iterator = settingList.iterator(); iterator.hasNext();) {
92 Setting aSetting = (Setting) iterator.next();
93 addSingleSettingElem(root, typeName, aSetting, request);
94 }
95
96 return root;
97 }
98
99 protected Element addSingleSettingElem(Element root, String typeName,
100 ISetting aSetting, ControllerRequest request) throws DBException {
101 Element settingElm = root.addElement(SETTING);
102 settingElm.addAttribute(Node.NODE_TYPE, typeName);
103 settingElm.addAttribute(Setting.SETTING_NODE_ID, aSetting.getSettingTargetId());
104 settingElm.addAttribute(Setting.TEMPLATE_NODE_ID,
105 aSetting.getTemplateId());
106 settingElm.addAttribute(Setting.VALUE, aSetting.getDisplayValue());
107
108 if (aSetting.getComment().length() > 0) {
109 settingElm.addAttribute(Setting.COMMENT, aSetting.getComment());
110 }
111
112 return settingElm;
113 }
114
115 public void parseXML(Element elem, Attribute attrib, Map allNodesByXML_ID,
116 ControllerRequest request) throws DBException {
117 List list = elem.selectNodes("./" + SETTING);
118
119 for (Iterator iterator = list.iterator(); iterator.hasNext();) {
120 Element settingElem = (Element) iterator.next();
121 Setting aSetting = new Setting(attrib.getRequestingUser());
122 aSetting.setDataContext(attrib.getDataContext());
123
124 parseSingleSetting(allNodesByXML_ID, settingElem, aSetting);
125
126 aSetting.add();
127 }
128 }
129
130 protected ISetting parseSingleSetting(Map allNodesByXML_ID,
131 Element settingElem, ISetting aSetting) throws DBException {
132 String parentID = settingElem.attributeValue(Setting.SETTING_NODE_ID);
133 Node parent = (Node) allNodesByXML_ID.get(parentID);
134
135 if (parent == null) {
136 try {
137 throw new DBException("cannot find node within xml with ID: " +
138 parentID + " expected within xml: " +
139 AddNodeAction.getPrettyXML(settingElem));
140 } catch (IOException e) {
141 throw new DBException("cannot find node within xml with ID: " +
142 parentID);
143 }
144 }
145
146 aSetting.setSettingTargetId(parent.getNodeId());
147
148 String templateID = settingElem.attributeValue(Setting.TEMPLATE_NODE_ID);
149 Node template = (Node) allNodesByXML_ID.get(templateID);
150
151 if (template == null) {
152 try {
153 throw new DBException("cannot find template within xml with ID: " + templateID +
154 " expected within xml: " +
155 AddNodeAction.getPrettyXML(settingElem));
156 } catch (IOException e) {
157 throw new DBException("cannot find template within xml with ID: " + templateID);
158 }
159 }
160
161 aSetting.setTemplateId(template.getNodeId());
162
163 aSetting.setValue(settingElem.attributeValue(Setting.VALUE));
164 aSetting.setComment(settingElem.attributeValue(Setting.COMMENT));
165
166 return aSetting;
167 }
168
169
170 /***
171 * @return null as a signal to use standard output
172 */
173 public Transition getViewTransition(Map params) throws DBException {
174 Transition trans = super.getViewTransition(params);
175 trans.setLabel("View Settings");
176 return trans;
177 }
178
179
180 /***
181 * get a map of Inputs for all settings.
182 *
183 * @return map keyed by setting key (from the getKey() function), value is list of Inputs
184 */
185 public abstract List getInputs(String templateId) throws Exception;
186
187 public abstract ISetting getSettingPrototype() throws DBException;
188
189
190 /***
191 * @return an array of SQL Insert statements that are equivalent to the contents of this attribute; can be empty but never null
192 */
193 public String[] getInsertStatements(AbstractNodeExportSupport treeExporter, DBObject obj, ISetting setting) throws DBException {
194 Attribute attrib = (Attribute) obj;
195 List result = new LinkedList();
196
197 setting.setTemplateId(attrib.getParentNodeId());
198 List settingList = setting.searchAndRetrieveList();
199 for (Iterator iterator = settingList.iterator(); iterator.hasNext();) {
200 DBObject aSetting = (DBObject) iterator.next();
201 result.addAll(treeExporter.getSQL(aSetting));
202 }
203
204 return (String[]) result.toArray(new String[result.size()]);
205 }
206 }