WEPSAction.java [src/nodes] Revision: default  Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package nodes;

import config.ConfigParser;
import config.nodes.Identity;
import csip.api.server.ServiceException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import static utils.Constants.*;

/**
 *
 * @author Brad
 */
public class WEPSAction {
  Identity identity;
  private final Map<String, String> parameters; // param name, param value

  public WEPSAction() {
    parameters = new HashMap<>();
  }

  public WEPSAction(String code, int id) {
    identity = new Identity(code, id);
    parameters = new HashMap<>();
  }

  public Identity getIdentity() {
    return identity;
  }

  public void setIdentity(Identity id) {
    identity = id;
  }

  public String getName() throws ServiceException {
    return ConfigParser.getInstance().getOpDefn().getActionFormat(identity).getActionName();
  }

  public void addParameter(String name, String value) {
    parameters.put(name, value);
  }

  public String getParameter(String param) {
    String value = parameters.get(param);
    if (value == null)
      value = "";
    return value;
  }

  public void parseXMLData(XMLEventReader reader, String cropName, int rowDirection, double residueAmount) throws XMLStreamException {
    XMLEvent event;
    boolean done = false;
    String name = null;
    String value = null;
    StartElement startElement;
    EndElement endElement;

    // start parsing
    while (reader.hasNext() && !done) {
      event = reader.nextEvent();

      switch (event.getEventType()) {
        case XMLStreamConstants.START_ELEMENT:
          startElement = event.asStartElement();
          if (startElement.getName().getLocalPart().equals(IDENTITY)) {
            identity = new Identity();
            identity.parse(reader);
            break;
          }

          if (startElement.getName().getLocalPart().equals(PARAM)) {
            while (reader.hasNext() && !done) {
              event = reader.nextEvent();
              switch (event.getEventType()) {
                case XMLStreamConstants.START_ELEMENT:
                  startElement = event.asStartElement();
                  if (startElement.getName().getLocalPart().equals(NAME)) {
                    name = reader.getElementText();
                    if (name.equals("gcropname") && cropName != null) {
                      value = cropName;
                      done = true;
                    }
                    if (name.equals("odirect")) {
                      value = Integer.toString(rowDirection);
                      done = true;
                    }
                    if (name.equals("rflatmass")) {
                      value = Double.toString(residueAmount * 0.000112085); // from lbs/acre to kg/m^2
                      done = true;
                    }
                    break;
                  }

                  if (startElement.getName().getLocalPart().equals(XML_VALUE)) {
                    value = reader.getElementText();
                    done = true;
                    break;
                  }
              }
            }
            done = false;
          }
          break;
        case XMLStreamConstants.END_ELEMENT:
          endElement = event.asEndElement();
          if (endElement.getName().getLocalPart().equals(PARAM))
            parameters.put(name, value);

          if (endElement.getName().getLocalPart().equals(ACTIONVALUE))
            done = true;

      }
    }
  }

  @Override
  public int hashCode() {
    int hash = 5;
    hash = 71 * hash + Objects.hashCode(this.identity);
    return hash;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    final WEPSAction other = ( WEPSAction ) obj;
    if (!Objects.equals(this.identity, other.identity)) {
      return false;
    }
    return true;
  }

}