Manage Legal Notices functionality
[habeas.git] / src / habeas / Utility.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 habeas;
7
8 import java.util.ArrayList;
9 import java.util.prefs.Preferences;
10 import java.sql.*;
11 import java.util.logging.Level;
12 import java.util.logging.Logger;
13
14 /**
15 *
16 * @author hari
17 */
18 public class Utility {
19
20 static void setConnectionURL(String text) {
21 connectionURL = text;
22 Preferences.userRoot().put("ConnectionURL", text);
23 }
24
25 static ArrayList<Object> getClientDetails(int r) {
26 ArrayList<Object> res = new ArrayList <>();
27 try {
28 Connection conn = DriverManager.getConnection("jdbc:sqlite:"+connectionURL);
29 PreparedStatement st = conn.prepareStatement("SELECT * FROM clients WHERE id=?;");
30 st.setInt(1, r);
31 ResultSet rs = st.executeQuery();
32 while (rs.next()) {
33 res.add (rs.getString("ClientName"));
34 res.add(rs.getString("ClientAddress"));
35 res.add (rs.getString("ContactPerson"));
36 res.add (rs.getString("MailID"));
37 res.add (rs.getString("ContactNumber"));
38 }
39 conn.close ();
40 return res;
41
42 } catch (SQLException ex) {
43 Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
44 return null;
45 }
46
47 }
48
49 static boolean addClient(String client_name, String client_address,
50 String contact_person, String email_id, String phone_number) {
51 if ("".equals(client_name))
52 return false;
53 try {
54 Connection conn = DriverManager.getConnection("jdbc:sqlite:" + connectionURL);
55 PreparedStatement st = conn.prepareStatement("INSERT INTO clients (ClientName,"
56 + "ClientAddress, ContactPerson, MailID, ContactNumber) VALUES (?, ?, ?, ?, ?);");
57 st.setString(1, client_name);
58 st.setString(2, client_address);
59 st.setString(3, contact_person);
60 st.setString (4, email_id);
61 st.setString (5, phone_number);
62 st.execute();
63 conn.close();
64 return true;
65
66 } catch (SQLException ex) {
67 Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
68 return false;
69 }
70
71
72 }
73
74 static boolean deleteClient(int r) {
75 try {
76 Connection conn = DriverManager.getConnection("jdbc:sqlite:" + connectionURL);
77 PreparedStatement st = conn.prepareStatement("DELETE FROM clients WHERE id=?;");
78 PreparedStatement st2 = conn.prepareStatement("DELETE FROM legalnotices WHERE ClientId=?;");
79 st.setInt(1, r);
80 st2.setInt (1, r);
81 st.execute();
82 st2.execute();
83 conn.close();
84 return true;
85 } catch (SQLException ex) {
86 Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
87 return false;
88 }
89
90 }
91
92 static boolean updateClient(int r, String client_name, String client_address,
93 String contact_person, String mail_id, String phone_number) {
94 if ("".equals(client_name))
95 return false;
96 try {
97 Connection conn = DriverManager.getConnection("jdbc:sqlite:" + connectionURL);
98 PreparedStatement st = conn.prepareStatement("UPDATE clients SET "
99 + "ClientName=?, ClientAddress=?,"
100 + "ContactPerson=?, MailID=?, ContactNumber=? WHERE id=?;");
101 st.setString(1, client_name);
102 st.setString(2, client_address);
103 st.setString(3, contact_person);
104 st.setString(4, mail_id);
105 st.setString(5, phone_number);
106 st.setInt (6, r);
107 st.execute();
108 conn.close();
109 return true;
110 } catch (SQLException ex) {
111 Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
112 return false;
113 }
114
115
116 }
117
118 static boolean addLegalNotice(String reference_number,
119 String description, java.util.Date entrustment_date, DBItem client) {
120 if ("".equals(reference_number) || "".equals(description) ||
121 entrustment_date == null || client == null)
122 return false;
123 try {
124 Connection conn = DriverManager.getConnection("jdbc:sqlite:" + connectionURL);
125 PreparedStatement st = conn.prepareStatement("INSERT INTO legalnotices"
126 + " (ReferenceNumber, Description, EntrustmentDate, ClientId) "
127 + "VALUES (?, ?, ?, ?);");
128 st.setString(1, reference_number);
129 st.setString(2, description);
130 st.setLong(3, entrustment_date.getTime()/1000);
131 st.setInt (4, client.getKey());
132 st.execute();
133 conn.close();
134 return true;
135
136 } catch (SQLException ex) {
137 Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
138 return false;
139 }
140
141 }
142
143 static ArrayList<Object> getNotices() {
144 ArrayList<Object> notices = new ArrayList<>();
145 try {
146 Connection conn = DriverManager.getConnection("jdbc:sqlite:" + connectionURL);
147 Statement st = conn.createStatement();
148 ResultSet rs = st.executeQuery("SELECT id, ReferenceNumber, Description "
149 + "FROM legalnotices;");
150 while (rs.next()) {
151 notices.add(rs.getInt("id"));
152 notices.add(rs.getString("ReferenceNumber"));
153 notices.add(rs.getString("Description"));
154 }
155 conn.close();
156 return notices;
157 } catch (SQLException ex) {
158 Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
159 return null;
160 }
161
162 }
163 public Utility () {
164
165 }
166 public static String connectionURL ;
167
168 /**
169 *
170 * @return
171 */
172 public static ArrayList<Object> getClientsNameAndId () {
173 ArrayList<Object> data = new ArrayList<>();
174 try {
175 Connection conn = DriverManager.getConnection("jdbc:sqlite:" + connectionURL);
176 Statement st = conn.createStatement();
177 ResultSet rs = st.executeQuery("SELECT id, ClientName from clients;");
178 while (rs.next()) {
179 data.add(rs.getInt("id"));
180 data.add(rs.getString("ClientName"));
181 }
182 conn.close();
183 return data;
184 } catch (SQLException ex) {
185 Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
186 return null;
187 }
188
189 }
190
191 public static void getConnectionURL () {
192 connectionURL = Preferences.userRoot().get("ConnectionURL", "legaldb");
193 }
194
195 }