source: vanHelsing/trunk/gui/src/de/dass_it/vanhelsing/gui/BeanUtil.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: 6.6 KB
Line 
1package de.dass_it.vanhelsing.gui;
2
3import javax.faces.application.FacesMessage;
4import javax.faces.application.FacesMessage.Severity;
5import javax.faces.context.FacesContext;
6import javax.faces.model.SelectItem;
7
8import edu.emory.mathcs.backport.java.util.Collections;
9
10import java.util.ArrayList;
11import java.util.ResourceBundle;
12import java.util.Locale;
13import java.util.HashMap;
14
15//import org.apache.log4j.Logger;
16
17/**
18 * Helper class to encapsulate common bean methods.
19 * Managed beans will extend this class either directly or
20 * via a data structure helper class like TreeNavigation
21 * @author tgoecke
22 */
23public class BeanUtil {
24 // Logging
25
26 // protected final Logger logger = Logger.getLogger(this.getClass());
27
28 // Messagebundles
29 /**
30 * getter method for a property value
31 * @param key key of the queried property, e.g. director.client.name.type
32 * @return s value of the property
33 */
34 public static String getProperty(String key) {
35 String s = PropertyBundle.getProperty(key);
36 return s;
37 }
38 /**
39 * convenience method to return all properties of a given attribute
40 * @param key key contains the key value of the set of properties for a given attribute<br/>
41 * except for the suffix which defines the content type of the property.
42 * @return bundle contains all four properties of an attribute
43 */
44 public static String[] getProperties(String key) {
45 String[] suffix = { "ref", "type", "required", "def" };
46 String[] bundle = new String[4];
47 for (int i = 0; i < 4; i++) {
48 bundle[i] = BeanUtil.getProperty(key + "." + suffix[i]);
49 }
50 return bundle;
51 }
52 /**
53 * returns true if the given attribute is required
54 * @param type resource type of the attribute, like Client
55 * @param key attribute name of the resource, like Port
56 * @return true if the field is required
57 */
58 public static boolean getRequired(String type, String key) {
59 String s = getFieldProperty(type, key, 2);
60 if (s == null) return false;
61 if (s.equals("true"))
62 return true;
63 return false;
64 }
65 /**
66 * returns the data type of a given attribute.
67 * @param type resource type of the attribute, like Client
68 * @param key attribute name of the resource, like Port
69 * @return data type of the attribute
70 */
71 public static String getDataType(String type, String key) {
72 return getFieldProperty(type, key, 1);
73 }
74
75 // getRenderer returns rendererType based on properties
76 /**
77 * returns the renderer for a given attribute
78 * @param type resource type of the attribute, like Client
79 * @param key attribute name of the resource, like Port
80 * @return "inputText", "radioGroup" or "selectOneMenu"
81 *
82 */
83 public static String getRenderer(String type, String key) {
84 return getFieldProperty(type, key, 0);
85 }
86 /**
87 * returns properties for a given attribute depending on the value of i.
88 * One should use the getter method for the property.
89 * @param type resource type of the attribute, like Client
90 * @param key attribute name of the resource, like Port
91 * @param i i maybe between 0 and 3. The return value of the method is selected by the value of i
92 * @return one of the four properties as a String of a given attribute.
93 */
94 public static String getFieldProperty(String type, String key, int i) {
95 String daemon = "director";
96 String res = "inputText";
97 if (type.startsWith("SD")) {
98 daemon = "storagedaemon";
99 }
100 if (type.startsWith("FD")) {
101 daemon = "filedaemon";
102 }
103 type = type.toLowerCase();
104 key = key.toLowerCase();
105 key = key.replaceAll(" ", "");
106 String result[] = getProperties((daemon + "." + type + "." + key));
107 if (result[0] == null) return null;
108 if (i == 0) {
109 if (result[1].equals("boolean")) {
110 return "radioGroup";
111 }
112 if (!(result[0].equals("0"))) {
113 return "selectOneMenu";
114 }
115 return res;
116 }
117 if (i == 2)
118 return result[2];
119 if (i == 1)
120 return result[1];
121 if (i == 3)
122 return result[3];
123 return res;
124 }
125
126 // Statusmeldungen an den Client
127 /**
128 * sends an information type message to the message field at the bottom of the screen
129 * @param client could contain an identifier of a client. value can be set to null
130 * @param message the message to be displayed at the clients view
131 */
132 public static void setInfoMessage(String client, String message) {
133 FacesContext.getCurrentInstance().addMessage(client,
134 new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));
135 }
136 /**
137 * sends an error type message to the message field at the bottom of the screen
138 * @param client could contain an identifier of a client. value can be set to null
139 * @param message the message to be displayed at the clients view
140 */
141 public static void setErrorMessage(String client, String message) {
142 FacesContext.getCurrentInstance()
143 .addMessage(
144 client,
145 new FacesMessage(FacesMessage.SEVERITY_ERROR, message,
146 message));
147 }
148
149 // Linkout to Bacula documentation
150 // ListItemsConfigView
151 // ListItemsTopologyView
152 // ListItemsJobSchedule
153
154 // GetParameterFromContext
155 /**
156 * returns values of a given parameter of the FacesContext
157 * @param name name of the parameter e.g. the id of a parameter in the view.
158 * @return value of a given parameter
159 */
160 public static String getRequestParameter(String name) {
161 return (String) FacesContext.getCurrentInstance().getExternalContext()
162 .getRequestParameterMap().get(name);
163 }
164 /**
165 * translation method for resource or object names.
166 * @param type untranslated name
167 * @return translated name if there is a translation. Otherwise type will be returned.
168 */
169 public static String getAccessType(String type) {
170 String result = new String();
171 HashMap<String, String> map = new HashMap<String, String>();
172 map.put("jobdefs", "job");
173 map.put("SDStorage", "Storage");
174 map.put("Storage", "SDStorage");
175 map.put("SDDevice", "Device");
176 map.put("Device", "SDDevice");
177 map.put("SDDevice", "Device");
178 result = map.get(type);
179 if (result != null)
180 return result;
181 return type;
182 }
183 /**
184 *
185 * @param key resource name
186 * @return newRes ArrayList of ViewItem
187 */
188 public static ArrayList<ViewItem> getTypeProperties(String key){
189 ArrayList<ViewItem> newRes = new ArrayList<ViewItem>();
190 ArrayList<ViewItem> newOptRes = new ArrayList<ViewItem>();
191 ArrayList<String> properties = PropertyBundle.getTypeAttributes("director."+key);
192
193 System.err.println(properties.size());
194 ViewItem vi;
195 for (String s : properties){
196 vi = new ViewItem();
197 vi.setRequired(BeanUtil.getRequired(key, s));
198 vi.setDataType(BeanUtil.getDataType(key, s));
199 vi.setKey(s);
200 vi.setRenderer(BeanUtil.getRenderer(key, s));
201 vi.setKeyValue(BeanUtil.getFieldProperty(key, s, 3));
202 if (vi.getRequired()){
203 newRes.add(vi);
204 } else {
205 newOptRes.add(vi);
206 }
207 }
208 newRes.addAll(newOptRes);
209 return newRes;
210 }
211}
Note: See TracBrowser for help on using the repository browser.