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

Last change on this file since 868 was 868, checked in by tobias, on May 3, 2010 at 7:12:46 PM

configurationView moved to new DataTree class.
implemented selectOneMenu on DataTree

File size: 14.9 KB
Line 
1package de.dass_it.vanhelsing.gui;
2
3import java.lang.reflect.Method;
4import java.util.ArrayList;
5
6import javax.faces.context.FacesContext;
7import javax.faces.event.ActionEvent;
8import javax.faces.event.ValueChangeEvent;
9import javax.faces.model.SelectItem;
10import javax.swing.tree.DefaultMutableTreeNode;
11import javax.swing.tree.DefaultTreeModel;
12import de.dass_it.vanhelsing.gui.items.*;
13import de.dass_it.www.vanhelsing.*;
14import de.dass_it.www.vanhelsing.VanHelsingCallbackHandler.*;
15import de.dass_it.www.vanhelsing.VanHelsingStub.*;
16
17/**
18 * List all Bacula configuration components by name.
19 */
20public class ConfigurationBean implements ViewBean{
21
22
23 private DataTree dataTree;
24 private ConcreteUserObjectItem selectedObject;
25 private boolean createResource;
26 private String selectedId;
27
28 public ConfigurationBean() {
29 init();
30 }
31
32 /**
33 * init() builds the data tree of the configuration view. The heading nodes are read from the properties file.
34 * the web service will be queried for each heading node(e.g. client).
35 */
36 private void init(){
37 DefaultMutableTreeNode parent;
38 ResourceInfo resItems[];
39 SimpleResource sr;
40 Client c = new Client();
41 UserObjectItemFactory UOFactory = new UserObjectItemFactory();
42 setDataTree(new DataTree());
43 dataTree.setTree("Configuration");
44 for (String name : BeanUtil.getProperty("nodes").split(",")){
45 parent = dataTree.createNode(dataTree.getRoot(),
46 UOFactory.createUserObjectItem(
47 c.makeResourceInfo(BeanUtil.getProperty("defaultdirector"), -1, name, name)
48 )
49 );
50 resItems = c.getListResources(BeanUtil.getProperty("defaultdirector"), name);
51 if (resItems != null){
52 for (ResourceInfo rinfo : resItems){
53 sr = c.getSimpleResource(rinfo.getDirector(), rinfo.getResId());
54 if (sr != null){
55 //TODO: discard the SimpleResource ResInfo return value and work with the query object
56 dataTree.createNode(parent,
57 UOFactory.createUserObjectItem(
58 sr.getResourceAttributeType(), rinfo));
59 }
60 }
61 }
62 }
63 dataTree.createKeyValueLists();
64 dataTree.updateUserObjects();
65 }
66
67 /**
68 * transfers the selected (by attribute resId) dataobject (resource) into an ArrayList of ViewItems.<br/>
69 * Each ViewItem contain the key value pair and the rendering type. If the attribute is rendered as a
70 * selectOneMenu, the values for the selectOneMenu will be read from the data tree.
71 *
72 * @param ae ActionEvent which contains relevant information about the component tree. the variable is not used.
73 */
74 public void renderResource(ActionEvent ae){
75 DefaultMutableTreeNode node;
76 String id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("userObject.resId");
77 int numId = new Integer(id);
78 String name = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("userObject.resName");
79 if (numId > 0){
80 node = dataTree.getNodeById(numId);
81 selectedObject = (ConcreteUserObjectItem)((UserObjectItemType)(node.getUserObject())).getUserObject();
82 } else {
83 node = dataTree.getNodeByName(name);
84 selectedObject = (ConcreteUserObjectItem)((UserObjectItemType)(node.getUserObject())).getUserObject();
85 }
86 }
87 /*public void userObjectItemNodeSelected(ActionEvent ae) {
88 DefaultMutableTreeNode node;
89 UserObjectItem userObject;
90 selectedObject.setViewItemList(viewItemList) = new ArrayList<ViewItem>();
91 String Id = BeanUtil.getRequestParameter("userObject.resId");
92 String name = BeanUtil.getRequestParameter("userObject.resName");
93 String selected = BeanUtil.getRequestParameter("ice.event.shift");
94 node = getNode(Id, tree);
95 if (selected.equals("true")){
96 ((UserObjectItemType)node.getUserObject()).switchSelected();
97 }
98 String type = ((UserObjectItem) ((UserObjectItemType) node
99 .getUserObject()).getUserObject()).getResType();
100 String resName = ((ItemType)((UserObjectItem) ((UserObjectItemType) node
101 .getUserObject()).getUserObject())).getResName();
102 String director = ((ItemType)((UserObjectItem) ((UserObjectItemType) node
103 .getUserObject()).getUserObject())).getDirector();
104 String classType = "de.dass_it.vanhelsing.gui.items." + type + "Item";
105 String accessType = BeanUtil.getAccessType(type.toLowerCase());
106 try {
107 UserObjectItem item = ((UserObjectItemType) (node.getUserObject()))
108 .getUserObject();
109 Class c = ((UserObjectItemType) (node.getUserObject()))
110 .getUserObject().getClass();
111 Method[] m = c.getDeclaredMethods();
112 String keyValue;
113 String key;
114 ViewItem vi;
115 for (Method n : m) {
116 if (n.getName().startsWith("get")) {
117 keyValue = (String) n.invoke(item, (Object[]) null);
118 if (keyValue == null)
119 continue;
120 key = n.getName().substring(3);
121 vi = new ViewItem();
122 vi.setResId(new Integer(Id));
123 vi.setKey(key);
124 vi.setKeyValue(keyValue);
125 vi.setResType(type);
126 vi.setResName(resName);
127 vi.setDirector(director);
128 vi.setRendererFlag(BeanUtil.getRenderer(accessType, key));
129 vi.setRequired(BeanUtil.getRequired(accessType, key));
130 if (vi.getSelectOneMenu() != null){
131 DefaultMutableTreeNode[] dmtn = getChildNodes(tree, vi.getKey());
132 if (dmtn != null) {
133 SelectItem[] si = new SelectItem[dmtn.length];
134 String objectType;
135 for (int i = 0; i < dmtn.length; i++){
136 UserObjectItem nodeObject = ((UserObjectItemType)dmtn[i].getUserObject()).getUserObject();
137 objectType = "de.dass_it.vanhelsing.gui.items." + vi.getKey() +"Item";
138 Class subClass = Class.forName(objectType);
139 Class myClass = nodeObject.getClass().asSubclass(subClass);
140 Method o = myClass.getMethod("getName", (Class[])null);
141 si[i] = new SelectItem((String)o.invoke(nodeObject, (Object[])null),
142 (String)o.invoke(nodeObject, (Object[])null));
143
144 }
145 vi.setKeyValueList(si);
146 }
147 }
148 selectedObject.add(vi);
149 }
150 selectedObject.trimToSize();
151 }
152 } catch (Exception e) {
153 e.printStackTrace();
154 }
155 }*/
156 /**
157 * return an array of nodes of a given resource type
158 *
159 * @param tree2 copy of the data tree
160 * @param key name of the requested resource type
161 * @return dmtn array of nodes of a given resource type
162 * @return null null object there are no nodes of the requested type
163 */
164 /*private DefaultMutableTreeNode[] getChildNodes(DefaultTreeModel tree2, String key) {
165 DefaultMutableTreeNode[] dmtn;
166 DefaultMutableTreeNode root = (DefaultMutableTreeNode)tree2.getRoot();
167 DefaultMutableTreeNode node;
168 for (int i = 0; i< root.getChildCount(); i++){
169 node = (DefaultMutableTreeNode)root.getChildAt(i);
170 if (((UserObjectItemType)(node.getUserObject())).getUserObject().getResType().equals(key)){
171 dmtn = new DefaultMutableTreeNode[node.getChildCount()];
172 for (int j = 0; j < node.getChildCount(); j++){
173 dmtn[j] = (DefaultMutableTreeNode)node.getChildAt(j);
174 }
175 return dmtn;
176 }
177 }
178 return null;
179 }*/
180
181 /**
182 *
183 * @param ae
184 */
185 /*public void newClientButtonListener(ActionEvent ae){
186 String type = "client";
187 ArrayList newResource = new ArrayList<ViewItem>();
188 ResourceInfo[] ri;
189 int i;
190 String s;
191 selectedObject = BeanUtil.getTypeProperties(type);
192 for (ViewItem vi : selectedObject){
193 vi.setRendererFlag(vi.getRenderer());
194 if (vi.getRenderer().equals("selectOneMenu")){
195 SelectItem[] keyValueList;
196 //TODO: implement getter for list of node types
197 Client c = new Client();
198 s = vi.getKey().replaceFirst(new Character(vi.getKey().charAt(0)).toString(),
199 new Character(Character.toUpperCase(vi.getKey().charAt(0))).toString());
200
201 ri = c.getListResources("bacula-dir", s);
202 if (ri != null){
203 keyValueList = new SelectItem[ri.length];
204 i = 0;
205 for (ResourceInfo rinfo : ri){
206 keyValueList[i] = new SelectItem();
207 keyValueList[i].setValue(rinfo.getResName());
208 keyValueList[i].setLabel(rinfo.getResName());
209 i++;
210 }
211 vi.setKeyValueList(keyValueList);
212 }
213
214 }
215 createResource = true;
216 }
217
218
219 }*/
220
221 /*public void newResourceButtonListener(ActionEvent ae){
222 DefaultMutableTreeNode node;
223 UserObjectItem userObject;
224 String Id = BeanUtil.getRequestParameter("userObject.ResId");
225 String value = BeanUtil.getRequestParameter("userObject.resName");
226 String shift = BeanUtil.getRequestParameter("ice.event.shift");
227 selectedObject = new ArrayList<ViewItem>();
228 System.out.println("value: "+value+" id: "+Id);
229
230 if (value.equals("")){
231 return;
232 }
233 //node = getNode(Id, tree);
234 node = getNodeByName(value, tree);
235 String type = ((UserObjectItem) ((UserObjectItemType) node
236 .getUserObject()).getUserObject()).getResType();
237 ViewItem vi;
238 String[] val;
239 try{
240 //value contains the header of the subtree
241 String type2 = "de.dass_it.vanhelsing.gui.items." + value + "Item";
242 Class newClass = Class.forName(type2);
243 for (Method m : newClass.getDeclaredMethods()){
244 if (m.getName().startsWith("get")) {
245 vi = new ViewItem();
246 vi.setKey(m.getName().substring(3));
247 vi.setRendererFlag(BeanUtil.getRenderer(type, vi.getKey()));
248 if (vi.getSelectOneMenu() != null){
249 DefaultMutableTreeNode[] dmtn = getChildNodes(tree, vi.getKey());
250 if (dmtn != null) {
251 SelectItem[] si = new SelectItem[dmtn.length];
252 String objectType;
253 for (int i = 0; i < dmtn.length; i++){
254 UserObjectItem nodeObject = ((UserObjectItemType)dmtn[i].getUserObject()).getUserObject();
255 objectType = "de.dass_it.vanhelsing.gui.items." + vi.getKey() +"Item";
256 Class subClass = Class.forName(objectType);
257 Class myClass = nodeObject.getClass().asSubclass(subClass);
258 Method o = myClass.getMethod("getName", (Class[])null);
259 si[i] = new SelectItem((String)o.invoke(nodeObject, (Object[])null),
260 (String)o.invoke(nodeObject, (Object[])null));
261
262 }
263 vi.setKeyValueList(si);
264 }
265 }
266 if (vi.getInputText() != null){
267 vi.setValue(BeanUtil.getProperty("director."+type+"."+vi.getKey()+".def"));
268 }
269 }
270 }
271 } catch (Exception e){
272 e.printStackTrace();
273 }
274
275 }*/
276 /**
277 * **EXPERIMENTAL** returns type of the selected resource.
278 * The method is used for an evaluation about dynamic navigation.
279 * @param ae ActionEvent contains relevant information about the jsf component tree.
280 * @return viewName name of the resource type of the selected data tree node
281 */
282 /*public String newResourceListener(ActionEvent ae){
283 if (selectedObject != null && selectedObject.size() > 0){
284 String resType = selectedObject.get(0).getResType();
285 String viewName = BeanUtil.getAccessType(resType);
286 return viewName;
287 }
288 return "";
289 }*/
290
291 public void userObjectItemOptionSelected(ValueChangeEvent vce){
292
293 }
294 /**
295 * repeat the entire construction process of the data tree
296 * @param ae
297 */
298 public void reloadResourceButtonListener(ActionEvent ae){
299 init();
300 }
301 public void deleteResourceButtonListener(ActionEvent ae){
302 BeanUtil.setInfoMessage(null, "Diese Methode ist noch nicht implementiert.");
303 }
304 /*public void saveClientButtonListener(ActionEvent ae){
305 Client c = new Client();
306 String name = "";
307 ResourceAttributeType[] rat = new ResourceAttributeType[selectedObject.size()];
308 int i =0;
309 for (ViewItem vi : selectedObject){
310 rat[i] = new ResourceAttributeType();
311 rat[i].setKey(vi.getKey());
312 rat[i].setValue(vi.getKeyValue());
313 i++;
314 if (vi.getKey().equals("name")){
315 name = vi.getKeyValue();
316 }
317
318 }
319 int newResId = c.createSimpleResource(rat,
320 c.makeResourceInfo("bacula-dir", 0, name, "Client")
321 );
322 System.err.println(newResId);
323 }*/
324 /**
325 * **EXPERIMENTAL** writes the selected resource to the data tree of the web service.
326 *
327 * @param ae ActionEvent contains relevant information
328 */
329 /*public void saveButtonListener(ActionEvent ae) {
330 if (selectedObject == null){
331 BeanUtil.setInfoMessage(null, "bitte eine Resource zum Ändern auswählen");
332 } else {
333 Client c = new Client();
334 ResourceInfo ri = new ResourceInfo();
335 SetSimpleResourceResponse s2r2;
336 ri.setDirector( selectedObject.get(0).getDirector() );
337 ri.setResId( selectedObject.get(0).getResId() );
338 ri.setResName( selectedObject.get(0).getResName() );
339 ri.setResType( selectedObject.get(0).getResType() );
340 // ResourceAttribute
341 String key, value;
342 ResourceAttributeType[] ra = new ResourceAttributeType[selectedObject.size()];
343 for (int i = 0; i < selectedObject.size(); i++) {
344 key = selectedObject.get(i).getKey();
345 value = selectedObject.get(i).getKeyValue();
346 ra[i] = new ResourceAttributeType();
347 ra[i].setKey(key);
348 ra[i].setValue(value);
349 }
350 ResourceInitialization rinit = new ResourceInitialization();
351 rinit.setResInfo(ri);
352 rinit.setResAttribute(ra);
353 rinit.setReplace(true);
354 s2r2 = c.setSimpleResource(rinit);
355 System.err.println("Status setSimpleRes: "+s2r2.getStatus());
356 BeanUtil.setErrorMessage(null, "Status setSimpleRessource: "+s2r2.getStatus());
357
358 /*String status = s2r2.getStatus();
359 if (selectedObject.get(0) != null){
360 selectedObject.get(0).setResId(s2r2.getResId());
361 }
362 // TODO: write ResId to selectedObject
363 // selectedObject.get(0).setResId(grt.getResId());
364 // valueChangeEffect.setFired(false);
365 }
366 }*/
367 /**
368 * Setter method for the tree attribute
369 * @param tree tree contains the DefaultTreeModel for the data tree
370 */
371 public void setDataTree(DataTree dataTree) {
372 this.dataTree = dataTree;
373 }
374 /**
375 * getter method for the tree attribute
376 * @return tree tree contains the DefaultTreeModel for the data tree
377 */
378 public DataTree getDataTree() {
379 return dataTree;
380 }
381
382 public void createNodeButtonListener(ActionEvent ae) {
383 // TODO Auto-generated method stub
384
385 }
386
387 public void deleteNodeButtonListener(ActionEvent ae) {
388 // TODO Auto-generated method stub
389
390 }
391
392
393 public void readNodeButtonListener(ActionEvent ae) {
394 // TODO Auto-generated method stub
395
396 }
397
398 public void selectNode(ActionEvent ae) {
399 // TODO Auto-generated method stub
400
401 }
402
403 public void updateNodeButtonListener(ActionEvent ae) {
404 // TODO Auto-generated method stub
405
406 }
407
408 /**
409 * gettter method for the selectedObject attribute
410 * @return selectedObject selectedObject contains the data object of the selected leaf node.
411 */
412 public ConcreteUserObjectItem getSelectedObject() {
413 return selectedObject;
414 }
415
416 /**
417 * setter method for the selectedObject attribute
418 * @param selectedObject selectedObject contains the data object of the selected leaf node
419 */
420 public void setSelectedObject(ConcreteUserObjectItem selectedObject) {
421 this.selectedObject = selectedObject;
422 }
423
424 /**
425 * getter method for the resId attribute of the selectedObject i.e. the selected leaf node of the data tree
426 * @return id resId attribute of the selected object
427 */
428 public int getSelectedId(){
429 if (selectedObject != null){
430 return selectedObject.getResId();
431 }
432 return -1;
433 }
434 /*public String getSelectedName(){
435 if (selectedObject != null){
436 if (selectedObject.size() > 0){
437 return selectedObject.get(0).getResName();
438 }
439 }
440 return "";
441 }*/
442 public void setCreateResource(boolean b){
443 this.createResource = b;
444 }
445 public boolean getCreateResource(){
446 return createResource;
447 }
448
449}
Note: See TracBrowser for help on using the repository browser.