org.openmrs.module.atlas.web.controller.AtlasModuleFormController.java Source code

Java tutorial

Introduction

Here is the source code for org.openmrs.module.atlas.web.controller.AtlasModuleFormController.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.atlas.web.controller;

import java.util.Collection;
import java.util.UUID;

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.openmrs.module.atlas.AtlasData;
import org.openmrs.module.atlas.AtlasService;
import org.openmrs.module.atlas.impl.AtlasServiceImpl;
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;
import org.springframework.web.servlet.mvc.SimpleFormController;

/**
 * This class configured as controller using annotation and mapped with the URL of
 * 'module/basicmodule/basicmoduleLink.form'.
 */
@Controller
@RequestMapping(value = "/module/atlas/managemarker.form")
public class AtlasModuleFormController {

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

    /** Success form view name */
    private final String SUCCESS_FORM_VIEW = "/module/atlas/managemarker";

    /**
     * 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("atlasData")
    protected AtlasData formBackingObject(HttpServletRequest request) throws Exception {

        AtlasService service = (AtlasService) Context.getService(AtlasService.class);
        AtlasData data = service.getAtlasData();

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

    }

}