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