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