edu.uiowa.icts.bluebutton.controller.LoincCodeCategoryController.java Source code

Java tutorial

Introduction

Here is the source code for edu.uiowa.icts.bluebutton.controller.LoincCodeCategoryController.java

Source

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 01/27/2015 10:53:12 CST
 */
@Controller
@RequestMapping("/loinccodecategory/*")
public class LoincCodeCategoryController extends AbstractBluebuttonController {

    private static final Log log = LogFactory.getLog(LoincCodeCategoryController.class);

    @RequestMapping(value = "list_alt", method = RequestMethod.GET)
    public String listNoScript(Model model) {
        model.addAttribute("loincCodeCategoryList", bluebuttonDaoService.getLoincCodeCategoryService().list());
        return "/bluebutton/loinccodecategory/list_alt";
    }

    @RequestMapping(value = { "list", "", "/" }, method = RequestMethod.GET)
    public String list() {
        return "/bluebutton/loinccodecategory/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.getLoincCodeCategoryService().count(options);

            options.setLimit(limit);
            options.setStart(start);
            options.setSorts(sorts);

            List<LoincCodeCategory> loincCodeCategoryList = bluebuttonDaoService.getLoincCodeCategoryService()
                    .list(options);

            ob.put("draw", draw);
            ob.put("recordsFiltered", count);
            ob.put("recordsTotal", count);
            JSONArray jsonArray = new JSONArray();
            for (LoincCodeCategory loincCodeCategory : loincCodeCategoryList) {
                JSONArray tableRow = new JSONArray();
                for (DataTableHeader header : headers) {
                    String headerName = header.getName();
                    if (StringUtils.equals("id", headerName)) {
                        tableRow.put(loincCodeCategory.getId());
                    } else if (StringUtils.equals("loincCode", headerName)) {
                        tableRow.put(loincCodeCategory.getLoincCode());
                    } else if (StringUtils.equals("name", headerName)) {
                        tableRow.put(loincCodeCategory.getName());
                    } else if (StringUtils.equals("rootCategoryName", headerName)) {
                        tableRow.put(loincCodeCategory.getRootCategoryName());
                    } else if (StringUtils.equals("subrootCategoryName", headerName)) {
                        tableRow.put(loincCodeCategory.getSubrootCategoryName());
                    } else if (StringUtils.equals("urls", headerName)) {
                        String urls = "";
                        if (StringUtils.equals("list", display)) {
                            urls += "<a href=\"show?" + "id=" + loincCodeCategory.getId()
                                    + "\"><span class=\"glyphicon glyphicon-eye-open\"></a>";
                            urls += "<a href=\"edit?" + "id=" + loincCodeCategory.getId()
                                    + "\"><span class=\"glyphicon glyphicon-pencil\"></a>";
                            urls += "<a href=\"delete?" + "id=" + loincCodeCategory.getId()
                                    + "\"><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 LoincCodeCategory", 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 LoincCodeCategory", je);
            }
        }

        return ob.toString();
    }

    @RequestMapping(value = "add", method = RequestMethod.GET)
    public String add(Model model) {
        model.addAttribute("loincCodeCategory", new LoincCodeCategory());

        return "/bluebutton/loinccodecategory/edit";
    }

    @RequestMapping(value = "edit", method = RequestMethod.GET)
    public String edit(ModelMap model, @RequestParam(value = "id") Integer loincCodeCategoryId) {

        model.addAttribute("loincCodeCategory",
                bluebuttonDaoService.getLoincCodeCategoryService().findById(loincCodeCategoryId));
        return "/bluebutton/loinccodecategory/edit";
    }

    @RequestMapping(value = "show", method = RequestMethod.GET)
    public String show(ModelMap model, @RequestParam(value = "id") Integer loincCodeCategoryId) {

        model.addAttribute("loincCodeCategory",
                bluebuttonDaoService.getLoincCodeCategoryService().findById(loincCodeCategoryId));
        return "/bluebutton/loinccodecategory/show";
    }

    @RequestMapping(value = "save", method = RequestMethod.POST)
    public String save(@Valid @ModelAttribute("loincCodeCategory") LoincCodeCategory loincCodeCategory,
            BindingResult result, Model model) {

        if (result.hasErrors()) {
            return "/bluebutton/loinccodecategory/edit";
        } else {

            try {
                bluebuttonDaoService.getLoincCodeCategoryService().saveOrUpdate(loincCodeCategory);
            } catch (NonUniqueObjectException e) {
                log.debug("Merging Results");
                bluebuttonDaoService.getLoincCodeCategoryService().merge(loincCodeCategory);
            }
            return "redirect:/loinccodecategory/list";
        }
    }

    @RequestMapping(value = "delete", method = RequestMethod.GET)
    public String confirmDelete(ModelMap model, @RequestParam(value = "id") Integer loincCodeCategoryId) {

        model.addAttribute("loincCodeCategory",
                bluebuttonDaoService.getLoincCodeCategoryService().findById(loincCodeCategoryId));
        return "/bluebutton/loinccodecategory/delete";
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public String doDelete(ModelMap model, @RequestParam(value = "submit") String submitButtonValue,
            @RequestParam(value = "id") Integer loincCodeCategoryId) {

        if (StringUtils.equalsIgnoreCase(submitButtonValue, "yes")) {
            bluebuttonDaoService.getLoincCodeCategoryService()
                    .delete(bluebuttonDaoService.getLoincCodeCategoryService().findById(loincCodeCategoryId));
        }
        return "redirect:/loinccodecategory/list";
    }

    @RequestMapping(value = "import", method = RequestMethod.GET)
    public String displayImportCSVFileUpload() {
        return "/bluebutton/loinccodecategory/import";
    }

    @RequestMapping(value = "import", method = RequestMethod.POST)
    public String importCSV(@RequestParam("file") MultipartFile file) throws IOException {
        this.bluebuttonDaoService.getLoincCodeCategoryService().importCSV(file.getInputStream());
        return "redirect:/loinccodecategory/";
    }

}