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.text.DateFormat; 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; 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; teichmann@5831: import org.dive4elements.river.artifacts.model.minfo.BedDiffCalculation; teichmann@5831: import org.dive4elements.river.artifacts.model.minfo.BedDiffEpochFacet; teichmann@5831: import org.dive4elements.river.artifacts.model.minfo.BedDiffEpochFilterFacet; teichmann@5831: import org.dive4elements.river.artifacts.model.minfo.BedDiffEpochResult; teichmann@5831: import org.dive4elements.river.artifacts.model.minfo.BedDiffYearFacet; teichmann@5831: import org.dive4elements.river.artifacts.model.minfo.BedDiffYearFilterFacet; 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; teichmann@5831: import org.dive4elements.river.utils.Formatter; 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: { raimund@3614: /** The logger used in this class. */ raimund@3614: private static Logger logger = Logger.getLogger(DifferencesState.class); raimund@3898: public static final String I18N_DIFF_YEAR = "beddifference.year"; raimund@3898: public static final String I18N_DIFF_EPOCH = "beddifference.epoch"; 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"; raimund@3898: public static final String I18N_FACET_BED_DIFF_MORPH = "facet.bedheight.diff.morph"; raimund@3898: public static final String I18N_FACET_BED_DIFF_EPOCH = "facet.bedheight.diff.epoch"; rrenkert@4673: public static final String I18N_FACET_BED_DIFF_EPOCH_RAW = "facet.bedheight.diff.epoch.raw"; 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) { felix@6646: logger.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) { raimund@3898: logger.warn("Calculation computed no results!"); raimund@3898: return res; raimund@3898: } raimund@3898: raimund@3898: generateFacets(context, newFacets, results, getID(), hash); raimund@3898: logger.debug("Created " + newFacets.size() + " new Facets."); raimund@3898: raimund@3898: facets.addAll(newFacets); raimund@3898: raimund@3898: return res; raimund@3898: } raimund@3898: raimund@3898: protected void generateFacets(CallContext context, List newFacets, raimund@3898: BedDifferencesResult[] results, String stateId, String hash) { felix@6646: logger.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) { raimund@3898: newFacets.add(new BedDiffYearFacet( 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)); raimund@3898: newFacets.add(new BedDiffYearFacet( raimund@3898: idx, raimund@3898: BED_DIFFERENCE_MORPH_WIDTH, raimund@3898: createBedDiffMorphDescription( raimund@3898: meta, raimund@3898: (BedDiffYearResult)results[idx]), raimund@3898: ComputeType.ADVANCE, raimund@3898: stateId, raimund@3898: hash)); raimund@3898: newFacets.add(new BedDiffYearFacet( 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)); raimund@3898: newFacets.add(new BedDiffYearFacet( 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)); raimund@3898: newFacets.add(new BedDiffYearFacet( raimund@3898: idx, raimund@3898: BED_DIFFERENCE_HEIGHT_YEAR, raimund@3898: createBedDiffAbsoluteDescription( raimund@3898: meta, rrenkert@4673: (BedDiffYearResult)results[idx], rrenkert@4676: true), rrenkert@4673: ComputeType.ADVANCE, rrenkert@4673: stateId, rrenkert@4673: hash)); rrenkert@4676: newFacets.add(new BedDiffYearFilterFacet( 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@4676: newFacets.add(new BedDiffYearFilterFacet( 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@4676: newFacets.add(new BedDiffYearFilterFacet( 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)); rrenkert@4676: newFacets.add(new BedDiffYearFilterFacet( rrenkert@4673: idx, rrenkert@4673: BED_DIFFERENCE_HEIGHT_YEAR_FILTERED, rrenkert@4673: createBedDiffAbsoluteDescription( rrenkert@4673: meta, rrenkert@4673: (BedDiffYearResult)results[idx], rrenkert@4676: false), raimund@3898: ComputeType.ADVANCE, raimund@3898: stateId, raimund@3898: hash)); raimund@3898: } raimund@3898: if (results[idx] instanceof BedDiffEpochResult) { raimund@3898: newFacets.add(new BedDiffEpochFacet( raimund@3898: idx, raimund@3898: BED_DIFFERENCE_EPOCH, raimund@3898: createBedDiffEpochDescription( raimund@3898: meta, rrenkert@4673: (BedDiffEpochResult)results[idx], rrenkert@4676: true), raimund@3898: ComputeType.ADVANCE, raimund@3898: stateId, raimund@3898: hash)); raimund@3898: newFacets.add(new BedDiffEpochFacet( raimund@3898: idx, raimund@3898: BED_DIFFERENCE_EPOCH_HEIGHT1, raimund@3898: createBedDiffHeightEpochDescription( raimund@3898: meta, raimund@3898: (BedDiffEpochResult)results[idx], rrenkert@4673: 0, rrenkert@4676: true), raimund@3898: ComputeType.ADVANCE, raimund@3898: stateId, raimund@3898: hash)); raimund@3898: newFacets.add(new BedDiffEpochFacet( raimund@3898: idx, raimund@3898: BED_DIFFERENCE_EPOCH_HEIGHT2, raimund@3898: createBedDiffHeightEpochDescription( raimund@3898: meta, raimund@3898: (BedDiffEpochResult)results[idx], rrenkert@4673: 1, rrenkert@4676: true), raimund@3898: ComputeType.ADVANCE, raimund@3898: stateId, raimund@3898: hash)); rrenkert@4676: newFacets.add(new BedDiffEpochFilterFacet( rrenkert@4673: idx, rrenkert@4673: BED_DIFFERENCE_EPOCH_FILTERED, rrenkert@4673: createBedDiffEpochDescription( rrenkert@4673: meta, rrenkert@4673: (BedDiffEpochResult)results[idx], rrenkert@4676: false), rrenkert@4673: ComputeType.ADVANCE, rrenkert@4673: stateId, rrenkert@4673: hash)); rrenkert@4676: newFacets.add(new BedDiffEpochFilterFacet( rrenkert@4673: idx, rrenkert@4673: BED_DIFFERENCE_EPOCH_HEIGHT1_FILTERED, rrenkert@4673: createBedDiffHeightEpochDescription( rrenkert@4673: meta, rrenkert@4673: (BedDiffEpochResult)results[idx], rrenkert@4673: 0, rrenkert@4676: false), rrenkert@4673: ComputeType.ADVANCE, rrenkert@4673: stateId, rrenkert@4673: hash)); rrenkert@4676: newFacets.add(new BedDiffEpochFilterFacet( rrenkert@4673: idx, rrenkert@4673: BED_DIFFERENCE_EPOCH_HEIGHT2_FILTERED, rrenkert@4673: createBedDiffHeightEpochDescription( rrenkert@4673: meta, rrenkert@4673: (BedDiffEpochResult)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: private String createBedDiffHeightEpochDescription( raimund@3898: CallMeta meta, raimund@3898: BedDiffEpochResult result, rrenkert@4673: int ndx, rrenkert@4673: boolean raw raimund@3898: ) { raimund@3898: DateFormat df = Formatter.getDateFormatter(meta, "yyyy"); raimund@3898: String range = raimund@3898: df.format(result.getStart()) raimund@3898: + " - " + raimund@3898: df.format(result.getEnd()); raimund@3898: rrenkert@4673: String i18nHeight1 = I18N_FACET_BED_DIFF_HEIGHT1; rrenkert@4673: String i18nHeight2 = I18N_FACET_BED_DIFF_HEIGHT2; rrenkert@4673: if (raw) { rrenkert@4673: i18nHeight1 = I18N_FACET_BED_DIFF_HEIGHT1_RAW; rrenkert@4673: i18nHeight2 = I18N_FACET_BED_DIFF_HEIGHT2_RAW; rrenkert@4673: } raimund@3898: if (ndx == 0) { rrenkert@4673: return Resources.getMsg(meta, i18nHeight1, rrenkert@4673: i18nHeight1, new Object[] { range }); raimund@3898: } raimund@3898: else { rrenkert@4673: return Resources.getMsg(meta, i18nHeight2, rrenkert@4673: i18nHeight2, new Object[] {range}); 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: ) { raimund@3898: String range = result.getStart() + " - " + result.getEnd(); 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: } rrenkert@4673: return Resources.getMsg(meta, i18n, i18n, new Object[] { range }); raimund@3898: } raimund@3898: raimund@3898: protected String createBedDiffMorphDescription( raimund@3898: CallMeta meta, raimund@3898: BedDiffYearResult result) { raimund@3898: String range = result.getStart() + " - " + result.getEnd(); raimund@3898: raimund@3898: return Resources.getMsg(meta, I18N_FACET_BED_DIFF_MORPH, raimund@3898: I18N_FACET_BED_DIFF_MORPH, new Object[] { range }); 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: raimund@3898: protected String createBedDiffEpochDescription( raimund@3898: CallMeta meta, rrenkert@4673: BedDiffEpochResult result, rrenkert@4673: boolean raw) { raimund@3898: DateFormat df = Formatter.getDateFormatter(meta, "yyyy"); raimund@3898: String range = raimund@3898: df.format(result.getStart()) raimund@3898: + " - " + raimund@3898: df.format(result.getEnd()); raimund@3898: rrenkert@4673: String i18n = I18N_FACET_BED_DIFF_EPOCH; rrenkert@4673: if (raw) { rrenkert@4673: i18n = I18N_FACET_BED_DIFF_EPOCH_RAW; rrenkert@4673: } rrenkert@4673: return Resources.getMsg(meta, i18n, i18n, new Object[] { range }); raimund@3898: } rrenkert@4676: 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@4676: if (name.equals(BED_DIFFERENCE_EPOCH) || rrenkert@4676: name.equals(BED_DIFFERENCE_EPOCH_HEIGHT1) || rrenkert@4676: name.equals(BED_DIFFERENCE_EPOCH_HEIGHT2) || rrenkert@6223: name.equals(BED_DIFFERENCE_EPOCH_HEIGHT1_FILTERED) || rrenkert@6223: name.equals(BED_DIFFERENCE_EPOCH_HEIGHT2_FILTERED) || rrenkert@4676: 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@4793: else if (name.equals(BED_DIFFERENCE_MORPH_WIDTH) || rrenkert@4793: name.equals(BED_DIFFERENCE_EPOCH_FILTERED) || 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 :