001/**
002 * Copyright 2016 Tampere University of Technology, Pori Department
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package service.tut.pori.apilta.sensors.datatypes;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlElement;
024import javax.xml.bind.annotation.XmlElementRef;
025import javax.xml.bind.annotation.XmlElementWrapper;
026import javax.xml.bind.annotation.XmlRootElement;
027
028import org.apache.log4j.Logger;
029
030/**
031 * representation of measurement data
032 * 
033 */
034@XmlRootElement(name=Definitions.ELEMENT_MEASUREMENT)
035@XmlAccessorType(XmlAccessType.NONE)
036public class Measurement {
037  private static final Logger LOGGER = Logger.getLogger(Measurement.class);
038  @XmlElement(name = Definitions.ELEMENT_BACKEND_ID)
039  private Long _backendId = null;
040  @XmlElementWrapper(name = Definitions.ELEMENT_DATAPOINT_LIST)
041  @XmlElementRef(type = DataPoint.class)
042  private List<DataPoint> _dataPoints = null;
043  @XmlElement(name = Definitions.ELEMENT_MEASUREMENT_ID)
044  private String _measurementId = null; // generated id
045  
046  /**
047   * @return the backendId
048   * @see #setBackendId(Long)
049   */
050  public Long getBackendId() {
051    return _backendId;
052  }
053
054  /**
055   * @param backendId the backendId to set
056   * @see #getBackendId()
057   */
058  public void setBackendId(Long backendId) {
059    _backendId = backendId;
060  }
061
062  /**
063   * @return the data points
064   * @see #setDataPoints(List)
065   */
066  public List<DataPoint> getDataPoints() {
067    return _dataPoints;
068  }
069
070  /**
071   * @param dataPoints the dataPoints to set
072   * @see #getDataPoints()
073   */
074  public void setDataPoints(List<DataPoint> dataPoints) {
075    _dataPoints = dataPoints;
076  }
077
078  /**
079   * @return the measurementId
080   * @see #setMeasurementId(String)
081   */
082  public String getMeasurementId() {
083    return _measurementId;
084  }
085  
086  /**
087   * @param measurementId the measurementId to set
088   * @see #getMeasurementId()
089   */
090  public void setMeasurementId(String measurementId) {
091    _measurementId = measurementId;
092  }
093  
094  /**
095   * for sub-classing, use the static
096   * 
097   * @return true if valid
098   * @see #isValid(Measurement)
099   */
100  protected boolean isValid() {
101    if(_backendId == null) {
102      LOGGER.debug("Invalid back end id.");
103      return false;
104    }
105    
106    if(_dataPoints == null || _dataPoints.isEmpty()) {
107      LOGGER.debug("No data points.");
108      return false;
109    }
110    
111    for(DataPoint dp : _dataPoints){
112      if(!DataPoint.isValid(dp)){
113        LOGGER.debug("Measurement for back end, id: "+_backendId+" contained an invalid data point.");
114        return false;
115      }
116    }
117    
118    return true;
119  }
120
121  /**
122   * 
123   * @param measurement
124   * @return false if the measurement is null or invalid
125   */
126  public static boolean isValid(Measurement measurement) {
127    if(measurement == null){
128      return false;
129    }else{
130      return measurement.isValid();
131    }
132  }
133  
134  /**
135   * 
136   * @param dataPoint
137   * @see #getDataPoints()
138   */
139  public void addDataPoint(DataPoint dataPoint){
140    if(_dataPoints == null){
141      _dataPoints = new ArrayList<>();
142    }
143    _dataPoints.add(dataPoint);
144  }
145}