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 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 09:27:59 CDT */ @Controller @RequestMapping("/labtestrange/*") public class LabTestRangeController extends AbstractBluebuttonController { private static final Log log = LogFactory.getLog(LabTestRangeController.class); @RequestMapping(value = "list_alt", method = RequestMethod.GET) public String listNoScript(Model model) { model.addAttribute("labTestRangeList", bluebuttonDaoService.getLabTestRangeService().list()); return "/bluebutton/labtestrange/list_alt"; } @RequestMapping(value = { "list", "", "/" }, method = RequestMethod.GET) public String list() { return "/bluebutton/labtestrange/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.getLabTestRangeService().count(options); options.setLimit(limit); options.setStart(start); options.setSorts(sorts); List<LabTestRange> labTestRangeList = bluebuttonDaoService.getLabTestRangeService().list(options); ob.put("draw", draw); ob.put("recordsFiltered", count); ob.put("recordsTotal", count); JSONArray jsonArray = new JSONArray(); for (LabTestRange labTestRange : labTestRangeList) { JSONArray tableRow = new JSONArray(); for (DataTableHeader header : headers) { String headerName = header.getName(); if (StringUtils.equals("labTestRangeId", headerName)) { tableRow.put(labTestRange.getLabTestRangeId()); } else if (StringUtils.equals("sex", headerName)) { tableRow.put(labTestRange.getSex()); } else if (StringUtils.equals("minAgeYears", headerName)) { tableRow.put(labTestRange.getMinAgeYears()); } else if (StringUtils.equals("maxAgeYears", headerName)) { tableRow.put(labTestRange.getMaxAgeYears()); } else if (StringUtils.equals("minNormal", headerName)) { tableRow.put(labTestRange.getMinNormal()); } else if (StringUtils.equals("maxNormal", headerName)) { tableRow.put(labTestRange.getMaxNormal()); } else if (StringUtils.equals("labTest", headerName)) { tableRow.put(labTestRange.getLabTest().getName()); } else if (StringUtils.equals("urls", headerName)) { String urls = ""; if (StringUtils.equals("list", display)) { urls += "<a href=\"show?" + "labTestRangeId=" + labTestRange.getLabTestRangeId() + "\"><span class=\"glyphicon glyphicon-eye-open\"></a>"; //urls += "<a href=\"edit?"+"labTestRangeId="+labTestRange.getLabTestRangeId()+"\"><span class=\"glyphicon glyphicon-pencil\"></a>"; urls += "<a href=\"delete?" + "labTestRangeId=" + labTestRange.getLabTestRangeId() + "\"><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 LabTestRange", 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 LabTestRange", je); } } return ob.toString(); } @RequestMapping(value = "add", method = RequestMethod.GET) public String add(Model model) { model.addAttribute("labTestRange", new LabTestRange()); model.addAttribute("labTestList", bluebuttonDaoService.getLabTestService().list()); return "/bluebutton/labtestrange/edit"; } @RequestMapping(value = "edit", method = RequestMethod.GET) public String edit(ModelMap model, @RequestParam(value = "labTestRangeId") Integer labTestRangeId) { model.addAttribute("labTestList", bluebuttonDaoService.getLabTestService().list()); model.addAttribute("labTestRange", bluebuttonDaoService.getLabTestRangeService().findById(labTestRangeId)); return "/bluebutton/labtestrange/edit"; } @RequestMapping(value = "show", method = RequestMethod.GET) public String show(ModelMap model, @RequestParam(value = "labTestRangeId") Integer labTestRangeId) { model.addAttribute("labTestRange", bluebuttonDaoService.getLabTestRangeService().findById(labTestRangeId)); return "/bluebutton/labtestrange/show"; } // @RequestMapping( value = "save", method = RequestMethod.POST ) // public String save( @RequestParam( value = "labTest.labTestId" ) Integer labTest_labTestId, @Valid @ModelAttribute( "labTestRange" ) LabTestRange labTestRange, BindingResult result ) { // // labTestRange.setLabTest( bluebuttonDaoService.getLabTestService().findById( labTest_labTestId ) ); // // // if (result.hasErrors()) { return "/bluebutton/labtestrange/edit"; } // else { // // try { // bluebuttonDaoService.getLabTestRangeService().saveOrUpdate( labTestRange ); // } catch (NonUniqueObjectException e) { // log.debug("Merging Results"); // bluebuttonDaoService.getLabTestRangeService().merge( labTestRange ); // } // return "redirect:/labtestrange/list"; // } // } @RequestMapping(value = "delete", method = RequestMethod.GET) public String confirmDelete(ModelMap model, @RequestParam(value = "labTestRangeId") Integer labTestRangeId) { model.addAttribute("labTestRange", bluebuttonDaoService.getLabTestRangeService().findById(labTestRangeId)); return "/bluebutton/labtestrange/delete"; } @RequestMapping(value = "delete", method = RequestMethod.POST) public String doDelete(ModelMap model, @RequestParam(value = "submit") String submitButtonValue, @RequestParam(value = "labTestRangeId") Integer labTestRangeId) { if (StringUtils.equalsIgnoreCase(submitButtonValue, "yes")) { bluebuttonDaoService.getLabTestRangeService() .delete(bluebuttonDaoService.getLabTestRangeService().findById(labTestRangeId)); } return "redirect:/labtestrange/list"; } @RequestMapping(value = "import", method = RequestMethod.GET) public String displayImportCSVFileUpload() { return "/bluebutton/labtestrange/import"; } @RequestMapping(value = "import", method = RequestMethod.POST) public String importCSV(@RequestParam("file") MultipartFile file) throws IOException { this.bluebuttonDaoService.getLabTestRangeService().importCSV(file.getInputStream()); return "redirect:/labtestrange/"; } }