Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.30 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.lang.Exception;
  3. import java.sql.*;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6.  
  7.  
  8. public class Functions {
  9. public static int getInt() {
  10. Scanner reader = new Scanner(System.in);
  11. boolean errorInInput = false;
  12. int number = 0;
  13.  
  14. do {
  15. try {
  16. number = reader.nextInt();
  17. errorInInput = false;
  18. } catch (Exception error) {
  19. System.out.println("Give an integer!");
  20. reader.nextLine();
  21. errorInInput = true;
  22. }
  23. } while (errorInInput);
  24.  
  25. return number;
  26. }
  27.  
  28. public static String getString() {
  29. Scanner reader = new Scanner(System.in);
  30. boolean errorInInput = false;
  31. String word = "";
  32.  
  33. do {
  34. try {
  35. word = reader.nextLine();
  36. errorInInput = false;
  37. } catch (Exception error) {
  38. System.out.println("Give a string!:");
  39. reader.nextLine();
  40. errorInInput = true;
  41. }
  42. } while (errorInInput);
  43.  
  44. return word;
  45. }
  46.  
  47. public static String driver = "com.mysql.jdbc.Driver";
  48. public static String url = "jdbc:mysql://mydb.tamk.fi/dbc0vkontt1?user=c0vkontt&password=Villeville1";
  49.  
  50. public static int debug = 1;
  51.  
  52. public static void debug(String message) {
  53. if (debug > 0) {
  54. System.out.println(message);
  55. }
  56. }
  57.  
  58. public static int login(String username, String givenPassword) {
  59. String sql = "";
  60. String password = "";
  61. int id = 0;
  62.  
  63. try {
  64. Class.forName(driver);
  65. Connection con = DriverManager.getConnection(url);
  66.  
  67. sql = "SELECT password, id" + " FROM user"
  68. + " WHERE LOWER(username) = '" + username + "'" + "";
  69.  
  70. Statement stmt = con.createStatement();
  71. ResultSet result = stmt.executeQuery(sql);
  72.  
  73. while (result.next()) {
  74. password = result.getString("password");
  75. id = result.getInt("id");
  76. }
  77.  
  78. } catch (Exception error) {
  79. System.err.println("[ERROR] " + error.getMessage());
  80. error.printStackTrace();
  81. System.exit(1);
  82. }
  83.  
  84. if (password.equals(givenPassword) && !givenPassword.equals("")) {
  85. debug("Login succesful");
  86. return id;
  87. } else {
  88. debug("Login failed, please retry:");
  89. return 0;
  90. }
  91.  
  92. }
  93.  
  94. public static int checkUsertype(int id) { {
  95. String sql = "";
  96. int type = 0;
  97.  
  98. try {
  99. Class.forName(driver);
  100. Connection con = DriverManager.getConnection(url);
  101.  
  102. sql = "SELECT type" + " FROM user" + " WHERE id = " + id + "";
  103.  
  104. Statement stmt = con.createStatement();
  105. ResultSet result = stmt.executeQuery(sql);
  106.  
  107. while (result.next()) {
  108. type = result.getInt("type");
  109. }
  110.  
  111. } catch (Exception error) {
  112. System.err.println("[ERROR] " + error.getMessage());
  113. error.printStackTrace();
  114. System.exit(1);
  115. }
  116. return type;
  117. }
  118.  
  119. }
  120.  
  121. public static void insertNotification(int type, int level, String desc, int id, String date) { // päivän saanti tässä metodissa?
  122. try {
  123. Class.forName(driver);
  124. Connection con = DriverManager.getConnection(url);
  125.  
  126. String sql = "INSERT INTO notification "
  127. + "( type, level, description, creator, createdate, handlingState ) "
  128. + "VALUES " + "(" + type + ", " + level + ", '" + desc
  129. + "', " + id + ", '" + date + "', " + "0" + ");";
  130. Statement stmt = con.createStatement();
  131.  
  132. stmt.executeUpdate(sql);
  133.  
  134. debug("Notification sent.");
  135. Ui.printUsermenu(id);
  136. } catch (Exception error) {
  137. System.err.println("[ERROR] " + error.getMessage());
  138. error.printStackTrace();
  139. System.exit(1);
  140. }
  141. }
  142.  
  143. public static boolean printNotification(int state) {
  144. String sql = "";
  145.  
  146. try {
  147. Class.forName(driver);
  148. Connection con = DriverManager.getConnection(url);
  149.  
  150. sql = "SELECT id, createDate, level, type" + " FROM notification"
  151. + " WHERE handlingState = " + state + " " + "";
  152. Statement stmt = con.createStatement();
  153. ResultSet result = stmt.executeQuery(sql);
  154.  
  155. boolean rowtest = true;
  156.  
  157. if (rowtest == false) {
  158. return false;
  159. } else {
  160. System.out.println("Current notifications:");
  161. System.out.println("Id date level type");
  162. while (result.next()) {
  163. System.out.printf("%-3d", result.getInt("id"));
  164. System.out.printf("%-12s", result.getString("createDate"));
  165. System.out.printf("%-7d", result.getInt("level"));
  166. System.out.printf("%-15d", result.getInt("type"));
  167. System.out.println();
  168. }
  169.  
  170. }
  171.  
  172. } catch (Exception error) {
  173. System.err.println("[ERROR] " + error.getMessage());
  174. error.printStackTrace();
  175. System.exit(1);
  176. }
  177. return true;
  178. }
  179.  
  180. public static void printSpecificOpen(int notificationId) {
  181. String sql = "";
  182.  
  183. try {
  184. Class.forName(driver);
  185. Connection con = DriverManager.getConnection(url);
  186.  
  187. sql = "SELECT type, level, handlingLevel, description, creator, createDate"
  188. + " FROM notification" + " WHERE id = " + notificationId
  189. + "";
  190.  
  191. Statement stmt = con.createStatement();
  192. ResultSet result = stmt.executeQuery(sql);
  193.  
  194. while (result.next()) {
  195. int type = result.getInt("type"); // modaa
  196. String createDate = result.getString("createDate");
  197. int handlingLevel = result.getInt("handlingLevel"); // modaa
  198. int level = result.getInt("level"); // modaa
  199. String description = result.getString("description");
  200. String creator = result.getString("creator");
  201.  
  202. // modaus
  203. String realtype = "";
  204.  
  205. switch (type) {
  206. case 1:
  207. realtype = "Software";
  208. break;
  209.  
  210. case 2:
  211. realtype = "Workstation";
  212. break;
  213.  
  214. case 3:
  215. realtype = "Infrastructure";
  216. break;
  217. }
  218.  
  219. String realHandlingLevel = "";
  220.  
  221. switch (handlingLevel) {
  222. case 1:
  223. realHandlingLevel = "Wishlist";
  224. break;
  225.  
  226. case 2:
  227. realHandlingLevel = "Low";
  228. break;
  229.  
  230. case 3:
  231. realHandlingLevel = "Medium";
  232. break;
  233.  
  234. case 4:
  235. realHandlingLevel = "High";
  236. break;
  237.  
  238. case 5:
  239. realHandlingLevel = "Urgent";
  240. break;
  241.  
  242. default:
  243. realHandlingLevel = "-";
  244. break;
  245. }
  246.  
  247. String realLevel = "";
  248.  
  249. switch (level) {
  250. case 1:
  251. realLevel = "Wishlist";
  252. break;
  253.  
  254. case 2:
  255. realLevel = "Low";
  256. break;
  257.  
  258. case 3:
  259. realLevel = "Medium";
  260. break;
  261.  
  262. case 4:
  263. realLevel = "High";
  264. break;
  265.  
  266. case 5:
  267. realLevel = "Urgent";
  268. break;
  269. }
  270.  
  271. System.out.println(
  272. "Type: " + realtype + " Date created: " + createDate
  273. + " Handling-level: " + realHandlingLevel + " Level: "
  274. + realLevel + " CreatorId: " + creator + "\n Desc:"
  275. + description);
  276. }
  277.  
  278. } catch (Exception error) {
  279. System.err.println("[ERROR] " + error.getMessage());
  280. error.printStackTrace();
  281. System.exit(1);
  282. }
  283.  
  284. }
  285.  
  286. public static void changeNotificationCriticality(int newlevel, int notificationId) {
  287. try {
  288. Class.forName(driver);
  289. Connection con = DriverManager.getConnection(url);
  290.  
  291. String sql = "UPDATE notification " + "SET handlingLevel="
  292. + newlevel + " " + "WHERE " + "id= " + notificationId + ";";
  293.  
  294. Statement stmt = con.createStatement();
  295.  
  296. stmt.executeUpdate(sql);
  297.  
  298. debug(
  299. "Criticality-level of notification ID " + notificationId
  300. + " changed to " + newlevel);
  301. } catch (Exception error) {
  302. System.err.println("[ERROR] " + error.getMessage());
  303. error.printStackTrace();
  304. System.exit(1);
  305. }
  306. }
  307.  
  308. public static void changeNotificationState(int id, int newstate, int notificationId, String handlingDescription) {
  309. String date = getDate();
  310. String sql = "";
  311.  
  312. try {
  313. Class.forName(driver);
  314. Connection con = DriverManager.getConnection(url);
  315.  
  316. if (newstate == 1) {
  317. sql = "UPDATE notification " + "SET handlingState=" + newstate
  318. + ", " + "handlerId=" + id + ", " + "handlingDate='"
  319. + date + "' " + "WHERE " + "id= " + notificationId + ";";
  320. } else if (newstate == 2) {
  321. sql = "UPDATE notification " + "SET handlingState=" + newstate
  322. + ", " + "handlerId=" + id + ", " + "handlingDate='"
  323. + date + "', " + "handlingDescription='"
  324. + handlingDescription + "' " + "WHERE " + "id= "
  325. + notificationId + ";";
  326. }
  327.  
  328. Statement stmt = con.createStatement();
  329.  
  330. stmt.executeUpdate(sql);
  331.  
  332. debug(
  333. "State of notification ID " + notificationId
  334. + " has been changed.");
  335. } catch (Exception error) {
  336. System.err.println("[ERROR] " + error.getMessage());
  337. error.printStackTrace();
  338. System.exit(1);
  339. }
  340. }
  341.  
  342. public static String getDate() {
  343.  
  344. String aikaMuotoiluISO = "yyyy-MM-dd";
  345.  
  346. SimpleDateFormat dateFormat = new SimpleDateFormat(aikaMuotoiluISO);
  347.  
  348. Date date = new Date();
  349.  
  350. String returndate = dateFormat.format(date);
  351.  
  352. return returndate;
  353. }
  354. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement