Merge branch 'development' of git@git.bfh.ch:/staff/due1/sed-hs15-srs-purple.git...
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / noTest / DataHandlerForExcel.java
CommitLineData
7903e50e
M
1package ch.bfh.ti.srs.noTest;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.ResultSet;
6import java.sql.Statement;
7import java.util.HashSet;
be594ada 8import java.util.List;
7903e50e 9import java.util.Set;
be594ada 10import java.util.UUID;
7903e50e
M
11
12import ch.bfh.ti.daterange.DateRange;
13import ch.bfh.ti.srs.datahandler.DataHandler;
14import ch.bfh.ti.srs.dataschema.Reservation;
15import ch.bfh.ti.srs.dataschema.Resource;
16import ch.bfh.ti.srs.dataschema.User;
25d43fa9 17
7903e50e
M
18/**
19 * @author Michael Grossen
20 */
b5b8ffc1
M
21/**
22 * @author Michael
23 *
24 */
25d43fa9 25public class DataHandlerForExcel implements DataHandler {
1e4d6813
M
26 private String filePath;
27 private Set<User> userList = new HashSet<User>();
25d43fa9
M
28
29 static {
30 try {
31 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
32 } catch (Exception e) {
33 System.err.println(e);
34 }
7903e50e 35 }
25d43fa9
M
36
37 public DataHandlerForExcel(String filePath) throws Exception {
7903e50e
M
38 this.filePath = filePath;
39 updateUserList();
40 }
25d43fa9 41
7903e50e
M
42 @Override
43 public User findUser(String userName) {
25d43fa9
M
44 if (userName == null)
45 throw new IllegalArgumentException("Username must not be null");
7903e50e 46 for (User user : userList) {
25d43fa9
M
47 if (user.getUserName().equals(userName))
48 return user;
7903e50e
M
49 }
50 return null;
51 }
52
53 @Override
5ff98db0 54 public boolean addUser(User user) throws Exception {
25d43fa9
M
55 if (findUser(user.getUserName()) != null)
56 throw new Exception("User already exists!");
5ff98db0 57 else{
7903e50e
M
58 insertNewUser(user);
59 updateUserList();
5ff98db0
M
60 return true;
61 }
7903e50e
M
62 }
63
25d43fa9
M
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 }
7903e50e
M
96 }
97
25d43fa9
M
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 }
7903e50e
M
124 }
125
126 @Override
127 public Resource findResource(String resourceName) {
128 // TODO Auto-generated method stub
129 return null;
130 }
131
132 @Override
5ff98db0 133 public boolean addResource(Resource resource) throws Exception {
7903e50e 134 // TODO Auto-generated method stub
5ff98db0 135 return false;
7903e50e
M
136 }
137
138 @Override
5ff98db0 139 public boolean addReservation(Reservation reservation) throws Exception {
be594ada 140 // TODO Auto-generated method stub
5ff98db0 141 return false;
be594ada
M
142 }
143
144 @Override
145 public Reservation findReservation(UUID id) {
7903e50e
M
146 // TODO Auto-generated method stub
147 return null;
148 }
149
150 @Override
be594ada 151 public List<Reservation> findGroupReservations(UUID groupID) {
7903e50e 152 // TODO Auto-generated method stub
be594ada
M
153 return null;
154 }
155
156 @Override
157 public List<Reservation> overlaps(DateRange range) {
158 // TODO Auto-generated method stub
159 return null;
7903e50e 160 }
a77189a6
M
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 }
b5b8ffc1
M
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 }
7903e50e 185}