diff artifacts/src/main/java/org/dive4elements/river/exports/AbstractExporter.java @ 9150:23945061daec

gigantic refactoring: exporter, result, results to support multiple jaspers -> collisions
author gernotbelger
date Thu, 14 Jun 2018 16:56:31 +0200
parents 93ea282c33e8
children 13a0c4be17ea
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/exports/AbstractExporter.java	Thu Jun 14 15:12:25 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/AbstractExporter.java	Thu Jun 14 16:56:31 2018 +0200
@@ -11,30 +11,22 @@
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
-
 import java.text.NumberFormat;
 
-import org.w3c.dom.Document;
-
 import org.apache.log4j.Logger;
-
-import au.com.bytecode.opencsv.CSVWriter;
-
+import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
+import org.dive4elements.artifactdatabase.state.Settings;
 import org.dive4elements.artifacts.Artifact;
 import org.dive4elements.artifacts.CallContext;
-
-import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
-import org.dive4elements.artifactdatabase.state.Settings;
-
 import org.dive4elements.artifacts.common.ArtifactNamespaceContext;
 import org.dive4elements.artifacts.common.utils.XMLUtils;
-
 import org.dive4elements.river.artifacts.resources.Resources;
 import org.dive4elements.river.collections.D4EArtifactCollection;
-
 import org.dive4elements.river.themes.ThemeDocument;
 import org.dive4elements.river.utils.Formatter;
+import org.w3c.dom.Document;
 
+import au.com.bytecode.opencsv.CSVWriter;
 
 /**
  * Abstract exporter that implements some basic methods for exporting data of
@@ -44,7 +36,7 @@
  */
 public abstract class AbstractExporter implements OutGenerator {
 
-    /** The log used in this exporter.*/
+    /** The log used in this exporter. */
     private static Logger log = Logger.getLogger(AbstractExporter.class);
 
     /* XXX: Why does AbstractExporter do not implement FacetTypes? */
@@ -82,7 +74,7 @@
     /** The selected facet. */
     protected String facet;
 
-    /** The collection.*/
+    /** The collection. */
     protected D4EArtifactCollection collection;
 
     /** The master artifact. */
@@ -106,24 +98,26 @@
      * Concrete subclasses need to use this method to write their special data
      * objects into the CSV document.
      *
-     * @param writer The CSVWriter.
+     * @param writer
+     *            The CSVWriter.
      */
     protected abstract void writeCSVData(CSVWriter writer) throws IOException;
 
-
     /**
      * Write lines of informative content to CSV file.
      * Usually this will be done above the column headers from within
      * the implementation of writeCSVData in concret subclasses.
      *
-     * @param writer The CSVWriter
-     * @param infolines Array of Strings with informative content.
-     * Each will be written to a separate line prefixed with START_META_CHAR.
+     * @param writer
+     *            The CSVWriter
+     * @param infolines
+     *            Array of Strings with informative content.
+     *            Each will be written to a separate line prefixed with START_META_CHAR.
      */
-    protected void  writeCSVInfo(CSVWriter writer, String[] infolines) {
-        String[] metaline = new String[1];
+    protected void writeCSVInfo(final CSVWriter writer, final String[] infolines) {
+        final String[] metaline = new String[1];
 
-        for (String infoline: infolines) {
+        for (final String infoline : infolines) {
             metaline[0] = START_META_CHAR + " " + infoline;
             writer.writeNext(metaline);
         }
@@ -136,74 +130,66 @@
      */
     protected abstract void writePDF(OutputStream out);
 
-
     /**
      * This method enables concrete subclasses to collected its own special
      * data.
      *
-     * @param data The artifact that stores the data that has to be
-     * exported.
+     * @param data
+     *            The artifact that stores the data that has to be
+     *            exported.
      */
     protected abstract void addData(Object data);
 
-    public void setup(Object config) {
+    @Override
+    public void setup(final Object config) {
         log.debug("AbstractExporter.setup");
     }
 
-
     @Override
-    public void init(
-        String       outName,
-        Document     request,
-        OutputStream out,
-        CallContext  context
-    ) {
+    public void init(final String outName, final Document request, final OutputStream out, final CallContext context) {
         log.debug("AbstractExporter.init");
 
         this.outName = outName;
         this.request = request;
-        this.out     = out;
+        this.out = out;
         this.context = context;
     }
 
-
     @Override
-    public void setMasterArtifact(Artifact master) {
+    public void setMasterArtifact(final Artifact master) {
         this.master = master;
     }
 
-    /** Get the callcontext that this exporter has been initialized
-     * with. */
+    /**
+     * Get the callcontext that this exporter has been initialized
+     * with.
+     */
     public CallContext getCallContext() {
         return this.context;
     }
 
-
     @Override
-    public void setCollection(D4EArtifactCollection collection) {
+    public void setCollection(final D4EArtifactCollection collection) {
         this.collection = collection;
     }
 
-
     /**
      * This doOut() just collects the data of multiple artifacts. Therefore, it
      * makes use of the addData() method which enables concrete subclasses to
      * store its data on its own. The real output creation takes place in the
      * concrete generate() methods.
      *
-     * @param artifactFacet The artifact and facet.
-     * The facet to add - NOTE: the facet needs to fit to the first
-     * facet inserted into this exporter. Otherwise this artifact/facet is
-     * skipped.
-     * @param attr The attr document.
+     * @param artifactFacet
+     *            The artifact and facet.
+     *            The facet to add - NOTE: the facet needs to fit to the first
+     *            facet inserted into this exporter. Otherwise this artifact/facet is
+     *            skipped.
+     * @param attr
+     *            The attr document.
      */
     @Override
-    public void doOut(
-        ArtifactAndFacet artifactFacet,
-        ThemeDocument    attr,
-        boolean          visible
-    ) {
-        String name = artifactFacet.getFacetName();
+    public void doOut(final ArtifactAndFacet artifactFacet, final ThemeDocument attr, final boolean visible) {
+        final String name = artifactFacet.getFacetName();
 
         log.debug("AbstractExporter.doOut: " + name);
 
@@ -212,109 +198,98 @@
             return;
         }
 
-        addData(artifactFacet.getData(context));
+        addData(artifactFacet.getData(this.context));
     }
 
-
     /**
      * Generates an export based on a specified facet.
      */
     @Override
-    public void generate()
-    throws IOException
-    {
+    public void generate() throws IOException {
         log.debug("AbstractExporter.generate");
 
-        if (facet == null) {
+        if (this.facet == null) {
             throw new IOException("invalid (null) facet for exporter");
         }
 
-        if (facet.equals(FACET_CSV)) {
+        if (this.facet.equals(FACET_CSV)) {
             generateCSV();
-        }
-        else if (facet.equals(FACET_PDF)) {
+        } else if (this.facet.equals(FACET_PDF)) {
             generatePDF();
-        }
-        else {
-            throw new IOException(
-                "invalid facet for exporter: '" + facet + "'");
+        } else {
+            throw new IOException("invalid facet for exporter: '" + this.facet + "'");
         }
     }
 
-
     /**
      * Determines if the desired facet is valid for this exporter. If no facet
      * is currently set, <i>facet</i> is set.
      *
-     * @param facet The desired facet.
+     * @param facet
+     *            The desired facet.
      *
      * @return true, if <i>facet</i> is valid, otherwise false.
      */
-    protected boolean isFacetValid(String facet) {
-        log.debug("AbstractExporter.isFacetValid : "
-            + facet + " (" + getFacet() + ")" );
+    protected boolean isFacetValid(final String facet) {
+        log.debug("AbstractExporter.isFacetValid : " + facet + " (" + getFacet() + ")");
 
-        String thisFacet = getFacet();
+        final String thisFacet = getFacet();
 
         if (thisFacet == null || thisFacet.length() == 0) {
             return false;
-        }
-        else if (facet == null || facet.length() == 0) {
+        } else if (facet == null || facet.length() == 0) {
             return false;
-        }
-        else {
+        } else {
             return thisFacet.equals(facet);
         }
     }
 
-
     /**
      * Returns the name of the desired facet.
      *
      * @return the name of the desired facet.
      */
     protected String getFacet() {
-        if (facet == null) {
-            facet = getFacetFromRequest();
+        if (this.facet == null) {
+            this.facet = getFacetFromRequest();
         }
 
-        return facet;
+        return this.facet;
     }
 
-
     /**
      * Extracts the name of the requested facet from request document.
      *
      * @return the name of the requested facet.
      */
     protected String getFacetFromRequest() {
-        return XMLUtils.xpathString(
-            request, XPATH_FACET, ArtifactNamespaceContext.INSTANCE);
+        return XMLUtils.xpathString(this.request, XPATH_FACET, ArtifactNamespaceContext.INSTANCE);
     }
 
     protected String msg(final String key) {
-        return Resources.getMsg(context.getMeta(), key, key);
+        return Resources.getMsg(this.context.getMeta(), key, key);
     }
 
-    protected String msg(String key, final String def) {
-        return Resources.getMsg(context.getMeta(), key, def);
+    protected String msg(final String key, final String def) {
+        return Resources.getMsg(this.context.getMeta(), key, def);
     }
 
-    protected String msg(String key, Object[] args) {
-        return Resources.getMsg(context.getMeta(), key, key, args);
+    protected String msg(final String key, final Object[] args) {
+        return Resources.getMsg(this.context.getMeta(), key, key, args);
     }
 
-    protected String msg(String key, String def, Object[] args) {
-        return Resources.getMsg(context.getMeta(), key, def, args);
+    protected String msg(final String key, final String def, final Object[] args) {
+        return Resources.getMsg(this.context.getMeta(), key, def, args);
     }
 
     protected String msgVarg(final String key, final Object... args) {
-        return Resources.getMsg(context.getMeta(), key, key, args);
+        return Resources.getMsg(this.context.getMeta(), key, key, args);
     }
 
     /**
      * Formats header with unit: msg [unit]
      */
+    // TODO: REMOVE, because it has moved to ExportContextCSV
     protected final String msgUnit(final String key, final String unit) {
         final String msg = msg(key);
         return String.format("%s [%s]", msg, unit);
@@ -324,116 +299,104 @@
      * This method starts CSV creation. It makes use of writeCSVData() which has
      * to be implemented by concrete subclasses.
      */
-    protected void generateCSV()
-    throws    IOException
-    {
+    protected void generateCSV() throws IOException {
         log.info("AbstractExporter.generateCSV");
 
-        char quote = '"';
-        char escape = '\\';
+        final char quote = '"';
+        final char escape = '\\';
 
-        CSVWriter writer = new CSVWriter(
-            new OutputStreamWriter(
-                out,
-                DEFAULT_CSV_CHARSET),
-            DEFAULT_CSV_SEPARATOR, quote, escape, "\r\n");
+        final CSVWriter writer = new CSVWriter(new OutputStreamWriter(this.out, DEFAULT_CSV_CHARSET), DEFAULT_CSV_SEPARATOR, quote, escape, "\r\n");
 
         writeCSVData(writer);
 
         writer.close();
     }
 
-
     /**
      * This method starts PDF creation.
      */
-    protected void generatePDF()
-    throws    IOException
-    {
+    protected void generatePDF() throws IOException {
         log.info("AbstractExporter.generatePDF");
         writePDF(this.out);
     }
 
-
     /**
      * Returns an instance of <i>EmptySettings</i> currently!
      *
      * @return an instance of <i>EmptySettings</i>.
      */
+    @Override
     public Settings getSettings() {
         return new EmptySettings();
     }
 
-
     /**
      * This method is not implemented. Override it in subclasses if those need a
      * <i>Settings</i> object.
      */
-    public void setSettings(Settings settings) {
+    @Override
+    public void setSettings(final Settings settings) {
         // do nothing
     }
 
-
     /**
      * Returns the number formatter for kilometer values.
      *
      * @return the number formatter for kilometer values.
      */
     protected NumberFormat getKmFormatter() {
-        if (kmFormat == null) {
-            kmFormat = Formatter.getWaterlevelKM(context);
+        if (this.kmFormat == null) {
+            this.kmFormat = Formatter.getWaterlevelKM(this.context);
         }
-        return kmFormat;
+        return this.kmFormat;
     }
 
-
     /**
      * Returns the number formatter for W values.
      *
      * @return the number formatter for W values.
      */
     protected NumberFormat getWFormatter() {
-        if (wFormat == null) {
-            wFormat = Formatter.getWaterlevelW(context);
+        if (this.wFormat == null) {
+            this.wFormat = Formatter.getWaterlevelW(this.context);
         }
-        return wFormat;
+        return this.wFormat;
     }
 
-
     /**
      * Returns the number formatter for Q values.
      *
      * @return the number formatter for Q values.
      */
     protected NumberFormat getQFormatter() {
-        if (qFormat == null) {
-            qFormat = Formatter.getWaterlevelQ(context);
+        if (this.qFormat == null) {
+            this.qFormat = Formatter.getWaterlevelQ(this.context);
         }
-        return qFormat;
+        return this.qFormat;
     }
-    
+
     protected final NumberFormat getMeanBedHeighFormatter() {
-        if( meanBedHeightFormat == null )
-            meanBedHeightFormat = Formatter.getMeanBedHeight(context); 
-        return meanBedHeightFormat;
+        if (this.meanBedHeightFormat == null)
+            this.meanBedHeightFormat = Formatter.getMeanBedHeight(this.context);
+        return this.meanBedHeightFormat;
     }
 
     protected final NumberFormat getTkhFormatter() {
-        if( tkhFormat == null )
-            tkhFormat = Formatter.getTkh(context);
-        return tkhFormat;
+        if (this.tkhFormat == null)
+            this.tkhFormat = Formatter.getTkh(this.context);
+        return this.tkhFormat;
     }
 
     protected final NumberFormat getFlowDepthFormatter() {
-        if( flowDepthFormat == null )
-            flowDepthFormat = Formatter.getFlowDepth(context);
-        return flowDepthFormat;
+        if (this.flowDepthFormat == null)
+            this.flowDepthFormat = Formatter.getFlowDepth(this.context);
+        return this.flowDepthFormat;
     }
 
     protected final NumberFormat getW2Formatter() {
-        if( w2Format == null )
-            w2Format = Formatter.getFlowDepth(context);
-        return w2Format;
+        if (this.w2Format == null)
+            this.w2Format = Formatter.getFlowDepth(this.context);
+        return this.w2Format;
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org