diff artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/BedQualityState.java @ 8587:07c9ac22f611

(issue1755) Generalise BedQuality result handling The bedquality calculation now produces a result for each time period which has BedQualityResultValues for each specific result type. Formally this was split up in density, porosity and diameter classes with some bedload diameter classes mixed in for extra fun. The intent of this commit is to allow more shared code and generic access patterns to the BedQuality results.
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 18 Mar 2015 18:42:08 +0100
parents e9343e4c9ae0
children 36faef4f8acb
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/BedQualityState.java	Tue Mar 17 18:52:00 2015 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/BedQualityState.java	Wed Mar 18 18:42:08 2015 +0100
@@ -25,24 +25,17 @@
 import org.dive4elements.river.artifacts.model.DataFacet;
 import org.dive4elements.river.artifacts.model.DateRange;
 import org.dive4elements.river.artifacts.model.FacetTypes;
-import org.dive4elements.river.artifacts.model.minfo.BedDensityTopFacet;
-import org.dive4elements.river.artifacts.model.minfo.BedDensitySubFacet;
-import org.dive4elements.river.artifacts.model.minfo.BedDiameterTopFacet;
+import org.dive4elements.river.artifacts.model.minfo.BedQualityDataFacet;
 import org.dive4elements.river.artifacts.model.minfo.BedDiameterDataFacet;
-import org.dive4elements.river.artifacts.model.minfo.BedDiameterResult;
-import org.dive4elements.river.artifacts.model.minfo.BedDiameterSubFacet;
-import org.dive4elements.river.artifacts.model.minfo.BedParametersResult;
-import org.dive4elements.river.artifacts.model.minfo.BedPorositySubFacet;
-import org.dive4elements.river.artifacts.model.minfo.BedPorosityTopFacet;
+import org.dive4elements.river.artifacts.model.minfo.BedloadDiameterDataFacet;
 import org.dive4elements.river.artifacts.model.minfo.BedQualityCalculation;
-import org.dive4elements.river.artifacts.model.minfo.BedQualityDiameterResult;
 import org.dive4elements.river.artifacts.model.minfo.BedQualityResult;
-import org.dive4elements.river.artifacts.model.minfo.BedloadDiameterDataFacet;
-import org.dive4elements.river.artifacts.model.minfo.BedloadDiameterFacet;
-import org.dive4elements.river.artifacts.model.minfo.BedloadDiameterResult;
+import org.dive4elements.river.artifacts.model.minfo.BedQualityResultValue;
 import org.dive4elements.river.artifacts.resources.Resources;
 import org.dive4elements.river.artifacts.states.DefaultState;
 
+/* TODO: Change data facets to live in the generalized data scheme and
+ * obsolute the obfuscated index magic. */
 
 public class BedQualityState extends DefaultState implements FacetTypes {
 
@@ -51,23 +44,19 @@
     private static final Logger log = Logger
         .getLogger(BedQualityState.class);
 
+    /* The suffix to append to interpol facets. */
+    public static final String I18N_INTERPOL_SUFFIX = "facet.bedquality.interpol.suffix";
+
+    /* I18n is in the pattern base.<name>.<type> with the optional suffix .data */
+    public static final String I18N_FACET_BED_BASE = "facet.bedquality.bed";
+
+    /* Data Layers */
+    public static final String I18N_FACET_BEDLOAD_DIAMETER_DATA = "facet.bedquality.bed.diameter.bedload.data";
+    public static final String I18N_FACET_BED_DIAMETER_DATA_TOPLAYER = "facet.bedquality.bed.diameter.toplayer.data";
+    public static final String I18N_FACET_BED_DIAMETER_DATA_SUBLAYER = "facet.bedquality.bed.diameter.sublayer.data";
     public static final String I18N_TOPLAYER = "bedquality.toplayer";
     public static final String I18N_SUBLAYER = "bedquality.sublayer";
 
-    /* Calculated layers */
-    public static final String I18N_FACET_BED_POROSITY_TOPLAYER = "facet.bedquality.bed.porosity.toplayer";
-    public static final String I18N_FACET_BED_POROSITY_SUBLAYER = "facet.bedquality.bed.porosity.sublayer";
-    public static final String I18N_FACET_BED_DENSITY_TOPLAYER = "facet.bedquality.bed.density.toplayer";
-    public static final String I18N_FACET_BED_DENSITY_SUBLAYER = "facet.bedquality.bed.density.sublayer";
-    public static final String I18N_FACET_BED_DIAMETER_TOPLAYER = "facet.bedquality.bed.diameter.toplayer";
-    public static final String I18N_FACET_BED_DIAMETER_SUBLAYER = "facet.bedquality.bed.diameter.sublayer";
-    public static final String I18N_FACET_BEDLOAD_DIAMETER = "facet.bedquality.bedload.diameter";
-
-    /* Data Layers */
-    public static final String I18N_FACET_BEDLOAD_DIAMETER_DATA = "facet.bedquality.bedload.diameter.data";
-    public static final String I18N_FACET_BED_DIAMETER_DATA_TOPLAYER = "facet.bedquality.bed.diameter.data.toplayer";
-    public static final String I18N_FACET_BED_DIAMETER_DATA_SUBLAYER = "facet.bedquality.bed.diameter.data.sublayer";
-
     static {
         // Active/deactivate facets.
         FacetActivity.Registry.getInstance().register(
@@ -123,13 +112,67 @@
         }
 
         generateFacets(context, newFacets, results, getID(), hash);
+        generateDataFacets(context, newFacets, access, getID(), hash);
         log.debug("Created " + newFacets.size() + " new Facets.");
-        generateDataFacets(context, newFacets, access, getID(), hash);
         facets.addAll(newFacets);
 
         return res;
     }
 
+    private int generateIndex(String diameter) {
+        int d = 0;
+        if(diameter.equals("d10")) {
+            d = 1;
+        }
+        else if (diameter.equals("d16")) {
+            d = 2;
+        }
+        else if (diameter.equals("d20")) {
+            d = 3;
+        }
+        else if (diameter.equals("d25")) {
+            d = 4;
+        }
+        else if (diameter.equals("d30")) {
+            d = 5;
+        }
+        else if (diameter.equals("d40")) {
+            d = 6;
+        }
+        else if (diameter.equals("d50")) {
+            d = 7;
+        }
+        else if (diameter.equals("d60")) {
+            d = 8;
+        }
+        else if (diameter.equals("d70")) {
+            d = 9;
+        }
+        else if (diameter.equals("d75")) {
+            d = 10;
+        }
+        else if (diameter.equals("d80")) {
+            d = 11;
+        }
+        else if (diameter.equals("d84")) {
+            d = 12;
+        }
+        else if (diameter.equals("d90")) {
+            d = 13;
+        }
+        else if (diameter.equals("dmin")) {
+            d = 14;
+        }
+        else if (diameter.equals("dmax")) {
+            d = 15;
+        }
+        else if (diameter.equals("dm")) {
+            d = 16;
+        }
+        int ndx = d << 1;
+        return ndx;
+    }
+
     private void generateDataFacets(
         CallContext context,
         List<Facet> newFacets,
@@ -143,7 +186,7 @@
             DateRange range = ranges.get(i);
             for (String diameter: diameters) {
                 int ndxTop = generateIndex(diameter);
-                int ndxSub = ndxTop; // TODO: Is this correct?
+                int ndxSub = ndxTop;
                 ndxTop += 1;
                 ndxTop = ndxTop << 3;
                 ndxSub = ndxSub << 3;
@@ -202,58 +245,12 @@
         }
     }
 
-    private int generateIndex(String diameter) {
-        int d = 0;
-        if(diameter.equals("d10")) {
-            d = 1;
-        }
-        else if (diameter.equals("d16")) {
-            d = 2;
-        }
-        else if (diameter.equals("d20")) {
-            d = 3;
-        }
-        else if (diameter.equals("d25")) {
-            d = 4;
-        }
-        else if (diameter.equals("d30")) {
-            d = 5;
-        }
-        else if (diameter.equals("d40")) {
-            d = 6;
-        }
-        else if (diameter.equals("d50")) {
-            d = 7;
-        }
-        else if (diameter.equals("d60")) {
-            d = 8;
-        }
-        else if (diameter.equals("d70")) {
-            d = 9;
-        }
-        else if (diameter.equals("d75")) {
-            d = 10;
-        }
-        else if (diameter.equals("d80")) {
-            d = 11;
-        }
-        else if (diameter.equals("d84")) {
-            d = 12;
-        }
-        else if (diameter.equals("d90")) {
-            d = 13;
-        }
-        else if (diameter.equals("dmin")) {
-            d = 14;
-        }
-        else if (diameter.equals("dmax")) {
-            d = 15;
-        }
-        else if (diameter.equals("dm")) {
-            d = 16;
-        }
-        int ndx = d << 1;
-        return ndx;
+
+    protected String getFacetName(BedQualityResultValue value) {
+        /* basename + name or "diameter" + .type */
+        return BED_QUALITY_DATA_FACET + "." +
+            (value.isDiameterResult() ? "diameter" : value.getName()) + "." +
+            value.getType();
     }
 
     protected void generateFacets(CallContext context, List<Facet> newFacets,
@@ -266,111 +263,37 @@
         for (int idx = 0; idx < results.length; idx++) {
             BedQualityResult result = results[idx];
             DateRange range = result.getDateRange();
-            BedDiameterResult[] bedDiameter = result.getBedResults();
-            for (int j = 0; j < bedDiameter.length; j++) {
-                newFacets.add(new BedDiameterTopFacet((idx << 8) + j,
-                    BED_QUALITY_BED_DIAMETER_TOPLAYER,
-                    getLayerDescription(
-                        meta,
-                        bedDiameter[j],
-                        range,
-                        I18N_FACET_BED_DIAMETER_TOPLAYER,
-                        true),
-                    ComputeType.ADVANCE, stateId, hash));
-
-                newFacets.add(new BedDiameterSubFacet((idx << 8) +j,
-                    BED_QUALITY_BED_DIAMETER_SUBLAYER,
-                    getLayerDescription(
-                        meta,
-                        bedDiameter[j],
-                        range,
-                        I18N_FACET_BED_DIAMETER_SUBLAYER,
-                        false),
-                    ComputeType.ADVANCE, stateId, hash));
-            }
-            BedloadDiameterResult[] bedloadDiameter = result.getBedloadResults();
-            for (int j = 0;  j < bedloadDiameter.length; j++) {
-                newFacets.add(new BedloadDiameterFacet(
-                    (idx << 8) + j,
-                    BED_QUALITY_BEDLOAD_DIAMETER,
-                    createDiameterDescription(
-                        meta, bedloadDiameter[j]),
+            int i = 0;
+            for (BedQualityResultValue value: result.getValues()) {
+                newFacets.add(new BedQualityDataFacet((idx << 8) + i++,
+                    getFacetName(value),
+                    getFacetDescription(meta, range, value),
                     ComputeType.ADVANCE,
-                    stateId,
-                    hash));
-
-            }
-            if (bedDiameter.length > 0) {
-                BedParametersResult[] bedParameters = result.getParameters();
-                for (int j = 0; j < bedParameters.length; j++) {
-                    newFacets.add(new BedPorosityTopFacet((idx << 8) + j,
-                        BED_QUALITY_POROSITY_TOPLAYER,
-                        getLayerDescription(
-                            meta,
-                            bedParameters[j],
-                            range,
-                            I18N_FACET_BED_POROSITY_TOPLAYER,
-                            true),
-                        ComputeType.ADVANCE, stateId, hash));
-
-                    newFacets.add(new BedPorositySubFacet((idx << 8) + j,
-                        BED_QUALITY_POROSITY_SUBLAYER,
-                        getLayerDescription(
-                            meta,
-                            bedParameters[j],
-                            range,
-                            I18N_FACET_BED_POROSITY_SUBLAYER,
-                            false),
-                        ComputeType.ADVANCE, stateId, hash));
-
-                    newFacets.add(new BedDensityTopFacet((idx << 8) + j,
-                        BED_QUALITY_SEDIMENT_DENSITY_TOPLAYER,
-                        getLayerDescription(
-                            meta,
-                            bedParameters[j],
-                            range,
-                            I18N_FACET_BED_DENSITY_TOPLAYER,
-                            true),
-                        ComputeType.ADVANCE, stateId, hash));
-
-                    newFacets.add(new BedDensitySubFacet((idx << 8) + j,
-                        BED_QUALITY_SEDIMENT_DENSITY_SUBLAYER,
-                        getLayerDescription(
-                            meta,
-                            bedParameters[j],
-                            range,
-                            I18N_FACET_BED_DENSITY_SUBLAYER,
-                            false),
-                        ComputeType.ADVANCE, stateId, hash));
-                }
+                    stateId, hash, value.getName(), value.getType()));
             }
         }
     }
 
-    protected String getLayerDescription(CallMeta meta,
-        Object result, DateRange range, String i18n, boolean topLayer) {
+    protected String getFacetDescription(CallMeta meta,
+                                         DateRange range,
+                                         BedQualityResultValue value) {
         Date from = range != null ? range.getFrom() : new Date();
         Date to = range != null ? range.getTo() : new Date();
 
-        String layer;
-        /* Maybe just a check if i18n ends with .sublayer? */
-        if (topLayer) {
-            layer = Resources.getMsg(meta, I18N_TOPLAYER, I18N_TOPLAYER);
+        final String layerSuffix =
+            Resources.getMsg(meta, "bedquality." + value.getType(), "");
+
+        /* This could probably be unified with the facet name */
+        final String i18n = I18N_FACET_BED_BASE + "." +
+            (value.isDiameterResult() ? "diameter" : value.getName()) + "." +
+            value.getType();
+
+        if (value.isDiameterResult()) {
+            /* Include the diameter in the description */
+            return Resources.getMsg(meta, i18n, i18n, new Object[] {
+                value.getName().toUpperCase(), from, to, layerSuffix });
         } else {
-            layer = Resources.getMsg(meta, I18N_SUBLAYER, I18N_SUBLAYER);
-        }
-        if (result instanceof BedDiameterResult) {
-            return Resources.getMsg(meta, i18n, i18n, new Object[] { ((BedDiameterResult)result).getType(),
-                from, to, layer });
-        } else {
-            return Resources.getMsg(meta, i18n, i18n, new Object[] { from, to, layer });
+            return Resources.getMsg(meta, i18n, i18n, new Object[] { from, to, layerSuffix });
         }
     }
-
-    protected String createDiameterDescription(CallMeta meta,
-        BedQualityDiameterResult result) {
-        return Resources.getMsg(meta, I18N_FACET_BEDLOAD_DIAMETER,
-            I18N_FACET_BEDLOAD_DIAMETER, new Object[] { result.getType() });
-    }
 }
-// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org