view flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/SQRelation.java @ 3344:cb376f48dd37 2.8

merged flys-backend/2.8
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:36 +0200
parents 0f7abd95c6e2
children 1df6984628c3
line wrap: on
line source
package de.intevation.flys.artifacts.states;

import de.intevation.artifactdatabase.state.Facet;

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

import de.intevation.flys.artifacts.FLYSArtifact;

import de.intevation.flys.artifacts.access.SQRelationAccess;

import de.intevation.flys.artifacts.model.CalculationResult;
import de.intevation.flys.artifacts.model.DataFacet;
import de.intevation.flys.artifacts.model.FacetTypes;

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.model.sq.SQOverviewFacet;

import de.intevation.flys.artifacts.resources.Resources;

import java.util.List;

import org.apache.log4j.Logger;

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

    private static Logger log = 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
    ) {
        log.debug("SQRelation.computeAdvance");

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

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

        SQResult [] sqr = (SQResult [])res.getData();
        if (sqr == null) {
            return res;
        }

        createFacets(context, facets, sqr, hash);

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

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

        facets.add(csv);
        facets.add(pdf);

        return res;
    }


    protected void createFacets(
        CallContext context,
        List<Facet> container,
        SQResult[]  sqr,
        String      hash
    ) {
        CallMeta meta    = context.getMeta();
        String   stateId = getID();
        for (int i = 0; i < 6; i++) {
            container.add(new SQOverviewFacet(
                    i,
                    i,
                    "sq_chart_overview",
                    Resources.getMsg(
                        context.getMeta(),
                        I18N_FACET_CURVE,
                        I18N_FACET_CURVE
                    ),
                    hash,
                    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) {
                    log.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 (log.isDebugEnabled()) {
                        log.debug("new outliers facet (index=" +index+ ")");
                        log.debug("   result index = " + res);
                        log.debug("   fraction idx = " + i);
                        log.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) {
        log.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