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