[830] | 1 | package de.dass_it.vanhelsing.gui;
|
---|
| 2 |
|
---|
[856] | 3 | import javax.faces.application.FacesMessage;
|
---|
| 4 | import javax.faces.application.FacesMessage.Severity;
|
---|
[848] | 5 | import javax.faces.context.FacesContext;
|
---|
[856] | 6 | import javax.faces.model.SelectItem;
|
---|
[867] | 7 |
|
---|
| 8 | import edu.emory.mathcs.backport.java.util.Collections;
|
---|
| 9 |
|
---|
| 10 | import java.util.ArrayList;
|
---|
[856] | 11 | import java.util.ResourceBundle;
|
---|
[857] | 12 | import java.util.Locale;
|
---|
[864] | 13 | import java.util.HashMap;
|
---|
[832] | 14 |
|
---|
| 15 | //import org.apache.log4j.Logger;
|
---|
| 16 |
|
---|
[864] | 17 | /**
|
---|
[830] | 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
|
---|
[865] | 21 | * @author tgoecke
|
---|
[830] | 22 | */
|
---|
| 23 | public class BeanUtil {
|
---|
[864] | 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) {
|
---|
[857] | 35 | String s = PropertyBundle.getProperty(key);
|
---|
| 36 | return s;
|
---|
| 37 | }
|
---|
[864] | 38 | /**
|
---|
[865] | 39 | * convenience method to return all properties of a given attribute
|
---|
[864] | 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" };
|
---|
[857] | 46 | String[] bundle = new String[4];
|
---|
| 47 | for (int i = 0; i < 4; i++) {
|
---|
[864] | 48 | bundle[i] = BeanUtil.getProperty(key + "." + suffix[i]);
|
---|
[857] | 49 | }
|
---|
| 50 | return bundle;
|
---|
| 51 | }
|
---|
[864] | 52 | /**
|
---|
[865] | 53 | * returns true if the given attribute is required
|
---|
[864] | 54 | * @param type resource type of the attribute, like Client
|
---|
| 55 | * @param key attribute name of the resource, like Port
|
---|
[865] | 56 | * @return true if the field is required
|
---|
[864] | 57 | */
|
---|
| 58 | public static boolean getRequired(String type, String key) {
|
---|
[858] | 59 | String s = getFieldProperty(type, key, 2);
|
---|
[868] | 60 | if (s == null) return false;
|
---|
[864] | 61 | if (s.equals("true"))
|
---|
| 62 | return true;
|
---|
[858] | 63 | return false;
|
---|
| 64 | }
|
---|
[864] | 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) {
|
---|
[862] | 72 | return getFieldProperty(type, key, 1);
|
---|
| 73 | }
|
---|
[864] | 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) {
|
---|
[862] | 84 | return getFieldProperty(type, key, 0);
|
---|
[858] | 85 | }
|
---|
[864] | 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) {
|
---|
[857] | 95 | String daemon = "director";
|
---|
| 96 | String res = "inputText";
|
---|
[864] | 97 | if (type.startsWith("SD")) {
|
---|
[857] | 98 | daemon = "storagedaemon";
|
---|
| 99 | }
|
---|
[864] | 100 | if (type.startsWith("FD")) {
|
---|
[857] | 101 | daemon = "filedaemon";
|
---|
| 102 | }
|
---|
| 103 | type = type.toLowerCase();
|
---|
| 104 | key = key.toLowerCase();
|
---|
[862] | 105 | key = key.replaceAll(" ", "");
|
---|
[864] | 106 | String result[] = getProperties((daemon + "." + type + "." + key));
|
---|
[868] | 107 | if (result[0] == null) return null;
|
---|
[862] | 108 | if (i == 0) {
|
---|
[864] | 109 | if (result[1].equals("boolean")) {
|
---|
| 110 | return "radioGroup";
|
---|
[858] | 111 | }
|
---|
[864] | 112 | if (!(result[0].equals("0"))) {
|
---|
[858] | 113 | return "selectOneMenu";
|
---|
[864] | 114 | }
|
---|
[858] | 115 | return res;
|
---|
[857] | 116 | }
|
---|
[864] | 117 | if (i == 2)
|
---|
| 118 | return result[2];
|
---|
| 119 | if (i == 1)
|
---|
| 120 | return result[1];
|
---|
| 121 | if (i == 3)
|
---|
| 122 | return result[3];
|
---|
[857] | 123 | return res;
|
---|
| 124 | }
|
---|
[864] | 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));
|
---|
[848] | 135 | }
|
---|
[864] | 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;
|
---|
[858] | 181 | return type;
|
---|
| 182 | }
|
---|
[867] | 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 | ViewItem vi;
|
---|
| 194 | for (String s : properties){
|
---|
| 195 | vi = new ViewItem();
|
---|
| 196 | vi.setRequired(BeanUtil.getRequired(key, s));
|
---|
| 197 | vi.setDataType(BeanUtil.getDataType(key, s));
|
---|
| 198 | vi.setKey(s);
|
---|
| 199 | vi.setRenderer(BeanUtil.getRenderer(key, s));
|
---|
| 200 | vi.setKeyValue(BeanUtil.getFieldProperty(key, s, 3));
|
---|
[871] | 201 | if (vi.getKeyValue().equals("0")){
|
---|
| 202 | vi.setKeyValue("");
|
---|
| 203 | }
|
---|
[867] | 204 | if (vi.getRequired()){
|
---|
| 205 | newRes.add(vi);
|
---|
| 206 | } else {
|
---|
| 207 | newOptRes.add(vi);
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 | newRes.addAll(newOptRes);
|
---|
| 211 | return newRes;
|
---|
| 212 | }
|
---|
[830] | 213 | }
|
---|