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