Untitled

mail@pastecode.io avatar
unknown
plain_text
21 days ago
5.9 kB
3
Indexable
Never
package com.jnj.mdm.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.jnj.mdm.beans.ContextUser;
import com.jnj.mdm.common.Constants;
import com.jnj.mdm.entity.FieldDetails;
import com.jnj.mdm.entity.Lov;
import com.jnj.mdm.entity.Validation;
import com.jnj.mdm.security.SecurityViewHelper;
import com.jnj.mdm.services.AdminServices;
import com.jnj.mdm.services.FieldServices;

/**
 * @author mkanakiy C:29/5/2019 U:17/06/2019
 *
 */
@Controller
public class FieldController extends MultiActionController {

	private static final Log LOGGER = LogFactory.getLog(FieldController.class);
	private static final String SRC_FLD_LIST_VIEW = "fieldViewList";
	private static final String SRC_ADD_FLD_VIEW = "addField";

	@Autowired
	private FieldServices fieldServices;

	@Autowired
	private AdminServices adminServices;

	@RequestMapping(value = "/saveField.view", method = RequestMethod.POST)
	public ModelAndView addField(HttpServletRequest req, HttpServletResponse res,
		@ModelAttribute("fieldForm") FieldDetails fieldDetails) throws Exception {
		ModelAndView modelAndView = new ModelAndView(SRC_FLD_LIST_VIEW);
		String resultText = "";
		String resultStatus = "";
		ContextUser currentUser = SecurityViewHelper.getUser();

		if (!req.getParameter("isMandate").equals("N")) {
			fieldDetails.setValidationId(fieldDetails.getValidationId() + "," + req.getParameter("isMandate"));
		}
		boolean success = fieldServices.addFieldDetails(fieldDetails, currentUser.getNtid());
		if (!success) {
			resultText = "Try again later!";
			resultStatus = "danger";
		}
		else {
			resultText = "Field added successfully";
			resultStatus = "success";
		}

		modelAndView.setViewName("redirect:/adminGetAllFieldList.view");
		modelAndView.addObject("resultMessage", resultText);
		modelAndView.addObject("resultStatus", resultStatus);

		return modelAndView;
	}

	@RequestMapping(value = "/adminEditField.view", method = RequestMethod.GET)
	public ModelAndView editField(HttpServletRequest req, HttpServletResponse res) throws Exception {

		ContextUser currentUser = SecurityViewHelper.getUser();
		ModelAndView modelAndView = new ModelAndView("editField");
		String param = req.getParameter("fieldId");
		if (param != null && !param.isEmpty()) {

			Integer fieldId = null;
			try {
				
				fieldId = new Integer(param);
				FieldDetails fieldDetails = fieldServices.getFieldByFieldId(fieldId);
				if (fieldDetails == null || fieldDetails.getIsDynamic() == 'N') {

					modelAndView.setViewName("redirect:/adminGetAllFieldList.view");

				} else {

					boolean isUsed = fieldServices.isFieldUsedInProduct(fieldId);
					String used = isUsed ? "used" : "";
					modelAndView.addObject("isFieldUsed", used);

					modelAndView.addObject("field", fieldDetails);
					List<Validation> validations = adminServices.getAllValidationTypes();
					if (validations != null)
						modelAndView.addObject("validationList", validations);
				}
			} catch (NumberFormatException e) {
				modelAndView.setViewName("redirect:/adminGetAllFieldList.view");
			}
		}else{
			modelAndView.setViewName("redirect:/adminGetAllFieldList.view");
		}

		return modelAndView;
	}

	@RequestMapping(value = "/updateField.view", method = RequestMethod.POST)
	public ModelAndView updateField(HttpServletRequest req, HttpServletResponse res,
			@ModelAttribute("fieldForm") FieldDetails fieldDetails) throws Exception {

		ContextUser currentUser = SecurityViewHelper.getUser();
		ModelAndView modelAndView = new ModelAndView(SRC_FLD_LIST_VIEW);
		if (req.getParameter("isMandate")!=null && !req.getParameter("isMandate").equals("N")) {
			fieldDetails.setValidationId(fieldDetails.getValidationId() + "," + req.getParameter("isMandate"));
		}
		boolean success = fieldServices.updateFieldDetails(fieldDetails, currentUser.getNtid());
		String resultText = "";
		String resultStatus = "";
		if (!success) {
			resultText = "Try again later!";
			resultStatus = "danger";
		}
		else {
			resultText = "Field updated successfully";
			resultStatus = "success";
		}

		modelAndView.setViewName("redirect:/adminGetAllFieldList.view");
		modelAndView.addObject("resultMessage", resultText);
		modelAndView.addObject("resultStatus", resultStatus);
		return modelAndView;
	}
  
	@RequestMapping(value = "/adminGetAllFieldList.view", method = RequestMethod.GET)
	public ModelAndView getAllFieldData(HttpServletRequest req,HttpServletResponse res) throws Exception {
		HttpSession session = req.getSession(true);
		
		ModelAndView modelAndView = new ModelAndView(SRC_FLD_LIST_VIEW);
		modelAndView.addObject("pageIdentifier", Constants.MDM_ADMINISTRATION);
		return modelAndView;

	}

	 
	@RequestMapping(value = "/adminAddFieldDetails.view", method = RequestMethod.GET)
	public ModelAndView addFieldDetails(HttpServletRequest req,HttpServletResponse res) throws Exception {
		HttpSession session = req.getSession(true);
		ModelAndView modelAndView = new ModelAndView(SRC_ADD_FLD_VIEW);

		List<Validation> validations = adminServices.getAllValidationTypes();
		if (validations != null)
			modelAndView.addObject("validationList", validations);

		return modelAndView;
	}
}
Leave a Comment