UserController extended and new methods of DataHander in
[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
54 public void addUser(User user) throws Exception {
25d43fa9
M
55 if (findUser(user.getUserName()) != null)
56 throw new Exception("User already exists!");
7903e50e
M
57 insertNewUser(user);
58 updateUserList();
59 }
60
25d43fa9
M
61 private void updateUserList() throws Exception {
62 Connection conn = null;
63 Statement stmt = null;
64 String sql = "";
65 ResultSet rs = null;
66
67 userList.clear();
68
69 try {
70 conn = DriverManager.getConnection("jdbc:odbc:excel", "", "");
71 stmt = conn.createStatement();
72 sql = "select * from [User$]";
73 rs = stmt.executeQuery(sql);
74
75 while (rs.next()) {
76 User user = new User(rs.getString("USERNAME"), rs.getString("PASSWORD"), rs.getString("MAIL"),
77 rs.getString("NAME"), rs.getString("LASTNAME"));
78 userList.add(user);
79 }
80 } catch (Exception e) {
81 System.err.println(e);
82 } finally {
83 try {
84 rs.close();
85 stmt.close();
86 conn.close();
87 rs = null;
88 stmt = null;
89 conn = null;
90 } catch (Exception e) {
91 }
92 }
7903e50e
M
93 }
94
25d43fa9
M
95 private void insertNewUser(User user) throws Exception {
96 Connection conn = null;
97 Statement stmt = null;
98 String sql = "";
99 ResultSet rs = null;
100
101 sql = "insert into [User$] values (" + user.getUserName() + "," + user.getEncryptedPassword() + ","
102 + user.getMail() + "," + user.getName() + "," + user.getLastname() + ")";
103
104 try {
105 conn = DriverManager.getConnection("jdbc:odbc:excel", "", "");
106 stmt = conn.createStatement();
107 stmt.executeQuery(sql);
108 } catch (Exception e) {
109 System.err.println(e);
110 } finally {
111 try {
112 rs.close();
113 stmt.close();
114 conn.close();
115 rs = null;
116 stmt = null;
117 conn = null;
118 } catch (Exception e) {
119 }
120 }
7903e50e
M
121 }
122
123 @Override
124 public Resource findResource(String resourceName) {
125 // TODO Auto-generated method stub
126 return null;
127 }
128
129 @Override
130 public void addResource(Resource resource) throws Exception {
131 // TODO Auto-generated method stub
25d43fa9 132
7903e50e
M
133 }
134
135 @Override
be594ada
M
136 public void addReservation(Reservation reservation) throws Exception {
137 // TODO Auto-generated method stub
25d43fa9 138
be594ada
M
139 }
140
141 @Override
142 public Reservation findReservation(UUID id) {
7903e50e
M
143 // TODO Auto-generated method stub
144 return null;
145 }
146
147 @Override
be594ada 148 public List<Reservation> findGroupReservations(UUID groupID) {
7903e50e 149 // TODO Auto-generated method stub
be594ada
M
150 return null;
151 }
152
153 @Override
154 public List<Reservation> overlaps(DateRange range) {
155 // TODO Auto-generated method stub
156 return null;
7903e50e 157 }
a77189a6
M
158
159 @Override
160 public List<Reservation> userReservations(User user) {
161 // TODO Auto-generated method stub
162 return null;
163 }
164
165 @Override
166 public List<Reservation> findAllReservations() {
167 // TODO Auto-generated method stub
168 return null;
169 }
b5b8ffc1
M
170
171 @Override
172 public void saveChanges() {
173 // TODO Auto-generated method stub
174
175 }
176
177 @Override
178 public void refresh(boolean discardChanges) throws Exception {
179 // TODO Auto-generated method stub
180
181 }
7903e50e 182}