raimund@3614: package de.intevation.flys.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:
raimund@3898: import de.intevation.artifactdatabase.state.Facet;
rrenkert@4676: import de.intevation.artifactdatabase.state.FacetActivity;
rrenkert@4676: import de.intevation.artifacts.Artifact;
raimund@3898: import de.intevation.artifacts.CallContext;
raimund@3898: import de.intevation.artifacts.CallMeta;
raimund@3898: import de.intevation.flys.artifacts.FLYSArtifact;
raimund@3898: import de.intevation.flys.artifacts.access.BedDifferencesAccess;
raimund@3898: import de.intevation.flys.artifacts.model.CalculationResult;
raimund@3898: import de.intevation.flys.artifacts.model.FacetTypes;
raimund@3898: import de.intevation.flys.artifacts.model.minfo.BedDiffCalculation;
raimund@3898: import de.intevation.flys.artifacts.model.minfo.BedDiffEpochFacet;
rrenkert@4676: import de.intevation.flys.artifacts.model.minfo.BedDiffEpochFilterFacet;
raimund@3898: import de.intevation.flys.artifacts.model.minfo.BedDiffEpochResult;
raimund@3898: import de.intevation.flys.artifacts.model.minfo.BedDiffYearFacet;
rrenkert@4676: import de.intevation.flys.artifacts.model.minfo.BedDiffYearFilterFacet;
raimund@3898: import de.intevation.flys.artifacts.model.minfo.BedDiffYearResult;
raimund@3898: import de.intevation.flys.artifacts.model.minfo.BedDifferencesResult;
raimund@3898: import de.intevation.flys.artifacts.resources.Resources;
raimund@3614: import de.intevation.flys.artifacts.states.DefaultState;
raimund@3898: import de.intevation.flys.utils.Formatter;
raimund@3614:
raimund@3614: /**
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:
raimund@3614: public DifferencesState() {
raimund@3614: }
raimund@3898:
raimund@3898: @Override
raimund@3898: public Object computeAdvance(FLYSArtifact artifact, String hash,
raimund@3898: CallContext context, List facets, Object old) {
raimund@3898: logger.debug("BedQualityState.computeAdvance");
raimund@3898:
raimund@3898: List newFacets = new ArrayList();
raimund@3898:
raimund@3898: BedDifferencesAccess access =
raimund@3898: new BedDifferencesAccess(artifact, context);
raimund@3898:
raimund@3898: CalculationResult res = old instanceof CalculationResult ? (CalculationResult) old
raimund@3898: : new BedDiffCalculation().calculate(access);
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) {
raimund@3898: logger.debug("BedQualityState.generateFacets");
raimund@3898:
raimund@3898: CallMeta meta = context.getMeta();
raimund@3898:
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: ) {
raimund@3898: String range = result.getStart() + " - " + 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: i18nHeight2, new Object[] { range });
raimund@3898: }
raimund@3898: else {
rrenkert@4673: return Resources.getMsg(meta, i18nHeight1,
rrenkert@4673: i18nHeight2, new Object[] {range});
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@4676: name.equals(BED_DIFFERENCE_HEIGHT_YEAR) ||
rrenkert@4676: name.equals(BED_DIFFERENCE_YEAR) ||
rrenkert@4676: name.equals(BED_DIFFERENCE_YEAR_HEIGHT1) ||
rrenkert@4676: name.equals(BED_DIFFERENCE_YEAR_HEIGHT2)) {
rrenkert@4676: return Boolean.FALSE;
rrenkert@4676: }
rrenkert@4676: else {
rrenkert@4676: return Boolean.TRUE;
rrenkert@4676: }
rrenkert@4676: }
rrenkert@4676: });
rrenkert@4676: }
raimund@3614: }