Java tutorial
package edu.uiowa.icts.bluebutton.controller; /* * #%L * blue-button Spring MVC Web App * %% * Copyright (C) 2014 - 2015 University of Iowa Institute for Clinical and Translational Science (ICTS) * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * #L% */ import java.io.IOException; import java.io.StringReader; import java.util.List; import java.util.ArrayList; import java.util.Map; import java.util.HashMap; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.NonUniqueObjectException; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import edu.uiowa.icts.bluebutton.domain.*; import edu.uiowa.icts.spring.GenericDaoListOptions; import edu.uiowa.icts.util.SortColumn; import edu.uiowa.icts.util.DataTableHeader; /** * Generated by Protogen * @since 04/02/2015 13:48:44 CDT */ @Controller @RequestMapping("/person/*") public class PersonController extends AbstractBluebuttonController { private static final Log log = LogFactory.getLog(PersonController.class); @RequestMapping(value = "list_alt", method = RequestMethod.GET) public String listNoScript(Model model) { model.addAttribute("personList", bluebuttonDaoService.getPersonService().list()); return "/bluebutton/person/list_alt"; } @RequestMapping(value = { "list", "", "/" }, method = RequestMethod.GET) public String list() { return "/bluebutton/person/list"; } @ResponseBody @RequestMapping(value = "datatable", produces = "application/json") public String datatable(HttpServletRequest request, @RequestParam(value = "length", required = false) Integer limit, @RequestParam(value = "start", required = false) Integer start, @RequestParam(value = "draw", required = false) String draw, @RequestParam(value = "search[regex]", required = false, defaultValue = "false") Boolean searchRegularExpression, @RequestParam(value = "search[value]", required = false) String search, @RequestParam(value = "columnCount", required = false, defaultValue = "0") Integer columnCount, @RequestParam(value = "individualSearch", required = false, defaultValue = "false") Boolean individualSearch, @RequestParam(value = "display", required = false, defaultValue = "list") String display) { List<DataTableHeader> headers = new ArrayList<DataTableHeader>(); for (int i = 0; i < columnCount; i++) { DataTableHeader dth = new DataTableHeader(); dth.setData(request.getParameter("columns[" + i + "][data]")); dth.setName(request.getParameter("columns[" + i + "][name]")); dth.setOrderable(Boolean.valueOf(request.getParameter("columns[" + i + "][orderable]"))); dth.setSearchable(Boolean.valueOf(request.getParameter("columns[" + i + "][searchable]"))); dth.setSearchValue(request.getParameter("columns[" + i + "][search][value]")); dth.setSearchRegex(Boolean.valueOf(request.getParameter("columns[" + i + "][search][regex]"))); headers.add(dth); } ArrayList<SortColumn> sorts = new ArrayList<SortColumn>(); JSONObject ob = new JSONObject(); try { for (int i = 0; i < columnCount; i++) { Integer columnIndex = null; String columnIndexString = request.getParameter("order[" + i + "][column]"); if (columnIndexString != null) { try { columnIndex = Integer.parseInt(columnIndexString); } catch (NumberFormatException e) { continue; } if (columnIndex != null) { sorts.add(new SortColumn(headers.get(columnIndex).getName(), request.getParameter("order[" + i + "][dir]"))); } } } GenericDaoListOptions options = new GenericDaoListOptions(); if (!individualSearch) { ArrayList<String> searchColumns = new ArrayList<String>(); for (int i = 0; i < columnCount; i++) { if (headers.get(i).getSearchable()) { searchColumns.add(headers.get(i).getName()); } } options.setSearch(search); options.setSearchColumns(searchColumns); } else { Map<String, List<Object>> likes = new HashMap<String, List<Object>>(); for (DataTableHeader header : headers) { if (header.getSearchable() && header.getSearchValue() != null) { List<Object> values = new ArrayList<Object>(); for (String splitColumnValue : StringUtils.split(header.getSearchValue().trim(), ' ')) { values.add(splitColumnValue.trim()); } likes.put(header.getName(), values); } } options.setLikes(likes); } Integer count = bluebuttonDaoService.getPersonService().count(options); options.setLimit(limit); options.setStart(start); options.setSorts(sorts); List<Person> personList = bluebuttonDaoService.getPersonService().list(options); ob.put("draw", draw); ob.put("recordsFiltered", count); ob.put("recordsTotal", count); JSONArray jsonArray = new JSONArray(); for (Person person : personList) { JSONArray tableRow = new JSONArray(); for (DataTableHeader header : headers) { String headerName = header.getName(); if (StringUtils.equals("personId", headerName)) { tableRow.put(person.getPersonId()); } else if (StringUtils.equals("firstName", headerName)) { tableRow.put(person.getFirstName()); } else if (StringUtils.equals("middleName", headerName)) { tableRow.put(person.getMiddleName()); } else if (StringUtils.equals("lastName", headerName)) { tableRow.put(person.getLastName()); } else if (StringUtils.equals("dateOfBirth", headerName)) { tableRow.put(person.getDateOfBirth()); } else if (StringUtils.equals("addressLineOne", headerName)) { tableRow.put(person.getAddressLineOne()); } else if (StringUtils.equals("addressLineTwo", headerName)) { tableRow.put(person.getAddressLineTwo()); } else if (StringUtils.equals("city", headerName)) { tableRow.put(person.getCity()); } else if (StringUtils.equals("state", headerName)) { tableRow.put(person.getState()); } else if (StringUtils.equals("zipcode", headerName)) { tableRow.put(person.getZipcode()); } else if (StringUtils.equals("country", headerName)) { tableRow.put(person.getCountry()); } else if (StringUtils.equals("email", headerName)) { tableRow.put(person.getEmail()); } else if (StringUtils.equals("registrationDate", headerName)) { tableRow.put(person.getRegistrationDate()); } else if (StringUtils.equals("firstLogin", headerName)) { tableRow.put(person.getFirstLogin()); } else if (StringUtils.equals("lastLogin", headerName)) { tableRow.put(person.getLastLogin()); } else if (StringUtils.equals("lastUpdated", headerName)) { tableRow.put(person.getLastUpdated()); } else if (StringUtils.equals("signature", headerName)) { tableRow.put(person.getSignature()); } else if (StringUtils.equals("clinicalDocuments", headerName)) { tableRow.put(person.getClinicalDocuments().size()); } else if (StringUtils.equals("urls", headerName)) { String urls = ""; if (StringUtils.equals("list", display)) { urls += "<a href=\"show?" + "personId=" + person.getPersonId() + "\"><span class=\"glyphicon glyphicon-eye-open\"></a>"; urls += "<a href=\"edit?" + "personId=" + person.getPersonId() + "\"><span class=\"glyphicon glyphicon-pencil\"></a>"; // urls += "<a href=\"delete?"+"personId="+person.getPersonId()+"\"><span class=\"glyphicon glyphicon-trash\"></a>"; } else { } tableRow.put(urls); } else { tableRow.put("[error: column " + headerName + " not supported]"); } } jsonArray.put(tableRow); } ob.put("data", jsonArray); } catch (Exception e) { log.error("error builing datatable json object for Person", e); try { String stackTrace = e.getMessage() + String.valueOf('\n'); for (StackTraceElement ste : e.getStackTrace()) { stackTrace += ste.toString() + String.valueOf('\n'); } JSONObject error = new JSONObject(); error.put("draw", draw); error.put("recordsFiltered", 0); error.put("recordsTotal", 0); error.put("error", stackTrace); return error.toString(); } catch (JSONException je) { log.error("error building json error object for Person", je); } } return ob.toString(); } @RequestMapping(value = "add", method = RequestMethod.GET) public String add(Model model) { model.addAttribute("person", new Person()); return "/bluebutton/person/edit"; } @RequestMapping(value = "edit", method = RequestMethod.GET) public String edit(ModelMap model, @RequestParam(value = "personId") Integer personId) { model.addAttribute("person", bluebuttonDaoService.getPersonService().findById(personId)); return "/bluebutton/person/edit"; } /* * display all of a person's BlueButton records on one chart... */ @RequestMapping(value = "show", method = RequestMethod.GET) public String show(ModelMap model, @RequestParam(value = "personId") Integer personId) { Person p = bluebuttonDaoService.getPersonService().findById(personId); if (p == null) { return "redirect:/person/"; } else { model.addAttribute("personId", p.getPersonId()); model.addAttribute("clinicalDocuments", p.getClinicalDocuments()); // reuse the clinicaldocument show.jsp return "/bluebutton/clinicaldocument/show"; } } @RequestMapping(value = "save", method = RequestMethod.POST) public String save(@Valid @ModelAttribute("person") Person person, BindingResult result, Model model) { if (result.hasErrors()) { return "/bluebutton/person/edit"; } else { try { bluebuttonDaoService.getPersonService().saveOrUpdate(person); } catch (NonUniqueObjectException e) { log.debug("Merging Results"); bluebuttonDaoService.getPersonService().merge(person); } return "redirect:/person/list"; } } @RequestMapping(value = "delete", method = RequestMethod.GET) public String confirmDelete(ModelMap model, @RequestParam(value = "personId") Integer personId) { model.addAttribute("person", bluebuttonDaoService.getPersonService().findById(personId)); return "/bluebutton/person/delete"; } @RequestMapping(value = "delete", method = RequestMethod.POST) public String doDelete(ModelMap model, @RequestParam(value = "submit") String submitButtonValue, @RequestParam(value = "personId") Integer personId) { if (StringUtils.equalsIgnoreCase(submitButtonValue, "yes")) { bluebuttonDaoService.getPersonService() .delete(bluebuttonDaoService.getPersonService().findById(personId)); } return "redirect:/person/list"; } }