ingo@109: package de.intevation.flys.artifacts.states;
ingo@109:
ingo@115: import java.util.List;
ingo@324: import java.util.Map;
ingo@115:
ingo@109: import org.apache.log4j.Logger;
ingo@109:
ingo@115: import org.w3c.dom.Element;
ingo@109: import org.w3c.dom.Node;
ingo@109:
ingo@313: import de.intevation.artifacts.Artifact;
ingo@109: import de.intevation.artifacts.CallContext;
ingo@109:
ingo@115: import de.intevation.artifacts.common.utils.XMLUtils;
ingo@115:
ingo@115: import de.intevation.artifactdatabase.ProtocolUtils;
ingo@115: import de.intevation.artifactdatabase.data.StateData;
ingo@109:
sascha@132: import de.intevation.flys.model.River;
sascha@132:
ingo@115: import de.intevation.flys.artifacts.model.RiverFactory;
ingo@126: import de.intevation.flys.artifacts.resources.Resources;
ingo@115:
ingo@109:
ingo@109: /**
ingo@109: * @author Ingo Weinzierl
ingo@109: */
ingo@126: public class RiverSelect extends DefaultState {
ingo@109:
ingo@109: /** The logger used in this class. */
ingo@109: private static Logger logger = Logger.getLogger(RiverSelect.class);
ingo@109:
ingo@324:
ingo@324: /** Error message that is thrown if no river was found based on a given
ingo@324: * name.*/
ingo@324: public static final String ERROR_NO_SUCH_RIVER =
ingo@324: "error_feed_no_such_river";
ingo@324:
ingo@324: /** Error message that is thrown if no river was found based on a given
ingo@324: * name.*/
ingo@324: public static final String ERROR_NO_RIVER_SELECTED =
ingo@324: "error_feed_no_river_selected";
ingo@324:
ingo@324:
ingo@109: /**
ingo@109: * The default constructor that initializes an empty State object.
ingo@109: */
ingo@109: public RiverSelect() {
ingo@109: }
ingo@109:
ingo@109:
ingo@109: /**
ingo@109: * Initialize the state based on the state node in the configuration.
ingo@109: *
ingo@109: * @param config The state configuration node.
ingo@109: */
ingo@109: public void setup(Node config) {
ingo@109: super.setup(config);
ingo@109: }
ingo@109:
ingo@109:
ingo@126: protected Element createData(
ingo@126: XMLUtils.ElementCreator cr,
ingo@313: Artifact artifact,
ingo@126: StateData data,
ingo@126: CallContext context)
ingo@109: {
ingo@126: Element select = ProtocolUtils.createArtNode(
ingo@126: cr, "select",
ingo@126: new String[] { "uiprovider" },
ingo@126: new String[] { "select_with_map" });
ingo@304: cr.addAttr(select, "name", data.getName(), true);
ingo@115:
ingo@126: Element label = ProtocolUtils.createArtNode(
ingo@126: cr, "label", null, null);
ingo@115:
ingo@126: Element choices = ProtocolUtils.createArtNode(
ingo@126: cr, "choices", null, null);
ingo@115:
ingo@304: select.appendChild(label);
ingo@304:
ingo@126: label.setTextContent(Resources.getMsg(
ingo@126: context.getMeta(),
ingo@126: getID(),
ingo@126: getID()));
ingo@115:
ingo@126: return select;
ingo@126: }
ingo@115:
ingo@126:
ingo@126: protected Element[] createItems(
ingo@126: XMLUtils.ElementCreator cr,
ingo@313: Artifact artifact,
ingo@126: String name,
ingo@126: CallContext context)
ingo@126: {
ingo@126: List rivers = RiverFactory.getRivers();
ingo@126: Element[] items = new Element[rivers.size()];
ingo@126:
ingo@126: int idx = 0;
ingo@126: for (River river: rivers) {
ingo@126: items[idx++] = createRiverItem(cr, river);
ingo@115: }
ingo@115:
ingo@126: return items;
ingo@115: }
ingo@115:
ingo@115:
ingo@115: /**
ingo@115: * This method creates a node that represents a river item. This node
ingo@115: * contains the label and the value that describe the river.
ingo@115: *
ingo@115: * @param cr The ElementCreator.
ingo@115: * @param river The river.
ingo@115: *
ingo@115: * @return the element that contains the information about the river.
ingo@115: */
ingo@115: protected Element createRiverItem(XMLUtils.ElementCreator cr, River river) {
ingo@115: Element item = ProtocolUtils.createArtNode(cr, "item", null, null);
ingo@115: Element label = ProtocolUtils.createArtNode(cr, "label", null, null);
ingo@115: Element value = ProtocolUtils.createArtNode(cr, "value", null, null);
ingo@115:
ingo@115: label.setTextContent(river.getName());
ingo@115: value.setTextContent(river.getName());
ingo@115:
ingo@115: item.appendChild(label);
ingo@115: item.appendChild(value);
ingo@115:
ingo@115: return item;
ingo@109: }
ingo@324:
ingo@324:
ingo@324: @Override
ingo@324: public boolean validate(Artifact artifact, CallContext context)
ingo@324: throws IllegalArgumentException
ingo@324: {
ingo@324: logger.debug("RiverSelect.validate");
ingo@324:
ingo@324: Map data = getData();
ingo@324:
ingo@324: StateData dRiver = data.get("river");
ingo@324:
ingo@324: if (dRiver == null || dRiver.getValue() == null) {
ingo@324: throw new IllegalArgumentException(ERROR_NO_RIVER_SELECTED);
ingo@324: }
ingo@324:
ingo@324: River river = RiverFactory.getRiver((String) dRiver.getValue());
ingo@324:
ingo@324: if (river == null) {
ingo@324: throw new IllegalArgumentException(ERROR_NO_SUCH_RIVER);
ingo@324: }
ingo@324:
ingo@324: return true;
ingo@324: }
ingo@424:
ingo@424:
ingo@424: @Override
ingo@424: protected String getUIProvider() {
ingo@424: return "river_panel";
ingo@424: }
ingo@109: }
ingo@109: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :