01d308b12a8944bb0056ea2a510545c070aea3ba
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / noTest / DataHandlerForExcel.java
1 /*
2 * Copyright (c) 2015 Berner Fachhochschule, Switzerland.
3 *
4 * Project Smart Reservation System.
5 *
6 * Distributable under GPL license. See terms of license at gnu.org.
7 */
8 package ch.bfh.ti.srs.noTest;
9
10 import java.sql.Connection;
11 import java.sql.DriverManager;
12 import java.sql.ResultSet;
13 import java.sql.Statement;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Set;
17 import java.util.UUID;
18
19 import ch.bfh.ti.daterange.DateRange;
20 import ch.bfh.ti.srs.datahandler.DataHandler;
21 import ch.bfh.ti.srs.dataschema.Reservation;
22 import ch.bfh.ti.srs.dataschema.Resource;
23 import ch.bfh.ti.srs.dataschema.User;
24
25 /**
26 * @author Michael Grossen
27 */
28 /**
29 * @author Michael
30 */
31 public class DataHandlerForExcel implements DataHandler {
32 private String filePath;
33 private Set<User> userList = new HashSet<User>();
34
35 static {
36 try {
37 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
38 } catch (Exception e) {
39 System.err.println(e);
40 }
41 }
42
43 public DataHandlerForExcel(String filePath) throws Exception {
44 this.filePath = filePath;
45 updateUserList();
46 }
47
48 @Override
49 public User findUser(String userName) {
50 if (userName == null)
51 throw new IllegalArgumentException("Username must not be null");
52 for (User user : userList) {
53 if (user.getUserName().equals(userName))
54 return user;
55 }
56 return null;
57 }
58
59 @Override
60 public boolean addUser(User user) throws Exception {
61 if (findUser(user.getUserName()) != null)
62 throw new Exception("User already exists!");
63 else {
64 insertNewUser(user);
65 updateUserList();
66 return true;
67 }
68 }
69
70 private void updateUserList() throws Exception {
71 Connection conn = null;
72 Statement stmt = null;
73 String sql = "";
74 ResultSet rs = null;
75
76 userList.clear();
77
78 try {
79 conn = DriverManager.getConnection("jdbc:odbc:excel", "", "");
80 stmt = conn.createStatement();
81 sql = "select * from [User$]";
82 rs = stmt.executeQuery(sql);
83
84 while (rs.next()) {
85 User user = new User(rs.getString("USERNAME"), rs.getString("PASSWORD"), rs.getString("MAIL"),
86 rs.getString("NAME"), rs.getString("LASTNAME"));
87 userList.add(user);
88 }
89 } catch (Exception e) {
90 System.err.println(e);
91 } finally {
92 try {
93 rs.close();
94 stmt.close();
95 conn.close();
96 rs = null;
97 stmt = null;
98 conn = null;
99 } catch (Exception e) {
100 }
101 }
102 }
103
104 private void insertNewUser(User user) throws Exception {
105 Connection conn = null;
106 Statement stmt = null;
107 String sql = "";
108 ResultSet rs = null;
109
110 sql = "insert into [User$] values (" + user.getUserName() + "," + user.getEncryptedPassword() + ","
111 + user.getMail() + "," + user.getName() + "," + user.getLastname() + ")";
112
113 try {
114 conn = DriverManager.getConnection("jdbc:odbc:excel", "", "");
115 stmt = conn.createStatement();
116 stmt.executeQuery(sql);
117 } catch (Exception e) {
118 System.err.println(e);
119 } finally {
120 try {
121 rs.close();
122 stmt.close();
123 conn.close();
124 rs = null;
125 stmt = null;
126 conn = null;
127 } catch (Exception e) {
128 }
129 }
130 }
131
132 @Override
133 public Resource findResource(String resourceName) {
134 // TODO Auto-generated method stub
135 return null;
136 }
137
138 @Override
139 public boolean addResource(Resource resource) throws Exception {
140 // TODO Auto-generated method stub
141 return false;
142 }
143
144 @Override
145 public boolean addReservation(Reservation reservation) throws Exception {
146 // TODO Auto-generated method stub
147 return false;
148 }
149
150 @Override
151 public Reservation findReservation(UUID id) {
152 // TODO Auto-generated method stub
153 return null;
154 }
155
156 @Override
157 public List<Reservation> findGroupReservations(UUID groupID) {
158 // TODO Auto-generated method stub
159 return null;
160 }
161
162 @Override
163 public List<Reservation> overlaps(DateRange range) {
164 // TODO Auto-generated method stub
165 return null;
166 }
167
168 @Override
169 public List<Reservation> userReservations(User user) {
170 // TODO Auto-generated method stub
171 return null;
172 }
173
174 @Override
175 public List<Reservation> findAllReservations() {
176 // TODO Auto-generated method stub
177 return null;
178 }
179
180 @Override
181 public void saveChanges() {
182 // TODO Auto-generated method stub
183
184 }
185
186 @Override
187 public void refresh(boolean discardChanges) throws Exception {
188 // TODO Auto-generated method stub
189
190 }
191
192 @Override
193 public boolean editReservation(Reservation reservation) {
194 // TODO Auto-generated method stub
195 return false;
196 }
197
198 @Override
199 public boolean deleteReservation(Reservation reservation) {
200 // TODO Auto-generated method stub
201 return false;
202 }
203
204 @Override
205 public Set<Resource> getAllResources() {
206 // TODO Auto-generated method stub
207 return null;
208 }
209
210 @Override
211 public boolean editResource(Resource resource) {
212 // TODO Auto-generated method stub
213 return false;
214 }
215
216 @Override
217 public boolean deleteResource(Resource resource) {
218 // TODO Auto-generated method stub
219 return false;
220 }
221
222 @Override
223 public boolean editUser(User user) {
224 // TODO Auto-generated method stub
225 return false;
226 }
227 }