teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.artifacts.states.minfo; raimund@3614: raimund@3898: import java.util.ArrayList; raimund@3898: import java.util.List; raimund@3898: raimund@3614: import org.apache.log4j.Logger; raimund@3614: teichmann@5831: import org.dive4elements.artifactdatabase.state.Facet; teichmann@5831: import org.dive4elements.artifactdatabase.state.FacetActivity; teichmann@5831: import org.dive4elements.artifacts.Artifact; teichmann@5831: import org.dive4elements.artifacts.CallContext; teichmann@5831: import org.dive4elements.artifacts.CallMeta; teichmann@5867: import org.dive4elements.river.artifacts.D4EArtifact; teichmann@5831: import org.dive4elements.river.artifacts.access.BedDifferencesAccess; tom@8169: import org.dive4elements.river.artifacts.model.Calculation; teichmann@5831: import org.dive4elements.river.artifacts.model.CalculationResult; rrenkert@6219: import org.dive4elements.river.artifacts.model.DataFacet; teichmann@5831: import org.dive4elements.river.artifacts.model.FacetTypes; tom@8169: import org.dive4elements.river.artifacts.model.ReportFacet; teichmann@5831: import org.dive4elements.river.artifacts.model.minfo.BedDiffCalculation; rrenkert@7985: import org.dive4elements.river.artifacts.model.minfo.BedDiffFacet; rrenkert@7985: import org.dive4elements.river.artifacts.model.minfo.BedDiffHeightMinFacet; rrenkert@7985: import org.dive4elements.river.artifacts.model.minfo.BedDiffHeightMinFilterFacet; rrenkert@7985: import org.dive4elements.river.artifacts.model.minfo.BedDiffHeightSubFacet; rrenkert@7985: import org.dive4elements.river.artifacts.model.minfo.BedDiffHeightSubFilterFacet; rrenkert@7985: import org.dive4elements.river.artifacts.model.minfo.BedDiffMorphMinFacet; rrenkert@7985: import org.dive4elements.river.artifacts.model.minfo.BedDiffMorphSubFacet; rrenkert@7985: import org.dive4elements.river.artifacts.model.minfo.BedDiffPerYearFacet; rrenkert@7985: import org.dive4elements.river.artifacts.model.minfo.BedDiffPerYearFilterFacet; rrenkert@7985: import org.dive4elements.river.artifacts.model.minfo.BedDiffSoundingWidthFacet; rrenkert@7985: import org.dive4elements.river.artifacts.model.minfo.BedDiffFilterFacet; teichmann@5831: import org.dive4elements.river.artifacts.model.minfo.BedDiffYearResult; teichmann@5831: import org.dive4elements.river.artifacts.model.minfo.BedDifferencesResult; teichmann@5831: import org.dive4elements.river.artifacts.resources.Resources; teichmann@5831: import org.dive4elements.river.artifacts.states.DefaultState; raimund@3614: raimund@3614: /** felix@6575: * State for BedDifferences. raimund@3614: * @author Raimund Renkert raimund@3614: */ raimund@3898: public class DifferencesState raimund@3898: extends DefaultState raimund@3898: implements FacetTypes raimund@3898: { teichmann@8202: /** The log used in this class. */ teichmann@8202: private static Logger log = Logger.getLogger(DifferencesState.class); raimund@3898: public static final String I18N_DIFF_YEAR = "beddifference.year"; raimund@3898: raimund@3898: public static final String I18N_FACET_BED_DIFF_YEAR = "facet.bedheight.diff.year"; rrenkert@4673: public static final String I18N_FACET_BED_DIFF_YEAR_RAW = "facet.bedheight.diff.year.raw"; raimund@3898: public static final String I18N_FACET_BED_DIFF_ABSOLUTE = "facet.bedheight.diff.absolute"; rrenkert@4673: public static final String I18N_FACET_BED_DIFF_ABSOLUTE_RAW = "facet.bedheight.diff.absolute.raw"; rrenkert@7830: public static final String I18N_FACET_BED_DIFF_SOUNDING = "facet.bedheight.diff.sounding"; rrenkert@7830: public static final String I18N_FACET_BED_DIFF_MORPH1 = "facet.bedheight.diff.morph1"; rrenkert@7830: public static final String I18N_FACET_BED_DIFF_MORPH2 = "facet.bedheight.diff.morph2"; raimund@3898: public static final String I18N_FACET_BED_DIFF_HEIGHT1 = "facet.bedheight.diff.height1"; rrenkert@4673: public static final String I18N_FACET_BED_DIFF_HEIGHT1_RAW = "facet.bedheight.diff.height1.raw"; raimund@3898: public static final String I18N_FACET_BED_DIFF_HEIGHT2 = "facet.bedheight.diff.height2"; rrenkert@4673: public static final String I18N_FACET_BED_DIFF_HEIGHT2_RAW = "facet.bedheight.diff.height2.raw"; raimund@3614: felix@6091: public static final String I18N_FACET_BED_DIFF_HEIGHT_RAW = "facet.bedheight.diff.height.raw"; felix@6091: raimund@3614: public DifferencesState() { raimund@3614: } raimund@3898: raimund@3898: @Override teichmann@5867: public Object computeAdvance(D4EArtifact artifact, String hash, raimund@3898: CallContext context, List facets, Object old) { teichmann@8202: log.debug("DifferencesState.computeAdvance"); raimund@3898: raimund@3898: List newFacets = new ArrayList(); raimund@3898: teichmann@6148: BedDifferencesAccess access = new BedDifferencesAccess(artifact); raimund@3898: teichmann@6148: CalculationResult res = old instanceof CalculationResult teichmann@6148: ? (CalculationResult)old teichmann@6148: : new BedDiffCalculation().calculate(access, context); raimund@3898: raimund@3898: if (facets == null || res == null) { raimund@3898: return res; raimund@3898: } raimund@3898: raimund@3898: BedDifferencesResult[] results = (BedDifferencesResult[]) res.getData(); raimund@3898: raimund@3898: if (results == null || results.length == 0) { teichmann@8202: log.warn("Calculation computed no results!"); raimund@3898: return res; raimund@3898: } raimund@3898: raimund@3898: generateFacets(context, newFacets, results, getID(), hash); teichmann@8202: log.debug("Created " + newFacets.size() + " new Facets."); raimund@3898: raimund@3898: facets.addAll(newFacets); raimund@3898: tom@8169: Calculation report = res.getReport(); tom@8169: if (report != null && report.hasProblems()) { tom@8169: facets.add(new ReportFacet(ComputeType.ADVANCE, hash, id)); teichmann@8202: log.warn("Problems: " + report.problemsToString()); tom@8169: } tom@8169: raimund@3898: return res; raimund@3898: } raimund@3898: felix@7574: /** Generate Facets based on given results. felix@7574: * @param newFacets list to place new facets into. felix@7574: */ felix@6768: protected void generateFacets( felix@6768: CallContext context, felix@6768: List newFacets, felix@6768: BedDifferencesResult[] results, felix@6768: String stateId, felix@6768: String hash felix@6768: ) { teichmann@8202: log.debug("DifferencesState.generateFacets"); raimund@3898: raimund@3898: CallMeta meta = context.getMeta(); raimund@3898: rrenkert@6219: newFacets.add(new DataFacet(CSV, "CSV data", ComputeType.ADVANCE, hash, id)); aheinecke@6242: newFacets.add(new DataFacet(PDF, "PDF data", ComputeType.ADVANCE, hash, id)); raimund@3898: for (int idx = 0; idx < results.length; idx++) { raimund@3898: if (results[idx] instanceof BedDiffYearResult) { rrenkert@7985: newFacets.add(new BedDiffFacet( raimund@3898: idx, raimund@3898: BED_DIFFERENCE_YEAR, raimund@3898: createBedDiffYearDescription( raimund@3898: meta, rrenkert@4673: (BedDiffYearResult)results[idx], rrenkert@4676: true), raimund@3898: ComputeType.ADVANCE, raimund@3898: stateId, raimund@3898: hash)); rrenkert@7985: newFacets.add(new BedDiffSoundingWidthFacet( raimund@3898: idx, rrenkert@7830: BED_DIFFERENCE_SOUNDING_WIDTH, rrenkert@7830: createBedDiffSoundingDescription(meta), rrenkert@7830: ComputeType.ADVANCE, rrenkert@7830: stateId, rrenkert@7830: hash)); rrenkert@7985: newFacets.add(new BedDiffMorphMinFacet( rrenkert@7830: idx, rrenkert@7830: BED_DIFFERENCE_MORPH_WIDTH1, rrenkert@7830: createBedDiffMorph1Description(meta), rrenkert@7830: ComputeType.ADVANCE, rrenkert@7830: stateId, rrenkert@7830: hash)); rrenkert@7985: newFacets.add(new BedDiffMorphSubFacet( rrenkert@7830: idx, rrenkert@7830: BED_DIFFERENCE_MORPH_WIDTH2, rrenkert@7830: createBedDiffMorph2Description(meta), raimund@3898: ComputeType.ADVANCE, raimund@3898: stateId, raimund@3898: hash)); rrenkert@7985: newFacets.add(new BedDiffHeightMinFacet( raimund@3898: idx, raimund@3898: BED_DIFFERENCE_YEAR_HEIGHT1, raimund@3898: createBedDiffHeightDescription( raimund@3898: meta, raimund@3898: (BedDiffYearResult)results[idx], rrenkert@4673: 0, rrenkert@4676: true), raimund@3898: ComputeType.ADVANCE, raimund@3898: stateId, raimund@3898: hash)); rrenkert@7985: newFacets.add(new BedDiffHeightSubFacet( raimund@3898: idx, raimund@3898: BED_DIFFERENCE_YEAR_HEIGHT2, raimund@3898: createBedDiffHeightDescription( raimund@3898: meta, raimund@3898: (BedDiffYearResult)results[idx], rrenkert@4673: 1, rrenkert@4676: true), raimund@3898: ComputeType.ADVANCE, raimund@3898: stateId, raimund@3898: hash)); tom@8169: if (((BedDiffYearResult)results[idx]) tom@8169: .getHeightPerYearData()[1].length > 0) { tom@8169: /* Skip facets with data per year if there are none tom@8169: (because of missing start or end year) */ tom@8169: newFacets.add(new BedDiffPerYearFacet( tom@8169: idx, tom@8169: BED_DIFFERENCE_HEIGHT_YEAR, tom@8169: createBedDiffAbsoluteDescription( tom@8169: meta, tom@8169: (BedDiffYearResult)results[idx], tom@8169: true), tom@8169: ComputeType.ADVANCE, tom@8169: stateId, tom@8169: hash)); tom@8169: newFacets.add(new BedDiffPerYearFilterFacet( tom@8169: idx, tom@8169: BED_DIFFERENCE_HEIGHT_YEAR_FILTERED, tom@8169: createBedDiffAbsoluteDescription( tom@8169: meta, tom@8169: (BedDiffYearResult)results[idx], tom@8169: false), tom@8169: ComputeType.ADVANCE, tom@8169: stateId, tom@8169: hash)); tom@8169: } rrenkert@7985: newFacets.add(new BedDiffFilterFacet( rrenkert@4673: idx, rrenkert@4673: BED_DIFFERENCE_YEAR_FILTERED, rrenkert@4673: createBedDiffYearDescription( rrenkert@4673: meta, rrenkert@4673: (BedDiffYearResult)results[idx], rrenkert@4676: false), rrenkert@4673: ComputeType.ADVANCE, rrenkert@4673: stateId, rrenkert@4673: hash)); rrenkert@7985: newFacets.add(new BedDiffHeightMinFilterFacet( rrenkert@4673: idx, rrenkert@4673: BED_DIFFERENCE_YEAR_HEIGHT1_FILTERED, rrenkert@4673: createBedDiffHeightDescription( rrenkert@4673: meta, rrenkert@4673: (BedDiffYearResult)results[idx], rrenkert@4673: 0, rrenkert@4676: false), rrenkert@4673: ComputeType.ADVANCE, rrenkert@4673: stateId, rrenkert@4673: hash)); rrenkert@7985: newFacets.add(new BedDiffHeightSubFilterFacet( rrenkert@4673: idx, rrenkert@4673: BED_DIFFERENCE_YEAR_HEIGHT2_FILTERED, rrenkert@4673: createBedDiffHeightDescription( rrenkert@4673: meta, rrenkert@4673: (BedDiffYearResult)results[idx], rrenkert@4673: 1, rrenkert@4676: false), rrenkert@4673: ComputeType.ADVANCE, rrenkert@4673: stateId, rrenkert@4673: hash)); raimund@3898: } raimund@3898: } raimund@3898: } raimund@3898: raimund@3898: private String createBedDiffHeightDescription( raimund@3898: CallMeta meta, raimund@3898: BedDiffYearResult result, rrenkert@4673: int ndx, rrenkert@4673: boolean raw raimund@3898: ) { felix@6091: if (raw && ndx == 0) { felix@6091: return Resources.getMsg(meta, I18N_FACET_BED_DIFF_HEIGHT_RAW, felix@6091: I18N_FACET_BED_DIFF_HEIGHT_RAW, new Object[] {result.getNameFirst()}); felix@6091: } felix@6091: if (raw && ndx == 1) { felix@6091: return Resources.getMsg(meta, I18N_FACET_BED_DIFF_HEIGHT_RAW, felix@6091: I18N_FACET_BED_DIFF_HEIGHT_RAW, new Object[] {result.getNameSecond()}); rrenkert@4673: } raimund@3898: if (ndx == 0) { felix@6091: return result.getNameFirst(); raimund@3898: } raimund@3898: else { felix@6091: return result.getNameSecond(); raimund@3898: } raimund@3898: } raimund@3898: raimund@3898: protected String createBedDiffYearDescription( raimund@3898: CallMeta meta, rrenkert@4673: BedDiffYearResult result, rrenkert@4673: boolean raw rrenkert@4673: ) { tom@8169: String start = result.getStart() != null ? tom@8169: result.getStart().toString() : result.getNameFirst(); tom@8169: String end = result.getEnd() != null ? tom@8169: result.getEnd().toString() : result.getNameSecond(); tom@8169: String range = start + " - " + end; raimund@3898: rrenkert@4673: String i18n = I18N_FACET_BED_DIFF_YEAR; rrenkert@4673: if (raw) { rrenkert@4673: i18n = I18N_FACET_BED_DIFF_YEAR_RAW; rrenkert@4673: } tom@8169: return Resources.getMsg(meta, i18n, new Object[] { range }); raimund@3898: } raimund@3898: rrenkert@7830: protected String createBedDiffSoundingDescription( felix@6784: CallMeta meta) { rrenkert@7830: return Resources.getMsg(meta, I18N_FACET_BED_DIFF_SOUNDING, rrenkert@7830: I18N_FACET_BED_DIFF_SOUNDING); rrenkert@7830: } rrenkert@7830: rrenkert@7830: protected String createBedDiffMorph1Description( rrenkert@7830: CallMeta meta) { rrenkert@7830: return Resources.getMsg(meta, I18N_FACET_BED_DIFF_MORPH1, rrenkert@7830: I18N_FACET_BED_DIFF_MORPH1); rrenkert@7830: } rrenkert@7830: rrenkert@7830: protected String createBedDiffMorph2Description( rrenkert@7830: CallMeta meta) { rrenkert@7830: return Resources.getMsg(meta, I18N_FACET_BED_DIFF_MORPH2, rrenkert@7830: I18N_FACET_BED_DIFF_MORPH2); raimund@3898: } raimund@3898: raimund@3898: protected String createBedDiffAbsoluteDescription( raimund@3898: CallMeta meta, rrenkert@4673: BedDiffYearResult result, rrenkert@4673: boolean raw rrenkert@4673: ) { raimund@3898: String range = result.getStart() + " - " + result.getEnd(); raimund@3898: rrenkert@4673: String i18n = I18N_FACET_BED_DIFF_ABSOLUTE; rrenkert@4673: if (raw) { rrenkert@4673: i18n = I18N_FACET_BED_DIFF_ABSOLUTE_RAW; rrenkert@4673: } rrenkert@4673: return Resources.getMsg(meta, i18n, i18n, new Object[] { range }); raimund@3898: } raimund@3898: rrenkert@4676: static { rrenkert@4676: // Active/deactivate facets. rrenkert@4676: FacetActivity.Registry.getInstance().register( rrenkert@4676: "minfo", rrenkert@4676: new FacetActivity() { rrenkert@4676: @Override rrenkert@4676: public Boolean isInitialActive( rrenkert@4676: Artifact artifact, rrenkert@4676: Facet facet, rrenkert@4676: String output rrenkert@4676: ) { rrenkert@4676: String name = facet.getName(); rrenkert@7919: if (name.equals(BED_DIFFERENCE_HEIGHT_YEAR) || rrenkert@4676: name.equals(BED_DIFFERENCE_YEAR) || rrenkert@4676: name.equals(BED_DIFFERENCE_YEAR_HEIGHT1) || rrenkert@6223: name.equals(BED_DIFFERENCE_YEAR_HEIGHT2) || rrenkert@6223: name.equals(BED_DIFFERENCE_YEAR_HEIGHT1_FILTERED) || rrenkert@6223: name.equals(BED_DIFFERENCE_YEAR_HEIGHT2_FILTERED)){ rrenkert@4676: return Boolean.FALSE; rrenkert@4676: } rrenkert@7830: else if (name.equals(BED_DIFFERENCE_SOUNDING_WIDTH) || rrenkert@4793: name.equals(BED_DIFFERENCE_HEIGHT_YEAR_FILTERED) || rrenkert@6223: name.equals(BED_DIFFERENCE_YEAR_FILTERED)) { rrenkert@4793: return Boolean.TRUE; rrenkert@4793: } rrenkert@4676: else { rrenkert@4793: return null; rrenkert@4676: } rrenkert@4676: } rrenkert@4676: }); rrenkert@4676: } raimund@3614: } felix@6648: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :