UnitPosition.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. package cz.hsrs.db.model;
  2. import java.sql.ResultSet;
  3. import java.sql.SQLException;
  4. import java.text.ParseException;
  5. import java.util.Date;
  6. import java.util.logging.Level;
  7. import cz.hsrs.db.DBObject;
  8. import cz.hsrs.db.pool.SQLExecutor;
  9. import cz.hsrs.db.util.DateUtil;
  10. import cz.hsrs.db.util.UnitUtil;
  11. public class UnitPosition implements DBObject {
  12. private final int gid;
  13. private final long unit_id;
  14. private final double x;
  15. private final double y;
  16. private final double alt;
  17. private final String time_stamp;
  18. private Date timeStamp;
  19. private final double dop;
  20. private final double speed;
  21. private int group_id;
  22. private final String geom;
  23. private final String srid;
  24. private final String time_received;
  25. private final String first_timestamp;
  26. public static final String SELECT = "gid, unit_id, time_stamp, st_x(the_geom), st_y(the_geom), speed, st_srid(the_geom) ";
  27. private static final String SCHEMA_NAME = "public";
  28. //private static final String ALTITUDE_COLUMN = "altitude";
  29. //private static final String DATASET_TABLE_NAME = "units_positions";
  30. /**
  31. * Empty constructor initializes all attributes to null or zero
  32. */
  33. public UnitPosition() {
  34. speed = Double.NaN;
  35. dop = Double.NaN;
  36. unit_id = 0;
  37. gid = 0;
  38. y = Double.NaN;
  39. x = Double.NaN;
  40. alt = Double.NaN;
  41. time_stamp = null;
  42. geom = null;
  43. srid = null;
  44. time_received = null;
  45. first_timestamp = null;
  46. }
  47. /**
  48. * Constructor instantiates new object from ResultSet, without altitude!
  49. * @param set ResultSet with columns:
  50. * gid, unit_id, time_stamp, speed, st_x, st_y, st_srid
  51. * @throws SQLException
  52. */
  53. public UnitPosition(ResultSet set) throws SQLException {
  54. this.gid = set.getInt("gid");
  55. this.unit_id = set.getLong("unit_id");
  56. this.time_stamp = set.getString("time_stamp");
  57. this.speed = set.getDouble("speed");
  58. this.x = set.getDouble("st_x");
  59. this.y = set.getDouble("st_y");
  60. this.alt = Double.NaN;
  61. this.geom = createPointString(x, y);
  62. this.srid = set.getString("st_srid");
  63. this.dop = Double.NaN;
  64. this.time_received = null;
  65. this.first_timestamp = null;
  66. UnitUtil uUtil = new UnitUtil();
  67. this.group_id = uUtil.getGroupID(unit_id);
  68. }
  69. /**
  70. * Constructor instantiates object from fields
  71. * @param unitId - id of unit
  72. * @param x - long coordinate
  73. * @param y - lat coordinate
  74. * @param timeStamp time stamp of position
  75. * @throws SQLException
  76. */
  77. public UnitPosition(long unitId, double x, double y, Date timeStamp) throws SQLException {
  78. this(unitId, x, y, 0, timeStamp, Double.NaN, "");
  79. }
  80. /**
  81. * Constructor instantiates object from fields
  82. * @param unitId - id of unit
  83. * @param x - long coordinate
  84. * @param y - lat coordinate
  85. * @param alt - altitude coordinate
  86. * @param timeStamp time stamp of position
  87. * @throws SQLException
  88. */
  89. public UnitPosition(long unitId, double x, double y, double alt, Date timeStamp) throws SQLException {
  90. this(unitId, x, y, alt, timeStamp, Double.NaN, "");
  91. }
  92. /**
  93. * Constructor for creating UnitPosition from WebService
  94. * @param unitId
  95. * @param x
  96. * @param y
  97. * @param alt
  98. * @param timeStamp
  99. * @param speed
  100. * @param srid
  101. * @throws SQLException
  102. */
  103. public UnitPosition(long unitId, double x, double y, double alt, Date timeStamp, Double speed, String srid) throws SQLException {
  104. super();
  105. this.unit_id = unitId;
  106. this.gid = getNextGid();
  107. this.x = x;
  108. this.y = y;
  109. this.alt = alt;
  110. this.geom = createPointString(x, y);
  111. this.timeStamp = timeStamp;
  112. this.speed = speed;
  113. this.dop = Double.NaN;
  114. this.srid = srid;
  115. this.time_stamp = null;
  116. this.time_received = null;
  117. this.first_timestamp = null;
  118. }
  119. /**
  120. * Constructor for creating UnitPosition object from WebService
  121. * @param unitId
  122. * @param x
  123. * @param y
  124. * @param alt
  125. * @param timeStamp
  126. * @param speed
  127. * @param dop
  128. * @param srid
  129. * @throws SQLException
  130. */
  131. public UnitPosition(long unitId, double x, double y, double alt, Date timeStamp, Double speed, Double dop, String srid) throws SQLException {
  132. super();
  133. this.unit_id = unitId;
  134. this.gid = getNextGid();
  135. this.x = x;
  136. this.y = y;
  137. this.alt = alt;
  138. this.geom = createPointString(x, y);
  139. this.timeStamp = timeStamp;
  140. this.speed = speed;
  141. this.dop = dop;
  142. this.srid = srid;
  143. this.time_stamp = DateUtil.formatSecsTZ.format(timeStamp);
  144. this.time_received = null;
  145. this.first_timestamp = null;
  146. }
  147. /**
  148. * Constructor for creating UnitPosition object from DB
  149. * @param gid
  150. * @param unit_id
  151. * @param alt
  152. * @param time_string
  153. * @param dop
  154. * @param speed
  155. * @param geom
  156. * @param srid
  157. * @param receivedString
  158. * @param firstTimestampString
  159. */
  160. public UnitPosition(int gid, long unit_id, double x, double y, double alt, String time_string, double dop, double speed,
  161. String srid, String receivedString, String firstTimestampString) {
  162. this.gid = gid;
  163. this.unit_id = unit_id;
  164. this.x = x;
  165. this.y = y;
  166. this.alt = alt;
  167. this.geom = null;
  168. this.time_stamp = time_string;
  169. this.dop = dop;
  170. this.speed = speed;
  171. this.srid = srid;
  172. this.time_received = receivedString;
  173. this.first_timestamp = firstTimestampString;
  174. }
  175. /**
  176. * Constructor for updating UnitPosition from webservice
  177. * @param gid
  178. * @param unit_id
  179. * @param x
  180. * @param y
  181. * @param alt
  182. * @param timestamp
  183. * @param dop
  184. * @param speed
  185. * @param srid
  186. */
  187. public UnitPosition(int gid, long unit_id, double x, double y, double alt, Date timestamp, double dop, double speed, String srid) {
  188. this.gid = gid;
  189. this.unit_id = unit_id;
  190. this.x = x;
  191. this.y = y;
  192. this.alt = alt;
  193. this.geom = this.createPointString(x, y);
  194. this.timeStamp = timestamp;
  195. this.dop = dop;
  196. this.speed = speed;
  197. this.srid = srid;
  198. this.time_stamp = DateUtil.formatSecsTZ.format(timestamp);
  199. this.time_received = null;
  200. this.first_timestamp = null;
  201. }
  202. public double getSpeed() {
  203. return speed;
  204. }
  205. public int getGroup_id() {
  206. return group_id;
  207. }
  208. public double getX() {
  209. return x;
  210. }
  211. public double getY() {
  212. return y;
  213. }
  214. /**
  215. * Method returns Altitude of the Positions
  216. * @return double value if position is defined,
  217. * NULL if altitude is NaN or null
  218. */
  219. public Double getAlt(){
  220. if(Double.isNaN(alt)){
  221. return null;
  222. }
  223. else{
  224. return alt;
  225. }
  226. }
  227. public String getSRID(){
  228. return srid;
  229. }
  230. /**
  231. * Inherited method converts content of ResultSet to UnitPosition object
  232. */
  233. public DBObject getDBObject(ResultSet set) throws SQLException {
  234. return new UnitPosition(set);
  235. }
  236. /**
  237. * Getter returns gid of unit_position
  238. * @return gid
  239. */
  240. public int getGid() {
  241. return gid;
  242. }
  243. /**
  244. * Method returns geom as String:
  245. * "POINT(x y)"
  246. * @return String "POINT(x y)"
  247. */
  248. public String internalGetGeom() {
  249. return geom;
  250. }
  251. /**
  252. * Method returns time stamp of position as Date
  253. * if Date time stamp is not set parses it from String time stamp
  254. * processes String time stamp in case of using microseconds from DB
  255. * @return timestamp of position as Date
  256. * @throws SQLException
  257. */
  258. public Date internalGetTimestamp() throws SQLException {
  259. if (timeStamp != null) {
  260. try {
  261. return DateUtil.parseTimestamp(time_stamp);
  262. } catch (ParseException e) {
  263. SQLExecutor.logger.log(Level.SEVERE, e.getMessage());
  264. throw new SQLException("Timestamp of position is not present!");
  265. }
  266. }
  267. else{
  268. return timeStamp;
  269. }
  270. }
  271. /**
  272. * Getter returns Unit_ID
  273. * @return unit_id
  274. */
  275. public long getUnit_id() {
  276. return unit_id;
  277. }
  278. /**
  279. * Method inserts new position into database
  280. * @return true if positions was successfully inserted, false otherwise
  281. * @throws SQLException is thrown if an Exception occurs during inserting
  282. */
  283. public boolean insertToDb() throws SQLException {
  284. StringBuffer queryBuff= new StringBuffer();
  285. if(SQLExecutor.isAltitudeEnabled()){
  286. queryBuff.append("INSERT INTO "+SCHEMA_NAME+".units_positions(altitude, the_geom, unit_id, time_stamp, gid, speed, dop) VALUES (");
  287. queryBuff.append(Double.isNaN(this.alt) ? "NULL, " : String.valueOf(this.alt)+", ");
  288. }
  289. else{
  290. queryBuff.append("INSERT INTO "+SCHEMA_NAME+".units_positions(the_geom, unit_id, time_stamp, gid, speed, dop) VALUES (");
  291. }
  292. queryBuff.append("st_geomfromtext('"+ this.geom + "', "+(this.srid.isEmpty() ? "4326" : this.srid)+"), ");
  293. queryBuff.append(this.unit_id + ", ");
  294. queryBuff.append("timestamp with time zone '" + DateUtil.formatSecsTZ.format(this.timeStamp) + "', ");
  295. queryBuff.append(this.gid + ", ");
  296. queryBuff.append(Double.isNaN(this.speed) ? "NULL, " : String.valueOf(this.speed)+", ");
  297. queryBuff.append(Double.isNaN(this.dop) ? "NULL" : String.valueOf(this.dop));
  298. queryBuff.append(");");
  299. String ins = queryBuff.toString();
  300. int i = SQLExecutor.executeUpdate(ins);
  301. /** If there is partitioning on table units_positions, function executeUpdate returns 0.
  302. * If there is only units_positions table, function executeUpdate returns 1.
  303. */
  304. if (i == 1 || i == 0){
  305. return true;
  306. }
  307. else{
  308. return false;
  309. }
  310. }
  311. /**
  312. * Method returns String to create geometry object in PostGIS
  313. * "st_geomfromtext('POINT(x y)', SRID)";
  314. * @return "st_geomfromtext('POINT(x y)', SRID)"; as String
  315. */
  316. public String getPostgisString() {
  317. if(this.srid == null){
  318. return "st_geomfromtext('" + this.geom + "', 4326)";
  319. }
  320. else{
  321. if(this.srid.isEmpty()){
  322. return "st_geomfromtext('" + this.geom + "', 4326)";
  323. }
  324. else{
  325. return "st_geomfromtext('" + this.geom + "', "+this.srid+")";
  326. }
  327. }
  328. }
  329. private String createPointString(double x, double y) {
  330. return new String("POINT(" + x + " " + y + ")");
  331. }
  332. /**
  333. *
  334. * @return the_geom
  335. */
  336. public String getPostgisGeomString() {
  337. return this.geom;
  338. }
  339. /**
  340. *
  341. * @return the time_stamp
  342. */
  343. public String getTime_stamp() {
  344. return time_stamp;
  345. }
  346. /**
  347. * Method returns DOP value of Position
  348. * @return the dop double value, NULL if dop is NaN or null
  349. */
  350. public Double getDop() {
  351. if(Double.isNaN(dop)){
  352. return null;
  353. }
  354. else{
  355. return dop;
  356. }
  357. }
  358. /**
  359. * @return the srid
  360. */
  361. public String getSrid() {
  362. return srid;
  363. }
  364. /**
  365. * @return the time_received
  366. * @throws ParseException
  367. */
  368. public Date internalGetTimeReceived() throws ParseException {
  369. return DateUtil.parseTimestampMicro(this.time_received);
  370. }
  371. /**
  372. * @return the time_received
  373. */
  374. public String getReceivedTimestampString() {
  375. return this.time_received;
  376. }
  377. /**
  378. * @return the first_timestamp
  379. * @throws ParseException
  380. */
  381. public Date internalGetFirstTimestamp() throws ParseException {
  382. return DateUtil.parseTimestampMicro(this.first_timestamp);
  383. }
  384. /**
  385. * @return the firstTsString
  386. */
  387. public String getFirstTimesstamp() {
  388. return this.first_timestamp;
  389. }
  390. /* (non-Javadoc)
  391. * @see java.lang.Object#toString()
  392. */
  393. @Override
  394. public String toString() {
  395. return "UnitPosition [unit_id=" + unit_id + ", gid=" + gid + ", speed="
  396. + speed + ", x=" + x + ", y=" + y + ", alt=" + alt
  397. + ", time_string=" + time_stamp + ", group_id=" + group_id
  398. + ", srid=" + srid + "]";
  399. }
  400. /**
  401. * Method get next value of sequence for GID
  402. * @return next value of GID as integer
  403. * @throws SQLException
  404. */
  405. private int getNextGid() throws SQLException{
  406. try{
  407. String queryGid = "SELECT nextval('"+SCHEMA_NAME+".units_positions_gid_seq'::regclass);";
  408. ResultSet resId = SQLExecutor.getInstance().executeQuery(queryGid);
  409. if(resId.next()){
  410. return resId.getInt(1);
  411. }
  412. else{
  413. throw new SQLException("Next value of GID cannot be selected!");
  414. }
  415. } catch(SQLException e){
  416. throw new SQLException("Next value of GID cannot be selected!");
  417. }
  418. }
  419. }