ingo@2191: package de.intevation.flys.artifacts.states;
ingo@2191: 
ingo@4174: import java.util.Collections;
ingo@2191: import java.util.List;
ingo@2191: 
ingo@2191: import org.w3c.dom.Element;
ingo@2191: 
ingo@2191: import de.intevation.artifacts.Artifact;
ingo@2191: import de.intevation.artifacts.CallContext;
ingo@2191: 
ingo@2191: import de.intevation.artifacts.common.utils.XMLUtils;
ingo@2191: 
ingo@2191: import de.intevation.flys.model.Gauge;
ingo@2191: import de.intevation.flys.model.River;
ingo@2191: 
ingo@2191: import de.intevation.flys.artifacts.FLYSArtifact;
ingo@2191: import de.intevation.flys.utils.FLYSUtils;
ingo@2191: 
ingo@2191: 
ingo@2191: /**
ingo@2191:  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
ingo@2191:  */
ingo@2191: public class ReferenceGaugeState extends DefaultState {
ingo@2191: 
ingo@2207:     public static final String DATA_NAME = "reference_gauge";
ingo@2207: 
ingo@2207: 
ingo@2191:     @Override
ingo@2191:     protected Element[] createItems(
ingo@2191:         XMLUtils.ElementCreator cr,
ingo@2191:         Artifact    artifact,
ingo@2191:         String      name,
ingo@2191:         CallContext context)
ingo@2191:     {
ingo@2191:         River       river   = FLYSUtils.getRiver((FLYSArtifact) artifact);
ingo@2191:         List<Gauge> gauges  = river.getGauges();
felix@4433:         Collections.sort(gauges);
ingo@2191: 
ingo@2191:         int num = gauges != null ? gauges.size() : 0;
ingo@2191: 
ingo@2191:         Element[] opts = new Element[num];
ingo@2191: 
ingo@2191:         for (int i = 0; i < num; i++ ) {
ingo@2191:             Gauge g = gauges.get(i);
ingo@2191: 
ingo@2191:             String gaugeName      = g.getName();
ingo@2191:             long   officialNumber = g.getOfficialNumber();
ingo@2191: 
ingo@2191:             opts[i] = createItem(
ingo@2191:                 cr, new String[] { gaugeName, String.valueOf(officialNumber) });
ingo@2191:         }
ingo@2191: 
ingo@2191:         return opts;
ingo@2191:     }
ingo@2207: 
ingo@2207: 
ingo@2207:     @Override
ingo@2207:     protected String getLabelFor(
ingo@2207:         CallContext cc,
ingo@2207:         String      name,
ingo@2207:         String      value,
ingo@2207:         String      type
ingo@2207:     ) {
ingo@2207:         if (name.equals(DATA_NAME)) {
ingo@2207:             try {
ingo@2207:                 long  number = Long.valueOf(value);
ingo@2207:                 Gauge gauge  = Gauge.getGaugeByOfficialNumber(number);
ingo@2207: 
ingo@2207:                 if (gauge != null) {
ingo@2207:                     return gauge.getName();
ingo@2207:                 }
ingo@2207:             }
ingo@2207:             catch (NumberFormatException nfe) {
ingo@2207:                 // do nothing
ingo@2207:             }
ingo@2207:         }
ingo@2207: 
ingo@2207:         return super.getLabelFor(cc, name, value, type);
ingo@2207:     }
ingo@2191: }
ingo@2191: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :