source: vanHelsing/trunk/gui/src/de/dass_it/vanhelsing/gui/BeanUtil.java@ 867

Last change on this file since 867 was 867, checked in by tobias, on Apr 26, 2010 at 6:20:44 PM

create Client function added

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.equals("true"))
61 return true;
62 return false;
63 }
64 /**
65 * returns the data type of a given attribute.
66 * @param type resource type of the attribute, like Client
67 * @param key attribute name of the resource, like Port
68 * @return data type of the attribute
69 */
70 public static String getDataType(String type, String key) {
71 return getFieldProperty(type, key, 1);
72 }
73
74 // getRenderer returns rendererType based on properties
75 /**
76 * returns the renderer for a given attribute
77 * @param type resource type of the attribute, like Client
78 * @param key attribute name of the resource, like Port
79 * @return "inputText", "radioGroup" or "selectOneMenu"
80 *
81 */
82 public static String getRenderer(String type, String key) {
83 return getFieldProperty(type, key, 0);
84 }
85 /**
86 * returns properties for a given attribute depending on the value of i.
87 * One should use the getter method for the property.
88 * @param type resource type of the attribute, like Client
89 * @param key attribute name of the resource, like Port
90 * @param i i maybe between 0 and 3. The return value of the method is selected by the value of i
91 * @return one of the four properties as a String of a given attribute.
92 */
93 public static String getFieldProperty(String type, String key, int i) {
94 String daemon = "director";
95 String res = "inputText";
96 if (type.startsWith("SD")) {
97 daemon = "storagedaemon";
98 }
99 if (type.startsWith("FD")) {
100 daemon = "filedaemon";
101 }
102 type = type.toLowerCase();
103 key = key.toLowerCase();
104 key = key.replaceAll(" ", "");
105 String result[] = getProperties((daemon + "." + type + "." + key));
106 if (i == 0) {
107 if (result[1].equals("boolean")) {
108 return "radioGroup";
109 }
110 if (!(result[0].equals("0"))) {
111 return "selectOneMenu";
112 }
113 return res;
114 }
115 if (i == 2)
116 return result[2];
117 if (i == 1)
118 return result[1];
119 if (i == 3)
120 return result[3];
121 return res;
122 }
123
124 // Statusmeldungen an den Client
125 /**
126 * sends an information type message to the message field at the bottom of the screen
127 * @param client could contain an identifier of a client. value can be set to null
128 * @param message the message to be displayed at the clients view
129 */
130 public static void setInfoMessage(String client, String message) {
131 FacesContext.getCurrentInstance().addMessage(client,
132 new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));
133 }
134 /**
135 * sends an error type message to the message field at the bottom of the screen
136 * @param client could contain an identifier of a client. value can be set to null
137 * @param message the message to be displayed at the clients view
138 */
139 public static void setErrorMessage(String client, String message) {
140 FacesContext.getCurrentInstance()
141 .addMessage(
142 client,
143 new FacesMessage(FacesMessage.SEVERITY_ERROR, message,
144 message));
145 }
146
147 // Linkout to Bacula documentation
148 // ListItemsConfigView
149 // ListItemsTopologyView
150 // ListItemsJobSchedule
151
152 // GetParameterFromContext
153 /**
154 * returns values of a given parameter of the FacesContext
155 * @param name name of the parameter e.g. the id of a parameter in the view.
156 * @return value of a given parameter
157 */
158 public static String getRequestParameter(String name) {
159 return (String) FacesContext.getCurrentInstance().getExternalContext()
160 .getRequestParameterMap().get(name);
161 }
162 /**
163 * translation method for resource or object names.
164 * @param type untranslated name
165 * @return translated name if there is a translation. Otherwise type will be returned.
166 */
167 public static String getAccessType(String type) {
168 String result = new String();
169 HashMap<String, String> map = new HashMap<String, String>();
170 map.put("jobdefs", "job");
171 map.put("SDStorage", "Storage");
172 map.put("Storage", "SDStorage");
173 map.put("SDDevice", "Device");
174 map.put("Device", "SDDevice");
175 map.put("SDDevice", "Device");
176 result = map.get(type);
177 if (result != null)
178 return result;
179 return type;
180 }
181 /**
182 *
183 * @param key resource name
184 * @return newRes ArrayList of ViewItem
185 */
186 public static ArrayList<ViewItem> getTypeProperties(String key){
187 ArrayList<ViewItem> newRes = new ArrayList<ViewItem>();
188 ArrayList<ViewItem> newOptRes = new ArrayList<ViewItem>();
189 ArrayList<String> properties = PropertyBundle.getTypeAttributes("director."+key);
190
191 System.err.println(properties.size());
192 ViewItem vi;
193 for (String s : properties){
194 vi = new ViewItem();
195 vi.setRequired(BeanUtil.getRequired(key, s));
196 vi.setDataType(BeanUtil.getDataType(key, s));
197 vi.setKey(s);
198 vi.setRenderer(BeanUtil.getRenderer(key, s));
199 vi.setKeyValue(BeanUtil.getFieldProperty(key, s, 3));
200 if (vi.getRequired()){
201 newRes.add(vi);
202 } else {
203 newOptRes.add(vi);
204 }
205 }
206 newRes.addAll(newOptRes);
207 return newRes;
208 }
209}
Note: See TracBrowser for help on using the repository browser.