@Override public String perform(HttpServletRequest request) { LoginForm form = formBeanFactory.create(request); List<String> errors = prepareErrors(request); if (!form.isPresent()) { return "home.jsp"; } request.setAttribute("form", form); errors.addAll(form.getValidationErrors()); if (errors.size() != 0) { return "home.jsp"; } User user; try { user = userDAO.lookup(form.getUserName()); } catch (DAOException e) { errors.add(e.getMessage()); return "home.jsp"; } if (user == null) { errors.add("Username not found"); return "home.jsp"; } if (!user.checkPassword(form.getPassword())) { errors.add("Incorrect password"); return "home.jsp"; } HttpSession session = request.getSession(); session.setAttribute("user", user); request.setAttribute("success", "Welcome back! " + user.getUserName()); return "home.jsp"; }
public String perform(HttpServletRequest request) { HttpSession session = request.getSession(); // If the user is not logged in, redirect to login screen User currUser = (User) session.getAttribute("user"); if (currUser == null) { return "login.do"; } List<String> errors = new ArrayList<String>(); request.setAttribute("errors", errors); try { UpdateProviderForm form = formBeanFactory.create(request); Provider provider = providerDAO.read(Integer.parseInt(form.getProviderIdAsString())); if (provider == null) { errors.add( "ProviderId: " + form.getProviderIdAsString() + ", Zipcode: " + currUser.getZipcode() + ". Invalid provider selected (not in database)"); return "error.jsp"; } else { currUser.setProviderId(provider.getId()); } DateTime lastSync = new DateTime(provider.getLastSync()); DateTime now = new DateTime(); int hours = Hours.hoursBetween(lastSync, now).getHours(); if (hours >= 24) { File tempDir = (File) request.getServletContext().getAttribute("javax.servlet.context.tempdir"); String contextPath = request.getServletContext().getRealPath("/"); int hoursFromNow = Math.max(7 * 24 - hours, 0); int hoursDuration = Math.min(hours, 7 * 24); DatabaseSync.syncAirings( model, tempDir, contextPath, provider.getZipcode(), provider.getName(), hoursFromNow, 24); /* int i; for (i = 0; i < hoursDuration - 24; i += 24) { DatabaseSync.syncAirings(model, tempDir, contextPath, provider.getZipcode(), provider.getName(), hoursFromNow + i, 24); } DatabaseSync.syncAirings(model, tempDir, contextPath, provider.getZipcode(), provider.getName(), hoursFromNow + i, hoursDuration - i); */ } userDAO.update(currUser); return "profile.do"; } catch (Exception e) { errors.add(e.getClass().getName() + ": " + e.getMessage()); return "error.jsp"; } }
@Override public String perform(HttpServletRequest request) { ExchangeForm form = formBeanFactory.create(request); List<String> errors = prepareErrors(request); User curUser = (User) request.getSession().getAttribute("user"); if (curUser == null) { errors.add("You are not logged in"); return "browse.do"; } if (!form.isPresent()) { return "browse.do"; } errors.addAll(form.getValidationErrors()); if (errors.size() != 0) return "browse.do"; Exchange xchg = null; User admin = null; try { xchg = exchangeDAO.lookup(form.getExchangeIdAsInt()); admin = userDAO.lookup("Admin"); curUser = userDAO.lookup(curUser.getUserName()); } catch (DAOException e) { errors.add(e.getMessage()); return "browse.do"; } request.getSession().setAttribute("user", curUser); if (xchg == null) { errors.add("Transaction not found"); return "browse.do"; } Item item = null; try { item = itemDAO.getItemById(xchg.getItem().getId()); } catch (DAOException e) { errors.add(e.getMessage()); return "browse.do"; } if (!xchg.getPoster().getUserName().equals(curUser.getUserName())) { errors.add("You are not the owner of the item"); return "showMyItems.do"; } if (xchg.getStatus() == Exchange.CLOSED || item.getStatus() == Item.CLOSED) { errors.add("Item already closed"); return "showMyItems.do"; } try { Transaction.begin(); if (xchg.getRespondType() == Exchange.ANSWER_REQUEST_FOR_CREDIT) { userDAO.transferCredit(item.getCredit(), xchg.getPoster(), xchg.getResponder()); curUser.setCredit(curUser.getCredit() - item.getCredit()); } String url1 = "<a href="showItems.do?itemId=" + item.getId() + "">item</a>"; itemDAO.closeItem(item.getId()); exchangeDAO.setSuccessTransaction(xchg.getId()); messageDAO.send( admin, curUser, "Transaction on (" + item.getItemName() + ") complete", "You have accepted the request from (" + "<a href="redirectSend.do?receiver=" + xchg.getResponder().getUserName() + "">" + xchg.getResponder().getUserName() + "</a>). Your " + url1 + " is now closed."); userDAO.updateNewMsgCount(curUser.getUserName(), 1); messageDAO.send( admin, xchg.getResponder(), "Transaction on (" + item.getItemName() + ") complete", "The <a href="redirectSend.do?receiver=" + item.getOwner().getUserName() + "">owner</a> of " + url1 + " has accepted your request"); userDAO.updateNewMsgCount(xchg.getResponder().getUserName(), 1); Exchange[] pending = exchangeDAO.findItemPendingTransactions(item); for (Exchange each : pending) { messageDAO.send( admin, each.getResponder(), "Transaction on (" + item.getItemName() + ") dismissed", "The item you have reponded to is now closed"); userDAO.updateNewMsgCount(each.getResponder().getUserName(), 1); } exchangeDAO.closeItemTransaction(item); Transaction.commit(); } catch (RollbackException e) { errors.add(e.getMessage()); return "showMyItems.do"; } finally { if (Transaction.isActive()) Transaction.rollback(); } try { curUser = userDAO.lookup(curUser.getUserName()); } catch (DAOException e) { errors.add(e.getMessage()); return "showMyItems.do"; } request.getSession().setAttribute("user", curUser); request.setAttribute("success", "Congrats, your transaction has been made"); return "showMyItems.do"; }