1 /* 2 * Copyright 2008-2013 smartics, Kronseder & Reiner GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package de.smartics.maven.issues.bugzilla; 17 18 import java.text.MessageFormat; 19 import java.util.Collections; 20 import java.util.HashMap; 21 import java.util.Map; 22 import java.util.MissingResourceException; 23 import java.util.ResourceBundle; 24 25 import org.apache.commons.lang.ArrayUtils; 26 import org.apache.maven.doxia.sink.Sink; 27 import org.eclipse.mylyn.internal.bugzilla.core.BugzillaAttribute; 28 import org.eclipse.mylyn.tasks.core.data.TaskAttribute; 29 30 import de.smartics.maven.issues.util.Utils; 31 32 /** 33 * Provides helper methods for reports. 34 * 35 * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a> 36 * @version $Revision:591 $ 37 */ 38 public final class ReportHelper 39 { 40 // ********************************* Fields ********************************* 41 42 // --- constants ------------------------------------------------------------ 43 44 /** 45 * An enumeration of Bugzilla attributes referring to person names (keys) and 46 * the corresponding eMail attribute (value). 47 */ 48 public static final Map<String, String> PERSON_NAME_TO_EMAIL; 49 50 // --- members -------------------------------------------------------------- 51 52 // ****************************** Initializer ******************************* 53 54 static 55 { 56 final Map<String, String> map = new HashMap<String, String>(7); 57 map.put(BugzillaAttribute.ASSIGNED_TO_NAME.getKey(), 58 BugzillaAttribute.ASSIGNED_TO.getKey()); 59 map.put(BugzillaAttribute.REPORTER_NAME.getKey(), 60 BugzillaAttribute.REPORTER.getKey()); 61 map.put(BugzillaAttribute.QA_CONTACT_NAME.getKey(), 62 BugzillaAttribute.QA_CONTACT.getKey()); 63 map.put(BugzillaAttribute.WHO_NAME.getKey(), BugzillaAttribute.WHO.getKey()); 64 PERSON_NAME_TO_EMAIL = Collections.unmodifiableMap(map); 65 } 66 67 // ****************************** Constructors ****************************** 68 69 /** 70 * Utility class pattern. 71 */ 72 private ReportHelper() 73 { 74 } 75 76 // ****************************** Inner Classes ***************************** 77 78 // ********************************* Methods ******************************** 79 80 // --- init ----------------------------------------------------------------- 81 82 // --- get&set -------------------------------------------------------------- 83 84 // --- business ------------------------------------------------------------- 85 86 /** 87 * Returns the attribute value for the given attribute key. 88 * 89 * @param root the root attribute to fetch the requested attribute from. 90 * @param name the name of the attribute whose value is requested. 91 * @return the value of the attribute or <code>null</code> if the value is not 92 * set. 93 */ 94 public static String getAttribute(final TaskAttribute root, 95 final BugzillaAttribute name) 96 { 97 return getAttribute(root, name.getKey()); 98 } 99 100 /** 101 * Returns the attribute value for the given attribute key. 102 * 103 * @param root the root attribute to fetch the requested attribute from. 104 * @param key the key of the attribute whose value is requested. 105 * @return the value of the attribute or <code>null</code> if the value is not 106 * set. 107 */ 108 public static String getAttribute(final TaskAttribute root, final String key) 109 { 110 final TaskAttribute attribute = root.getAttribute(key); 111 if (attribute != null) 112 { 113 return attribute.getValue(); 114 } 115 return null; 116 } 117 118 /** 119 * Returns the label for the given attribute. 120 * 121 * @param bundle the bundle to fetch the label from. 122 * @param attributeKey the key to the attribute that is normalized via 123 * {@link Utils#normalizeKey(String)}. 124 * @return the label for the given key or the key, if no label is defined. 125 */ 126 public static String getLabel(final ResourceBundle bundle, 127 final String attributeKey) 128 { 129 String label = attributeKey; 130 try 131 { 132 label = 133 bundle.getString("report.label." + Utils.normalizeKey(attributeKey)); 134 } 135 catch (final MissingResourceException e) 136 { 137 // Ignore and use default. 138 } 139 return label; 140 } 141 142 /** 143 * Returns the label for the given attribute. 144 * 145 * @param bundle the bundle to fetch the label from. 146 * @param attributeKey the key to the attribute that is normalized via 147 * {@link Utils#normalizeKey(String)}. 148 * @param subcategory the subcategory of the label. May be structured by dots. 149 * @return the label for the given key or the key, if no label is defined. 150 */ 151 public static String getLabel(final ResourceBundle bundle, 152 final String subcategory, final String attributeKey) 153 { 154 return getLabel(bundle, subcategory, attributeKey, 155 ArrayUtils.EMPTY_OBJECT_ARRAY); 156 } 157 158 /** 159 * Returns the label for the given attribute. 160 * 161 * @param bundle the bundle to fetch the label from. 162 * @param attributeKey the key to the attribute that is normalized via 163 * {@link Utils#normalizeKey(String)}. 164 * @param subcategory the subcategory of the pattern to construct the label. 165 * May be structured by dots. 166 * @param arguments applied to the pattern found by the key. May be 167 * <code>null</code> or empty in which case the pattern is the label 168 * to be returned without substitution. 169 * @return the label for the given key or the key, if no label is defined. 170 */ 171 public static String getLabel(final ResourceBundle bundle, 172 final String subcategory, final String attributeKey, 173 final Object... arguments) 174 { 175 String label = attributeKey; 176 try 177 { 178 final String pattern = 179 bundle.getString("report.label." + subcategory + '.' 180 + Utils.normalizeKey(attributeKey)); 181 if (arguments != null && arguments.length > 0) 182 { 183 final MessageFormat format = new MessageFormat(pattern); 184 label = format.format(arguments); 185 } 186 else 187 { 188 label = pattern; 189 } 190 return label; 191 } 192 catch (final MissingResourceException e) 193 { 194 // Ignore and use default. 195 } 196 return label; 197 } 198 199 /** 200 * Renders a table cell containing a link with the given label and URL. 201 * 202 * @param sink the sink to write to. 203 * @param url the URL the link points to. 204 * @param label the label of the link. 205 */ 206 public static void renderTableCellWithLink(final Sink sink, final String url, 207 final String label) 208 { 209 sink.tableCell(); 210 renderLink(sink, url, label); 211 sink.tableCell_(); 212 } 213 214 /** 215 * Renders a link. 216 * 217 * @param sink the sink to write to. 218 * @param url the URL the link points to. 219 * @param label the label of the link. 220 */ 221 public static void renderLink(final Sink sink, final String url, 222 final String label) 223 { 224 sink.link(url); 225 sink.text(label); 226 sink.link_(); 227 } 228 229 /** 230 * Renders a table cell with the given label as content. 231 * 232 * @param sink the sink to write to. 233 * @param root the root attribute to fetch the given <code>attribute</code> 234 * from. 235 * @param attribute the attribute whose value is to be rendered. 236 */ 237 public static void renderTableCell(final Sink sink, final TaskAttribute root, 238 final BugzillaAttribute attribute) 239 { 240 final String label = getAttribute(root, attribute); 241 renderTableCell(sink, label); 242 } 243 244 /** 245 * Renders a table cell with the given label as content. 246 * 247 * @param sink the sink to write to. 248 * @param root the root attribute to fetch the given <code>attributeKey</code> 249 * from. 250 * @param attributeKey the key to the attribute whose value is to be rendered. 251 */ 252 public static void renderTableCell(final Sink sink, final TaskAttribute root, 253 final String attributeKey) 254 { 255 final String label = getAttribute(root, attributeKey); 256 renderTableCell(sink, label); 257 } 258 259 /** 260 * Renders a table cell with the given label as content. 261 * 262 * @param sink the sink to write to. 263 * @param label the content of the cell. 264 */ 265 public static void renderTableCell(final Sink sink, final String label) 266 { 267 sink.tableCell(); 268 sink.text(label); 269 sink.tableCell_(); 270 } 271 272 /** 273 * Renders a table cell with a mailto link. 274 * 275 * @param sink the sink to write to. 276 * @param root the root attribute to fetch the values for name and eMail. 277 * @param nameKey the key to the value of the name attribute. 278 * @param eMailKey the key to the value of the eMail attribute. 279 */ 280 public static void renderTableCellWithMailto(final Sink sink, 281 final TaskAttribute root, final String nameKey, final String eMailKey) 282 { 283 final String name = getAttribute(root, nameKey); 284 final String eMail = getAttribute(root, eMailKey); 285 sink.tableCell(); 286 sink.link("mailto:" + eMail); 287 sink.text(name); 288 sink.link_(); 289 sink.tableCell_(); 290 } 291 292 // --- object basics -------------------------------------------------------- 293 294 }