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.FileInputStream; 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.hibernate.criterion.Restrictions; 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 org.springframework.web.multipart.MultipartFile; 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 10/29/2014 10:57:15 CDT */ @Controller @RequestMapping("/labtest/*") public class LabTestController extends AbstractBluebuttonController { private static final Log log = LogFactory.getLog(LabTestController.class); @RequestMapping(value = "list_alt", method = RequestMethod.GET) public String listNoScript(Model model) { model.addAttribute("labTestList", bluebuttonDaoService.getLabTestService().list()); return "/bluebutton/labtest/list_alt"; } @RequestMapping(value = { "list", "", "/" }, method = RequestMethod.GET) public String list() { return "/bluebutton/labtest/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, Object> likes = new HashMap<String, Object>(); for (DataTableHeader header : headers) { if (header.getSearchable() && header.getSearchValue() != null) { for (String splitColumnValue : StringUtils.split(header.getSearchValue().trim(), ' ')) { likes.put(header.getName(), splitColumnValue); } } } options.setIndividualLikes(likes); } Integer count = bluebuttonDaoService.getLabTestService().count(options); options.setLimit(limit); options.setStart(start); options.setSorts(sorts); List<LabTest> labTestList = bluebuttonDaoService.getLabTestService().list(options); ob.put("draw", draw); ob.put("recordsFiltered", count); ob.put("recordsTotal", count); JSONArray jsonArray = new JSONArray(); for (LabTest labTest : labTestList) { JSONArray tableRow = new JSONArray(); for (DataTableHeader header : headers) { String headerName = header.getName(); if (StringUtils.equals("labTestId", headerName)) { tableRow.put(labTest.getLabTestId()); } else if (StringUtils.equals("name", headerName)) { tableRow.put(labTest.getName()); } else if (StringUtils.equals("description", headerName)) { tableRow.put(labTest.getDescription()); } else if (StringUtils.equals("units", headerName)) { tableRow.put(labTest.getUnits()); } else if (StringUtils.equals("loincCode", headerName)) { tableRow.put(labTest.getLoincCode()); } else if (StringUtils.equals("dateCreated", headerName)) { tableRow.put(labTest.getDateCreated()); } else if (StringUtils.equals("dateUpdated", headerName)) { tableRow.put(labTest.getDateUpdated()); } else if (StringUtils.equals("labTestRanges", headerName)) { tableRow.put(labTest.getLabTestRanges().size()); } else if (StringUtils.equals("urls", headerName)) { String urls = ""; if (StringUtils.equals("list", display)) { urls += "<a href=\"show?" + "labTestId=" + labTest.getLabTestId() + "\"><span class=\"glyphicon glyphicon-eye-open\"></a>"; // urls += "<a href=\"edit?"+"labTestId="+labTest.getLabTestId()+"\"><span class=\"glyphicon glyphicon-pencil\"></a>"; urls += "<a href=\"delete?" + "labTestId=" + labTest.getLabTestId() + "\"><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 LabTest", e); try { String stackTrace = e.getMessage() + String.valueOf('\n'); for (StackTraceElement ste : e.getStackTrace()) { stackTrace += ste.toString() + String.valueOf('\n'); } ob = new JSONObject(); ob.put("draw", draw); ob.put("recordsFiltered", 0); ob.put("recordsTotal", 0); ob.put("error", stackTrace); } catch (JSONException je) { log.error("error building json error object for LabTest", je); } } return ob.toString(); } @RequestMapping(value = "add", method = RequestMethod.GET) public String add(Model model) { model.addAttribute("labTest", new LabTest()); return "/bluebutton/labtest/edit"; } @RequestMapping(value = "edit", method = RequestMethod.GET) public String edit(ModelMap model, @RequestParam(value = "labTestId") Integer labTestId) { model.addAttribute("labTest", bluebuttonDaoService.getLabTestService().findById(labTestId)); return "/bluebutton/labtest/edit"; } @RequestMapping(value = "show", method = RequestMethod.GET) public String show(ModelMap model, @RequestParam(value = "labTestId") Integer labTestId) { model.addAttribute("labTest", bluebuttonDaoService.getLabTestService().findById(labTestId)); return "/bluebutton/labtest/show"; } // @RequestMapping( value = "save", method = RequestMethod.POST ) // public String save( @Valid @ModelAttribute( "labTest" ) LabTest labTest, BindingResult result ) { // if (result.hasErrors()) { return "/bluebutton/labtest/edit"; } // else { // // try { // bluebuttonDaoService.getLabTestService().saveOrUpdate( labTest ); // } catch (NonUniqueObjectException e) { // log.debug("Merging Results"); // bluebuttonDaoService.getLabTestService().merge( labTest ); // } // return "redirect:/labtest/list"; // } // } @RequestMapping(value = "delete", method = RequestMethod.GET) public String confirmDelete(ModelMap model, @RequestParam(value = "labTestId") Integer labTestId) { model.addAttribute("labTest", bluebuttonDaoService.getLabTestService().findById(labTestId)); return "/bluebutton/labtest/delete"; } @RequestMapping(value = "delete", method = RequestMethod.POST) public String doDelete(ModelMap model, @RequestParam(value = "submit") String submitButtonValue, @RequestParam(value = "labTestId") Integer labTestId) { if (StringUtils.equalsIgnoreCase(submitButtonValue, "yes")) { bluebuttonDaoService.getLabTestService() .delete(bluebuttonDaoService.getLabTestService().findById(labTestId)); } return "redirect:/labtest/list"; } @RequestMapping(value = "import", method = RequestMethod.GET) public String displayImportCSVFileUpload() { return "/bluebutton/labtest/import"; } @RequestMapping(value = "import", method = RequestMethod.POST) public String importCSV(@RequestParam("file") MultipartFile file) throws IOException { this.bluebuttonDaoService.getLabTestService().importCSV(file.getInputStream()); return "redirect:/labtest/"; } @RequestMapping(value = "ranges_with_categories", produces = "application/json", method = RequestMethod.GET) @ResponseBody public String rangesWithTypes(@RequestParam(value = "sex", required = true) String sex, @RequestParam(value = "age", required = true) Double age, @RequestParam(value = "loincCodeCsvList", required = true) String loincCodeCsvList) { JSONObject json = new JSONObject(); json.put("ranges", this.bluebuttonDaoService.getLabTestService().getRangesByGenderAgeAndLoincCodes(sex, age, loincCodeCsvList)); json.put("categories", this.bluebuttonDaoService.getLoincCodeCategoryService().findByLoincCodes(loincCodeCsvList)); return json.toString(); } }