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; 017 018import java.util.Collection; 019import java.util.Date; 020import java.util.List; 021import java.util.Set; 022import java.util.UUID; 023 024import org.apache.log4j.Logger; 025import org.apache.solr.common.SolrException; 026 027import core.tut.pori.dao.SQLSelectBuilder.OrderDirection; 028import core.tut.pori.dao.SolrDAO; 029import core.tut.pori.dao.SolrQueryBuilder; 030import core.tut.pori.dao.filter.AbstractQueryFilter.QueryType; 031import core.tut.pori.dao.filter.AndQueryFilter; 032import core.tut.pori.dao.filter.AndSubQueryFilter; 033import core.tut.pori.dao.filter.RangeQueryFilter; 034import core.tut.pori.http.parameters.DateIntervalParameter.Interval; 035import core.tut.pori.http.parameters.Limits; 036import core.tut.pori.http.parameters.SortOptions; 037import service.tut.pori.apilta.sensors.datatypes.DataPoint; 038import service.tut.pori.apilta.sensors.datatypes.Definitions; 039 040/** 041 * DAO for accessing data points 042 * 043 */ 044public class DataPointDAO extends SolrDAO { 045 private static final String BEAN_ID_SOLR_SERVER = "solrServerSensorsDataPoints"; 046 private static final SortOptions DEFAULT_SORT_OPTIONS; 047 static{ 048 DEFAULT_SORT_OPTIONS = new SortOptions(); 049 DEFAULT_SORT_OPTIONS.addSortOption(new SortOptions.Option(SOLR_FIELD_ID, OrderDirection.ASCENDING, null)); 050 } 051 private static final Logger LOGGER = Logger.getLogger(DataPointDAO.class); 052 053 /** 054 * 055 * @param createdFilter optional filter for created timestamps 056 * @param limits 057 * @param measurementId 058 * @return list of data points or null if none was found 059 */ 060 public List<DataPoint> getDataPoints(Set<Interval> createdFilter, Limits limits, String measurementId) { 061 SolrQueryBuilder solr = new SolrQueryBuilder(); 062 solr.setLimits(limits); 063 solr.setSortOptions(DEFAULT_SORT_OPTIONS); 064 solr.addCustomFilter(new AndQueryFilter(Definitions.SOLR_FIELD_MEASUREMENT_ID, measurementId)); 065 066 if(createdFilter != null && !createdFilter.isEmpty()){ 067 LOGGER.debug("Using created filter..."); 068 AndSubQueryFilter cf = new AndSubQueryFilter(); 069 for(Interval interval : createdFilter){ 070 cf.addFilter(new RangeQueryFilter(SolrDAO.SOLR_FIELD_CREATED, interval.getStart(), interval.getEnd(), QueryType.OR)); 071 } 072 solr.addCustomFilter(cf); 073 } 074 075 return getSolrTemplate(BEAN_ID_SOLR_SERVER).queryForList(solr.toSolrQuery(Definitions.ELEMENT_DATAPOINT_LIST), DataPoint.class); 076 } 077 078 /** 079 * create points for the given measurement id 080 * 081 * @param dataPoints 082 * @param measurementId 083 */ 084 public void createDataPoints(List<DataPoint> dataPoints, String measurementId) { 085 for(DataPoint dp : dataPoints) { // populate id and timestamps 086 dp.setDataPointId(UUID.randomUUID().toString()); 087 dp.setMeasurementId(measurementId); 088 Date created = dp.getCreated(); 089 if(created == null){ 090 LOGGER.debug("No created timestamp, using current timestamp for data point, id: "+dp.getDataPointId()); 091 created = new Date(); 092 dp.setCreated(created); 093 } 094 } 095 096 int status = getSolrTemplate(BEAN_ID_SOLR_SERVER).addBeans(dataPoints).getStatus(); 097 if(status != SolrException.ErrorCode.UNKNOWN.code){ 098 LOGGER.warn("Failed to create data points for measurement, id: "+measurementId+", status: "+status); 099 } 100 } 101 102 /** 103 * 104 * @param measurementIds 105 */ 106 public void deleteDataPoints(Collection<Long> measurementIds) { 107 SolrQueryBuilder solr = new SolrQueryBuilder(); 108 solr.addCustomFilter(new AndQueryFilter(Definitions.SOLR_FIELD_MEASUREMENT_ID, measurementIds)); 109 int status = getSolrTemplate(BEAN_ID_SOLR_SERVER).deleteByQuery(solr.toSolrQuery()).getStatus(); 110 if(status != SolrException.ErrorCode.UNKNOWN.code){ 111 LOGGER.warn("Failed to delete data points, status: "+status); 112 } 113 } 114}