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