// method to retrieve all the skills that are stored in the database and add it to arraylist public void getSkills() throws SQLException { skill_list = new ArrayList<JCheckBox>(); // initialise list SqlConnection.connect(); // connect to database SqlConnection.statement = SqlConnection.connection.createStatement(); SqlConnection.statement.executeQuery("SELECT skill_id FROM SKILL"); // get all skills SqlConnection.result = SqlConnection.statement.getResultSet(); // store skills while (SqlConnection.result.next()) { skill_list.add( new JCheckBox(SqlConnection.result.getString("skill_id"))); // add skill skill to list } }
public void actionPerformed(ActionEvent event) { // if clear button is pressed, clear all fields if (event.getSource() == clear_button) { // erase all fields taskname_field.setText(""); taskdesc_field.setText(""); priority_checkbox.setSelected(false); numberofdays_field.setText(""); // isallocatedcheckbox.setSelected(false); duedate_field.setText(""); } // if submit button is pressed, save all data onto database if (event.getSource() == submit_button) { try { { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); // set to current date calendar.add( Calendar.DAY_OF_MONTH, Integer.parseInt(this.numberofdays_field.getText()) - 1); // add days required... then: // ^ subtract one day, because if task is created today and allocated tomorrow for one // day, // then it should be done by end of day tomorrow Date date = calendar.getTime(); // (see next *1) use this date to be the earliest // because of expected task duration /*This is a constraint * the date and the length of days that user expected to take * if not feasible then the program will say so * */ // method compares the current date and the date that was entered by the user if (this.task_idfield.getText().length() != 7 // check chars || this.taskname_field.getText().length() > 50 // check chars || this.taskdesc_field.getText().length() > 50 || ((Date) this.duedate_field.getValue()).compareTo(date) < 0 // check that the date is possible (see last *1) || this.numberofdays_field.getText().length() > 2 || this.numberofdays_field.getText().length() == 0 // check number of digits || Integer.parseInt(this.numberofdays_field.getText()) < 1) // check number of days required for task { JOptionPane.showMessageDialog( this, "Task ID has to be 7 chars; " + "\n" + "Description <= 50 chars; " + "\n" + "Because the expected duration of the task is " + Integer.parseInt(this.numberofdays_field.getText()) + " days, the due date cannot be earlier than: " + new StringBuffer(new SimpleDateFormat("dd/MM/yyyy").format(date)).toString() + "; " + "\n" + "Task duration must be 2 digit number maximum and must be an integer greater than 0"); } else // check if taskid_field is equals to any of the fields on the database if (task_idfield.equals("")) { JOptionPane.showMessageDialog( this, "Please insert an identification number for a task"); } else // add constraint and tell customer to enter 7 values if (taskname_field.equals("")) { JOptionPane.showMessageDialog(this, "Please insert a name to identity the task"); } else { // get all inputs from user inputs and store in variables taskid_input = task_idfield.getText(); taskname_input = taskname_field.getText(); taskdesc_input = taskdesc_field.getText(); if (priority_checkbox.isSelected()) { priority_input = 1; } else { priority_input = 0; } // create and store date from user input date = (Date) duedate_field.getValue(); // store number of days input from user numberofdays_input = Integer.parseInt(numberofdays_field.getText()); // String[] column_names = { "Task ID", "Task Name", "Task Description", "Task // Priority", "Due Date", "Number of Days", "Is Allocated" , "Unallocateable" }; task = new Object[8]; task[0] = taskid_input; task[1] = taskname_input; task[2] = taskdesc_input; task[3] = priority_input; task[4] = date; task[5] = numberofdays_input; task[6] = 0; task[7] = 0; try { // SqlConnection.connect(); SqlConnection.ps = SqlConnection.connection.prepareStatement( "INSERT INTO TASK VALUES (?,?,?,?,?,?,?,?)"); SqlConnection.ps.setString(1, taskid_input); SqlConnection.ps.setString(2, taskname_input); SqlConnection.ps.setString(3, taskdesc_input); SqlConnection.ps.setInt(4, priority_input); SqlConnection.ps.setDate(5, new java.sql.Date(date.getTime())); SqlConnection.ps.setInt(6, numberofdays_input); SqlConnection.ps.setInt(7, 0); SqlConnection.ps.setInt(8, 0); SqlConnection.ps.executeUpdate(); // now enter skills into TASK SKILL database for (int i = 0; i < skill_list.size(); i++) { if (skill_list.get(i).isSelected()) { String skillid = skill_list.get(i).getText(); SqlConnection.statement.executeUpdate( "INSERT INTO TASKSKILL VALUES ('" + taskid_input + "', '" + skillid + "')"); } } SqlConnection.ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } // SqlConnection.ps.close(); SqlConnection.closeConnection(); this.setVisible(false); } } } catch (Exception exc) { JOptionPane.showMessageDialog( this, "Check your inputs:" + "\n" + "Priority has to be a 1-digit number;" + "\n" + "Date has to be in the format dd/MM/yyyy;" + "\n" + "Task length has to be 2-digit number"); System.out.println(exc); } } // display dialog box asking task manager if they want to quit, // if task manager does not want to quit, go back to adding more tasks // if customer quits, close the dialog box and the frame // now close the window after saving }