| 1 | /** |
| 2 | * BSD-style license; for more info see http://xradar.sourceforge.net/license.html |
| 3 | */ |
| 4 | package org.sourceforge.xradar.dynamics; |
| 5 | |
| 6 | import java.io.InputStream; |
| 7 | import java.io.Writer; |
| 8 | import java.util.logging.Logger; |
| 9 | |
| 10 | import org.sourceforge.xradar.AbstractProcess; |
| 11 | import org.sourceforge.xradar.DefaultReportValues; |
| 12 | import org.sourceforge.xradar.XRadarException; |
| 13 | import org.sourceforge.xradar.statics.Param; |
| 14 | import org.sourceforge.xradar.statics.Preambule; |
| 15 | import org.sourceforge.xradar.util.FileUtils; |
| 16 | import org.sourceforge.xradar.util.StreamUtils; |
| 17 | import org.sourceforge.xradar.util.XRadarUtils; |
| 18 | import org.xml.sax.InputSource; |
| 19 | |
| 20 | /** |
| 21 | * <p>Implements the Dynamic part of the XRadar QA report.</p> |
| 22 | * |
| 23 | * @author Romain Pelisse, belaran@gmail.com |
| 24 | * |
| 25 | */ |
| 26 | public class Dynamics extends AbstractProcess |
| 27 | { |
| 28 | private static final Logger logger = Logger.getLogger(Dynamics.class.getSimpleName()); |
| 29 | |
| 30 | private static final String REPORT_TO_MERGE = "report-to-merge"; |
| 31 | private static final String QUALITY_REPORT_PATH = "quality-report-path"; |
| 32 | private static final String SEQUENCE_ID = "sequence-id"; |
| 33 | private static final String DAYS_SINCE_LAST ="days-since-last"; |
| 34 | |
| 35 | /* |
| 36 | * Stylesheets to use |
| 37 | */ |
| 38 | private static final String DYNAMICS_STYLE_REPORT = DefaultReportValues.getString("radar.internal.dynamics.process.style.report");; |
| 39 | private static final String NORMALIZED_PROCESS = DefaultReportValues.getString("radar.internal.dynamics.process.merge.normalized"); |
| 40 | private static final String POST_PROCESSING = DefaultReportValues.getString("radar.internal.dynamics.post.processing"); |
| 41 | |
| 42 | public static final String SEQUENCE_ID_ONE = "1"; |
| 43 | public static final String DAYS_SINCE_LAST_DEFAULT = "0"; |
| 44 | public static final String DEFAULT_BASE_MASTER_REPORT = "xradar://resources/etc/config/dynamics/dynamics-master-base.xml"; |
| 45 | public static final String EMPTY = "empty.html"; |
| 46 | private static final String STATICS_CONFIGURED = "statics-configured"; |
| 47 | private static final String STATICS_ROOT = "statics-root"; |
| 48 | |
| 49 | |
| 50 | private String qualityReportPath; |
| 51 | private String sequenceId; |
| 52 | private String daysSinceLast; |
| 53 | private String masterFinal; |
| 54 | private String staticRoot = null; |
| 55 | |
| 56 | private Preambule preambule; |
| 57 | private String mergedFile; |
| 58 | |
| 59 | /** |
| 60 | * <p>Default constructor:</p> |
| 61 | * <ol> |
| 62 | * <li>Get logger</li> |
| 63 | * <li>Initialize catalog use to false (default setting)</li> |
| 64 | * <ol> |
| 65 | * |
| 66 | */ |
| 67 | public Dynamics() |
| 68 | { |
| 69 | super.initializedXmlCatalog(false); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | public void init() throws XRadarException |
| 74 | { |
| 75 | if ( ! "".equals(this.getDocsHome() ) ) |
| 76 | { |
| 77 | // Creating the xml directory, along with the docs-home directory |
| 78 | FileUtils.createDir(this.getDocsHome() + SYSTEM_FILE_SEPARATOR + "xml" + SYSTEM_FILE_SEPARATOR); |
| 79 | // Creating the img directory |
| 80 | FileUtils.createDir(this.getDocsHome() + SYSTEM_FILE_SEPARATOR + "img" + SYSTEM_FILE_SEPARATOR); |
| 81 | } |
| 82 | else |
| 83 | throw new XRadarException("DocsHome parameter must be setted before called to init()."); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * |
| 88 | * |
| 89 | * @param previousVersion |
| 90 | * @param currentVersion |
| 91 | * @return complete filename to product of the merge |
| 92 | * @throws XRadarException |
| 93 | */ |
| 94 | public void executeMerge(String previousVersion, String currentVersion) throws XRadarException |
| 95 | { |
| 96 | String product = this.getDocsHome() + SYSTEM_FILE_SEPARATOR + "tmp.xml"; |
| 97 | logger.info("Merging previous version file," + previousVersion + ", with " + currentVersion + " file, using " + product + " as a temporary buffer"); |
| 98 | this.preambule = new Preambule(); |
| 99 | preambule.getParams().put(Dynamics.XRADAR_CONFIG_FILE, new Param(Dynamics.XRADAR_CONFIG_FILE,super.getConfig())); |
| 100 | preambule.getParams().put(Dynamics.REPORT_TO_MERGE, new Param(Dynamics.REPORT_TO_MERGE,currentVersion)); |
| 101 | preambule.getParams().put(Dynamics.QUALITY_REPORT_PATH, new Param(Dynamics.QUALITY_REPORT_PATH,this.qualityReportPath)); |
| 102 | preambule.getParams().put(Dynamics.DAYS_SINCE_LAST, new Param(Dynamics.DAYS_SINCE_LAST,this.daysSinceLast)); |
| 103 | preambule.getParams().put(Dynamics.SEQUENCE_ID, new Param(Dynamics.SEQUENCE_ID,this.sequenceId)); |
| 104 | merger.setXmlResult(StreamUtils.createOuput(StreamUtils.createFileWriter(product))); |
| 105 | merger.setPreambule(preambule); |
| 106 | logger.fine("Executing merge now"); |
| 107 | // FIXME: this is not really elegant |
| 108 | InputStream input = null; |
| 109 | if ( previousVersion.contains(XRADAR_PROTOCOL) ) |
| 110 | input = StreamUtils.retrieveFileContentAsStream(previousVersion); |
| 111 | else |
| 112 | input = StreamUtils.readFile(previousVersion); |
| 113 | // Merging.. |
| 114 | merger.merge(new InputSource(input),StreamUtils.retrieveFileContentAsStream(NORMALIZED_PROCESS)); |
| 115 | // Processing file |
| 116 | logger.fine("Merging finished, saving result in file " + product); |
| 117 | // If this is 'base' file from xradar we have to change the value of previousVersion to "real" file |
| 118 | if ( DEFAULT_BASE_MASTER_REPORT.equals(previousVersion) ) |
| 119 | previousVersion = this.getMasterFinal(); |
| 120 | this.mergedFile = previousVersion; |
| 121 | // Now we update the previous version file with the product of the merge |
| 122 | StreamUtils.copyFileToFile(product,previousVersion); |
| 123 | // products may be now deleted |
| 124 | FileUtils.deleteFile(product); |
| 125 | // Processing file |
| 126 | logger.fine("Deleting temporaty file " + product); |
| 127 | |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * <p>Post process de dynamics merge report.</p> |
| 132 | * |
| 133 | * @param output |
| 134 | * @return complete filename to product of the post processing. |
| 135 | * @throws XRadarException |
| 136 | */ |
| 137 | public void postProcess(String output) throws XRadarException |
| 138 | { |
| 139 | logger.info("Post processsing file " + output); |
| 140 | // Verifying methods arguments |
| 141 | if ( "".equals(output) && ! "".equals(this.getMasterFinal()) ) |
| 142 | output = this.getMasterFinal(); |
| 143 | // Processing file |
| 144 | String tmpFile = FileUtils.addSeparatorIfMissing(this.getDocsHome()) + "tmp.xml"; |
| 145 | merger.setXmlResult(StreamUtils.createOuput(StreamUtils.createFileWriter(tmpFile))); |
| 146 | logger.fine("Post processing input file " + this.mergedFile + "."); |
| 147 | merger.merge(new InputSource(StreamUtils.readFile(this.mergedFile)),StreamUtils.retrieveFileContentAsStream(POST_PROCESSING)); |
| 148 | logger.fine("Result saved in " + output); |
| 149 | StreamUtils.copyFileToFile(tmpFile,output); |
| 150 | } |
| 151 | |
| 152 | public void copyImagesToWebSite() throws XRadarException |
| 153 | { |
| 154 | this.copyImagesToWebsite(this.getDocsHome() + SYSTEM_FILE_SEPARATOR); |
| 155 | } |
| 156 | |
| 157 | public void dynamicsStyleReport(String input, String output) throws XRadarException |
| 158 | { |
| 159 | logger.info("Generating website from file " + output); |
| 160 | logger.fine("dynamicsStyleReport called."); |
| 161 | // Creating the output writer |
| 162 | Writer writer = StreamUtils.createFileWriter(output); |
| 163 | merger.setXmlResult(StreamUtils.createOuput(writer)); |
| 164 | // Defining the transformation preambule |
| 165 | // Note that we reuse preambule from previous operation |
| 166 | logger.fine("isStaticRoot enabled:" + this.getStaticRoot()); |
| 167 | if ( this.getStaticRoot() != null && ! "".equals(this.getStaticRoot()) ) |
| 168 | { |
| 169 | this.preambule.getParams().put(STATICS_CONFIGURED, new Param(STATICS_CONFIGURED,"true")); |
| 170 | this.preambule.getParams().put(STATICS_ROOT, new Param(STATICS_ROOT,this.getStaticRoot())); |
| 171 | } |
| 172 | else |
| 173 | this.preambule.getParams().put(STATICS_CONFIGURED, new Param(STATICS_CONFIGURED,"false")); |
| 174 | |
| 175 | if (XRadarUtils.isRunningOnWindowsOS()) { |
| 176 | preambule.getParams().put(Dynamics.DOCS_HOME, new Param(Dynamics.DOCS_HOME,SYSTEM_FILE_SEPARATOR + super.getDocsHome())); |
| 177 | } else { |
| 178 | preambule.getParams().put(Dynamics.DOCS_HOME, new Param(Dynamics.DOCS_HOME,super.getDocsHome())); |
| 179 | } |
| 180 | merger.setPreambule(this.preambule); |
| 181 | // execute the operation |
| 182 | logger.fine("Executing the transformation."); |
| 183 | merger.merge(new InputSource(StreamUtils.readFile(input)),StreamUtils.retrieveFileContentAsStream(DYNAMICS_STYLE_REPORT)); |
| 184 | // Cleaning dynamics 'state' |
| 185 | this.reset(); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * This object keep some kind of 'state', after the call of the following methods: |
| 190 | * <ol> |
| 191 | * <li>executeMerge</li> |
| 192 | * <li>dynamicsStyleReport</li> |
| 193 | * </ol> |
| 194 | * To clean the instance ( ensure that not state is conserved, you may call this method. |
| 195 | * However, note that this method is already called at the end of the dynacsStyleReport method. |
| 196 | */ |
| 197 | public void reset() |
| 198 | { |
| 199 | // We remove all the parameter |
| 200 | if ( this.preambule != null && this.preambule.getParams() != null ) |
| 201 | this.preambule.getParams().clear(); |
| 202 | // We also set to null to come back to 'init' state |
| 203 | this.preambule = null; |
| 204 | this.staticRoot = null; |
| 205 | this.masterFinal = null; |
| 206 | this.qualityReportPath = null; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * @return the masterFinal |
| 211 | */ |
| 212 | public String getMasterFinal() { |
| 213 | return masterFinal; |
| 214 | } |
| 215 | /** |
| 216 | * @param masterFinal the masterFinal to set |
| 217 | */ |
| 218 | public void setMasterFinal(String masterFinal) { |
| 219 | this.masterFinal = masterFinal; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * @return the sequenceId |
| 224 | */ |
| 225 | public String getSequenceId() { |
| 226 | return sequenceId; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * @param sequenceId the sequenceId to set |
| 231 | */ |
| 232 | public void setSequenceId(String sequenceId) { |
| 233 | this.sequenceId = sequenceId; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * @return the daysSinceLast |
| 238 | */ |
| 239 | public String getDaysSinceLast() { |
| 240 | return daysSinceLast; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * @return the qualityReportPath |
| 245 | */ |
| 246 | public String getQualityReportPath() { |
| 247 | return qualityReportPath; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * @param qualityReportPath the qualityReportPath to set |
| 252 | */ |
| 253 | public void setQualityReportPath(String qualityReportPath) { |
| 254 | this.qualityReportPath = qualityReportPath; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * @param daysSinceLast the daysSinceLast to set |
| 259 | */ |
| 260 | public void setDaysSinceLast(String daysSinceLast) { |
| 261 | this.daysSinceLast = daysSinceLast; |
| 262 | } |
| 263 | |
| 264 | |
| 265 | /** |
| 266 | * @return the staticRoot |
| 267 | */ |
| 268 | public String getStaticRoot() { |
| 269 | return staticRoot; |
| 270 | } |
| 271 | /** |
| 272 | * @param staticRoot the staticRoot to set |
| 273 | */ |
| 274 | public void setStaticRoot(String staticRoot) { |
| 275 | this.staticRoot = staticRoot; |
| 276 | } |
| 277 | } |