/Users/16484/Desktop/IA_Final_Submission_0003070061/Product/IA_Netbeans_Project/src/ia/prototype/shubha/n/Reminder.java
  1 /*
  2  * To change this license header, choose License Headers in Project Properties.
  3  * To change this template file, choose Tools | Templates
  4  * and open the template in the editor.
  5  */
  6 package ia.prototype.shubha.n;
  7 
  8 import java.sql.*;
  9 import java.text.SimpleDateFormat;
 10 import java.util.*;
 11 import java.util.logging.*;
 12 
 13 /**
 14  *
 15  * @author 16484
 16  */
 17 public class Reminder {
 18 
 19   
 20 
 21     private String name = "";
 22     private String category = "";
 23     private Calendar date; // The calendar class was used in order to best represent the date of the Reminder in an accurate format.
 24     private static Statement stmt = null; // To write the SQL command and execute 
 25     private static Connection cnn = null; // To connect to the database
 26 
 27     public Reminder() {
 28 
 29     }
 30 
 31     public Reminder(String name, String category, Calendar d) {
 32         this.name = name;
 33         this.category = category;
 34         this.date = d;
 35         // This sends this data to the database 
 36         saveReminder();
 37     }
 38 
 39     // x value is to not save reminder.. just to show from the database
 40     public Reminder(String name, String category, Calendar d, int x) {
 41         this.name = name;
 42         this.category = category;
 43         this.date = d;
 44         
 45     }
 46     public void setName(String name) {
 47         this.name = name;
 48     }
 49 
 50     public void setCategory(String category) {
 51         this.category = category;
 52     }
 53 
 54     public void setDateTime(Calendar date) {
 55         this.date = date;
 56     }
 57 
 58     public String getName() {
 59         return name;
 60     }
 61 
 62     public String getCategory() {
 63         return category;
 64     }
 65 
 66     public Calendar getDateTime() {
 67         return date;
 68     }
 69 
 70     /* Database Code */
 71     boolean saveReminder() {
 72         try {
 73 
 74             String dt = date.get(Calendar.YEAR) + "-" + (date.get(Calendar.MONTH) + 1) + "-" + date.get(Calendar.DATE);
 75             String tm = date.get(Calendar.HOUR_OF_DAY) + ":" + date.get(Calendar.MINUTE);
 76 
 77             String sql = "insert into Reminders values ("
 78                     + "'" + name + "',"
 79                     + "'" + dt + "', '" + tm + "',"
 80                     + "'" + category + "')";
 81 
 82             if (createConnection()) {
 83 
 84                 stmt = cnn.createStatement();
 85                 stmt.executeUpdate(sql);
 86                 cnn.close();
 87                 return true;
 88             }
 89         } catch (SQLException ex) {
 90             ex.printStackTrace(); // to know what error happenned 
 91             return false;
 92         }
 93         return false;
 94 
 95     }
 96 
 97     private static boolean createConnection() {
 98         try {
 99             Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
100             cnn = DriverManager.getConnection(CommonCode.dbURL);
101             return true;
102         } catch (Exception ex) {
103             ex.printStackTrace();
104             return false;
105         }
106     }
107 
108     static ArrayList<Reminder> getAllReminders() {
109         // Retrieval of Reminders from the database.
110         ArrayList<Reminder> R = new ArrayList();
111         try {
112             String sql = "select * from Reminders order by date, time"; // * is for "All columns of the reminder table"
113             Reminder T;
114 
115             if (createConnection()) {
116 
117                 stmt = cnn.createStatement();
118 
119                 ResultSet rs = stmt.executeQuery(sql);
120                 while (rs.next()) {
121                     SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
122                     java.util.Date date = formatter.parse(rs.getString(2));
123                     Calendar C = Calendar.getInstance();
124                     C.setTime(date);
125                     C.set(Calendar.HOUR_OF_DAY, rs.getTime(3).getHours());
126                     C.set(Calendar.MINUTE, rs.getTime(3).getMinutes());
127                     //C.set(Calendar.HOUR_OF_DAY, 0);
128                     T = new Reminder(rs.getString(1), rs.getString(4), C, 1);
129                     R.add(T);
130                 }
131                 cnn.close();
132             }
133         } catch (Exception ex) {
134             ex.printStackTrace();
135             return null;
136         }
137         return R;
138     }
139     
140     public String getDate()
141     {
142         return   date.get(Calendar.DATE) + "-" + (date.get(Calendar.MONTH) + 1) + "-" + date.get(Calendar.YEAR);
143     }
144     public String getTime()
145     {
146                 return date.get(Calendar.HOUR_OF_DAY) + ":" + date.get(Calendar.MINUTE);
147     }
148      static ArrayList<Reminder> getTodayReminders() {
149         // Retrieval of Reminders from the database.
150         ArrayList<Reminder> Reminder = new ArrayList();
151         try {
152             String sql = "select * from Reminders " +
153                     " where date = CURRENT_DATE " 
154                     + " order by date,time"; // * is for "All columns of the reminder table"
155             
156             Reminder T;
157 
158             if (createConnection()) {
159 
160                 stmt = cnn.createStatement();
161 
162                 ResultSet rs = stmt.executeQuery(sql);
163                 while (rs.next()) {
164                     SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
165                     java.util.Date date = formatter.parse(rs.getString(2));
166                     Calendar C = Calendar.getInstance();
167                     C.setTime(date);
168                     C.set(Calendar.HOUR_OF_DAY, rs.getTime(3).getHours());
169                     C.set(Calendar.MINUTE, rs.getTime(3).getMinutes());
170                     //C.set(Calendar.HOUR_OF_DAY, 0);
171                     T = new Reminder(rs.getString(1), rs.getString(4), C, 1);
172                     Reminder.add(T);
173                 }
174                 cnn.close();
175             }
176         } catch (Exception ex) {
177             ex.printStackTrace();
178             return null;
179         }
180         return Reminder;
181     }
182 }
183