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