com.abm.mainet.water.ui.controller.PlumberLicenseFormController.java Source code

Java tutorial

Introduction

Here is the source code for com.abm.mainet.water.ui.controller.PlumberLicenseFormController.java

Source

package com.abm.mainet.water.ui.controller;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

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

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.abm.mainet.common.constant.MainetConstants;
import com.abm.mainet.common.dto.CommonChallanDTO;
import com.abm.mainet.common.service.IPortalServiceMasterService;
import com.abm.mainet.common.service.IServiceMasterService;
import com.abm.mainet.common.ui.controller.AbstractFormController;
import com.abm.mainet.common.ui.view.JsonViewObject;
import com.abm.mainet.common.util.ApplicationSession;
import com.abm.mainet.common.util.UserSession;
import com.abm.mainet.dms.service.FileUploadService;
import com.abm.mainet.dms.utility.FileUploadUtility;
import com.abm.mainet.water.ui.model.PlumberLicenseFormModel;

@Controller
@RequestMapping("/PlumberLicenseForm.html")
public class PlumberLicenseFormController extends AbstractFormController<PlumberLicenseFormModel> {

    private static Logger log = Logger.getLogger(PlumberLicenseFormController.class);
    @Autowired
    private transient IServiceMasterService serviceMaster;
    @Autowired
    IPortalServiceMasterService iPortalService;

    @RequestMapping(method = { RequestMethod.GET, RequestMethod.POST })
    public ModelAndView index(HttpServletRequest httpServletRequest) {
        sessionCleanup(httpServletRequest);
        FileUploadService.getCurrent().sessionCleanUpForFileUpload();
        try {
            this.getModel().initializeApplicantDetail();
            long orgId = UserSession.getCurrent().getOrganisation().getOrgid();
            Long serviceId = serviceMaster.getServiceId("WPL", orgId);
            Long deptId = iPortalService.getServiceDeptIdId(serviceId);
            this.getModel().setServiceId(serviceId);
            this.getModel().setDeptId(deptId);
            this.getModel().getPlumberQualificationDTOList().clear();
        } catch (Exception ex) {
            log.error("Error Occurred while rendering Form:", ex);
            return defaultExceptionView();
        }
        ModelAndView mv = null;
        mv = new ModelAndView("PlumberLicenseForm", MainetConstants.FORM_NAME, getModel());
        mv.addObject("command", getModel());
        return mv;
    }

    @RequestMapping(method = RequestMethod.POST, params = "getCheckListAndCharges")
    public ModelAndView doGetApplicableCheckListAndCharges(HttpServletRequest httpServletRequest) {
        this.bindModel(httpServletRequest);
        long orgId = UserSession.getCurrent().getOrganisation().getOrgid();
        ModelAndView modelAndView = null;
        PlumberLicenseFormModel model = this.getModel();
        try {
            for (Iterator<Entry<Long, Set<File>>> it = FileUploadUtility.getCurrent().getFileMap().entrySet()
                    .iterator(); it.hasNext();) {
                Entry<Long, Set<File>> entry = it.next();
                if (entry.getKey().longValue() == 0) {
                    Set<File> set = entry.getValue();
                    File file = set.iterator().next();
                    String bytestring = "";
                    Base64 base64 = new Base64();
                    try {
                        bytestring = base64.encodeToString(FileUtils.readFileToByteArray(file));
                    } catch (IOException e) {
                        logger.error("Exception has been occurred in file byte to string conversions", e);
                    }
                    String plumberImage = file.getName();
                    model.setPlumberImage(plumberImage);
                    model.getPlumberLicenseReqDTO().setPlumberImage(plumberImage);
                    model.getPlumberLicenseReqDTO().setImageByteCode(bytestring);
                    break;
                }
            }
            if (model.getPlumberImage() == null || model.getPlumberImage().isEmpty()) {
                this.getModel().addValidationError(ApplicationSession.getInstance()
                        .getMessage("water.plumberLicense.valMsg.uploadPlumberPhoto"));
            } else {
                model.findApplicableCheckListAndCharges(this.getModel().getServiceId(), orgId);
            }
            modelAndView = new ModelAndView("PlumberLicenseFormValidn", "command", model);
            if (model.getBindingResult() != null) {
                modelAndView.addObject(BindingResult.MODEL_KEY_PREFIX + MainetConstants.FORM_NAME,
                        getModel().getBindingResult());
            }
        } catch (Exception ex) {
            modelAndView = defaultExceptionFormView();
        }
        return modelAndView;
    }

    @RequestMapping(params = "save", method = RequestMethod.POST)
    public ModelAndView savePlumberLicenseForm(HttpServletRequest httpServletRequest,
            HttpServletResponse httpServletResponse) {
        this.getModel().bind(httpServletRequest);
        PlumberLicenseFormModel model = this.getModel();
        ModelAndView modelAndView = null;
        if (model.saveForm()) {
            CommonChallanDTO offline = model.getOfflineDTO();
            if (offline.getOnlineOfflineCheck() != null
                    && offline.getOnlineOfflineCheck().equals(MainetConstants.NewWaterServiceConstants.NO)) {
                return jsonResult(
                        JsonViewObject.successResult(getApplicationSession().getMessage("continue.forchallan")));
            } else {
                return jsonResult(
                        JsonViewObject.successResult(getApplicationSession().getMessage("continue.forpayment")));
            }
        }
        modelAndView = new ModelAndView("PlumberLicenseFormValidn", MainetConstants.FORM_NAME, getModel());
        modelAndView.addObject(BindingResult.MODEL_KEY_PREFIX + MainetConstants.FORM_NAME,
                getModel().getBindingResult());
        return modelAndView;
    }
}