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 java.util.LinkedHashMap; 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; import edu.uiowa.icts.datatable.DataTable; /** * Generated by Protogen * @since 04/14/2015 11:39:11 CDT */ @Controller @RequestMapping("/labresultsynonym/*") public class LabResultSynonymController extends AbstractBluebuttonController { private static final Log log = LogFactory.getLog(LabResultSynonymController.class); @RequestMapping(value = "list_alt", method = RequestMethod.GET) public String listNoScript(Model model) { model.addAttribute("labResultSynonymList", bluebuttonDaoService.getLabResultSynonymService().list()); return "/bluebutton/labresultsynonym/list_alt"; } @RequestMapping(value = { "list", "", "/" }, method = RequestMethod.GET) public String list() { return "/bluebutton/labresultsynonym/list"; } @ResponseBody @RequestMapping(value = "datatable", produces = "application/json") public DataTable 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) { String contextPath = request.getContextPath(); 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>(); DataTable dt = new DataTable(); 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.getLabResultSynonymService().count(options); options.setLimit(limit); options.setStart(start); options.setSorts(sorts); List<LabResultSynonym> labResultSynonymList = bluebuttonDaoService.getLabResultSynonymService() .list(options); List<LinkedHashMap<String, Object>> data = new ArrayList<LinkedHashMap<String, Object>>(); for (LabResultSynonym labResultSynonym : labResultSynonymList) { LinkedHashMap<String, Object> tableRow = new LinkedHashMap<String, Object>(); for (DataTableHeader header : headers) { String headerName = header.getName(); String dataName = header.getData(); if (StringUtils.equals("labResultSynonymId", headerName)) { tableRow.put(dataName, labResultSynonym.getLabResultSynonymId()); } else if (StringUtils.equals("officialName", headerName)) { tableRow.put(dataName, labResultSynonym.getOfficialName()); } else if (StringUtils.equals("unofficialName", headerName)) { tableRow.put(dataName, labResultSynonym.getUnofficialName()); } else if (StringUtils.equals("panel", headerName)) { tableRow.put(dataName, labResultSynonym.getPanel()); } else if (StringUtils.equals("urls", headerName)) { String urls = ""; if (StringUtils.equals("list", display)) { urls += "<a href=\"" + contextPath + "/labresultsynonym/show?" + "labResultSynonymId=" + labResultSynonym.getLabResultSynonymId() + "\"><span class=\"glyphicon glyphicon-eye-open\"></a>"; urls += "<a href=\"" + contextPath + "/labresultsynonym/edit?" + "labResultSynonymId=" + labResultSynonym.getLabResultSynonymId() + "\"><span class=\"glyphicon glyphicon-pencil\"></a>"; urls += "<a href=\"" + contextPath + "/labresultsynonym/delete?" + "labResultSynonymId=" + labResultSynonym.getLabResultSynonymId() + "\"><span class=\"glyphicon glyphicon-trash\"></a>"; } else { } tableRow.put(dataName, urls); } else { tableRow.put("error", "[error: column " + headerName + " not supported]"); } } data.add(tableRow); } dt.setDraw(draw); dt.setRecordsFiltered(count); dt.setRecordsTotal(count); dt.setData(data); } catch (Exception e) { log.error("error builing datatable json object for LabResultSynonym", e); String stackTrace = e.getMessage() + String.valueOf('\n'); for (StackTraceElement ste : e.getStackTrace()) { stackTrace += ste.toString() + String.valueOf('\n'); } DataTable error = new DataTable(); error.setDraw(draw); error.setRecordsFiltered(0); error.setRecordsTotal(0); error.setError(stackTrace); return error; } return dt; } @RequestMapping(value = "add", method = RequestMethod.GET) public String add(Model model) { model.addAttribute("labResultSynonym", new LabResultSynonym()); return "/bluebutton/labresultsynonym/edit"; } @RequestMapping(value = "edit", method = RequestMethod.GET) public String edit(ModelMap model, @RequestParam(value = "labResultSynonymId") Integer labResultSynonymId) { model.addAttribute("labResultSynonym", bluebuttonDaoService.getLabResultSynonymService().findById(labResultSynonymId)); return "/bluebutton/labresultsynonym/edit"; } @RequestMapping(value = "show", method = RequestMethod.GET) public String show(ModelMap model, @RequestParam(value = "labResultSynonymId") Integer labResultSynonymId) { model.addAttribute("labResultSynonym", bluebuttonDaoService.getLabResultSynonymService().findById(labResultSynonymId)); return "/bluebutton/labresultsynonym/show"; } @RequestMapping(value = "save", method = RequestMethod.POST) public String save(@Valid @ModelAttribute("labResultSynonym") LabResultSynonym labResultSynonym, BindingResult result, Model model) { if (result.hasErrors()) { return "/bluebutton/labresultsynonym/edit"; } else { try { labResultSynonym.setOfficialName(labResultSynonym.getOfficialName().trim()); labResultSynonym.setUnofficialName(labResultSynonym.getUnofficialName().trim()); labResultSynonym.setPanel(labResultSynonym.getPanel().trim()); bluebuttonDaoService.getLabResultSynonymService().saveOrUpdate(labResultSynonym); } catch (NonUniqueObjectException e) { log.debug("Merging Results"); bluebuttonDaoService.getLabResultSynonymService().merge(labResultSynonym); } return "redirect:/labresultsynonym/list"; } } @RequestMapping(value = "delete", method = RequestMethod.GET) public String confirmDelete(ModelMap model, @RequestParam(value = "labResultSynonymId") Integer labResultSynonymId) { model.addAttribute("labResultSynonym", bluebuttonDaoService.getLabResultSynonymService().findById(labResultSynonymId)); return "/bluebutton/labresultsynonym/delete"; } @RequestMapping(value = "delete", method = RequestMethod.POST) public String doDelete(ModelMap model, @RequestParam(value = "submit") String submitButtonValue, @RequestParam(value = "labResultSynonymId") Integer labResultSynonymId) { if (StringUtils.equalsIgnoreCase(submitButtonValue, "yes")) { bluebuttonDaoService.getLabResultSynonymService() .delete(bluebuttonDaoService.getLabResultSynonymService().findById(labResultSynonymId)); } return "redirect:/labresultsynonym/list"; } }