Просмотр исходного кода

Added service for listing all sensor types

Michal Kepka 4 лет назад
Родитель
Сommit
80ff660365

+ 205 - 198
src/main/java/cz/hsrs/db/model/Sensor.java

@@ -1,199 +1,206 @@
-package cz.hsrs.db.model;
-
-import java.sql.ResultSet;
-import java.sql.SQLException;
-
-import cz.hsrs.db.DBObject;
-import cz.hsrs.db.util.SensorUtil;
-
-public class Sensor implements DBObject {
-
-    private long sensorId;
-    private String sensorName;
-    private String sensorType;
-    private Phenomenon phenomenon;
-
-    /**
-     * Constructor creates object with all attributes and Phenomenon object
-     * @param sensorId
-     * @param sensorName
-     * @param sensorType
-     * @param phenomenon
-     */
-    public Sensor(long sensorId, String sensorName, String sensorType, Phenomenon phenomenon) {
-        super();
-        this.sensorId = sensorId;
-        this.sensorName = sensorName;
-        this.sensorType = sensorType;
-        this.phenomenon = phenomenon;
-    }
-    
-    /**
-     * Empty constructor for serialization
-     */
-    public Sensor(){
-    }
-    
-    /**
-     * Constructor for inserting new Sensor to DB
-     * @param sensorName
-     * @param sensorType
-     * @param phenomenon
-     */
-    public Sensor(String sensorName, String sensorType, Phenomenon phenomenon) {
-        super();
-        this.sensorName = sensorName;
-        this.sensorType = sensorType;
-        this.phenomenon = phenomenon;
-    }
-
-    @Override
-    public DBObject getDBObject(ResultSet set) throws SQLException {
-        this.sensorId = set.getLong("sensor_id");
-        this.sensorName = set.getString("sensor_name");
-        this.sensorType = set.getString("sensor_type");
-        SensorUtil sUtil = new SensorUtil();
-        this.phenomenon = sUtil.getPhenomenonById(set.getString("phenomenon_id"));
-        return this;
-    }
-
-    public long getSensorId() {
-        return sensorId;
-    }
-    public void internalSetSensorId(Long id){
-        this.sensorId = id;
-    }
-
-    public String getSensorName() {
-        return sensorName;
-    }
-
-    public String getSensorType() {
-        return sensorType;
-    }
-
-    public Phenomenon getPhenomenon() {
-        return phenomenon;
-    }
-    
-    @Override
-    public String toString() {
-        return "[sensorId=" + sensorId + ", sensorName=" + sensorName
-                + ", sensorType=" + sensorType + ", phenomenon=" + phenomenon
-                + "]";
-    }
-
-    /**
-     * Method to insert new Sensor to DB
-     * @return Sensor instance with generated ID
-     * @throws SQLException if an error occurs during inserting
-     * @throws NoItemFoundException 
-     */
-    public Sensor insertToDb(Long unitId) throws SQLException, NoItemFoundException {
-        SensorUtil sUtil = new SensorUtil();
-        if(this.sensorId != 0){
-            Sensor senDB = sUtil.getSensorByIdOrName(sensorId, null);
-            if(this.sensorName == null && this.sensorType == null && this.phenomenon == null){
-                if(senDB == null){
-                    throw new NoItemFoundException("Sensor with given ID="+this.sensorId+" was not found!");
-                }
-                else{
-                    // there is sensor with same ID in DB
-                    if(sUtil.isSensorPairedToUnit(sensorId, unitId) == false){
-                        sUtil.pairUnitToSensor(unitId, sensorId);
-                    }
-                    return senDB;
-                }
-            }
-            else if(this.sensorName != null && this.sensorType != null && this.phenomenon != null){
-                if(senDB == null){
-                    // insert new sensor + new phenomenon
-                    this.phenomenon = this.phenomenon.insertToDb();
-                    int i = sUtil.insertSensor(this);
-                    if (i == 1){
-                        sUtil.pairUnitToSensor(unitId, this.sensorId);
-                        return this;
-                    }
-                    else{
-                        throw new SQLException("Sensor was not inserted!");
-                    }
-                }
-                else{
-                    if(senDB.getSensorName().equalsIgnoreCase(sensorName) == true){
-                        if(senDB.getSensorType().equalsIgnoreCase(sensorType) == true && this.phenomenon.internalGetPhenomenonId() != null){
-                            if(senDB.getPhenomenon().internalGetPhenomenonId().equalsIgnoreCase(this.phenomenon.internalGetPhenomenonId())){
-                                // there is sensor with same name, type and phenomenon Id in DB
-                                if(sUtil.isSensorPairedToUnit(sensorId, unitId) == false){
-                                    sUtil.pairUnitToSensor(unitId, sensorId);
-                                }
-                                return senDB;
-                            }
-                            else{
-                                throw new SQLException("It is not possible to insert Sensor with given attributes!");
-                            }
-                        }
-                        else{
-                            throw new SQLException("It is not possible to insert Sensor with given attributes!");
-                        }
-                    }
-                    else if(senDB.getSensorName().equalsIgnoreCase(sensorName)==false && senDB.getSensorType().equalsIgnoreCase(sensorType) == false){
-                        throw new SQLException("It is not possible to insert Sensor with given attributes! There is Sensor with same ID.");
-                    }
-                    else{
-                        throw new SQLException("It is not possible to insert Sensor with given attributes!");
-                    }
-                }
-            }
-            else{
-                throw new SQLException("It is not possible to insert Sensor with given attributes!");
-            }
-        }
-        else{
-            Sensor senDB = sUtil.getSensorByIdOrName(null, sensorName);
-            if(senDB == null){
-                // new ID + insert new sensor + insert phenomenon
-                this.phenomenon = this.phenomenon.insertToDb();
-                this.sensorId = sUtil.getNextSensorId();
-                int i = sUtil.insertSensor(this);
-                if (i == 1){
-                    sUtil.pairUnitToSensor(unitId, this.sensorId);
-                    return this;
-                }
-                else{
-                    throw new SQLException("Sensor was not inserted!");
-                }
-            }
-            else{
-                if(this.sensorType != null && this.phenomenon != null){
-                // check if there is same phenomenon in DB
-                    if(this.phenomenon.getPhenomenonName() != null && this.phenomenon.getUnit() != null){
-                        Phenomenon phenDB = sUtil.getPhenomenonByName(this.phenomenon.getPhenomenonName());
-                        if(phenDB != null){
-                            if(this.phenomenon.getUnit().equalsIgnoreCase(phenDB.getUnit())){
-                                this.phenomenon = phenDB;
-                            }
-                        }
-                    }
-                    if(senDB.getSensorType().equalsIgnoreCase(this.sensorType)==true && this.getPhenomenon().internalGetPhenomenonId() != null){
-                        if(senDB.getPhenomenon().internalGetPhenomenonId().equalsIgnoreCase(this.phenomenon.internalGetPhenomenonId())){
-                            // there is sensor with same name and type in DB
-                            if(sUtil.isSensorPairedToUnit(senDB.getSensorId(), unitId) == false){
-                                sUtil.pairUnitToSensor(unitId, senDB.getSensorId());
-                            }
-                            return senDB;
-                        }
-                        else{
-                            throw new SQLException("It is not possible to insert Sensor with given attributes!");
-                        }
-                    }
-                    else{
-                        throw new SQLException("It is not possible to insert Sensor with given attributes!");
-                    }
-                }
-                else{
-                    throw new SQLException("It is not possible to insert Sensor with given attributes!");
-                }
-            }
-        }
-    }
+package cz.hsrs.db.model;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import cz.hsrs.db.DBObject;
+import cz.hsrs.db.util.SensorUtil;
+
+public class Sensor implements DBObject {
+
+    private long sensorId;
+    private String sensorName;
+    private String sensorType;
+    private Phenomenon phenomenon;
+
+    /**
+     * Constructor creates object with all attributes and Phenomenon object
+     * @param sensorId
+     * @param sensorName
+     * @param sensorType
+     * @param phenomenon
+     */
+    public Sensor(long sensorId, String sensorName, String sensorType, Phenomenon phenomenon) {
+        super();
+        this.sensorId = sensorId;
+        this.sensorName = sensorName;
+        this.sensorType = sensorType;
+        this.phenomenon = phenomenon;
+    }
+    
+    /**
+     * Empty constructor for serialization
+     */
+    public Sensor(){
+    }
+    
+    /**
+     * Constructor for SensorType lists
+     */
+    public Sensor(String sensorType){
+    	this.sensorType = sensorType;
+    }
+    
+    /**
+     * Constructor for inserting new Sensor to DB
+     * @param sensorName
+     * @param sensorType
+     * @param phenomenon
+     */
+    public Sensor(String sensorName, String sensorType, Phenomenon phenomenon) {
+        super();
+        this.sensorName = sensorName;
+        this.sensorType = sensorType;
+        this.phenomenon = phenomenon;
+    }
+
+    @Override
+    public DBObject getDBObject(ResultSet set) throws SQLException {
+        this.sensorId = set.getLong("sensor_id");
+        this.sensorName = set.getString("sensor_name");
+        this.sensorType = set.getString("sensor_type");
+        SensorUtil sUtil = new SensorUtil();
+        this.phenomenon = sUtil.getPhenomenonById(set.getString("phenomenon_id"));
+        return this;
+    }
+
+    public long getSensorId() {
+        return sensorId;
+    }
+    public void internalSetSensorId(Long id){
+        this.sensorId = id;
+    }
+
+    public String getSensorName() {
+        return sensorName;
+    }
+
+    public String getSensorType() {
+        return sensorType;
+    }
+
+    public Phenomenon getPhenomenon() {
+        return phenomenon;
+    }
+    
+    @Override
+    public String toString() {
+        return "[sensorId=" + sensorId + ", sensorName=" + sensorName
+                + ", sensorType=" + sensorType + ", phenomenon=" + phenomenon
+                + "]";
+    }
+
+    /**
+     * Method to insert new Sensor to DB
+     * @return Sensor instance with generated ID
+     * @throws SQLException if an error occurs during inserting
+     * @throws NoItemFoundException 
+     */
+    public Sensor insertToDb(Long unitId) throws SQLException, NoItemFoundException {
+        SensorUtil sUtil = new SensorUtil();
+        if(this.sensorId != 0){
+            Sensor senDB = sUtil.getSensorByIdOrName(sensorId, null);
+            if(this.sensorName == null && this.sensorType == null && this.phenomenon == null){
+                if(senDB == null){
+                    throw new NoItemFoundException("Sensor with given ID="+this.sensorId+" was not found!");
+                }
+                else{
+                    // there is sensor with same ID in DB
+                    if(sUtil.isSensorPairedToUnit(sensorId, unitId) == false){
+                        sUtil.pairUnitToSensor(unitId, sensorId);
+                    }
+                    return senDB;
+                }
+            }
+            else if(this.sensorName != null && this.sensorType != null && this.phenomenon != null){
+                if(senDB == null){
+                    // insert new sensor + new phenomenon
+                    this.phenomenon = this.phenomenon.insertToDb();
+                    int i = sUtil.insertSensor(this);
+                    if (i == 1){
+                        sUtil.pairUnitToSensor(unitId, this.sensorId);
+                        return this;
+                    }
+                    else{
+                        throw new SQLException("Sensor was not inserted!");
+                    }
+                }
+                else{
+                    if(senDB.getSensorName().equalsIgnoreCase(sensorName) == true){
+                        if(senDB.getSensorType().equalsIgnoreCase(sensorType) == true && this.phenomenon.internalGetPhenomenonId() != null){
+                            if(senDB.getPhenomenon().internalGetPhenomenonId().equalsIgnoreCase(this.phenomenon.internalGetPhenomenonId())){
+                                // there is sensor with same name, type and phenomenon Id in DB
+                                if(sUtil.isSensorPairedToUnit(sensorId, unitId) == false){
+                                    sUtil.pairUnitToSensor(unitId, sensorId);
+                                }
+                                return senDB;
+                            }
+                            else{
+                                throw new SQLException("It is not possible to insert Sensor with given attributes!");
+                            }
+                        }
+                        else{
+                            throw new SQLException("It is not possible to insert Sensor with given attributes!");
+                        }
+                    }
+                    else if(senDB.getSensorName().equalsIgnoreCase(sensorName)==false && senDB.getSensorType().equalsIgnoreCase(sensorType) == false){
+                        throw new SQLException("It is not possible to insert Sensor with given attributes! There is Sensor with same ID.");
+                    }
+                    else{
+                        throw new SQLException("It is not possible to insert Sensor with given attributes!");
+                    }
+                }
+            }
+            else{
+                throw new SQLException("It is not possible to insert Sensor with given attributes!");
+            }
+        }
+        else{
+            Sensor senDB = sUtil.getSensorByIdOrName(null, sensorName);
+            if(senDB == null){
+                // new ID + insert new sensor + insert phenomenon
+                this.phenomenon = this.phenomenon.insertToDb();
+                this.sensorId = sUtil.getNextSensorId();
+                int i = sUtil.insertSensor(this);
+                if (i == 1){
+                    sUtil.pairUnitToSensor(unitId, this.sensorId);
+                    return this;
+                }
+                else{
+                    throw new SQLException("Sensor was not inserted!");
+                }
+            }
+            else{
+                if(this.sensorType != null && this.phenomenon != null){
+                // check if there is same phenomenon in DB
+                    if(this.phenomenon.getPhenomenonName() != null && this.phenomenon.getUnit() != null){
+                        Phenomenon phenDB = sUtil.getPhenomenonByName(this.phenomenon.getPhenomenonName());
+                        if(phenDB != null){
+                            if(this.phenomenon.getUnit().equalsIgnoreCase(phenDB.getUnit())){
+                                this.phenomenon = phenDB;
+                            }
+                        }
+                    }
+                    if(senDB.getSensorType().equalsIgnoreCase(this.sensorType)==true && this.getPhenomenon().internalGetPhenomenonId() != null){
+                        if(senDB.getPhenomenon().internalGetPhenomenonId().equalsIgnoreCase(this.phenomenon.internalGetPhenomenonId())){
+                            // there is sensor with same name and type in DB
+                            if(sUtil.isSensorPairedToUnit(senDB.getSensorId(), unitId) == false){
+                                sUtil.pairUnitToSensor(unitId, senDB.getSensorId());
+                            }
+                            return senDB;
+                        }
+                        else{
+                            throw new SQLException("It is not possible to insert Sensor with given attributes!");
+                        }
+                    }
+                    else{
+                        throw new SQLException("It is not possible to insert Sensor with given attributes!");
+                    }
+                }
+                else{
+                    throw new SQLException("It is not possible to insert Sensor with given attributes!");
+                }
+            }
+        }
+    }
 }

+ 18 - 0
src/main/java/cz/hsrs/db/util/SensorUtil.java

@@ -2,6 +2,7 @@ package cz.hsrs.db.util;
 
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.util.LinkedList;
 import java.util.List;
 
 import cz.hsrs.db.model.Phenomenon;
@@ -69,6 +70,23 @@ public class SensorUtil extends TrackUtil {
         ResultSet res = stmt.executeQuery(queryObservations);
         return (List<Sensor>) generateObjectList(new Sensor(), res);
     }
+    
+    /**
+     * Method selects list of sensor types stored in DB
+     * @return
+     * @throws SQLException
+     */
+    public List<Sensor> getSensorTypes() throws SQLException{
+    	String query = "select distinct(sensor_type) sensor_type from sensors order by sensor_type;";
+    	ResultSet res = stmt.executeQuery(query);
+    	List<Sensor> senTypeList = new LinkedList<Sensor>();
+    	if(res != null) {
+    		while(res.next()) {
+    			senTypeList.add(new Sensor(res.getString("sensor_type")));
+    		}
+    	}
+    	return senTypeList;
+    }
 
     /**
      * Method gets description of sensor by given identifier

+ 5 - 1
src/main/java/cz/hsrs/servlet/provider/SensorService.java

@@ -26,6 +26,7 @@ public class SensorService extends DBServlet {
 
     public static final String GET_SENSORS = "GetSensors";
     public static final String GET_ALL_SENSORS = "GetAllSensors";
+    public static final String GET_ALL_SENSOR_TYPES = "GetAllSensorTypes";
     public static final String GET_ALL_PHENOMENONS = "GetAllPhenomenons";
     public static final String GET_OBSERVATIONS = "GetObservations";
     public static final String GET_LAST_OBSERVATIONS = "GetLastObservations";
@@ -81,7 +82,10 @@ public class SensorService extends DBServlet {
                 /* Get All Sensors */
             } else if (request.getParameter(ServiceParameters.OPERATION).equalsIgnoreCase(GET_ALL_SENSORS)) {
             	DBJsonUtils.writeJSON(out,db.sensorUtil.getSensors());
-            /* GetAllPhenomenons */
+            /* GET All Sensor Types */
+            } else if (request.getParameter(ServiceParameters.OPERATION).equalsIgnoreCase(GET_ALL_SENSOR_TYPES)) {
+            	DBJsonUtils.writeJSON(out,db.sensorUtil.getSensorTypes());
+            	/* GetAllPhenomenons */
             } else if (request.getParameter(ServiceParameters.OPERATION).equalsIgnoreCase(GET_ALL_PHENOMENONS)) {
             	DBJsonUtils.writeJSON(out,db.sensorUtil.getPhenomenons());
             /* GetObservations */

BIN
src/main/webapp/img/logo-sah.png