1 /* 2 * Copyright 2012-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.properties.report.data; 17 18 import org.apache.commons.lang.StringUtils; 19 20 import de.smartics.properties.api.core.domain.DocumentMetaData; 21 import de.smartics.properties.api.core.domain.PropertyComment; 22 import de.smartics.properties.api.core.domain.PropertyDescriptor; 23 import de.smartics.properties.api.core.domain.PropertyValueComment; 24 import de.smartics.properties.api.core.domain.SourceInfo; 25 import de.smartics.util.lang.Arg; 26 27 /** 28 * Report information for a particular property. 29 */ 30 public final class PropertyReportItem 31 { 32 // ********************************* Fields ********************************* 33 34 // --- constants ------------------------------------------------------------ 35 36 // --- members -------------------------------------------------------------- 37 38 /** 39 * The descriptor to the property. 40 */ 41 private final PropertyDescriptor descriptor; 42 43 /** 44 * The Javadoc comment to the descriptor. 45 */ 46 private final PropertyComment comment; 47 48 /** 49 * The information about the source the property was defined in. 50 */ 51 private final SourceInfo sourceInfo; 52 53 /** 54 * The optional value in case this is a runtime report. 55 */ 56 private Object value; 57 58 // ****************************** Initializer ******************************* 59 60 // ****************************** Constructors ****************************** 61 62 private PropertyReportItem(final Builder builder) 63 { 64 this.descriptor = builder.descriptor; 65 this.comment = builder.comment; 66 this.sourceInfo = builder.sourceInfo; 67 } 68 69 // ****************************** Inner Classes ***************************** 70 71 /** 72 * Builder to create instances of enclosing class. 73 */ 74 public static final class Builder 75 { 76 // ******************************** Fields ******************************** 77 78 // --- constants ---------------------------------------------------------- 79 80 // --- members ------------------------------------------------------------ 81 82 /** 83 * The descriptor to the property. 84 */ 85 private PropertyDescriptor descriptor; 86 87 /** 88 * The Javadoc comment to the descriptor. 89 */ 90 private PropertyComment comment; 91 92 /** 93 * The information about the source the property was defined in. 94 */ 95 private SourceInfo sourceInfo; 96 97 // ***************************** Initializer ****************************** 98 99 // ***************************** Constructors ***************************** 100 101 // ***************************** Inner Classes **************************** 102 103 // ******************************** Methods ******************************* 104 105 // --- init --------------------------------------------------------------- 106 107 // --- get&set ------------------------------------------------------------ 108 109 /** 110 * Sets the descriptor to the property. 111 * 112 * @param descriptor the descriptor to the property. 113 * @return a reference to the builder. 114 */ 115 public Builder with(final PropertyDescriptor descriptor) 116 { 117 this.descriptor = descriptor; 118 return this; 119 } 120 121 /** 122 * Sets the property comment. 123 * 124 * @param comment the comment to this property. 125 * @return a reference to the builder. 126 */ 127 public Builder with(final PropertyComment comment) 128 { 129 this.comment = comment; 130 return this; 131 } 132 133 /** 134 * Sets the information about the source the property was defined in. 135 * 136 * @param sourceInfo the information about the source the property was 137 * defined in. 138 * @return a reference to the builder. 139 */ 140 public Builder with(final SourceInfo sourceInfo) 141 { 142 this.sourceInfo = sourceInfo; 143 return this; 144 } 145 146 // --- business ----------------------------------------------------------- 147 148 /** 149 * Creates the report item. 150 * 151 * @return the report item. 152 */ 153 public PropertyReportItem build() 154 { 155 Arg.checkNotNull("descriptor", descriptor); 156 157 if (comment == null) 158 { 159 comment = new PropertyComment.Builder().build(); 160 } 161 if (sourceInfo == null) 162 { 163 sourceInfo = SourceInfo.EMPTY; 164 } 165 return new PropertyReportItem(this); 166 } 167 168 // --- object basics ------------------------------------------------------ 169 } 170 171 // ********************************* Methods ******************************** 172 173 // --- init ----------------------------------------------------------------- 174 175 // --- get&set -------------------------------------------------------------- 176 177 /** 178 * Returns the descriptor to the property. 179 * 180 * @return the descriptor to the property. 181 */ 182 public PropertyDescriptor getDescriptor() 183 { 184 return descriptor; 185 } 186 187 /** 188 * Returns the Javadoc comment to the descriptor. 189 * 190 * @return the Javadoc comment to the descriptor. 191 */ 192 public PropertyComment getComment() 193 { 194 return comment; 195 } 196 197 /** 198 * Returns the property comment. 199 * <p> 200 * If the {@link #getComment() comment} is <code>null</code>, this method 201 * returns safely <code>null</code>. 202 * </p> 203 * 204 * @return the property comment. 205 */ 206 public String getCommentText() 207 { 208 if (comment != null) 209 { 210 return comment.getText(); 211 } 212 return null; 213 } 214 215 /** 216 * Returns the property value comment. 217 * <p> 218 * If the {@link #getComment() comment} is <code>null</code>, this method 219 * returns safely <code>null</code>. 220 * </p> 221 * 222 * @return the property value comment. 223 */ 224 public PropertyValueComment getValueComment() 225 { 226 if (comment != null) 227 { 228 return comment.getValueComment(); 229 } 230 return null; 231 } 232 233 /** 234 * Returns the information about the source the property was defined in. 235 * 236 * @return the information about the source the property was defined in. 237 */ 238 public SourceInfo getSourceInfo() 239 { 240 return sourceInfo; 241 } 242 243 /** 244 * Returns the document instance metadata. 245 * 246 * @return the document instance metadata. 247 */ 248 public DocumentMetaData getMetaData() 249 { 250 return descriptor.getDocumentMetaData(); 251 } 252 253 /** 254 * Returns the optional value in case this is a runtime report. 255 * 256 * @return the optional value in case this is a runtime report. 257 */ 258 public Object getValue() 259 { 260 return value; 261 } 262 263 /** 264 * Sets the optional value in case this is a runtime report. 265 * 266 * @param value the optional value in case this is a runtime report. 267 */ 268 public void setValue(final Object value) 269 { 270 this.value = value; 271 } 272 273 /** 274 * Returns the name that uniquely identifies the property report within the 275 * report set. 276 * 277 * @return the name that uniquely identifies the property report within the 278 * report set. 279 */ 280 public String getLocalName() 281 { 282 final String metadataValue = descriptor.getDocumentMetaData().getName(); 283 if (StringUtils.isBlank(metadataValue)) 284 { 285 return descriptor.getDocumentName().getName(); 286 } 287 288 return metadataValue; 289 } 290 291 /** 292 * Returns the name that uniquely identifies the property report document. 293 * 294 * @return the name that uniquely identifies the property report document. 295 */ 296 public String getName() 297 { 298 // final String set = getPropertySet(); 299 final String name = getLocalName(); 300 // final String id = set + '.' + name; 301 return name; 302 } 303 304 /** 305 * Returns the space the report item belongs to. 306 * 307 * @return the space the report item belongs to. 308 */ 309 public String getSpace() 310 { 311 final String metadataValue = descriptor.getDocumentMetaData().getSpace(); 312 if (StringUtils.isBlank(metadataValue)) 313 { 314 return "property:" + sourceInfo.getElementTypeName(); 315 } 316 317 return metadataValue; 318 } 319 320 /** 321 * Returns the unique title of the report within the space. 322 * 323 * @return the unique title of the report within the space. 324 */ 325 public String getTitle() 326 { 327 final String metadataValue = descriptor.getDocumentMetaData().getTitle(); 328 329 if (StringUtils.isBlank(metadataValue)) 330 { 331 return descriptor.getKey().toString(); 332 } 333 334 return metadataValue; 335 } 336 337 /** 338 * Returns the set of properties this property belongs to. 339 * 340 * @return the set of properties this property belongs to. 341 */ 342 public String getPropertySet() 343 { 344 final String descriptorValue = descriptor.getKey().getPropertySet(); 345 if (StringUtils.isBlank(descriptorValue)) 346 { 347 return sourceInfo.getElementTypeName(); 348 } 349 350 return descriptorValue; 351 } 352 353 // --- business ------------------------------------------------------------- 354 355 // --- object basics -------------------------------------------------------- 356 357 }