[PATCH] Part of flys/issue1168: Make facet names unique. It should help but there must be bug before the facet generation

Wald Commits scm-commit at wald.intevation.org
Thu Aug 15 18:56:34 CEST 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1376585780 -7200
# Node ID 437d056ce426e3adb733d706d2ee06ceb33b659a
# Parent  2e002f989c2495d607bbce1753f9b5f9f0c2014d
Part of flys/issue1168: Make facet names unique. It should help but there must be bug before the facet generation.

diff -r 2e002f989c24 -r 437d056ce426 artifacts/src/main/java/org/dive4elements/river/artifacts/states/fixation/FixAnalysisCompute.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/states/fixation/FixAnalysisCompute.java	Thu Aug 15 18:00:16 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/states/fixation/FixAnalysisCompute.java	Thu Aug 15 18:56:20 2013 +0200
@@ -9,8 +9,11 @@
 package org.dive4elements.river.artifacts.states.fixation;
 
 import java.text.DateFormat;
+import java.util.Collection;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.log4j.Logger;
 
@@ -41,6 +44,7 @@
 import org.dive4elements.river.artifacts.model.fixings.FixWQCurveFacet;
 import org.dive4elements.river.artifacts.resources.Resources;
 import org.dive4elements.river.artifacts.states.DefaultState;
+import org.dive4elements.river.utils.Formatter;
 import org.dive4elements.river.utils.IdGenerator;
 
 /**
@@ -171,15 +175,15 @@
         int qsS = access.getQSectorStart();
         int qsE = access.getQSectorEnd();
 
-        // TODO: i18n
-        DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
+        DateFormat df = Formatter.getDateFormatter(context.getMeta(), "dd.MM.yyyy");
+        DateFormat lf = Formatter.getDateFormatter(context.getMeta(), "dd.MM.yyy'T'HH:mm");
 
         DateRange [] periods = access.getAnalysisPeriods();
 
         for (int i = 0; i < periods.length; i++) {
             DateRange period = periods[i];
             String startDate = df.format(period.getFrom());
-            String endDate = df.format(period.getTo());
+            String endDate   = df.format(period.getTo());
 
             for (int j = qsS; j <= qsE; j++) {
 
@@ -233,19 +237,22 @@
                                  I18N_ANALYSIS,
                                  I18N_ANALYSIS);
 
+            Collection<Date> aeds = fr.getAnalysisEventsDates(i);
+            UniqueDateFormatter cf = new UniqueDateFormatter(df, lf, aeds);
+
             int k = 0;
-            for (Date d: fr.getAnalysisEventsDates(i)) {
+            for (Date d: aeds) {
                 int anaNdx = i << 8;
                 anaNdx = anaNdx | k;
                 facets.add(new FixAnalysisEventsFacet(anaNdx,
                     FIX_ANALYSIS_EVENTS_DWT,
-                    eventDesc + (i+1) + " - " + df.format(d)));
+                    eventDesc + (i+1) + " - " + cf.format(d)));
                 facets.add(new FixLongitudinalAnalysisFacet(anaNdx,
                     FIX_ANALYSIS_EVENTS_LS,
-                    eventDesc + (i+1) + " - " + df.format(d)));
+                    eventDesc + (i+1) + " - " + cf.format(d)));
                 facets.add(new FixAnalysisEventsFacet(anaNdx,
                     FIX_ANALYSIS_EVENTS_WQ,
-                    eventDesc + (i+1) +" - " + df.format(d)));
+                    eventDesc + (i+1) +" - " + cf.format(d)));
                 k++;
             }
         }
@@ -259,27 +266,29 @@
                 I18N_REFERENCEDEVIATION,
                 I18N_REFERENCEDEVIATION);
 
+        Collection<Date> reds = fr.getReferenceEventsDates();
+        UniqueDateFormatter cf = new UniqueDateFormatter(df, lf, reds);
+
         int i = 0;
-        for (Date d: fr.getReferenceEventsDates()) {
+        for (Date d: reds) {
             int refNdx = idg.next() << 8;
             refNdx |=  i;
             facets.add(new FixReferenceEventsFacet(refNdx,
                 FIX_REFERENCE_EVENTS_DWT,
-                i18n_ref + " - " + df.format(d)));
+                i18n_ref + " - " + cf.format(d)));
             refNdx = idg.next() << 8;
             refNdx = refNdx | i;
             facets.add(new FixLongitudinalReferenceFacet(refNdx,
                 FIX_REFERENCE_EVENTS_LS,
-                i18n_ref + " - " + df.format(d)));
+                i18n_ref + " - " + cf.format(d)));
             refNdx = idg.next() << 8;
             refNdx |= i;
             facets.add(new FixReferenceEventsFacet(refNdx,
                 FIX_REFERENCE_EVENTS_WQ,
-                i18n_ref + " - " + df.format(d)));
+                i18n_ref + " - " + cf.format(d)));
             i++;
         }
 
-
         facets.add(new FixLongitudinalDeviationFacet(idg.next(),
             FIX_DEVIATION_LS,
             i18n_dev));
@@ -333,5 +342,46 @@
                 I18N_DEVIATION)));
         return res;
     }
+
+    /** Little hack to format dates unique if they collide. */
+    private static class UniqueDateFormatter {
+
+        private DateFormat df;
+        private DateFormat lf;
+        private Map<String, int[]> collisions;
+
+        UniqueDateFormatter(
+            DateFormat df,
+            DateFormat lf,
+            Collection<Date> dates
+        ) {
+            this.df = df;
+            this.lf = lf;
+            collisions = build(dates);
+        }
+
+        private Map<String, int []> build(Collection<Date> dates) {
+            Map<String, int []> collisions = new HashMap<String, int[]>();
+            for (Date d: dates) {
+                String s = df.format(d);
+                int [] count = collisions.get(s);
+                if (count == null) {
+                    collisions.put(s, count = new int[1]);
+                }
+                if (++count[0] > 1) {
+                    log.debug("date collsion found: " + d);
+                }
+            }
+            return collisions;
+        }
+
+        String format(Date date) {
+            String s = df.format(date);
+            int [] count = collisions.get(s);
+            return count == null || count[0] < 2
+                ? s
+                : lf.format(date);
+        }
+    } // class UniqueDateFormatter
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :


More information about the Dive4elements-commits mailing list