Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.lang.Exception;
- import java.sql.*;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- public class Functions {
- public static int getInt() {
- Scanner reader = new Scanner(System.in);
- boolean errorInInput = false;
- int number = 0;
- do {
- try {
- number = reader.nextInt();
- errorInInput = false;
- } catch (Exception error) {
- System.out.println("Give an integer!");
- reader.nextLine();
- errorInInput = true;
- }
- } while (errorInInput);
- return number;
- }
- public static String getString() {
- Scanner reader = new Scanner(System.in);
- boolean errorInInput = false;
- String word = "";
- do {
- try {
- word = reader.nextLine();
- errorInInput = false;
- } catch (Exception error) {
- System.out.println("Give a string!:");
- reader.nextLine();
- errorInInput = true;
- }
- } while (errorInInput);
- return word;
- }
- public static String driver = "com.mysql.jdbc.Driver";
- public static String url = "jdbc:mysql://mydb.tamk.fi/dbc0vkontt1?user=c0vkontt&password=Villeville1";
- public static int debug = 1;
- public static void debug(String message) {
- if (debug > 0) {
- System.out.println(message);
- }
- }
- public static int login(String username, String givenPassword) {
- String sql = "";
- String password = "";
- int id = 0;
- try {
- Class.forName(driver);
- Connection con = DriverManager.getConnection(url);
- sql = "SELECT password, id" + " FROM user"
- + " WHERE LOWER(username) = '" + username + "'" + "";
- Statement stmt = con.createStatement();
- ResultSet result = stmt.executeQuery(sql);
- while (result.next()) {
- password = result.getString("password");
- id = result.getInt("id");
- }
- } catch (Exception error) {
- System.err.println("[ERROR] " + error.getMessage());
- error.printStackTrace();
- System.exit(1);
- }
- if (password.equals(givenPassword) && !givenPassword.equals("")) {
- debug("Login succesful");
- return id;
- } else {
- debug("Login failed, please retry:");
- return 0;
- }
- }
- public static int checkUsertype(int id) { {
- String sql = "";
- int type = 0;
- try {
- Class.forName(driver);
- Connection con = DriverManager.getConnection(url);
- sql = "SELECT type" + " FROM user" + " WHERE id = " + id + "";
- Statement stmt = con.createStatement();
- ResultSet result = stmt.executeQuery(sql);
- while (result.next()) {
- type = result.getInt("type");
- }
- } catch (Exception error) {
- System.err.println("[ERROR] " + error.getMessage());
- error.printStackTrace();
- System.exit(1);
- }
- return type;
- }
- }
- public static void insertNotification(int type, int level, String desc, int id, String date) { // päivän saanti tässä metodissa?
- try {
- Class.forName(driver);
- Connection con = DriverManager.getConnection(url);
- String sql = "INSERT INTO notification "
- + "( type, level, description, creator, createdate, handlingState ) "
- + "VALUES " + "(" + type + ", " + level + ", '" + desc
- + "', " + id + ", '" + date + "', " + "0" + ");";
- Statement stmt = con.createStatement();
- stmt.executeUpdate(sql);
- debug("Notification sent.");
- Ui.printUsermenu(id);
- } catch (Exception error) {
- System.err.println("[ERROR] " + error.getMessage());
- error.printStackTrace();
- System.exit(1);
- }
- }
- public static boolean printNotification(int state) {
- String sql = "";
- try {
- Class.forName(driver);
- Connection con = DriverManager.getConnection(url);
- sql = "SELECT id, createDate, level, type" + " FROM notification"
- + " WHERE handlingState = " + state + " " + "";
- Statement stmt = con.createStatement();
- ResultSet result = stmt.executeQuery(sql);
- boolean rowtest = true;
- if (rowtest == false) {
- return false;
- } else {
- System.out.println("Current notifications:");
- System.out.println("Id date level type");
- while (result.next()) {
- System.out.printf("%-3d", result.getInt("id"));
- System.out.printf("%-12s", result.getString("createDate"));
- System.out.printf("%-7d", result.getInt("level"));
- System.out.printf("%-15d", result.getInt("type"));
- System.out.println();
- }
- }
- } catch (Exception error) {
- System.err.println("[ERROR] " + error.getMessage());
- error.printStackTrace();
- System.exit(1);
- }
- return true;
- }
- public static void printSpecificOpen(int notificationId) {
- String sql = "";
- try {
- Class.forName(driver);
- Connection con = DriverManager.getConnection(url);
- sql = "SELECT type, level, handlingLevel, description, creator, createDate"
- + " FROM notification" + " WHERE id = " + notificationId
- + "";
- Statement stmt = con.createStatement();
- ResultSet result = stmt.executeQuery(sql);
- while (result.next()) {
- int type = result.getInt("type"); // modaa
- String createDate = result.getString("createDate");
- int handlingLevel = result.getInt("handlingLevel"); // modaa
- int level = result.getInt("level"); // modaa
- String description = result.getString("description");
- String creator = result.getString("creator");
- // modaus
- String realtype = "";
- switch (type) {
- case 1:
- realtype = "Software";
- break;
- case 2:
- realtype = "Workstation";
- break;
- case 3:
- realtype = "Infrastructure";
- break;
- }
- String realHandlingLevel = "";
- switch (handlingLevel) {
- case 1:
- realHandlingLevel = "Wishlist";
- break;
- case 2:
- realHandlingLevel = "Low";
- break;
- case 3:
- realHandlingLevel = "Medium";
- break;
- case 4:
- realHandlingLevel = "High";
- break;
- case 5:
- realHandlingLevel = "Urgent";
- break;
- default:
- realHandlingLevel = "-";
- break;
- }
- String realLevel = "";
- switch (level) {
- case 1:
- realLevel = "Wishlist";
- break;
- case 2:
- realLevel = "Low";
- break;
- case 3:
- realLevel = "Medium";
- break;
- case 4:
- realLevel = "High";
- break;
- case 5:
- realLevel = "Urgent";
- break;
- }
- System.out.println(
- "Type: " + realtype + " Date created: " + createDate
- + " Handling-level: " + realHandlingLevel + " Level: "
- + realLevel + " CreatorId: " + creator + "\n Desc:"
- + description);
- }
- } catch (Exception error) {
- System.err.println("[ERROR] " + error.getMessage());
- error.printStackTrace();
- System.exit(1);
- }
- }
- public static void changeNotificationCriticality(int newlevel, int notificationId) {
- try {
- Class.forName(driver);
- Connection con = DriverManager.getConnection(url);
- String sql = "UPDATE notification " + "SET handlingLevel="
- + newlevel + " " + "WHERE " + "id= " + notificationId + ";";
- Statement stmt = con.createStatement();
- stmt.executeUpdate(sql);
- debug(
- "Criticality-level of notification ID " + notificationId
- + " changed to " + newlevel);
- } catch (Exception error) {
- System.err.println("[ERROR] " + error.getMessage());
- error.printStackTrace();
- System.exit(1);
- }
- }
- public static void changeNotificationState(int id, int newstate, int notificationId, String handlingDescription) {
- String date = getDate();
- String sql = "";
- try {
- Class.forName(driver);
- Connection con = DriverManager.getConnection(url);
- if (newstate == 1) {
- sql = "UPDATE notification " + "SET handlingState=" + newstate
- + ", " + "handlerId=" + id + ", " + "handlingDate='"
- + date + "' " + "WHERE " + "id= " + notificationId + ";";
- } else if (newstate == 2) {
- sql = "UPDATE notification " + "SET handlingState=" + newstate
- + ", " + "handlerId=" + id + ", " + "handlingDate='"
- + date + "', " + "handlingDescription='"
- + handlingDescription + "' " + "WHERE " + "id= "
- + notificationId + ";";
- }
- Statement stmt = con.createStatement();
- stmt.executeUpdate(sql);
- debug(
- "State of notification ID " + notificationId
- + " has been changed.");
- } catch (Exception error) {
- System.err.println("[ERROR] " + error.getMessage());
- error.printStackTrace();
- System.exit(1);
- }
- }
- public static String getDate() {
- String aikaMuotoiluISO = "yyyy-MM-dd";
- SimpleDateFormat dateFormat = new SimpleDateFormat(aikaMuotoiluISO);
- Date date = new Date();
- String returndate = dateFormat.format(date);
- return returndate;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement