public NewTaskFrame() throws SQLException { this.setModal(true); this.setTitle("Create Task"); // title of frame this.setSize(400, 350); this.setLayout(new BorderLayout()); // layout of frame JLabel task_id = new JLabel("ID:"); JLabel task_name = new JLabel("Name:"); JLabel task_desc = new JLabel("Description:"); JLabel priority = new JLabel("Level of Priority:"); JLabel duedate = new JLabel("Due Date (yyyy-mm-dd)"); JLabel numberofdays = new JLabel("Number of days required:"); JLabel skills_required = new JLabel("Skills Required:"); Object[] id_possibilities = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; StringBuffer generated_name = new StringBuffer("t"); for (int i = 0; i < 6; ++i) { generated_name.append( id_possibilities[new java.util.Random().nextInt(id_possibilities.length)]); } task_idfield = new JTextField(generated_name.toString()); taskname_field = new JTextField(); taskdesc_field = new JTextArea(); priority_checkbox = new JCheckBox("High", false); numberofdays_field = new JTextField(); format = new SimpleDateFormat("dd-MM-yyyy"); // set format duedate_field = new JFormattedTextField(format); // set the duedatefield to tomorrow (since minimum task length = 1) java.util.Calendar cal = java.util.Calendar.getInstance(); cal.setTime(new Date()); // get current date cal.add(Calendar.DAY_OF_MONTH, 1); // add 1 to current date Date date = cal.getTime(); // get this date duedate_field.setValue(date); // set this date to the field // panel to hold labels and text fields JPanel labeltextpanel = new JPanel(); labeltextpanel.setLayout(new GridLayout(7, 2)); labeltextpanel.add(task_id); labeltextpanel.add(task_idfield); labeltextpanel.add(task_name); labeltextpanel.add(taskname_field); labeltextpanel.add(task_desc); labeltextpanel.add(taskdesc_field); labeltextpanel.add(priority); labeltextpanel.add(priority_checkbox); labeltextpanel.add(numberofdays); labeltextpanel.add(numberofdays_field); labeltextpanel.add(duedate); labeltextpanel.add(duedate_field); labeltextpanel.add(skills_required); this.getSkills(); // main panel for frame including its layout JPanel mainPanel = new JPanel(); mainPanel.setLayout(new BorderLayout()); mainPanel.add(labeltextpanel, BorderLayout.CENTER); // buttons clear_button = new JButton("Clear"); submit_button = new JButton("Submit"); // add listeners to buttons clear_button.addActionListener(this); submit_button.addActionListener(this); // create panel for buttons and add buttons JPanel buttonpanel = new JPanel(); buttonpanel.setLayout(new FlowLayout()); buttonpanel.add(clear_button); buttonpanel.add(submit_button); JPanel checkbox_panel = new JPanel(); checkbox_panel.setLayout(new FlowLayout()); for (int i = 0; i < skill_list.size(); i++) { checkbox_panel.add(skill_list.get(i)); // make this panel flowlayout } scrollpane = new JScrollPane(checkbox_panel); scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.add(mainPanel, BorderLayout.CENTER); panel.add(scrollpane, BorderLayout.SOUTH); // add the main panel to main frame add(panel, BorderLayout.CENTER); add(buttonpanel, BorderLayout.SOUTH); this.setVisible(true); }
/** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String[] courseids = request.getParameterValues("courses"); // Saving the course ids as a session attribute (not necessary, might be better for saving) HttpSession session = request.getSession(true); session.setAttribute("courses", courseids); // Get course and time info for each id if (courseids == null) { } else { try { for (String id : courseids) { Integer courseid = Integer.parseInt(id); PreparedStatement getCourse = con.prepareStatement( "SELECT title, department, number, section " + "FROM courses WHERE id=?;"); getCourse.setInt(1, courseid); PreparedStatement getTime = con.prepareStatement( "SELECT day, starttime, endtime FROM times " + "WHERE course_id=?;"); getTime.setInt(1, courseid); ResultSet courseResult = getCourse.executeQuery(); ResultSet timesResult = getTime.executeQuery(); while (courseResult.next()) { // Generating the course from the DB, lets us utilize timeblocks Course course = new Course(); course.setTitle(courseResult.getString("title")); course.setDepartment(courseResult.getString("department")); course.setNumber(courseResult.getString("number")); course.setSection(courseResult.getString("section")); String courseTitle = course.getDepartment() + course.getNumber() + "-" + course.getSection() + ": " + course.getTitle(); while (timesResult.next()) { course.addTime( timesResult.getString("day").charAt(0), timesResult.getInt("starttime"), timesResult.getInt("endtime")); } // Now parse the timeshttp://ical4j.sourceforge.net/introduction.html for the timeblocks Map<String, Set<Character>> timeblocks = course.getTimeBlocks(); for (String startend : timeblocks.keySet()) { String[] StartEnd = startend.split("-"); Integer startInt = Integer.parseInt(StartEnd[0]); Integer endInt = Integer.parseInt(StartEnd[1]); java.util.Calendar startDate = this.getTimeCalendar(startInt); java.util.Calendar endDate = this.getTimeCalendar(endInt); DateTime startTime = new DateTime(startDate.getTime()); DateTime endTime = new DateTime(endDate.getTime()); VEvent courseEvent = new VEvent(startTime, endTime, courseTitle); Recur recur = new Recur(Recur.WEEKLY, null); // add the proper days to the recurrence for (char day : timeblocks.get(startend)) { recur.getDayList().add(dayMap.get(day)); } courseEvent.getProperties().add(new RRule(recur)); // Get unique identifier UidGenerator ug = new UidGenerator("uidGen"); Uid uid = ug.generateUid(); courseEvent.getProperties().add(uid); // Finally, add the time block to the calendar this.ical.getComponents().add(courseEvent); } } } } catch (SQLException e) { e.printStackTrace(); } } // Time to handle file download try { // making the file and outputting the calendar info File file = new File("mycalendar.ics"); int length = 0; FileOutputStream fout = new FileOutputStream(file); CalendarOutputter outputter = new CalendarOutputter(); outputter.output(ical, fout); ServletOutputStream outStream = response.getOutputStream(); response.setContentType("text/calendar"); response.setContentLength((int) file.length()); // sets HTTP header response.setHeader("Content-Disposition", "attachment; filename=calendar.ics"); byte[] byteBuffer = new byte[BUFSIZE]; DataInputStream in = new DataInputStream(new FileInputStream(file)); // reads the file's bytes and writes them to the response stream while ((in != null) && ((length = in.read(byteBuffer)) != -1)) { outStream.write(byteBuffer, 0, length); } in.close(); outStream.close(); } catch (ValidationException e) { // TODO Auto-generated catch block e.printStackTrace(); } }