e38e2a9f528f6e1be9889a5b8b0adb8997fc0a2d
[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 void addUser(User user) throws Exception {
55 if (findUser(user.getUserName()) != null)
56 throw new Exception("User already exists!");
57 insertNewUser(user);
58 updateUserList();
59 }
60
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 }
93 }
94
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 }
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
132
133 }
134
135 @Override
136 public void addReservation(Reservation reservation) throws Exception {
137 // TODO Auto-generated method stub
138
139 }
140
141 @Override
142 public Reservation findReservation(UUID id) {
143 // TODO Auto-generated method stub
144 return null;
145 }
146
147 @Override
148 public List<Reservation> findGroupReservations(UUID groupID) {
149 // TODO Auto-generated method stub
150 return null;
151 }
152
153 @Override
154 public List<Reservation> overlaps(DateRange range) {
155 // TODO Auto-generated method stub
156 return null;
157 }
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 }
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 }
182 }