Mercurial > dive4elements > river
view flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WQSelect.java @ 1970:368040e5c400
Improved the Scheduler to be able to cancel running WSPLGEN jobs.
flys-artifacts/trunk@3384 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Mon, 12 Dec 2011 08:15:12 +0000 |
parents | 8a2cbf947395 |
children | 6762f54b23b1 |
line wrap: on
line source
package de.intevation.flys.artifacts.states; import java.text.NumberFormat; import gnu.trove.TDoubleArrayList; import org.apache.log4j.Logger; import org.w3c.dom.Element; import de.intevation.artifacts.Artifact; import de.intevation.artifacts.CallContext; import de.intevation.artifacts.common.utils.XMLUtils; import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator; import de.intevation.artifactdatabase.ProtocolUtils; import de.intevation.artifactdatabase.data.StateData; import de.intevation.flys.model.Gauge; import de.intevation.flys.model.River; import de.intevation.flys.model.Wst; import de.intevation.flys.artifacts.FLYSArtifact; import de.intevation.flys.artifacts.WINFOArtifact; import de.intevation.flys.artifacts.model.WstFactory; import de.intevation.flys.artifacts.resources.Resources; import de.intevation.flys.utils.FLYSUtils; /** * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> */ public class WQSelect extends DefaultState { /** The logger used in this class. */ private static Logger logger = Logger.getLogger(WQSelect.class); /** The default step width for Qs. */ public static final String DEFAULT_STEP_Q = "50"; /** The default step width for Qs. */ public static final String DEFAULT_STEP_W = "30"; /** The name of the 'mode' field. */ public static final String WQ_MODE = "wq_mode"; /** Them name fo the 'free' field. */ public static final String WQ_FREE = "wq_free"; /** The name of the 'selection' field. */ public static final String WQ_SELECTION = "wq_selection"; /** The name of the 'from' field. */ public static final String WQ_FROM = "wq_from"; /** The name of the 'to' field. */ public static final String WQ_TO = "wq_to"; /** The name of the 'step' field. */ public static final String WQ_STEP = "wq_step"; /** The name of the 'single' field. */ public static final String WQ_SINGLE = "wq_single"; /** * The default constructor that initializes an empty State object. */ public WQSelect() { } @Override protected Element createStaticData( FLYSArtifact flys, ElementCreator creator, CallContext cc, String name, String value, String type ) { if (!name.equals(WQ_SINGLE)) { return super.createStaticData(flys, creator, cc, name, value, type); } String mode = flys.getDataAsString(WQ_MODE); String free = flys.getDataAsString(WQ_FREE); if (mode == null || mode.equals("W") || Boolean.valueOf(free)) { return super.createStaticData(flys, creator, cc, name, value, type); } WINFOArtifact winfo = (WINFOArtifact) flys; Element dataElement = creator.create("data"); creator.addAttr(dataElement, "name", name, true); creator.addAttr(dataElement, "type", type, true); Element itemElement = creator.create("item"); creator.addAttr(itemElement, "value", value, true); creator.addAttr(itemElement, "label", getLabel(winfo, cc, value), true); dataElement.appendChild(itemElement); return dataElement; } protected static String getLabel( WINFOArtifact winfo, CallContext cc, String raw ) { String[] values = raw.split(" "); String label = null; NumberFormat nf = NumberFormat.getInstance( Resources.getLocale(cc.getMeta())); for (String value: values) { try { double v = Double.valueOf(value.trim()); String tmp = nf.format(v); String mv = FLYSUtils.getNamedMainValue(winfo.getGauge(),v); if (mv != null && mv.length() > 0) { String add = mv + ": " + tmp; label = label != null ? label + ", " + add : add; } else { label = label != null ? label + ", " + tmp : tmp; } } catch (NumberFormatException nfe) { // do nothing here } } return label; } @Override protected Element createData( XMLUtils.ElementCreator cr, Artifact artifact, StateData data, CallContext context) { Element select = ProtocolUtils.createArtNode( cr, "select", null, null); cr.addAttr(select, "name", data.getName(), true); Element label = ProtocolUtils.createArtNode( cr, "label", null, null); Element choices = ProtocolUtils.createArtNode( cr, "choices", null, null); label.setTextContent(Resources.getMsg( context.getMeta(), data.getName(), data.getName())); select.appendChild(label); return select; } @Override protected Element[] createItems( XMLUtils.ElementCreator cr, Artifact artifact, String name, CallContext context) { // TODO Insert correct min/max values! double[] minmaxW = determineMinMaxW(artifact); double[] minmaxQ = determineMinMaxQ(artifact); if (name.equals("wq_from")) { Element minW = createItem( cr, new String[] {"minW", new Double(minmaxW[0]).toString()}); Element minQ = createItem( cr, new String[] {"minQ", new Double(minmaxQ[0]).toString()}); return new Element[] { minW, minQ }; } else if (name.equals("wq_to")) { Element maxW = createItem( cr, new String[] {"maxW", new Double(minmaxW[1]).toString()}); Element maxQ = createItem( cr, new String[] {"maxQ", new Double(minmaxQ[1]).toString()}); return new Element[] { maxW, maxQ }; } else { Element stepW = createItem( cr, new String[] {"stepW", DEFAULT_STEP_W}); Element stepQ = createItem( cr, new String[] {"stepQ", DEFAULT_STEP_Q}); return new Element[] { stepW, stepQ }; } } protected Element createItem(XMLUtils.ElementCreator cr, Object obj) { Element item = ProtocolUtils.createArtNode(cr, "item", null, null); Element label = ProtocolUtils.createArtNode(cr, "label", null, null); Element value = ProtocolUtils.createArtNode(cr, "value", null, null); String[] arr = (String[]) obj; label.setTextContent(arr[0]); value.setTextContent(arr[1]); item.appendChild(label); item.appendChild(value); return item; } @Override protected String getUIProvider() { return "wq_panel"; } /** * Determines the min and max W value for the current gauge. If no min and * max values could be determined, this method will return * [Double.MIN_VALUE, Double.MAX_VALUE]. * * @param artifact The FLYSArtifact. * * @return the min and max W values for the current gauge. */ protected double[] determineMinMaxW(Artifact artifact) { logger.debug("WQSelect.determineCurrentGauge"); Gauge gauge = ((WINFOArtifact) artifact).getGauge(); double[] minmaxW = gauge != null ? gauge.determineMinMaxW() : null; double minW = minmaxW != null ? minmaxW[0] : Double.MIN_VALUE; double maxW = minmaxW != null ? minmaxW[1] : Double.MAX_VALUE; return new double[] { minW, maxW }; } /** * Determines the min and max Q value for the current gauge. If no min and * max values could be determined, this method will return * [Double.MIN_VALUE, Double.MAX_VALUE]. * * @param artifact The FLYSArtifact. * * @return the min and max Q values for the current gauge. */ protected double[] determineMinMaxQ(Artifact artifact) { logger.debug("WQSelect.determineMinMaxQ"); WINFOArtifact flysArtifact = (WINFOArtifact) artifact; River river = FLYSUtils.getRiver(flysArtifact); Gauge gauge = flysArtifact.getGauge(); Wst wst = WstFactory.getWst(river); double[] minmaxQ = gauge != null ? wst.determineMinMaxQ(gauge.getRange()) : null; double minQ = minmaxQ != null ? minmaxQ[0] : Double.MIN_VALUE; double maxQ = minmaxQ != null ? minmaxQ[1] : Double.MAX_VALUE; return new double[] { minQ, maxQ }; } @Override public boolean validate(Artifact artifact) throws IllegalArgumentException { logger.debug("WQSelect.validate"); WINFOArtifact flys = (WINFOArtifact) artifact; StateData data = getData(flys, WQ_SELECTION); String selectionMode = data != null ? (String) data.getValue() : null; if (selectionMode == null || selectionMode.equals("single")) { return validateSingle(artifact); } else { return validateRange(artifact); } } protected boolean validateBounds( double fromValid, double toValid, double from, double to, double step) throws IllegalArgumentException { logger.debug("RangeState.validateRange"); if (from < fromValid) { logger.error( "Invalid 'from'. " + from + " is smaller than " + fromValid); throw new IllegalArgumentException("error_feed_from_out_of_range"); } else if (to > toValid) { logger.error( "Invalid 'to'. " + to + " is bigger than " + toValid); throw new IllegalArgumentException("error_feed_to_out_of_range"); } return true; } protected boolean validateSingle(Artifact artifact) throws IllegalArgumentException { logger.debug("WQSelect.validateSingle"); WINFOArtifact flys = (WINFOArtifact) artifact; StateData data = getData(flys, WQ_SINGLE); String tmp = data != null ? (String) data.getValue() : null; if (tmp == null || tmp.length() == 0) { throw new IllegalArgumentException("error_empty_state"); } String[] strValues = tmp.split(" "); TDoubleArrayList all = new TDoubleArrayList(); for (String strValue: strValues) { try { all.add(Double.parseDouble(strValue)); } catch (NumberFormatException nfe) { logger.warn(nfe, nfe); } } all.sort(); StateData dMode = getData(flys, WQ_MODE); String mode = dMode != null ? (String) data.getValue() : null; logger.debug("WQ Mode: " + mode); double[] minmax = null; if (mode != null && mode.trim().toLowerCase().equals("w")) { minmax = determineMinMaxW(artifact); } else { minmax = determineMinMaxQ(artifact); } double min = all.get(0); double max = all.get(all.size()-1); logger.debug("Inserted min value = " + min); logger.debug("Inserted max value = " + max); return validateBounds(minmax[0], minmax[1], min, max, 0d); } protected boolean validateRange(Artifact artifact) throws IllegalArgumentException { logger.debug("WQSelect.validateRange"); WINFOArtifact flys = (WINFOArtifact) artifact; StateData data = flys.getData(WQ_MODE); String mode = data != null ? (String) data.getValue() : null; logger.debug("WQ Mode: " + mode); if (mode == null || mode.length() == 0) { throw new IllegalArgumentException("error_feed_invalid_wq_mode"); } StateData dFrom = flys.getData(WQ_FROM); StateData dTo = flys.getData(WQ_TO); StateData dStep = flys.getData(WQ_STEP); String fromStr = dFrom != null ? (String) dFrom.getValue() : null; String toStr = dTo != null ? (String) dTo.getValue() : null; String stepStr = dStep != null ? (String) dStep.getValue() : null; if (fromStr == null || toStr == null || stepStr == null) { throw new IllegalArgumentException("error_empty_state"); } try { double from = Double.parseDouble(fromStr); double to = Double.parseDouble(toStr); double step = Double.parseDouble(stepStr); if (mode != null && mode.trim().toLowerCase().equals("w")) { return validateW(artifact, from, to, step); } else if (mode != null && mode.trim().toLowerCase().equals("q")) { return validateQ(artifact, from, to, step); } else { throw new IllegalArgumentException( "error_feed_invalid_wq_mode"); } } catch (NumberFormatException nfe) { throw new IllegalArgumentException("error_feed_number_format"); } } /** * Validates the inserted W values. * * @param artifact The owner artifact. * @param from The lower value of the W range. * @param to The upper value of the W range. * @param step The step width. * * @return true, if everything was fine, otherwise an exception is thrown. */ protected boolean validateW( Artifact artifact, double from, double to, double step) throws IllegalArgumentException { logger.debug("WQSelect.validateW"); double[] minmaxW = determineMinMaxW(artifact); return validateBounds(minmaxW[0], minmaxW[1], from, to, step); } /** * Validates the inserted Q values. * * @param artifact The owner artifact. * @param from The lower value of the Q range. * @param to The upper value of the Q range. * @param step The step width. * * @return true, if everything was fine, otherwise an exception is thrown. */ protected boolean validateQ( Artifact artifact, double from, double to, double step) throws IllegalArgumentException { logger.debug("WQSelect.validateQ"); double[] minmaxQ = determineMinMaxQ(artifact); return validateBounds(minmaxQ[0], minmaxQ[1], from, to, step); } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :