001package service.tut.pori.apilta.shock.datatypes;
002
003import java.io.InputStream;
004import java.util.Iterator;
005import java.util.List;
006
007import org.apache.commons.lang3.StringUtils;
008import org.apache.commons.lang3.tuple.Pair;
009import org.apache.log4j.Logger;
010
011import core.tut.pori.http.parameters.HTTPParameter;
012
013/**
014 * For parsing latitude/longitude limit parameters
015 * 
016 * Format:
017 * {@value service.tut.pori.apilta.shock.datatypes.LocationLimits#PARAMETER_DEFAULT_NAME}=lat{@value core.tut.pori.http.Definitions#SEPARATOR_URI_QUERY_TYPE_VALUE}lon{@value core.tut.pori.http.Definitions#SEPARATOR_URI_QUERY_PARAM_VALUES}lat{@value core.tut.pori.http.Definitions#SEPARATOR_URI_QUERY_TYPE_VALUE}lon
018 * 
019 * The decimal separator for latitude and longitude is .
020 * 
021 * The first coordinate is the lower left corner of the bounding box, the second coordinate is the upper right corner.
022 * 
023 */
024public class LocationLimits extends HTTPParameter {
025  /** parameter default name */
026  public static final String PARAMETER_DEFAULT_NAME = "location_limits";
027  private static final Logger LOGGER = Logger.getLogger(LocationLimits.class);
028  private LatLng _lowerLeft = null;
029  private LatLng _upperRight = null;
030
031  @Override
032  public void initialize(List<String> parameterValues) throws IllegalArgumentException {
033    if(parameterValues.size() != 2){
034      throw new IllegalArgumentException("Two parameters are required.");
035    }
036    
037    try {
038    Iterator<String> iter = parameterValues.iterator();
039    String value = iter.next();
040    String[] parts = StringUtils.split(value, core.tut.pori.http.Definitions.SEPARATOR_URI_QUERY_TYPE_VALUE);
041    _lowerLeft = new LatLng(Double.valueOf(parts[0]), Double.valueOf(parts[1]));
042    
043    value = iter.next();
044    parts = StringUtils.split(value, core.tut.pori.http.Definitions.SEPARATOR_URI_QUERY_TYPE_VALUE);
045    _upperRight = new LatLng(Double.valueOf(parts[0]), Double.valueOf(parts[1]));
046    
047    } catch (NumberFormatException ex) {
048      LOGGER.debug(ex, ex);
049      throw new IllegalArgumentException("Invalid value for parameter: "+getParameterName());
050    }
051  }
052
053  @Override
054  public void initialize(String parameterValue) throws IllegalArgumentException {
055    throw new IllegalArgumentException("Two parameters are required.");
056  }
057
058  @Override
059  public boolean hasValues() {
060    return (_lowerLeft != null && _upperRight != null);
061  }
062
063  /**
064   * Return Pair of lower left/upper right coordinates
065   * 
066   * @see #getLowerLeft()
067   * @see #getUpperRight()
068   */
069  @Override
070  public Pair<LatLng, LatLng> getValue() {
071    if(hasValues()){
072      return Pair.of(getLowerLeft(), getUpperRight());
073    }else{
074      return null;
075    }
076  }
077  
078  /**
079   * 
080   * @return lower left corner bounding box coordinate
081   */
082  public LatLng getLowerLeft() {
083    return _lowerLeft;
084  }
085  
086  /**
087   * 
088   * @return upper right corner bounding box coordinate
089   */
090  public LatLng getUpperRight() {
091    return _upperRight;
092  }
093  
094  @Override
095  public void initialize(InputStream parameterValue) throws UnsupportedOperationException {
096    throw new UnsupportedOperationException("The use of HTTP Body is not implemented for this parameter.");
097  }
098  
099  /**
100   * 
101   * 
102   */
103  public static class LatLng {
104    private Double _latitude = null;
105    private Double _longitude = null;
106    
107    /**
108     * 
109     * @param latitude
110     * @param longitude
111     */
112    public LatLng(Double latitude, Double longitude) {
113      _latitude = latitude;
114      _longitude = longitude;
115    }
116
117    /**
118     * @return the latitude
119     */
120    public Double getLatitude() {
121      return _latitude;
122    }
123
124    /**
125     * @return the longitude
126     */
127    public Double getLongitude() {
128      return _longitude;
129    }
130  } // class LatLng
131}