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.model.fixings;
raimund@3035:
ingo@3466: import org.apache.log4j.Logger;
teichmann@5831: import org.dive4elements.artifacts.Artifact;
teichmann@5831: import org.dive4elements.artifacts.CallContext;
teichmann@5867: import org.dive4elements.river.artifacts.D4EArtifact;
teichmann@5831: import org.dive4elements.river.artifacts.model.CalculationResult;
teichmann@5831: import org.dive4elements.river.artifacts.model.FacetTypes;
teichmann@5831: import org.dive4elements.river.artifacts.states.DefaultState.ComputeType;
teichmann@5831: import org.dive4elements.river.utils.KMIndex;
raimund@3035:
raimund@3035: /**
raimund@3035: * Facet to show W values for Q values at km for a date.
raimund@3035: *
raimund@3035: * @author Raimund Renkert
raimund@3035: */
gernotbelger@9348: public class FixReferenceEventsFacet extends FixingsFacet implements FacetTypes {
raimund@3035:
teichmann@8202: /** House log. */
teichmann@8202: private static Logger log = Logger.getLogger(FixReferenceEventsFacet.class);
raimund@3035:
raimund@3035: /** Trivial Constructor. */
raimund@3035: public FixReferenceEventsFacet() {
raimund@3035: }
raimund@3035:
gernotbelger@9348: public FixReferenceEventsFacet(final int index, final String name, final String description) {
gernotbelger@9348: super(index, name, description, ComputeType.ADVANCE, null, null);
raimund@3035: }
raimund@3035:
raimund@3035: /**
raimund@3035: * Returns the data this facet requires.
raimund@3035: *
gernotbelger@9348: * @param artifact
gernotbelger@9348: * the owner artifact.
gernotbelger@9348: * @param context
gernotbelger@9348: * the CallContext (ignored).
raimund@3035: *
raimund@3035: * @return the data.
raimund@3035: */
raimund@3035: @Override
gernotbelger@9348: public Object getData(final Artifact artifact, final CallContext context) {
teichmann@8202: log.debug("FixReferenceEventsFacet.getData");
raimund@3035:
teichmann@6868: if (!(artifact instanceof D4EArtifact)) {
teichmann@8202: log.debug("Not an instance of FixationArtifact.");
raimund@3035: return null;
raimund@3035: }
teichmann@6868:
gernotbelger@9348: final D4EArtifact flys = (D4EArtifact) artifact;
teichmann@6868:
gernotbelger@9348: final CalculationResult res = (CalculationResult) flys.compute(context, ComputeType.ADVANCE, false);
teichmann@6868:
gernotbelger@9348: final FixResult result = (FixResult) res.getData();
gernotbelger@9348: final double currentKm = getCurrentKm(context);
teichmann@6868:
gernotbelger@9360: if (log.isDebugEnabled())
teichmann@8202: log.debug("current km in FRE: " + currentKm);
teichmann@6868:
gernotbelger@9348: /* first search in referenced */
gernotbelger@9360: final KMIndex referenced = result.getFixings();
gernotbelger@9360: return find(referenced, currentKm);
raimund@3035: }
raimund@3035:
gernotbelger@9348: private QWD find(final KMIndex kmQWs, final double currentKm) {
gernotbelger@9348:
gernotbelger@9348: final KMIndex.Entry kmQWsEntry = kmQWs.binarySearch(currentKm);
gernotbelger@9348: if (kmQWsEntry != null) {
gernotbelger@9348: final int ndx = this.index & 255;
gernotbelger@9348: for (final QWD qwd : kmQWsEntry.getValue()) {
gernotbelger@9348: if (qwd.getIndex() == ndx)
gernotbelger@9348: return qwd;
gernotbelger@9348: }
gernotbelger@9348: }
gernotbelger@9348:
gernotbelger@9348: return null;
gernotbelger@9348: }
raimund@3035:
raimund@3035: /**
raimund@3035: * Create a deep copy of this Facet.
gernotbelger@9348: *
raimund@3035: * @return a deep copy.
raimund@3035: */
raimund@3035: @Override
raimund@3035: public FixReferenceEventsFacet deepCopy() {
gernotbelger@9348: final FixReferenceEventsFacet copy = new FixReferenceEventsFacet();
raimund@3035: copy.set(this);
raimund@3035: return copy;
raimund@3035: }
gernotbelger@9348: }