· 9 years ago · May 02, 2017, 11:16 AM
1//visitor controller
2package com.accenture.adf.businesstier.controller;
3
4import java.util.ArrayList;
5import java.util.List;
6
7import javax.servlet.http.HttpServletRequest;
8import javax.servlet.http.HttpServletResponse;
9import javax.servlet.http.HttpSession;
10import javax.swing.JOptionPane;
11
12import org.apache.log4j.Logger;
13import org.springframework.stereotype.Controller;
14import org.springframework.web.bind.annotation.RequestMapping;
15import org.springframework.web.servlet.ModelAndView;
16
17import com.accenture.adf.businesstier.entity.Event;
18import com.accenture.adf.businesstier.entity.Visitor;
19import com.accenture.adf.businesstier.service.EventFacade;
20import com.accenture.adf.businesstier.service.EventServiceImpl;
21import com.accenture.adf.businesstier.service.VisitorFacade;
22import com.accenture.adf.businesstier.service.VisitorServiceImpl;
23import com.accenture.adf.exceptions.FERSGenericException;
24
25/**
26 * <br/>
27 * CLASS DESCRIPTION: <br/>
28 * A controller class for receiving and handling all visitor related transactions from the
29 * User Interface including visitor account access, visitor account maintenance,
30 * and visitor event registration requests. <br/>
31 *
32 */
33
34@Controller
35public class VisitorController {
36
37 private static Logger log = Logger.getLogger(VisitorController.class);
38
39 /**
40 * <br/>
41 * METHOD DESCRIPTION: <br/>
42 * This method will receive request from Registration.jsp and directs to
43 * service class to register new Visitor into system
44 * by accepting details and persist into database <br/>
45 *
46 * @param request (type HttpServletRequest)
47 * @param response (type HttpServletResponse)
48 *
49 * @return ModelAndView
50 *
51 * @throws FERSGenericException
52 *
53 *
54 */
55 @RequestMapping("/newVistor.htm")
56 public ModelAndView newVisitor(HttpServletRequest request,
57 HttpServletResponse response) throws Exception {
58 if(request==null || response==null)
59 {
60 log.info("Request or Response failed for NEWVISITOR METHOD..");
61 throw new FERSGenericException("Error in Transaction, Please re-Try. for more information check Logfile in C:\\FERSLOG folder", new NullPointerException());
62 }
63 String username=request.getParameter("USERNAME");
64 String password=request.getParameter("PASSWORD");
65 String firstname=request.getParameter("FIRSTNAME");
66 String lastname=request.getParameter("LASTNAME");
67 String email=request.getParameter("EMAIL");
68 String phoneno=request.getParameter("PHONENO");
69 String address=request.getParameter("ADDRESS");
70
71 log.info("creating new visitor with UserName :"+username);
72
73 Visitor visitor=new Visitor();
74 visitor.setUserName(username);
75 visitor.setPassword(password);
76 visitor.setFirstName(firstname);
77 visitor.setLastName(lastname);
78 visitor.setEmail(email);
79 visitor.setPhoneNumber(phoneno);
80 visitor.setAddress(address);
81
82 VisitorFacade vServiceImpl=new VisitorServiceImpl();
83 boolean insertStatus=vServiceImpl.createVisitor(visitor);
84 System.out.println("insert status"+insertStatus);
85 ModelAndView mv=new ModelAndView();
86 if(insertStatus==true)
87 {
88 mv.addObject("REGISTRATIONSTATUSMESSAGE", "User Registered Succesfully !!!");
89 log.info("Succesfully created visitor "+ username);
90 mv.setViewName("/registration.jsp");
91 }
92 else
93 {
94 mv.addObject("REGISTRATIONSTATUSMESSAGE", "USERNAME already exists.. please register again with different USERNAME..");
95 log.info("Username "+ username+" already exists and visitor creation failed..");
96 mv.setViewName("/registration.jsp");
97 }
98 return mv;
99 }
100
101
102 /**
103 * <br/>
104 * METHOD DESCRIPTION:<br/>
105 * The method is for authenticating visitor in the index.jsp page and redirects visitor to
106 * the homepage based on their credentials. if validation fails, redirects to index.jsp and
107 * error message is printed on page.<br/>
108 *
109 * @param request (type HttpServletRequest)
110 * @param response (type HttpServletResponse)
111 *
112 * @return ModelAndView
113 *
114 * @throws FERSGenericException
115 *
116 *
117 */
118
119 @RequestMapping("/searchVisitor.htm")
120 public ModelAndView searchVisitor(HttpServletRequest request,
121 HttpServletResponse response) throws Exception {
122 if(request==null || response==null)
123 {
124 log.info("Request or Response failed for SEARCHVISITOR METHOD..");
125 throw new FERSGenericException("Error in Transaction, Please re-Try. for more information check Logfile in C:\\FERSLOG folder", new NullPointerException());
126 }
127 String username=request.getParameter("USERNAME");
128 String password=request.getParameter("PASSWORD");
129 HttpSession hs=request.getSession();
130 if(hs.isNew())
131 {
132 hs.setAttribute("USERNAME", username);
133 hs.setAttribute("PASSWORD", password);
134 }
135 else
136 {
137 username=hs.getAttribute("USERNAME").toString();
138 password=hs.getAttribute("PASSWORD").toString();
139 }
140
141 log.info("Logging into FERS using username :"+username+" and password :"+password);
142
143 Visitor visitor=new Visitor();
144 VisitorFacade vServiceImpl=new VisitorServiceImpl();
145 visitor=vServiceImpl.searchVisitor(username, password);
146
147 ModelAndView mv=new ModelAndView();
148
149 if(visitor==null)
150 {
151 mv.addObject("ERROR","Invalid Username / Password.");
152 mv.setViewName("/index.jsp");
153 return mv;
154 }
155 else
156 {
157
158 log.info("Visitor details available for the username :"+username);
159
160 List<Event> eventList=new ArrayList<Event>();
161 EventFacade serviceImpl=new EventServiceImpl();
162 eventList=serviceImpl.getAllEvents();
163
164 log.info("All events listed for th visitor :"+eventList);
165
166 List<Event> regList=new ArrayList<Event>();
167 regList=vServiceImpl.showRegisteredEvents(visitor);
168
169 log.info("All Registered events listed for the visitor :"+regList);
170
171 HttpSession session=request.getSession();
172 session.setAttribute("VISITOR", visitor);
173
174 mv.addObject("visitor",visitor);
175 mv.addObject("allEvents",eventList);
176 mv.addObject("regEvents",regList);
177 mv.setViewName("/visitormain.jsp");
178 return mv;
179 }
180 }
181
182
183 /**
184 * <br/>
185 * METHOD DESCRIPTION:<br/>
186 * The method is used to register a visitor for an event from visitormain.jsp and maintains the list
187 * of all the events visitor registered. if user status is already registered for an event, then displays
188 * error message in visitormain.jsp page.<br/>
189
190 *
191 * @param request (type HttpServletRequest)
192 * @param response (type HttpServletResponse)
193 *
194 * @return ModelAndView
195 *
196 * @throws FERSGenericException
197 *
198 */
199
200
201 /* @RequestMapping("/eventreg.htm")
202 public ModelAndView registerVisitor(HttpServletRequest request,
203 HttpServletResponse response) throws Exception {
204
205 if(request==null || response==null)
206 {
207 log.info("Request or Response failed for REGISTERVISITOR METHOD..");
208 throw new FERSGenericException("Error in Transaction, Please re-Try. for more information check Logfile in C:\\FERSLOG folder", new NullPointerException());
209 }
210
211 HttpSession session=request.getSession();
212 Visitor visitor=(Visitor)session.getAttribute("VISITOR");
213 int eventid=Integer.parseInt(request.getParameter("eventId"));
214
215 log.info("Visitor registered for the event :"+eventid);
216
217 ModelAndView mv=new ModelAndView();
218
219 VisitorFacade vServiceImpl=new VisitorServiceImpl();
220 EventFacade serviceImpl=new EventServiceImpl();
221
222 boolean regStatus=serviceImpl.checkEventsofVisitor(visitor, eventid);
223
224 log.info("Status of the visitor for the event :"+regStatus);
225
226 if(regStatus==false)
227 {
228 vServiceImpl.RegisterVisitor(visitor, eventid);
229 log.info("Visitor succesfully registed for event :"+eventid);
230 }
231 else
232 {
233 mv.addObject("RegError", "User already Registered for the EVENT !!");
234 }
235
236 List<Event> regList=new ArrayList<Event>();
237 regList=vServiceImpl.showRegisteredEvents(visitor);
238
239 List<Event> eventList=new ArrayList<Event>();
240
241 eventList=serviceImpl.getAllEvents();
242
243
244 mv.addObject("visitor",visitor);
245 mv.addObject("allEvents",eventList);
246 mv.addObject("regEvents",regList);
247 mv.setViewName("/visitormain.jsp");
248 return mv;
249
250 }
251
252 /**
253 * <br/>
254 * METHOD DESCRIPTION:<br/>
255 * The method will update account details of the visitor and logout the visitor
256 * and to force the visitor to re-login and confirm the updated account details
257 * and new password.<br/>
258 *
259 * @param request (type HttpServletRequest)
260 * @param response (type HttpServletResponse)
261 *
262 * @return ModelAndView
263 *
264 * @throws FERSGenericException
265 *
266 */
267
268 @RequestMapping("/updatevisitor.htm")
269 public ModelAndView updateVisitor(HttpServletRequest request,
270 HttpServletResponse response) throws Exception {
271
272 if(request==null || response==null)
273 {
274 log.info("Request or Response failed for UPDATEVISITOR METHOD..");
275 throw new FERSGenericException("Error in Transaction, Please re-Try. for more information check Logfile in C:\\FERSLOG folder", new NullPointerException());
276 }
277
278 HttpSession session=request.getSession();
279 Visitor visitor=(Visitor)session.getAttribute("VISITOR");
280
281 log.info("Updating visitor details with VisitorID :"+visitor.getVisitorId());
282
283 String username=request.getParameter("username");
284 //String password=request.getParameter("password");
285 String firstname=request.getParameter("firstname");
286 String lastname=request.getParameter("lastname");
287 String email=request.getParameter("email");
288 String phoneno=request.getParameter("phoneno");
289 String address=request.getParameter("address");
290
291 visitor.setFirstName(firstname);
292 visitor.setLastName(lastname);
293 visitor.setUserName(username);
294 //visitor.setPassword(password);
295 visitor.setEmail(email);
296 visitor.setPhoneNumber(phoneno);
297 visitor.setAddress(address);
298
299 VisitorFacade vServiceImpl=new VisitorServiceImpl();
300 int status=vServiceImpl.updateVisitorDetails(visitor);
301
302 log.info("Number of Visitor records updated is :"+status);
303
304 ModelAndView mv=new ModelAndView();
305
306 if(status>0)
307 {
308 mv.addObject("status","success");
309 mv.setViewName("/updatevisitor.jsp");
310 }
311 else
312 {
313 mv.addObject("updatestatus", "Error in updation.. Please Check fields and retry");
314 mv.setViewName("/updatevisitor.jsp");
315 }
316 return mv;
317 }
318 }
319
320 /**
321 * <br/>
322 * METHOD DESCRIPTION: <br/>
323 * The method is to unregisters a visitor from an event within the visitormain.jsp
324 * page and the seats will be released. The visitormain.jsp page is then refreshed
325 * to confirm the updates. <br/>
326 *
327 * @param request (type HttpServletRequest)
328 * @param response (type HttpServletResponse)
329 *
330 * @return ModelAndView
331 *
332 * @throws FERSGenericException
333 *
334 */
335
336 /* @RequestMapping("/eventunreg.htm")
337 public ModelAndView unregisterEvent(HttpServletRequest request,
338 HttpServletResponse response) throws Exception {
339
340 if(request==null || response==null)
341 {
342 log.info("Request or Response failed for UNREGISTEREVENT METHOD..");
343 throw new FERSGenericException("Error in Transaction, Please re-Try. for more information check Logfile in C:\\FERSLOG folder", new NullPointerException());
344 }
345
346 HttpSession session=request.getSession();
347 Visitor visitor=(Visitor)session.getAttribute("VISITOR");
348 int eventid=Integer.parseInt(request.getParameter("eventId"));
349
350 log.info("Unregistering for the event :"+eventid);
351
352 VisitorFacade vServiceImpl=new VisitorServiceImpl();
353 vServiceImpl.unregisterEvent(visitor, eventid);
354
355
356
357 List<Event> regList=new ArrayList<Event>();
358 regList=vServiceImpl.showRegisteredEvents(visitor);
359
360 List<Event> eventList=new ArrayList<Event>();
361 EventFacade serviceImpl=new EventServiceImpl();
362
363 serviceImpl.updateEventDeletions(eventid);
364
365 log.info("Seats allocated for the event are released :"+eventid);
366
367 eventList=serviceImpl.getAllEvents();
368
369
370
371 ModelAndView mv=new ModelAndView();
372 mv.addObject("visitor",visitor);
373 mv.addObject("allEvents",eventList);
374 mv.addObject("regEvents",regList);
375 mv.setViewName("/visitormain.jsp");
376 return mv;
377 }
378
379 /*
380 @RequestMapping("/changePWD.htm")
381 public ModelAndView changePassword(HttpServletRequest request, HttpServletResponse response) throws FERSGenericException {
382 int status = -1;
383
384 HttpSession session=request.getSession();
385 Visitor visitor=(Visitor)session.getAttribute("VISITOR");
386
387 if(visitor != null){
388 log.info("Changing visitor password with VisitorID :"+visitor.getVisitorId());
389
390 String password=request.getParameter("password");
391
392 if(password != null)
393 {
394 visitor.setPassword(password);
395
396 VisitorFacade vServiceImpl=new VisitorServiceImpl();
397 status=vServiceImpl.changePassword(visitor);
398 }
399 else{
400 log.error("Password cannot be blank");
401 }
402
403 log.info("Visitor password changed :"+status);
404 }
405
406 else
407 {
408 log.error("Visitor details are invalid");
409 }
410
411 ModelAndView mv=new ModelAndView();
412
413 if(status > 0){
414 mv.addObject("status","success");
415 mv.setViewName("/changePWD.jsp");
416 }else if(status == -5){
417 mv.addObject("status", "error");
418 mv.addObject("errorMsg", "System error occurred, Please verify log file for more details");
419 mv.setViewName("/changePWD.jsp");
420 }else if(status == -10){
421 mv.addObject("status", "error");
422 mv.addObject("errorMsg", "New password must be different from current password, please choose a different password and retry");
423 mv.setViewName("/changePWD.jsp");
424 }else{
425 mv.addObject("status", "error");
426 mv.addObject("errorMsg", "Error while changing password.. Please verify visitor and password details and retry again");
427 mv.setViewName("/changePWD.jsp");
428 }
429 return mv;
430 }
431
432
433
434}*/
435
436//visitorDEO
437package com.accenture.adf.businesstier.dao;
438
439import java.sql.Connection;
440import java.sql.PreparedStatement;
441import java.sql.ResultSet;
442import java.sql.SQLException;
443import java.sql.Statement;
444import java.util.ArrayList;
445
446import org.apache.log4j.Logger;
447import org.springframework.context.ApplicationContext;
448import org.springframework.context.support.ClassPathXmlApplicationContext;
449
450import com.accenture.adf.businesstier.entity.Event;
451import com.accenture.adf.businesstier.entity.Visitor;
452import com.accenture.adf.exceptions.FERSGenericException;
453import com.accenture.adf.helper.FERSDataConnection;
454import com.accenture.adf.helper.FERSDbQuery;
455
456/**
457 *
458 * <br/>
459 * CLASS DESCRIPTION:<br/>
460 * A Data Access Object (DAO) class for handling and managing visitor related data requested,
461 * used, and processed in the application and maintained in the database.
462 * The interface between the application and visitor data persisting in the database.
463 *
464 */
465
466public class VisitorDAO {
467
468 // LOGGER for handling all transaction messages in VISITORDAO
469 private static Logger log = Logger.getLogger(VisitorDAO.class);
470
471 //JDBC API classes for data persistence
472 private Connection connection = null;
473 private PreparedStatement statement = null;
474 private ResultSet resultSet = null;
475 private FERSDbQuery query;
476
477 //Default constructor for injecting Spring dependencies for SQL queries
478 public VisitorDAO() {
479 ApplicationContext context = new ClassPathXmlApplicationContext(
480 "applicationContext.xml");
481 query = (FERSDbQuery) context.getBean("SqlBean");
482 }
483
484 /**
485 * <br/>
486 * METHOD DESCRIPTION:<br/>
487 * DAO method to loading visitor details into VISITOR table in database<br/>
488 * and validating about existing visitor details before inserting a visitor <br/>
489 *
490 * <br/>
491 * PSEUDOCODE: <br/>
492 * Create a connection to database<br/>
493 * Prepare a statement object using the connection that uses a query that inserts visitor information <br/>
494 * into the visitor table <br/>
495 * Execute a statement object selects all the usernames from the visitor table<br/>
496 * if the username is not in the visitor table <br/>
497 *
498 * @param visitor (type Visitor)
499 *
500 * @return boolean
501 *
502 * @throws ClassNotFoundException
503 * @throws SQLException
504 * @throws Exception
505 *
506 */
507
508 public boolean insertData(Visitor visitor) throws ClassNotFoundException,
509 SQLException, Exception {
510
511 // TODO: Add code here.....
512 // TODO: Pseudo-code are in the block comments above this method
513 // TODO: For more comprehensive pseudo-code with details, refer to the Component/Class Detailed Design Document
514 int row=0;
515 connection = FERSDataConnection.createConnection();
516 boolean userFound = false;
517 userFound = validateUserName(visitor);
518 if (userFound == false) {
519 PreparedStatement ps = connection.prepareStatement(query.getInsertQuery());
520 ps.setString(1, visitor.getUserName());
521 ps.setString(2, visitor.getPassword());
522 ps.setString(3, visitor.getFirstName());
523 ps.setString(4, visitor.getLastName());
524 ps.setString(5, visitor.getEmail());
525 ps.setString(6, visitor.getPhoneNumber());
526 ps.setString(7, visitor.getAddress());
527
528
529 int insertRows = ps.executeUpdate();
530 if (insertRows != 0) {
531 System.out.println("Data inserted successfully!");
532 }
533 connection.close();
534 return true;
535 }else{
536 connection.close();
537
538 return false;
539 }
540 }
541
542 private boolean validateUserName(Visitor visitor) throws ClassNotFoundException, SQLException {
543 Connection connection =FERSDataConnection.createConnection();
544 Statement statement=connection.createStatement();
545 ResultSet resultSet=statement.executeQuery(query.getValidateVisitor());
546 boolean userFound=false;
547 while(resultSet.next()){
548 if(resultSet.getString("USERNAME").equals(visitor.getUserName()))
549 {
550 userFound= true;
551 break;
552 }
553 }
554 connection.close();
555 return userFound;
556 }
557
558
559 /**
560 * <br/>
561 * METHOD DESCRIPTION:<br/>
562 * DAO method for searching for visitor details using USERNAME and PASSWORD<br/>
563 *
564 * <br/>
565 * PSEUDOCODE: <br/>
566 * Create a connection to database<br/>
567 * Prepare a statement object using the connection<br/>
568 * that uses a query that retrieves all the data from the visitor
569 * table based on the username and password provided. Execute the query and <br/>
570 * Using a WHILE LOOP, store the results in the result set record in the visitor object.<br/>
571 *
572 * @param username (type String)
573 * @param password (type String)
574 *
575 * @return Visitor
576 *
577 * @throws ClassNotFoundException
578 * @throws SQLException
579 *
580 *
581 */
582 public Visitor searchUser(String username, String password)
583 throws ClassNotFoundException, SQLException {
584
585 Connection connection= FERSDataConnection.createConnection();
586 PreparedStatement ps = connection.prepareStatement(query.getSearchQuery());
587 ps.setString(1, username);
588 ps.setString(2, password);
589
590 ResultSet rs = ps.executeQuery();
591 Visitor visitor = null;
592 while(rs.next()){
593 visitor = new Visitor();
594 visitor.setVisitorId(rs.getInt("visitorId"));
595 visitor.setUserName(rs.getString("username"));
596 visitor.setFirstName(rs.getString("firstname"));
597 visitor.setLastName(rs.getString("lastname"));
598 visitor.setEmail(rs.getString("email"));
599 visitor.setPhoneNumber(rs.getString("phonenumber"));
600 visitor.setAddress(rs.getString("address"));
601 }
602
603 connection.close();
604 return visitor;
605 }
606
607 /**
608 * <br/>
609 * METHOD DESCRIPTION: <br/>
610 * DAO method to register visitor to specific event and checking about status
611 * of visitor to particular event. <br/>
612 *
613 * PSEUDO-CODE: <br/>
614 * Create a connection to the database <br/>
615 * Prepare a statement object using the connection: that inserts the
616 * visitor and event IDs into the EVENTSESSIONSIGNUP table <br/>
617 * Execute the query to perform the update <br/>
618 *
619 *
620 * @param visitor
621 * @param eventid
622 *
623 * @throws ClassNotFoundException
624 * @throws SQLException
625 * @throws Exception
626 *
627 */
628
629 public void registerVisitorToEvent(Visitor visitor, int eventid)
630 throws ClassNotFoundException, SQLException, Exception {
631
632 // TODO: Add code here.....
633 // TODO: Pseudo-code are in the block comments above this method
634 // TODO: For more comprehensive pseudo-code with details, refer to the Component/Class Detailed Design Document
635
636 }
637
638 /**
639 * <br/>
640 * METHOD DESCRIPTION:<br/>
641 * DAO method to display all the events registered by particular visitor<br/>
642 *
643 * PSEUDO-CODE: <br/>
644 * Create a connection to the database <br/>
645 * Prepare a statement object using the connection: that returns the event
646 * information for all the events that are registered to a visitor<br/>
647 * Execute the query to retrieve the results into a result set<br/>
648 * Place each event record‘s information in an event list. <br/>
649 *
650 * @param visitor (type Visitor)
651 *
652 * @return Collection of Event Arrays (type Event)
653 *
654 * @throws ClassNotFoundException
655 * @throws SQLException
656 *
657 */
658 public ArrayList<Event> registeredEvents(Visitor visitor)
659 throws ClassNotFoundException, SQLException {
660
661 // TODO: Add code here.....
662 // TODO: Pseudo-code are in the block comments above this method
663 // TODO: For more comprehensive pseudo-code with details, refer to the Component/Class Detailed Design Document
664
665
666 return null;
667 }
668
669 /**
670 * <br/>
671 * METHOD DESCRIPTION:<br/>
672 * DAO method to update visitor with additional information <br/>
673 * <br/>
674 *
675 * @param visitor (type Visitor)
676 *
677 * @return int
678 *
679 * @throws ClassNotFoundException
680 * @throws SQLException
681 *
682 *
683 */
684 public int updateVisitor(Visitor visitor) throws ClassNotFoundException,
685 SQLException {
686 connection = FERSDataConnection.createConnection();
687 statement = connection.prepareStatement(query.getUpdateQuery());
688 statement.setString(1, visitor.getFirstName());
689 statement.setString(2, visitor.getLastName());
690 statement.setString(3, visitor.getUserName());
691 //statement.setString(4, visitor.getPassword());
692 statement.setString(4, visitor.getEmail());
693 statement.setString(5, visitor.getPhoneNumber());
694 statement.setString(6, visitor.getAddress());
695 statement.setInt(7, visitor.getVisitorId());
696
697 int status = statement.executeUpdate();
698 log.info("Updating visitor details in Database for Visitor ID :"
699 + visitor.getVisitorId());
700 FERSDataConnection.closeConnection();
701 return status;
702 }
703
704 /**
705 * <br/>
706 * METHOD DESCRIPTION: <br/>
707 * DAO method to unregister from events <br/>
708 *
709 *
710 * @param visitor (type Visitor)
711 * @param eventid (type int)
712 *
713 * @throws ClassNotFoundException
714 * @throws SQLException
715 * @throws Exception
716 *
717 */
718
719 public void unregisterEvent(Visitor visitor, int eventid)
720 throws ClassNotFoundException, SQLException, Exception {
721 connection = FERSDataConnection.createConnection();
722 statement = connection.prepareStatement(query.getDeleteEventQuery());
723 statement.setInt(1, eventid);
724 statement.setInt(2, visitor.getVisitorId());
725 int status = statement.executeUpdate();
726 if (status <= 0)
727 throw new FERSGenericException("Records not updated properly",
728 new Exception());
729 log.info("unregistering event in Database for the visitor :"
730 + visitor.getFirstName());
731 FERSDataConnection.closeConnection();
732 }
733
734
735}