Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/common/AbstractResultType.java @ 9539:4deb6b24f072
PDF-Export zu 2.8 Nachtrag
author | gernotbelger |
---|---|
date | Wed, 17 Oct 2018 16:00:38 +0200 |
parents | d7d22ea8573d |
children |
rev | line source |
---|---|
8999 | 1 /** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde |
2 * Software engineering by | |
3 * Björnsen Beratende Ingenieure GmbH | |
4 * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt | |
5 * | |
6 * This file is Free Software under the GNU AGPL (>=v3) | |
7 * and comes with ABSOLUTELY NO WARRANTY! Check out the | |
8 * documentation coming with Dive4Elements River for details. | |
9 */ | |
10 package org.dive4elements.river.artifacts.common; | |
11 | |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
12 import java.text.DateFormat; |
8999 | 13 import java.text.NumberFormat; |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
14 import java.util.Date; |
8999 | 15 import java.util.HashMap; |
16 import java.util.Locale; | |
17 import java.util.Map; | |
18 | |
19 import org.apache.commons.lang.StringUtils; | |
20 import org.dive4elements.artifacts.CallContext; | |
21 import org.dive4elements.artifacts.CallMeta; | |
22 import org.dive4elements.river.artifacts.resources.Resources; | |
23 | |
24 /** | |
25 * @author Domenico Nardi Tironi | |
26 */ | |
27 public abstract class AbstractResultType implements IResultType { | |
28 | |
29 /* Cache for formatters because Formatter will always create new formats (which is very expensive) */ | |
30 private final Map<Locale, NumberFormat> formatters = new HashMap<>(); | |
31 | |
32 private final String unit; | |
33 | |
34 private final String csvHeader; | |
35 | |
36 private final String pdfHeader; | |
37 | |
38 protected AbstractResultType(final String unit, final String csvHeader, final String pdfHeader) { | |
39 this.unit = unit; | |
40 this.csvHeader = csvHeader; | |
41 this.pdfHeader = pdfHeader; | |
42 | |
43 } | |
44 | |
45 @Override | |
46 public abstract String exportValue(final CallContext context, final Object value); | |
47 | |
48 protected final String exportStringValue(final Object value) { | |
49 | |
50 if (value == null) | |
51 return StringUtils.EMPTY; | |
52 | |
53 if (!(value instanceof String)) | |
54 throw new IllegalStateException(); | |
55 | |
56 return (String) value; | |
57 } | |
58 | |
9448 | 59 protected final String exportBooleanValue(final CallContext context, final Object value) { |
60 | |
61 if (value == null) | |
62 return StringUtils.EMPTY; // DISKUSSION; könnte auch "Nein" sein | |
63 | |
64 if (!(value instanceof Boolean)) | |
65 throw new IllegalStateException(); | |
66 | |
67 if ((boolean) value) { | |
9539 | 68 return Resources.getMsg(context.getMeta(), String.valueOf(value)); |
9448 | 69 } else { |
9539 | 70 return Resources.getMsg(context.getMeta(), String.valueOf(value)); |
9448 | 71 } |
72 } | |
73 | |
8999 | 74 @Override |
75 public final double asDouble(final Object value) { | |
76 if (value == null) | |
77 return Double.NaN; | |
78 | |
79 if (!(value instanceof Number)) | |
80 throw new IllegalStateException(); | |
81 | |
82 final Number number = (Number) value; | |
83 return number.doubleValue(); | |
84 } | |
85 | |
86 protected final String exportDoubleValue(final CallContext context, final double value) { | |
87 if (Double.isNaN(value)) | |
88 return StringUtils.EMPTY; | |
89 | |
90 final NumberFormat formatter = getFormatter(context); | |
91 return formatter.format(value); | |
92 } | |
93 | |
9491 | 94 protected NumberFormat getFormatter(final CallContext context) { |
8999 | 95 final CallMeta meta = context.getMeta(); |
96 final Locale locale = Resources.getLocale(meta); | |
97 | |
98 if (!this.formatters.containsKey(locale)) | |
99 this.formatters.put(locale, createFormatter(context)); | |
100 | |
101 return this.formatters.get(locale); | |
102 } | |
103 | |
9176
1614cb14308f
Work on calculations for S-Info flood duration workflow
mschaefer
parents:
9168
diff
changeset
|
104 protected abstract NumberFormat createFormatter(CallContext context); |
1614cb14308f
Work on calculations for S-Info flood duration workflow
mschaefer
parents:
9168
diff
changeset
|
105 |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
106 protected final String exportDateValue(final CallContext context, final Date value) { |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
107 final Locale locale = Resources.getLocale(context.getMeta()); |
9168
d2472018740c
Fixed dateformat with 4-digit year, added query method for collision years of a river
mschaefer
parents:
9157
diff
changeset
|
108 final DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
109 return df.format(value); |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
110 } |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
111 |
9150
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
8999
diff
changeset
|
112 @Override |
8999 | 113 public final String getCsvHeader() { |
114 return this.csvHeader; | |
115 } | |
116 | |
117 public final String getCsvHeader(final CallMeta meta) { | |
118 return Resources.getMsg(meta, this.csvHeader, this.csvHeader); | |
119 } | |
120 | |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
121 @Override |
8999 | 122 public final String getPdfHeader(final CallMeta meta) { |
123 return Resources.getMsg(meta, this.pdfHeader, this.pdfHeader); | |
124 } | |
125 | |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
126 @Override |
8999 | 127 public final String getUnit() { |
128 return this.unit; | |
129 } | |
130 } |