|
|
@@ -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
|