source: vanHelsing/trunk/gui/src/de/dass_it/vanhelsing/gui/ConfigurationBean.java@ 864

Last change on this file since 864 was 864, checked in by tobias, on Apr 16, 2010 at 5:55:54 PM

javadoc added

File size: 11.6 KB
Line 
1package de.dass_it.vanhelsing.gui;
2
3import java.lang.reflect.Method;
4import java.util.ArrayList;
5
6import javax.faces.event.ActionEvent;
7import javax.faces.event.ValueChangeEvent;
8import javax.faces.model.SelectItem;
9import javax.swing.tree.DefaultMutableTreeNode;
10import javax.swing.tree.DefaultTreeModel;
11import de.dass_it.vanhelsing.gui.items.*;
12import de.dass_it.www.vanhelsing.*;
13import de.dass_it.www.vanhelsing.VanHelsingCallbackHandler.*;
14import de.dass_it.www.vanhelsing.VanHelsingStub.*;
15
16/**
17 * List all Bacula configuration components by name.
18 */
19public class ConfigurationBean extends TreeNavigation {
20
21 private DefaultTreeModel tree;
22 private ArrayList<ViewItem> selectedObject;
23 private String selectedId;
24
25 public ConfigurationBean() {
26 init();
27 }
28
29 /**
30 * init() builds the data tree of the configuration view. The heading nodes are read from the properties file.
31 * the web service will be queried for each heading node(e.g. client).
32 */
33 private void init(){
34 DefaultMutableTreeNode root = new DefaultMutableTreeNode();
35 DefaultMutableTreeNode header;
36 DefaultMutableTreeNode leaf;
37 String type;
38 ListResourcesType lrt;
39 GetResourceType grt;
40 DefaultMutableTreeNode myNode;
41 ResourceInfo[] ri;
42 SimpleResource sr;
43
44 Client c = new Client();
45 setTree(super.createTree(root, BeanUtil.getProperty("nodes").split(","), "Configuration"));
46
47
48 for (int i = 0; i < tree.getChildCount(root); i++) {
49 header = (DefaultMutableTreeNode) tree.getChild(root, i);
50 type = ((UserObjectItemType) header.getUserObject())
51 .getUserObject().getResType();
52 lrt = new ListResourcesType();
53 lrt.setDirector("bacula-dir");
54 lrt.setResourceType(type);
55 ri = c.getListResources(lrt);
56 if (ri == null || ri.length == 0)
57 continue;
58 for (int j = 0; j < ri.length; j++) {
59 myNode = addNode(header, type, ri[j]);
60 grt = new GetResourceType();
61 grt.setDirector(lrt.getDirector());
62 grt.setResId(ri[j].getResId());
63 sr = c.getSimpleResource(grt);
64 myNode = addNode(myNode, type, sr.getResourceAttributeType());
65 if (myNode != null) {
66 header.add(myNode);
67 }
68 }
69 }
70 }
71
72 /**
73 * transfers the selected (by attribute resId) dataobject (resource) into an ArrayList of ViewItems.<br/>
74 * Each ViewItem contain the key value pair and the rendering type. If the attribute is rendered as a
75 * selectOneMenu, the values for the selectOneMenu will be read from the data tree.
76 *
77 * @param ae ActionEvent which contains relevant information about the component tree. the variable is not used.
78 */
79 public void userObjectItemNodeSelected(ActionEvent ae) {
80 DefaultMutableTreeNode node;
81 UserObjectItem userObject;
82 selectedObject = new ArrayList<ViewItem>();
83 String Id = BeanUtil.getRequestParameter("userObject.resId");
84 String selected = BeanUtil.getRequestParameter("ice.event.shift");
85 node = getNode(Id, tree);
86 if (selected.equals("true")){
87 ((UserObjectItemType)node.getUserObject()).switchSelected();
88 }
89 String type = ((UserObjectItem) ((UserObjectItemType) node
90 .getUserObject()).getUserObject()).getResType();
91 String resName = ((ItemType)((UserObjectItem) ((UserObjectItemType) node
92 .getUserObject()).getUserObject())).getResName();
93 String director = ((ItemType)((UserObjectItem) ((UserObjectItemType) node
94 .getUserObject()).getUserObject())).getDirector();
95 String classType = "de.dass_it.vanhelsing.gui.items." + type + "Item";
96 String accessType = BeanUtil.getAccessType(type.toLowerCase());
97 try {
98 UserObjectItem item = ((UserObjectItemType) (node.getUserObject()))
99 .getUserObject();
100 Class c = ((UserObjectItemType) (node.getUserObject()))
101 .getUserObject().getClass();
102 Method[] m = c.getDeclaredMethods();
103 String keyValue;
104 String key;
105 ViewItem vi;
106 for (Method n : m) {
107 if (n.getName().startsWith("get")) {
108 keyValue = (String) n.invoke(item, (Object[]) null);
109 if (keyValue == null)
110 continue;
111 key = n.getName().substring(3);
112 vi = new ViewItem();
113 vi.setResId(new Integer(Id));
114 vi.setKey(key);
115 vi.setKeyValue(keyValue);
116 vi.setResType(type);
117 vi.setResName(resName);
118 vi.setDirector(director);
119 vi.setRendererFlag(BeanUtil.getRenderer(accessType, key));
120 vi.setRequired(BeanUtil.getRequired(accessType, key));
121 if (vi.getSelectOneMenu() != null){
122 DefaultMutableTreeNode[] dmtn = getChildNodes(tree, vi.getKey());
123 if (dmtn != null) {
124 SelectItem[] si = new SelectItem[dmtn.length];
125 String objectType;
126 for (int i = 0; i < dmtn.length; i++){
127 UserObjectItem nodeObject = ((UserObjectItemType)dmtn[i].getUserObject()).getUserObject();
128 objectType = "de.dass_it.vanhelsing.gui.items." + vi.getKey() +"Item";
129 Class subClass = Class.forName(objectType);
130 Class myClass = nodeObject.getClass().asSubclass(subClass);
131 Method o = myClass.getMethod("getName", (Class[])null);
132 si[i] = new SelectItem((String)o.invoke(nodeObject, (Object[])null),
133 (String)o.invoke(nodeObject, (Object[])null));
134
135 }
136 vi.setKeyValueList(si);
137 }
138 }
139 selectedObject.add(vi);
140 }
141 selectedObject.trimToSize();
142 }
143 } catch (Exception e) {
144 e.printStackTrace();
145 }
146 }
147 /**
148 * return an array of nodes of a given resource type
149 *
150 * @param tree2 copy of the data tree
151 * @param key name of the requested resource type
152 * @return dmtn array of nodes of a given resource type
153 * @return null null object there are no nodes of the requested type
154 */
155 private DefaultMutableTreeNode[] getChildNodes(DefaultTreeModel tree2, String key) {
156 DefaultMutableTreeNode[] dmtn;
157 DefaultMutableTreeNode root = (DefaultMutableTreeNode)tree2.getRoot();
158 DefaultMutableTreeNode node;
159 for (int i = 0; i< root.getChildCount(); i++){
160 node = (DefaultMutableTreeNode)root.getChildAt(i);
161 if (((UserObjectItemType)(node.getUserObject())).getUserObject().getResType().equals(key)){
162 dmtn = new DefaultMutableTreeNode[node.getChildCount()];
163 for (int j = 0; j < node.getChildCount(); j++){
164 dmtn[j] = (DefaultMutableTreeNode)node.getChildAt(j);
165 }
166 return dmtn;
167 }
168 }
169 return null;
170 }
171 /**
172 *
173 * @param ae
174 */
175 public void newResourceButtonListener(ActionEvent ae){
176 DefaultMutableTreeNode node;
177 UserObjectItem userObject;
178 selectedObject = new ArrayList<ViewItem>();
179 String Id = BeanUtil.getRequestParameter("userObject.ResId");
180 String shift = BeanUtil.getRequestParameter("ice.event.shift");
181 System.err.println(shift);
182 if (Id == null){
183 return;
184 }
185 node = getNode(Id, tree);
186 String type = ((UserObjectItem) ((UserObjectItemType) node
187 .getUserObject()).getUserObject()).getResType();
188 ViewItem vi;
189 String[] val;
190 try{
191 String type2 = "de.dass_it.vanhelsing.gui.items." + type + "Item";
192 Class newClass = Class.forName(type2);
193 for (Method m : newClass.getDeclaredMethods()){
194 if (m.getName().startsWith("get")) {
195 vi = new ViewItem();
196 vi.setKey(m.getName().substring(3));
197 vi.setRendererFlag(BeanUtil.getRenderer(type, vi.getKey()));
198 if (vi.getSelectOneMenu() != null){
199 DefaultMutableTreeNode[] dmtn = getChildNodes(tree, vi.getKey());
200 if (dmtn != null) {
201 SelectItem[] si = new SelectItem[dmtn.length];
202 String objectType;
203 for (int i = 0; i < dmtn.length; i++){
204 UserObjectItem nodeObject = ((UserObjectItemType)dmtn[i].getUserObject()).getUserObject();
205 objectType = "de.dass_it.vanhelsing.gui.items." + vi.getKey() +"Item";
206 Class subClass = Class.forName(objectType);
207 Class myClass = nodeObject.getClass().asSubclass(subClass);
208 Method o = myClass.getMethod("getName", (Class[])null);
209 si[i] = new SelectItem((String)o.invoke(nodeObject, (Object[])null),
210 (String)o.invoke(nodeObject, (Object[])null));
211
212 }
213 vi.setKeyValueList(si);
214 }
215 }
216 if (vi.getInputText() != null){
217 vi.setValue(BeanUtil.getProperty("director."+type+"."+vi.getKey()+".def"));
218 }
219 }
220 }
221 } catch (Exception e){
222 e.printStackTrace();
223 }
224
225 }
226 /**
227 * **EXPERIMENTAL** returns type of the selected resource.
228 * The method is used for an evaluation about dynamic navigation.
229 * @param ae ActionEvent contains relevant information about the jsf component tree.
230 * @return viewName name of the resource type of the selected data tree node
231 */
232 public String newResourceListener(ActionEvent ae){
233 if (selectedObject != null && selectedObject.size() > 0){
234 String resType = selectedObject.get(0).getResType();
235 String viewName = BeanUtil.getAccessType(resType);
236 return viewName;
237 }
238 return "";
239 }
240
241 public void userObjectItemOptionSelected(ValueChangeEvent vce){
242
243 }
244 /**
245 * repeat the entire construction process of the data tree
246 * @param ae
247 */
248 public void reloadResourceButtonListener(ActionEvent ae){
249 init();
250 }
251 public void deleteResourceButtonListener(ActionEvent ae){
252 BeanUtil.setInfoMessage(null, "Diese Methode ist noch nicht implementiert.");
253 }
254 /**
255 * **EXPERIMENTAL** writes the selected resource to the data tree of the web service.
256 *
257 * @param ae ActionEvent contains relevant information
258 */
259 public void saveButtonListener(ActionEvent ae) {
260 if (selectedObject == null){
261 BeanUtil.setInfoMessage(null, "bitte eine Resource zum Ändern auswählen");
262 } else {
263 Client c = new Client();
264 ResourceInfo ri = new ResourceInfo();
265 SetSimpleResourceResponse s2r2;
266 ri.setDirector( selectedObject.get(0).getDirector() );
267 ri.setResId( selectedObject.get(0).getResId() );
268 ri.setResName( selectedObject.get(0).getResName() );
269 ri.setResType( selectedObject.get(0).getResType() );
270 // ResourceAttribute
271 String key, value;
272 ResourceAttributeType[] ra = new ResourceAttributeType[selectedObject.size()];
273 for (int i = 0; i < selectedObject.size(); i++) {
274 key = selectedObject.get(i).getKey();
275 value = selectedObject.get(i).getKeyValue();
276 ra[i] = new ResourceAttributeType();
277 ra[i].setKey(key);
278 ra[i].setValue(value);
279 }
280 ResourceInitialization rinit = new ResourceInitialization();
281 rinit.setResInfo(ri);
282 rinit.setResAttribute(ra);
283 rinit.setReplace(true);
284 s2r2 = c.setSimpleResource(rinit);
285 System.err.println("Status setSimpleRes: "+s2r2.getStatus());
286 BeanUtil.setErrorMessage(null, "Status setSimpleRessource: "+s2r2.getStatus());
287
288 /*String status = s2r2.getStatus();
289 if (selectedObject.get(0) != null){
290 selectedObject.get(0).setResId(s2r2.getResId());
291 }*/
292 // TODO: write ResId to selectedObject
293 // selectedObject.get(0).setResId(grt.getResId());
294 // valueChangeEffect.setFired(false);
295 }
296 }
297 /**
298 * Setter method for the tree attribute
299 * @param tree tree contains the DefaultTreeModel for the data tree
300 */
301 public void setTree(DefaultTreeModel tree) {
302 this.tree = tree;
303 }
304 /**
305 * getter method for the tree attribute
306 * @return tree tree contains the DefaultTreeModel for the data tree
307 */
308 public DefaultTreeModel getTree() {
309 return tree;
310 }
311
312 /**
313 * gettter method for the selectedObject attribute
314 * @return selectedObject selectedObject contains the data object of the selected leaf node.
315 */
316 public ArrayList<ViewItem> getSelectedObject() {
317 return selectedObject;
318 }
319
320 /**
321 * setter method for the selectedObject attribute
322 * @param selectedObject selectedObject contains the data object of the selected leaf node
323 */
324 public void setSelectedObject(ArrayList<ViewItem> selectedObject) {
325 this.selectedObject = selectedObject;
326 }
327
328 /**
329 * getter method for the resId attribute of the selectedObject i.e. the selected leaf node of the data tree
330 * @return id resId attribute of the selected object
331 */
332 public int getSelectedId(){
333 if (selectedObject != null){
334 if (selectedObject.size() > 0){
335 return selectedObject.get(0).getResId();
336 }
337 }
338 return -1;
339 }
340
341}
Note: See TracBrowser for help on using the repository browser.