Przeglądaj źródła

Fixing Jetty, adding filtering for Export

Fixed Jetty issue
Updated service for Observation Export - filtering by unitIds
mkepka 4 lat temu
rodzic
commit
a205adaf14

+ 0 - 67
.gitignore

@@ -1,67 +0,0 @@
-# ---> Java
-*.class
-
-# Mobile Tools for Java (J2ME)
-.mtj.tmp/
-
-# Package Files #
-*.jar
-*.war
-*.ear
-
-# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
-hs_err_pid*
-
-# ---> JetBrains
-# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio
-
-*.iml
-
-## Directory-based project format:
-.idea/
-# if you remove the above rule, at least ignore the following:
-
-# User-specific stuff:
-# .idea/workspace.xml
-# .idea/tasks.xml
-# .idea/dictionaries
-
-# Sensitive or high-churn files:
-# .idea/dataSources.ids
-# .idea/dataSources.xml
-# .idea/sqlDataSources.xml
-# .idea/dynamic.xml
-# .idea/uiDesigner.xml
-
-# Gradle:
-# .idea/gradle.xml
-# .idea/libraries
-
-# Mongo Explorer plugin:
-# .idea/mongoSettings.xml
-
-## File-based project format:
-*.ipr
-*.iws
-
-## Plugin-specific files:
-
-# IntelliJ
-/out/
-
-# mpeltonen/sbt-idea plugin
-.idea_modules/
-
-# JIRA plugin
-atlassian-ide-plugin.xml
-
-# Crashlytics plugin (for Android Studio and IntelliJ)
-com_crashlytics_export_strings.xml
-crashlytics.properties
-crashlytics-build.properties
-
-.DS_Store
-/target/
-
-.classpath
-.project

+ 24 - 3
pom.xml

@@ -43,9 +43,9 @@
         
 <!-- HACK for testing -->
     <plugin>
-        <groupId>org.mortbay.jetty</groupId>
-        <artifactId>maven-jetty-plugin</artifactId>
-        <version>6.1.22</version>
+	<groupId>org.eclipse.jetty</groupId>
+	<artifactId>jetty-maven-plugin</artifactId>
+	<version>9.4.44.v20210927</version>
     </plugin>
 <!-- HACK for testing -->
 
@@ -137,6 +137,25 @@
         </dependency>
     
 <!-- JETTY -->
+<!--
+<dependency>
+    <groupId>org.eclipse.jetty</groupId>
+    <artifactId>jetty-server</artifactId>
+    <version>${jetty.version}</version>
+    <scope>provided</scope>
+</dependency>
+<dependency>
+    <groupId>org.eclipse.jetty</groupId>
+    <artifactId>jetty-util</artifactId>
+    <version>${jetty.version}</version>
+</dependency>
+<dependency>
+    <groupId>org.mortbay.jetty</groupId>
+    <artifactId>jsp-2.1-jetty</artifactId>
+    <version>6.1.26</version>
+</dependency>
+-->
+
         <dependency>
             <groupId>org.mortbay.jetty</groupId>
             <artifactId>jetty</artifactId>
@@ -168,6 +187,7 @@
             <scope>provided</scope>
         </dependency>
 <!-- JETTY -->    
+
         <!-- Provided dependencies -->
         <dependency>
             <groupId>net.sf.jasperreports</groupId>
@@ -269,6 +289,7 @@
 
     <properties>
        <jetty.version>6.1.22</jetty.version>
+       <!--<jetty.version>9.4.44.v20210927</jetty.version>-->
        <jersey2.version>2.23</jersey2.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.build.resourceEncoding>UTF-8</project.build.resourceEncoding>

+ 5 - 1
src/main/java/cz/hsrs/db/model/Unit.java

@@ -28,8 +28,12 @@ public class Unit implements DBObject{
 		this.description = description;
 	}
 	
+	public Unit(long unitId){
+		super();
+		this.unitId = unitId;
+	}
+	
 	public Unit(){
-		
 	}
 	
 	public Unit(ResultSet set) throws SQLException {

+ 55 - 0
src/main/java/cz/hsrs/db/util/ExportUtil.java

@@ -16,6 +16,61 @@ import cz.hsrs.db.pool.SQLExecutor;
  */
 public class ExportUtil {
     
+	/**
+     * Export method for crosstab style 
+     * e.g.: time_stamp;unit1;unit2;...;unitN
+     * @param sensorId
+     * @param unitIds - String of unitIDs used for filtering, comma-separated
+     * @param fromTime
+     * @param toTime
+     * @return
+     * @throws SQLException
+     */
+    public static String getObservationsBySensorByUnitsCross(long sensorId, String unitIds, int groupId, String fromTime, String toTime, boolean nullable) throws SQLException {
+        String[] unitIdArr = unitIds.split(","); 
+        String CSV = "";
+        // there are some units to export
+        if(unitIdArr.length > 0) {
+            // define Query
+            StringBuilder selectString = new StringBuilder();
+            StringBuilder unitString = new StringBuilder();
+            selectString.append("SELECT time_stamp\n");
+            unitString.append("AS ct( \"time_stamp\" text\n");
+            
+            for (int i = 0; i <unitIdArr.length; i++) {
+                long unitID = Long.valueOf(unitIdArr[i]);
+                if(nullable == true) {
+                	selectString.append(", \""+unitID+"\"");
+                }
+                else {
+                	selectString.append(", COALESCE(\""+unitID+"\", 0) AS \""+unitID+"\"");
+                }
+                unitString.append(", \""+unitID+"\" double precision");
+            }
+            unitString.append(" ) ORDER BY time_stamp;");
+            StringBuilder fromString = new StringBuilder();
+            fromString.append(" FROM crosstab( $$\r\n" + 
+                    " SELECT to_char(time_stamp, 'DD.MM.YYYY HH24:MI:SS'), unit_id, observed_value FROM observations\r\n" + 
+                    " WHERE unit_id IN (SELECT unit_id FROM units_to_groups WHERE group_id = "+groupId+" AND unit_id IN ("+unitIds+"))\r\n" + 
+                    " AND sensor_id = "+sensorId+"\r\n"); 
+            if(fromTime != null) {
+                fromString.append(" AND time_stamp >= '"+fromTime+"'\r\n");
+            }
+            if(toTime != null) {
+                fromString.append(" AND time_stamp < '"+toTime+"'\r\n");
+            }
+            fromString.append(" ORDER BY time_stamp, unit_id\r\n$$) ");
+            
+            String query = selectString.toString() + fromString + unitString.toString();
+            
+            ResultSet res = SQLExecutor.getInstance().executeQuery(query);
+            if(res != null) {
+                CSV = CSVWriter(res);
+            }
+        }
+        return CSV;
+    }
+	
     /**
      * Export method for crosstab style 
      * e.g.: time_stamp;unit1;unit2;...;unitN

+ 1 - 0
src/main/java/cz/hsrs/main/StartJetty.java

@@ -1,5 +1,6 @@
 package cz.hsrs.main;
 
+
 import org.mortbay.jetty.Connector;
 import org.mortbay.jetty.Server;
 import org.mortbay.jetty.bio.SocketConnector;

+ 6 - 6
src/main/java/cz/hsrs/rest/SCListener.java

@@ -1,16 +1,16 @@
 package cz.hsrs.rest;
 
-import cz.hsrs.db.pool.SQLExecutor;
-import cz.hsrs.rest.provider.InfoRest;
+import static cz.hsrs.core.ApplicationInfo.appVersion;
+import static cz.hsrs.core.ApplicationInfo.buildVersion;
 
-import javax.servlet.ServletContextEvent;
-import javax.servlet.ServletContextListener;
 import java.io.FileInputStream;
 import java.util.Properties;
 import java.util.logging.Logger;
 
-import static cz.hsrs.core.ApplicationInfo.appVersion;
-import static cz.hsrs.core.ApplicationInfo.buildVersion;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+
+import cz.hsrs.db.pool.SQLExecutor;
 
 /**
  * 

+ 9 - 1
src/main/java/cz/hsrs/rest/provider/ObservationRest.java

@@ -47,11 +47,13 @@ public class ObservationRest {
      * @return
      * /rest/observation/export?group_id=25&sensor_id=340340092&from_time=2021-05-30&to_time=2021-05-31&style=crosstab
      * /rest/observation/export?group_id=25&sensor_id=340340092&from_time=2021-05-30&to_time=2021-05-30+04:00:00&style=crosstab&nullable=false
+     * /rest/observation/export?unit_id=1305167563044155,1305167563032199,1305167563051210&sensor_id=340340092&from_time=2021-05-30&to_time=2021-05-30+04:00:00&style=crosstab&nullable=false
      */
     @Path("/export")
     @GET
     public Response getObservationsCrossTab(
             @QueryParam(ParamsList.SENSOR_ID) Long sensorId,
+            @QueryParam(ParamsList.UNIT_ID) String unitIds,
             @QueryParam(ParamsList.GROUP_ID) Integer groupId,
             @QueryParam(ParamsList.FROM_TIME) String fromTime,
             @QueryParam(ParamsList.TO_TIME) String toTime,
@@ -64,7 +66,13 @@ public class ObservationRest {
                 if(groupId == null) {
                 	groupId = UserUtil.getUserGroupId(loggedUser.getUserName());
                 }
-                String CSV = ExportUtil.getObservationsBySensorByGroupCross(sensorId, groupId, fromTime, toTime, nullable);
+                String CSV;
+                if(unitIds!= null && !unitIds.isEmpty()){
+                	CSV = ExportUtil.getObservationsBySensorByUnitsCross(sensorId, unitIds, groupId, fromTime, toTime, nullable);
+                }
+                else {
+                	CSV = ExportUtil.getObservationsBySensorByGroupCross(sensorId, groupId, fromTime, toTime, nullable);
+                }           
                 return Response.ok()
                         .entity(CSV)
                         .header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN)

+ 2 - 2
src/main/java/cz/hsrs/servlet/provider/DBServlet.java

@@ -68,8 +68,8 @@ public abstract class DBServlet extends HttpServlet {
             if (SQLExecutor.getConfigFile()!=null) {
                 conffile  = SQLExecutor.getConfigFile();
             }
-            //FileInputStream fstrem = new FileInputStream(new File(getServletContext().getRealPath("WEB-INF/"+conffile)));
-            FileInputStream fstrem = new FileInputStream(new File(getServletContext().getRealPath(conffile)));
+            FileInputStream fstrem = new FileInputStream(new File(getServletContext().getRealPath("WEB-INF/"+conffile)));
+            //FileInputStream fstrem = new FileInputStream(new File(getServletContext().getRealPath(conffile)));
             LogManager.getLogManager().readConfiguration(fstrem);
             LogManager.getLogManager().addLogger(logger);