Create ResourceManagerController, create Modify interface in dataschema,
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / noTest / DataHandlerForExcel.java
CommitLineData
66cc653d
D
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 */
7903e50e
M
8package ch.bfh.ti.srs.noTest;
9
10import java.sql.Connection;
11import java.sql.DriverManager;
12import java.sql.ResultSet;
13import java.sql.Statement;
14import java.util.HashSet;
be594ada 15import java.util.List;
7903e50e 16import java.util.Set;
be594ada 17import java.util.UUID;
7903e50e
M
18
19import ch.bfh.ti.daterange.DateRange;
20import ch.bfh.ti.srs.datahandler.DataHandler;
21import ch.bfh.ti.srs.dataschema.Reservation;
22import ch.bfh.ti.srs.dataschema.Resource;
23import ch.bfh.ti.srs.dataschema.User;
25d43fa9 24
7903e50e
M
25/**
26 * @author Michael Grossen
27 */
b5b8ffc1
M
28/**
29 * @author Michael
b5b8ffc1 30 */
25d43fa9 31public class DataHandlerForExcel implements DataHandler {
1e4d6813
M
32 private String filePath;
33 private Set<User> userList = new HashSet<User>();
25d43fa9
M
34
35 static {
36 try {
37 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
38 } catch (Exception e) {
39 System.err.println(e);
40 }
7903e50e 41 }
25d43fa9
M
42
43 public DataHandlerForExcel(String filePath) throws Exception {
7903e50e
M
44 this.filePath = filePath;
45 updateUserList();
46 }
25d43fa9 47
7903e50e
M
48 @Override
49 public User findUser(String userName) {
25d43fa9
M
50 if (userName == null)
51 throw new IllegalArgumentException("Username must not be null");
7903e50e 52 for (User user : userList) {
25d43fa9
M
53 if (user.getUserName().equals(userName))
54 return user;
7903e50e
M
55 }
56 return null;
57 }
58
59 @Override
5ff98db0 60 public boolean addUser(User user) throws Exception {
25d43fa9
M
61 if (findUser(user.getUserName()) != null)
62 throw new Exception("User already exists!");
66cc653d
D
63 else {
64 insertNewUser(user);
65 updateUserList();
66 return true;
5ff98db0 67 }
7903e50e
M
68 }
69
25d43fa9
M
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 }
7903e50e
M
102 }
103
25d43fa9
M
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 }
7903e50e
M
130 }
131
132 @Override
133 public Resource findResource(String resourceName) {
134 // TODO Auto-generated method stub
135 return null;
136 }
137
138 @Override
5ff98db0 139 public boolean addResource(Resource resource) throws Exception {
7903e50e 140 // TODO Auto-generated method stub
5ff98db0 141 return false;
7903e50e
M
142 }
143
144 @Override
5ff98db0 145 public boolean addReservation(Reservation reservation) throws Exception {
be594ada 146 // TODO Auto-generated method stub
5ff98db0 147 return false;
be594ada
M
148 }
149
150 @Override
151 public Reservation findReservation(UUID id) {
7903e50e
M
152 // TODO Auto-generated method stub
153 return null;
154 }
155
156 @Override
be594ada 157 public List<Reservation> findGroupReservations(UUID groupID) {
7903e50e 158 // TODO Auto-generated method stub
be594ada
M
159 return null;
160 }
161
162 @Override
163 public List<Reservation> overlaps(DateRange range) {
164 // TODO Auto-generated method stub
165 return null;
7903e50e 166 }
a77189a6
M
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 }
b5b8ffc1
M
179
180 @Override
181 public void saveChanges() {
182 // TODO Auto-generated method stub
66cc653d 183
b5b8ffc1
M
184 }
185
186 @Override
187 public void refresh(boolean discardChanges) throws Exception {
188 // TODO Auto-generated method stub
66cc653d 189
b5b8ffc1 190 }
e16f8b0c
M
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
e86af888 205 public Set<Resource> getAllResources() {
e16f8b0c
M
206 // TODO Auto-generated method stub
207 return null;
208 }
5fca88b4
MT
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 }
7903e50e 221}