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.sq; ingo@3428: andre@8620: import java.util.Date; andre@8620: import java.util.List; andre@8621: import java.util.TreeSet; andre@8620: andre@8620: import org.w3c.dom.Element; andre@8620: teichmann@5831: import org.dive4elements.river.artifacts.states.PeriodsSelect; andre@8620: import org.dive4elements.river.artifacts.access.RangeAccess; andre@8620: import org.dive4elements.river.artifacts.D4EArtifact; andre@8620: import org.dive4elements.river.artifacts.model.SQOverview; andre@8620: import org.dive4elements.river.artifacts.model.SQOverviewFactory; andre@8620: andre@8620: import org.dive4elements.river.utils.KMIndex; andre@8620: andre@8620: import org.dive4elements.artifacts.common.utils.XMLUtils; andre@8620: import org.dive4elements.artifacts.Artifact; andre@8620: import org.dive4elements.artifacts.CallContext; andre@8620: andre@8622: import org.dive4elements.river.backend.utils.EpsilonComparator; ingo@3428: ingo@3428: public class SQPeriodSelect extends PeriodsSelect { ingo@3428: ingo@3428: public static final String UI_PROVIDER = "sq.period.select"; ingo@3428: ingo@3428: private static final long serialVersionUID = 1L; ingo@3428: andre@8620: /** Get either the start date of the data or the end. */ andre@8622: protected Long getDataMinMaxDate(Artifact artifact, boolean minDate) { andre@8620: D4EArtifact arti = (D4EArtifact) artifact; andre@8620: RangeAccess access = new RangeAccess(arti); andre@8620: double km = access.getLocations()[0]; andre@8620: tom@8856: SQOverview overview = SQOverviewFactory.getOverview( tom@8856: access.getRiverName()); andre@8620: andre@8622: /* Filter is not implemented and only checks if a complete andre@8622: * KMIndex list is acceptable or not. So KMFiltering wont work */ andre@8622: KMIndex> entries = overview.filter(SQOverview.ACCEPT); andre@8621: TreeSet allDates = new TreeSet(); andre@8621: andre@8621: for (int i = 0; i < entries.size(); i++) { tom@8856: if (EpsilonComparator.CMP.compare(entries.get(i).getKm(), km) tom@8856: == 0 tom@8856: ) { andre@8622: allDates.addAll(entries.get(i).getValue()); andre@8622: } andre@8622: } andre@8622: if (allDates.size() < 2) { andre@8622: return null; andre@8620: } andre@8620: tom@8856: return minDate tom@8856: ? allDates.first().getTime() tom@8856: : allDates.last().getTime(); andre@8620: } andre@8620: andre@8620: @Override andre@8620: protected Element[] createItems( andre@8620: XMLUtils.ElementCreator cr, andre@8620: Artifact artifact, andre@8620: String name, andre@8620: CallContext context) andre@8620: { andre@8620: if (!name.equals("start") && !name.equals("end")) { andre@8620: return null; andre@8620: } andre@8622: Long value = getDataMinMaxDate(artifact, name.equals("start")); andre@8622: if (value == null) { andre@8622: return null; andre@8622: } andre@8620: Element def = createItem( andre@8620: cr, andre@8622: new String[] {"default", value.toString()}); andre@8620: andre@8620: return new Element[] { def }; andre@8620: } ingo@3428: ingo@3428: @Override ingo@3428: public String getUIProvider() { ingo@3428: return UI_PROVIDER; ingo@3428: } ingo@3428: } ingo@3428: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :