DataHandler angepasst
[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 public class DataHandlerForExcel implements DataHandler {
22 String filePath;
23 Set<User> userList = new HashSet<User>();
24
25 static {
26 try {
27 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
28 } catch (Exception e) {
29 System.err.println(e);
30 }
31 }
32
33 public DataHandlerForExcel(String filePath) throws Exception {
34 this.filePath = filePath;
35 updateUserList();
36 }
37
38 @Override
39 public User findUser(String userName) {
40 if (userName == null)
41 throw new IllegalArgumentException("Username must not be null");
42 for (User user : userList) {
43 if (user.getUserName().equals(userName))
44 return user;
45 }
46 return null;
47 }
48
49 @Override
50 public void addUser(User user) throws Exception {
51 if (findUser(user.getUserName()) != null)
52 throw new Exception("User already exists!");
53 insertNewUser(user);
54 updateUserList();
55 }
56
57 private void updateUserList() throws Exception {
58 Connection conn = null;
59 Statement stmt = null;
60 String sql = "";
61 ResultSet rs = null;
62
63 userList.clear();
64
65 try {
66 conn = DriverManager.getConnection("jdbc:odbc:excel", "", "");
67 stmt = conn.createStatement();
68 sql = "select * from [User$]";
69 rs = stmt.executeQuery(sql);
70
71 while (rs.next()) {
72 User user = new User(rs.getString("USERNAME"), rs.getString("PASSWORD"), rs.getString("MAIL"),
73 rs.getString("NAME"), rs.getString("LASTNAME"));
74 userList.add(user);
75 }
76 } catch (Exception e) {
77 System.err.println(e);
78 } finally {
79 try {
80 rs.close();
81 stmt.close();
82 conn.close();
83 rs = null;
84 stmt = null;
85 conn = null;
86 } catch (Exception e) {
87 }
88 }
89 }
90
91 private void insertNewUser(User user) throws Exception {
92 Connection conn = null;
93 Statement stmt = null;
94 String sql = "";
95 ResultSet rs = null;
96
97 sql = "insert into [User$] values (" + user.getUserName() + "," + user.getEncryptedPassword() + ","
98 + user.getMail() + "," + user.getName() + "," + user.getLastname() + ")";
99
100 try {
101 conn = DriverManager.getConnection("jdbc:odbc:excel", "", "");
102 stmt = conn.createStatement();
103 stmt.executeQuery(sql);
104 } catch (Exception e) {
105 System.err.println(e);
106 } finally {
107 try {
108 rs.close();
109 stmt.close();
110 conn.close();
111 rs = null;
112 stmt = null;
113 conn = null;
114 } catch (Exception e) {
115 }
116 }
117 }
118
119 @Override
120 public Resource findResource(String resourceName) {
121 // TODO Auto-generated method stub
122 return null;
123 }
124
125 @Override
126 public void addResource(Resource resource) throws Exception {
127 // TODO Auto-generated method stub
128
129 }
130
131 @Override
132 public void addReservation(Reservation reservation) throws Exception {
133 // TODO Auto-generated method stub
134
135 }
136
137 @Override
138 public Reservation findReservation(UUID id) {
139 // TODO Auto-generated method stub
140 return null;
141 }
142
143 @Override
144 public List<Reservation> findGroupReservations(UUID groupID) {
145 // TODO Auto-generated method stub
146 return null;
147 }
148
149 @Override
150 public List<Reservation> overlaps(DateRange range) {
151 // TODO Auto-generated method stub
152 return null;
153 }
154 }