/opup/src/old/java/com/atlassian/labs/opup/Report.java
Java | 167 lines | 133 code | 34 blank | 0 comment | 2 complexity | ba7506eb6d30e63fbc1f7e4c8e67f248 MD5 | raw file
Possible License(s): BSD-3-Clause
- package com.atlassian.labs.opup;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.Writer;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Properties;
- import org.apache.commons.io.FileUtils;
- import org.apache.maven.artifact.versioning.ArtifactVersion;
- import org.apache.velocity.Template;
- import org.apache.velocity.VelocityContext;
- import org.apache.velocity.app.VelocityEngine;
- public class Report implements ReportTarget
- {
- private final String groupId, artifactId;
- private final Collection<MultipleThingVersionWarning> thingVersionWarnings = new ArrayList<MultipleThingVersionWarning>();
- private final Collection<String> miscWarnings = new ArrayList<String>();
- private Map<String, ArtifactVersion> currentVersions;
- private UpgradeSchedule schedule;
- private String strategy;
- public Report(String groupId, String artifactId)
- {
- this.groupId = groupId;
- this.artifactId = artifactId;
- }
- public void setUpgradeStrategy(String name)
- {
- this.strategy = name;
- }
- public void writeReport(Writer w) throws IOException
- {
- VelocityEngine ve = new VelocityEngine();
- Properties props = new Properties();
- props.setProperty("resource.loader", "class");
- props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
- props.setProperty("eventhandler.referenceinsertion.class", "org.apache.velocity.app.event.implement.EscapeHtmlReference");
- ve.init(props);
- VelocityContext ctxt = new VelocityContext();
- ctxt.put("groupId", groupId);
- ctxt.put("artifactId", artifactId);
- ctxt.put("upgradeStrategy", strategy);
- ctxt.put("warnings", thingVersionWarnings);
- ctxt.put("miscWarnings", miscWarnings);
- ctxt.put("currentVersions", currentVersions);
- ctxt.put("upgradeSchedule", schedule);
- Template template = ve.getTemplate("/com/atlassian/labs/opup/report.vm");
- template.merge(ctxt, w);
- w.flush();
- }
- public void copyStylesheet(File base) throws IOException
- {
- InputStream in = getClass().getResourceAsStream("style.css");
- if (in == null)
- {
- throw new FileNotFoundException("Resource style.css not found");
- }
- FileUtils.copyInputStreamToFile(in, new File(base, "style.css"));
- }
- @Override
- public void multipleVersions(String artifact, Collection<ArtifactVersion> versions)
- {
- addWarning(new MultipleThingVersionWarning("artifact", artifact, versions));
- }
- @Override
- public void multipleValues(String propname, Collection<ArtifactVersion> values)
- {
- addWarning(new MultipleThingVersionWarning("property", propname, values));
- }
- @Override
- public void obsolete(String artifact, String advice)
- {
- miscWarnings.add(artifact + " is obsolete. " + advice);
- }
- private void addWarning(MultipleThingVersionWarning w)
- {
- thingVersionWarnings.add(w);
- System.out.println(w);
- }
- @Override
- public void packagingChange(String coords, ArtifactVersion latestVersionWithDifferentPackaging,
- String differentPackaging, String requiredPackaging, ArtifactVersion artifactVersion)
- {
- String diag = "Latest " + coords + " ("
- + latestVersionWithDifferentPackaging + ") is a " + differentPackaging + ", not a " + requiredPackaging
- + ". Sticking with " + artifactVersion + ".";
- System.out.println(diag);
- miscWarnings.add(diag);
- }
- public static class MultipleThingVersionWarning
- {
- private final String thingType;
- private final String thingName;
- private final Collection<ArtifactVersion> versions;
- public MultipleThingVersionWarning(String thingType, String thingName, Collection<ArtifactVersion> versions)
- {
- this.thingType = thingType;
- this.thingName = thingName;
- this.versions = versions;
- }
- public String getType()
- {
- return thingType;
- }
- public String getName()
- {
- return thingName;
- }
- public Collection<ArtifactVersion> getVersions()
- {
- List<ArtifactVersion> inOrder = new ArrayList<ArtifactVersion>(versions);
- Collections.sort(inOrder);
- return inOrder;
- }
- public String toString()
- {
- return "Multiple versions for " + thingType + " '" + thingName + "': " + versions;
- }
- }
- public void schedule(Map<String, ArtifactVersion> currentVersions, Map<String, ArtifactVersion> currentProperties, UpgradeSchedule upgradeSchedule)
- {
- Map<String, ArtifactVersion> current = new HashMap<String, ArtifactVersion>();
- current.putAll(currentVersions);
- current.putAll(currentProperties);
- this.currentVersions = current ;
- this.schedule = upgradeSchedule;
- }
- }