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