view flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/SQRelation.java @ 3194:e815cf20bab2

Add point labels to W/Q curve flys-artifacts/trunk@4810 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Christian Lins <christian.lins@intevation.de>
date Wed, 27 Jun 2012 14:48:35 +0000
parents 238803b2cb8b
children 116b342893e3
line wrap: on
line source
package de.intevation.flys.artifacts.states;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import de.intevation.artifacts.CallContext;
import de.intevation.artifacts.CallMeta;

import de.intevation.artifactdatabase.state.Facet;

import de.intevation.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.MINFOArtifact;
import de.intevation.flys.artifacts.model.DataFacet;
import de.intevation.flys.artifacts.model.FacetTypes;
import de.intevation.flys.artifacts.model.CalculationResult;
import de.intevation.flys.artifacts.model.sq.SQCurveFacet;
import de.intevation.flys.artifacts.model.sq.SQFractionResult;
import de.intevation.flys.artifacts.model.sq.SQMeasurementFacet;
import de.intevation.flys.artifacts.model.sq.SQOutlierFacet;
import de.intevation.flys.artifacts.model.sq.SQRelationCalculation;
import de.intevation.flys.artifacts.model.sq.SQResult;
import de.intevation.flys.artifacts.resources.Resources;
import de.intevation.flys.artifacts.states.DefaultState;

/**
 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
 */
public class SQRelation extends DefaultState implements FacetTypes {

    private static Logger logger = Logger.getLogger(SQRelation.class);


    public static final String I18N_FACET_CURVE =
        "facet.sq_relation.curve";

    public static final String I18N_FACET_MEASUREMENTS =
        "facet.sq_relation.measurements";

    public static final String I18N_FACET_OUTLIERS =
        "facet.sq_relation.outliers";


    public SQRelation() {
    }


    @Override
    public Object computeAdvance(
        FLYSArtifact artifact,
        String       hash,
        CallContext  context,
        List<Facet>  facets,
        Object       old
    ) {
        logger.debug("SQRelation.computeAdvance");

        List<Facet> newFacets = new ArrayList<Facet>();

        CalculationResult res = old instanceof CalculationResult
            ? (CalculationResult) old
            : new SQRelationCalculation().calculate((MINFOArtifact) artifact);

        if (facets == null || res == null) {
            return res;
        }

        SQResult[] sqr = (SQResult[]) res.getData();
        logger.info("Calculation computed " + sqr.length + " SQResult objects");

        createFacets(context, newFacets, sqr, hash);

        Facet csv = new DataFacet(
            CSV, "CSV data", ComputeType.ADVANCE, hash, id);

        Facet pdf = new DataFacet(
            PDF, "PDF data", ComputeType.ADVANCE, hash, id);

        newFacets.add(csv);
        newFacets.add(pdf);

        logger.debug("Created " + newFacets.size() + " new Facets.");

        facets.addAll(newFacets);

        return res;
    }


    protected void createFacets(
        CallContext context,
        List<Facet> container,
        SQResult[]  sqr,
        String      hash
    ) {
        CallMeta meta    = context.getMeta();
        String   stateId = getID();

        for (int res = 0, n = sqr.length; res < n; res++) {

            for (int i = 0; i < 6; i++) {
                SQFractionResult result = sqr[res].getFraction(i);

                if (result == null) {
                    logger.warn("Fraction at index " + i + " is empty!");
                    continue;
                }

                container.add(new SQMeasurementFacet(
                    res,
                    i,
                    getFractionFacetname(1, i),
                    Resources.getMsg(
                        meta,
                        I18N_FACET_MEASUREMENTS,
                        I18N_FACET_MEASUREMENTS
                    ),
                    hash,
                    stateId
                ));

                container.add(new SQCurveFacet(
                    res,
                    i,
                    getFractionFacetname(0, i),
                    Resources.getMsg(
                        meta,
                        I18N_FACET_CURVE,
                        I18N_FACET_CURVE
                    ),
                    hash,
                    stateId
                ));

                for (int j = 0, C = result.getOutliersCount(); j < C; j++) {
                    int index = res;
                    index     = index << 16;
                    index     = index + j;

                    if (logger.isDebugEnabled()) {
                        logger.debug("new outliers facet (index=" +index+ ")");
                        logger.debug("   result index = " + res);
                        logger.debug("   fraction idx = " + i);
                        logger.debug("   iteration    = " + j);
                    }

                    container.add(new SQOutlierFacet(
                        index,
                        i,
                        getFractionFacetname(2, i),
                        Resources.getMsg(
                            meta,
                            I18N_FACET_OUTLIERS,
                            I18N_FACET_OUTLIERS,
                            new Object[] { j }
                        ),
                        hash,
                        stateId
                    ));
                }
            }
        }
    }


    protected String getFractionFacetname(int type, int fractionIdx) {
        logger.debug("getFractionFacetname(): " + type + " | " + fractionIdx);

        switch (type) {
            case 0:
                switch (fractionIdx) {
                    case 0: return SQ_A_CURVE;
                    case 1: return SQ_B_CURVE;
                    case 2: return SQ_C_CURVE;
                    case 3: return SQ_D_CURVE;
                    case 4: return SQ_E_CURVE;
                    case 5: return SQ_F_CURVE;
                }
            case 1:
                switch (fractionIdx) {
                    case 0: return SQ_A_MEASUREMENT;
                    case 1: return SQ_B_MEASUREMENT;
                    case 2: return SQ_C_MEASUREMENT;
                    case 3: return SQ_D_MEASUREMENT;
                    case 4: return SQ_E_MEASUREMENT;
                    case 5: return SQ_F_MEASUREMENT;
                }
            case 2:
                switch (fractionIdx) {
                    case 0: return SQ_A_OUTLIER;
                    case 1: return SQ_B_OUTLIER;
                    case 2: return SQ_C_OUTLIER;
                    case 3: return SQ_D_OUTLIER;
                    case 4: return SQ_E_OUTLIER;
                    case 5: return SQ_F_OUTLIER;
                }
        }

        return null;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org