set instance variables to private
[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 */
25d43fa9 21public class DataHandlerForExcel implements DataHandler {
1e4d6813
M
22 private String filePath;
23 private Set<User> userList = new HashSet<User>();
25d43fa9
M
24
25 static {
26 try {
27 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
28 } catch (Exception e) {
29 System.err.println(e);
30 }
7903e50e 31 }
25d43fa9
M
32
33 public DataHandlerForExcel(String filePath) throws Exception {
7903e50e
M
34 this.filePath = filePath;
35 updateUserList();
36 }
25d43fa9 37
7903e50e
M
38 @Override
39 public User findUser(String userName) {
25d43fa9
M
40 if (userName == null)
41 throw new IllegalArgumentException("Username must not be null");
7903e50e 42 for (User user : userList) {
25d43fa9
M
43 if (user.getUserName().equals(userName))
44 return user;
7903e50e
M
45 }
46 return null;
47 }
48
49 @Override
50 public void addUser(User user) throws Exception {
25d43fa9
M
51 if (findUser(user.getUserName()) != null)
52 throw new Exception("User already exists!");
7903e50e
M
53 insertNewUser(user);
54 updateUserList();
55 }
56
25d43fa9
M
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 }
7903e50e
M
89 }
90
25d43fa9
M
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 }
7903e50e
M
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
25d43fa9 128
7903e50e
M
129 }
130
131 @Override
be594ada
M
132 public void addReservation(Reservation reservation) throws Exception {
133 // TODO Auto-generated method stub
25d43fa9 134
be594ada
M
135 }
136
137 @Override
138 public Reservation findReservation(UUID id) {
7903e50e
M
139 // TODO Auto-generated method stub
140 return null;
141 }
142
143 @Override
be594ada 144 public List<Reservation> findGroupReservations(UUID groupID) {
7903e50e 145 // TODO Auto-generated method stub
be594ada
M
146 return null;
147 }
148
149 @Override
150 public List<Reservation> overlaps(DateRange range) {
151 // TODO Auto-generated method stub
152 return null;
7903e50e
M
153 }
154}