org.openmrs.module.pharmacy.web.controller.PharmacyFormController.java Source code

Java tutorial

Introduction

Here is the source code for org.openmrs.module.pharmacy.web.controller.PharmacyFormController.java

Source

/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.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://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.module.pharmacy.web.controller;

import java.util.Collection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.Patient;
import org.openmrs.api.context.Context;
import org.springframework.stereotype.Controller;
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;

/**
 * This class configured as controller using annotation and mapped with the URL of 'module/pharmacy/pharmacyLink.form'.
 */
@Controller
@RequestMapping(value = "module/pharmacy/pharmacyLink.form")
public class PharmacyFormController {

    /** Logger for this class and subclasses */
    protected final Log log = LogFactory.getLog(getClass());

    /** Success form view name */
    private final String SUCCESS_FORM_VIEW = "/module/pharmacy/pharmacyForm";

    /**
     * Initially called after the formBackingObject method to get the landing form name  
     * @return String form view name
     */
    @RequestMapping(method = RequestMethod.GET)
    public String showForm() {
        return SUCCESS_FORM_VIEW;
    }

    /**
     * All the parameters are optional based on the necessity  
     * 
     * @param httpSession
     * @param anyRequestObject
     * @param errors
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public String onSubmit(HttpSession httpSession, @ModelAttribute("anyRequestObject") Object anyRequestObject,
            BindingResult errors) {

        if (errors.hasErrors()) {
            // return error view
        }
        return SUCCESS_FORM_VIEW;
    }

    /**
     * This class returns the form backing object. This can be a string, a boolean, or a normal java
     * pojo. The bean name defined in the ModelAttribute annotation and the type can be just
     * defined by the return type of this method
     */
    @ModelAttribute("thePatientList")
    protected Collection<Patient> formBackingObject(HttpServletRequest request) throws Exception {
        // get all patients that have an identifier "101" (from the demo sample data)
        // see http://resources.openmrs.org/doc/index.html?org/openmrs/api/PatientService.html for
        // a list of all PatientService methods
        Collection<Patient> patients = Context.getPatientService().findPatients("101", false);

        // this object will be made available to the jsp page under the variable name
        // that is defined in the @ModuleAttribute tag
        return patients;
    }

}