/actions/src/main/java/org/ala/spatial/services/web/ApplicationController.java

http://alageospatialportal.googlecode.com/ · Java · 107 lines · 63 code · 25 blank · 19 comment · 2 complexity · ce08487a0fb190e29373987aec44c5e2 MD5 · raw file

  1. /**************************************************************************
  2. * Copyright (C) 2010 Atlas of Living Australia
  3. * All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public
  6. * License Version 1.1 (the "License"); you may not use this file
  7. * except in compliance with the License. You may obtain a copy of
  8. * the License at http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS
  11. * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  12. * implied. See the License for the specific language governing
  13. * rights and limitations under the License.
  14. ***************************************************************************/
  15. package org.ala.spatial.services.web;
  16. import java.util.List;
  17. import javax.annotation.Resource;
  18. import javax.servlet.http.HttpServletRequest;
  19. import org.ala.spatial.services.dao.ApplicationDAO;
  20. import org.ala.spatial.services.dto.Application;
  21. import org.apache.log4j.Logger;
  22. import org.springframework.stereotype.Controller;
  23. import org.springframework.web.bind.annotation.ModelAttribute;
  24. import org.springframework.web.bind.annotation.PathVariable;
  25. import org.springframework.web.bind.annotation.RequestMapping;
  26. import org.springframework.web.bind.annotation.RequestMethod;
  27. import org.springframework.web.bind.annotation.ResponseBody;
  28. import org.springframework.web.servlet.ModelAndView;
  29. /**
  30. * @author ajay
  31. */
  32. @Controller
  33. public class ApplicationController {
  34. private static final Logger logger = Logger.getLogger(ApplicationController.class);
  35. private final String APP_LIST = "/apps/";
  36. private final String APP_VIEW = "/app/{appid}";
  37. private final String APP_NEW = "/app/new";
  38. @Resource(name = "applicationDao")
  39. private ApplicationDAO applicationDao;
  40. @RequestMapping(value = APP_LIST)
  41. public ModelAndView viewApplications() {
  42. List<Application> apps = applicationDao.findApplications();
  43. System.out.println("Got " + apps.size() + " items");
  44. ModelAndView m = new ModelAndView("apps/list");
  45. m.addObject("apps", apps);
  46. return m;
  47. }
  48. @RequestMapping(value = APP_VIEW)
  49. public ModelAndView appInfo(@PathVariable String appid) {
  50. Application app = applicationDao.findApplicationByAppId(appid);
  51. ModelAndView m = new ModelAndView("apps/view");
  52. m.addObject("app", app);
  53. return m;
  54. }
  55. @RequestMapping(method = RequestMethod.GET, value = APP_NEW)
  56. public String newApplicationForm() {
  57. return "apps/new";
  58. }
  59. @RequestMapping(method = RequestMethod.POST, value = APP_NEW)
  60. public ModelAndView newApplication(@ModelAttribute("application") Application app, HttpServletRequest req) {
  61. logger.info("Adding application: \n" + app.toString());
  62. List<Application> apps = applicationDao.findApplicationsByEmail(app.getEmail());
  63. ModelAndView m = new ModelAndView("message");
  64. if (apps.size() > 0) {
  65. m.addObject("msg", "An application with the email has already been registered. Would you like to register yet another application?");
  66. //return "Application with the email has already been registered.";
  67. } else {
  68. applicationDao.addApplication(app);
  69. logger.info("now application is: \n" + app.toString());
  70. String html = "";
  71. html += "Thank you for registering your application.";
  72. html += "Please keep your AppID secure and use it as part of your client application.";
  73. html += "<br />";
  74. html += "<strong>" + apps.get(0).getAppid() + "</strong>";
  75. html += "<br /><br />";
  76. m.addObject("msg", html);
  77. m.addObject("isnew", true);
  78. //return "application successfully created. Your appid is '" + app.getAppid() + "', please keep this safe.";
  79. }
  80. return m;
  81. }
  82. }