Advertisement
Guest User

Untitled

a guest
Jun 6th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.85 KB | None | 0 0
  1. package org.openxdata.gis.server;
  2.  
  3. import java.io.FileInputStream;
  4. import java.sql.*;
  5. import java.text.SimpleDateFormat;
  6. import java.util.ArrayList;
  7. import java.util.Date;
  8. import java.util.Properties;
  9.  
  10. import org.openxdata.gis.client.data.MarkerData;
  11. import org.openxdata.gis.client.data.ReportData;
  12. import org.openxdata.gis.client.service.ConnectionService;
  13.  
  14. import com.extjs.gxt.ui.client.widget.MessageBox;
  15. import com.google.gwt.user.server.rpc.RemoteServiceServlet;
  16.  
  17. /**
  18. * This class creates the connection to the database and runs a SQL query
  19. *
  20. * @author Hans Terje Møller
  21. */
  22. @SuppressWarnings("serial")
  23. public class ConnectionServiceImpl extends RemoteServiceServlet implements
  24. ConnectionService {
  25.  
  26. // Class variables
  27. // These variables are for getting the different cells from the database
  28. private int reportId;
  29. private int formId;
  30. private String name;
  31. private String description;
  32. private String query;
  33.  
  34. // This variable is the report query
  35. private String reportQuery;
  36.  
  37. // These variables are for establishing the database connection
  38. private Connection connection;
  39. private Statement statement;
  40. private ResultSet resultSet;
  41.  
  42. // These variables defines the settings for connecting to the database
  43. private String driver = "com.mysql.jdbc.Driver";
  44. private String databaseUrl = "jdbc:mysql://localhost:3306/";
  45. private String databaseName = "openxdata";
  46. private String username;
  47. private String password;
  48.  
  49. // These variables are for setting and getting the different values
  50. private int intData;
  51. private String stringData;
  52. private double doubleData;
  53. private Date dateData;
  54.  
  55. // This variable is for adding the converted object to the array
  56. private String valueData;
  57.  
  58. // This variable converts the date from the database to this format
  59. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
  60.  
  61. /**
  62. * This method establishes a connection and runs the SQL query for
  63. * getting the reports
  64. */
  65. public ReportData[] getReportData() {
  66. // This array list stores the data retrieved from the SQL query
  67. ArrayList<ReportData> reportValues = new ArrayList<ReportData>();
  68. Properties oxdProperties = new Properties();
  69.  
  70. try {
  71. // Loads the OpenXdata properties
  72. oxdProperties.load(new FileInputStream("OPENXDATA_SETTINGS.properties"));
  73.  
  74. // Gets user name, password and database name from OpenXdata properties
  75. username = oxdProperties.getProperty("hibernate.connection.username");
  76. password = oxdProperties.getProperty("hibernate.connection.password");
  77.  
  78. // Creates the database connection
  79. Class.forName(driver);
  80. connection = DriverManager.getConnection(databaseUrl + databaseName, username, password);
  81. statement = connection.createStatement();
  82.  
  83. // This is the report SQL query
  84. String sql = "SELECT report_id, form_definition_version_id, name, description, query_sql FROM reports";
  85. resultSet = statement.executeQuery(sql);
  86.  
  87. // This is a while-loop which runs to the database and retrieves all
  88. // the rows of the given columns
  89. while (resultSet.next()) {
  90. reportId = resultSet.getInt("report_id");
  91. formId = resultSet.getInt("form_definition_version_id");
  92. name = resultSet.getString("name");
  93. description = resultSet.getString("description");
  94. query = resultSet.getString("query_sql");
  95.  
  96. reportValues.add(new ReportData(reportId, formId, name,
  97. description, query));
  98. }
  99.  
  100. resultSet.close();
  101. statement.close();
  102. connection.close();
  103.  
  104. } catch (Exception e) {
  105. MessageBox.alert("Error",
  106. "Something went wrong! Error as follows: " + e, null);
  107. }
  108.  
  109. // Returns the values to the array
  110. return (ReportData[]) reportValues.toArray(new ReportData[reportValues
  111. .size()]);
  112. }
  113.  
  114. /**
  115. * This method establishes the connection and runs the SQL query for getting
  116. * the data in the reports
  117. */
  118. public MarkerData[] getMarkerData() {
  119.  
  120. // This array list stores the data retrieved from the SQL query
  121. ArrayList<MarkerData> values = new ArrayList<MarkerData>();
  122.  
  123. try {
  124. // Creates the database connection
  125. Class.forName(driver);
  126. connection = DriverManager.getConnection(
  127. databaseUrl + databaseName, username, password);
  128. statement = connection.createStatement();
  129.  
  130. // This is the SQL query
  131. resultSet = statement.executeQuery(reportQuery);
  132.  
  133. // Gets the meta data from the result set
  134. ResultSetMetaData rsMetaData = resultSet.getMetaData();
  135. // Gets the number of columns in the table
  136. int columnCount = rsMetaData.getColumnCount();
  137.  
  138. while (resultSet.next()) {
  139. for (int i=1; i <= columnCount; i++) {
  140. // Gets the column names in the table
  141. String columnName = rsMetaData.getColumnName(i);
  142. // Gets the column types in the table
  143. String columnType = rsMetaData.getColumnTypeName(i);
  144. // Handles all of the values as objects
  145. Object value = resultSet.getObject(i);
  146.  
  147. // Checks what type the object is and converts it accordingly
  148. // At the time being it supports INTEGER, VARCHAR, TEXT, DOUBLE and DATE.
  149. // TODO: Add support for all types
  150. if (value != null) {
  151. if (columnType.equals("INTEGER")) {
  152. String valueData = value.toString();
  153. setIntData(Integer.parseInt(valueData));
  154. } else if (columnType.equals("VARCHAR")) {
  155. String valueData = value.toString();
  156. setStringData(valueData);
  157. } else if (columnType.equals("TEXT")) {
  158. String valueData = value.toString();
  159. setStringData(valueData);
  160. } else if (columnType.equals("DOUBLE")) {
  161. String valueData = value.toString();
  162. setDoubleData(Double.parseDouble(valueData));
  163. } else if (columnType.equals("DATE")) {
  164. String valueData = value.toString();
  165. setDateData(sdf.parse(valueData));
  166. }
  167. }
  168.  
  169. // Sets a 'null' value if the object doesn't contain any data
  170. if (value == null) {
  171. valueData = "";
  172. } else {
  173. valueData = value.toString();
  174. }
  175.  
  176. values.add(new MarkerData(valueData, columnName, columnType, columnCount));
  177. }
  178. }
  179.  
  180. resultSet.close();
  181. statement.close();
  182. connection.close();
  183.  
  184. } catch (Exception e) {
  185. MessageBox.alert("Error",
  186. "Something went wrong! Error as follows: " + e, null);
  187. }
  188.  
  189. return (MarkerData[]) values.toArray(new MarkerData[values.size()]);
  190. }
  191.  
  192. /**
  193. * Method for getting the database URL
  194. */
  195. public String getDatabaseUrl(String databaseUrl) {
  196. return this.databaseUrl = databaseUrl;
  197. }
  198.  
  199. /**
  200. * Method for getting the database name
  201. */
  202. public String getDatabaseName(String databaseName) {
  203. return this.databaseName = databaseName;
  204. }
  205.  
  206. /**
  207. * This method gets the report query
  208. */
  209. public String getReportQuery(String reportQuery) {
  210. return this.reportQuery = reportQuery;
  211. }
  212.  
  213. /**
  214. * Method for setting intData
  215. * @param intData
  216. */
  217. public void setIntData(int intData) {
  218. this.intData = intData;
  219. }
  220.  
  221. /**
  222. * Method for getting intData
  223. * @return intData
  224. */
  225. public int getIntData() {
  226. return intData;
  227. }
  228.  
  229. /**
  230. * Method for setting stringData
  231. * @param stringData
  232. */
  233. public void setStringData(String stringData) {
  234. this.stringData = stringData;
  235. }
  236.  
  237. /**
  238. * Method for getting stringData
  239. * @return stringData
  240. */
  241. public String getStringData() {
  242. return stringData;
  243. }
  244.  
  245. /**
  246. * Method for setting doubleData
  247. * @param doubleData
  248. */
  249. public void setDoubleData(double doubleData) {
  250. this.doubleData = doubleData;
  251. }
  252.  
  253. /**
  254. * Method for getting doubleData
  255. * @return doubleData
  256. */
  257. public double getDoubleData() {
  258. return doubleData;
  259. }
  260.  
  261. /**
  262. * Method for setting dateData
  263. * @param date
  264. */
  265. public void setDateData(Date dateData) {
  266. this.dateData = dateData;
  267. }
  268.  
  269. /**
  270. * Method for getting dateData
  271. * @return dateData
  272. */
  273. public Date getDateData() {
  274. return dateData;
  275. }
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement