4646import java .util .HashMap ;
4747import java .util .List ;
4848import java .util .Map ;
49+ import java .util .Set ;
50+ import java .util .stream .Collectors ;
4951
5052import javax .imageio .ImageIO ;
5153import javax .servlet .ServletConfig ;
@@ -99,6 +101,7 @@ public class PreRenderingJob extends ScheduledJob implements InterruptableJob, S
99101 private static final String JOB_DATA_CONFIG_FILE = "configFile" ;
100102 private static final String JOB_DATA_WEBAPP_FOLDER = "webappFolder" ;
101103 private static final String IMAGE_EXTENSION = "png" ;
104+ private static final String DEFAULT_OUTPUT_PATH = "generated/prerendered" ;
102105
103106 @ Autowired
104107 @ Qualifier ("datasetService" )
@@ -154,22 +157,24 @@ public void execute(JobExecutionContext context) throws JobExecutionException {
154157
155158 List <RenderingConfig > phenomenonStyles = taskConfigPrerendering .getPhenomenonStyles ();
156159 List <RenderingConfig > styles = taskConfigPrerendering .getDatasetStyles ();
160+ Set <String > styleIds = styles .stream ().map (s -> s .getId ()).collect (Collectors .toSet ());
157161 for (RenderingConfig config : phenomenonStyles ) {
158162 Map <String , String > parameters = new HashMap <>();
159- parameters .put ("phenomenon" , config .getId ());
163+ parameters .put (Parameters . PHENOMENA , config .getId ());
160164 IoParameters query = IoParameters .createFromSingleValueMap (parameters );
161165 for (DatasetOutput < ? > metadata : datasetService .getCondensedParameters (query )) {
162166 String timeseriesId = metadata .getId ();
163- renderConfiguredIntervals (timeseriesId , config );
164- if (interrupted ) {
165- return ;
167+ if (!styleIds .contains (timeseriesId )) {
168+ renderConfiguredIntervals (timeseriesId , config );
169+ if (interrupted ) {
170+ return ;
171+ }
166172 }
167173 }
168174 }
169175
170176 for (RenderingConfig config : styles ) {
171177 renderConfiguredIntervals (config .getId (), config );
172-
173178 if (interrupted ) {
174179 return ;
175180 }
@@ -272,6 +277,7 @@ public void writePrerenderedGraphToOutputStream(String datasetId, String qualifi
272277 if (taskConfigPrerendering == null ) {
273278 taskConfigPrerendering = readJobConfig (configFile );
274279 }
280+ checkQualifier (qualifier );
275281 try {
276282 BufferedImage image = loadImage (datasetId , qualifier );
277283 if (image == null ) {
@@ -290,16 +296,25 @@ private BufferedImage loadImage(String datasetId, String qualifier) throws IOExc
290296 return ImageIO .read (new FileInputStream (createFileName (datasetId , qualifier )));
291297 }
292298
299+ private void checkQualifier (String qualifier ) {
300+ if (qualifier != null && !qualifier .isEmpty ()) {
301+ QualifierPeriod .validate (qualifier );
302+ }
303+ }
304+
293305 private IntervalWithTimeZone createTimespanFromInterval (String datasetId , String period ) {
294- DateTime now = new DateTime ();
295- if (period .equals ("lastDay" )) {
296- Interval interval = new Interval (now .minusDays (1 ), now );
306+ return createTimespanFromInterval (datasetId , period , new DateTime ());
307+ }
308+
309+ private IntervalWithTimeZone createTimespanFromInterval (String datasetId , String period , DateTime end ) {
310+ if (period .equals (QualifierPeriod .LAST_DAY .getValue ())) {
311+ Interval interval = new Interval (end .minusDays (1 ), end );
297312 return new IntervalWithTimeZone (interval .toString ());
298- } else if (period .equals ("lastWeek" )) {
299- Interval interval = new Interval (now .minusWeeks (1 ), now );
313+ } else if (period .equals (QualifierPeriod . LAST_WEEK . getValue () )) {
314+ Interval interval = new Interval (end .minusWeeks (1 ), end );
300315 return new IntervalWithTimeZone (interval .toString ());
301- } else if (period .equals ("lastMonth" )) {
302- Interval interval = new Interval (now .minusMonths (1 ), now );
316+ } else if (period .equals (QualifierPeriod . LAST_MONTH . getValue () )) {
317+ Interval interval = new Interval (end .minusMonths (1 ), end );
303318 return new IntervalWithTimeZone (interval .toString ());
304319 } else {
305320 throw new ResourceNotFoundException ("Unknown interval '" + period + "' for datatset " + datasetId );
@@ -334,9 +349,10 @@ private File createFileName(String datasetId, String qualifier) {
334349
335350 private Path getOutputFolder () {
336351 final Map <String , String > generalConfig = taskConfigPrerendering .getGeneralConfig ();
337- String outputPath = generalConfig .get ("outputPath" );
338- Path outputDirectory = Paths .get (webappFolder )
339- .resolve (outputPath );
352+ String outputPath = generalConfig .containsKey (PrerenderingJobConfig .CONFIG_OUTPUT_PATH )
353+ ? generalConfig .get (PrerenderingJobConfig .CONFIG_OUTPUT_PATH )
354+ : DEFAULT_OUTPUT_PATH ;
355+ Path outputDirectory = Paths .get (webappFolder ).resolve (outputPath );
340356 File dir = outputDirectory .toFile ();
341357 if (!dir .exists () && !dir .mkdirs ()) {
342358 LOGGER .warn ("Unable to create output folder '{}'." , outputDirectory );
@@ -348,11 +364,11 @@ private IoParameters createConfig(String datasetId, String interval, RenderingCo
348364 Map <String , String > configuration = new HashMap <>();
349365
350366 // set defaults
351- configuration .put ("width" , Integer .toString (WIDTH_DEFAULT ));
352- configuration .put ("height" , Integer .toString (HEIGHT_DEFAULT ));
353- configuration .put ("grid" , Boolean .toString (GRID_DEFAULT ));
367+ configuration .put (PrerenderingJobConfig . CONFIG_WIDTH , Integer .toString (WIDTH_DEFAULT ));
368+ configuration .put (PrerenderingJobConfig . CONFIG_HEIGHT , Integer .toString (HEIGHT_DEFAULT ));
369+ configuration .put (PrerenderingJobConfig . CONFIG_GRID , Boolean .toString (GRID_DEFAULT ));
354370 configuration .put ("legend" , Boolean .toString (LEGEND_DEFAULT ));
355- configuration .put ("generalize" , Boolean .toString (GENERALIZE_DEFAULT ));
371+ configuration .put (PrerenderingJobConfig . CONFIG_GENERALIZE , Boolean .toString (GENERALIZE_DEFAULT ));
356372 configuration .put ("locale" , LANGUAGE_DEFAULT );
357373 configuration .put ("timespan" , interval );
358374
@@ -376,4 +392,24 @@ private IoParameters createConfig(String datasetId, String interval, RenderingCo
376392
377393 return IoParameters .createFromSingleValueMap (configuration );
378394 }
395+
396+ private enum QualifierPeriod {
397+ LAST_DAY ("lastDay" ), LAST_WEEK ("lastWeek" ), LAST_MONTH ("lastMonth" );
398+
399+ private String value ;
400+
401+ QualifierPeriod (String value ) {
402+ this .value = value ;
403+ }
404+
405+ public String getValue () {
406+ return value ;
407+ }
408+
409+ public static void validate (String qualifier ) {
410+ if (!Arrays .stream (QualifierPeriod .values ()).anyMatch (q -> q .getValue ().equals (qualifier ))) {
411+ throw new IllegalArgumentException ("Invalid qualifier value!" );
412+ }
413+ }
414+ }
379415}
0 commit comments