com.safedocs.license.controller.SafeDocsLicenseController.java Source code

Java tutorial

Introduction

Here is the source code for com.safedocs.license.controller.SafeDocsLicenseController.java

Source

package com.safedocs.license.controller;

import com.safedocs.license.model.SafeDocsLicenseModel;
import com.safedocs.license.service.ISafeDocsLicenseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
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.servlet.ModelAndView;
import org.springframework.web.servlet.view.xml.MarshallingView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Controller
public class SafeDocsLicenseController {

    @Autowired
    @Qualifier(value = "safeDocsLicenseService")
    public ISafeDocsLicenseService<SafeDocsLicenseModel> licenseService;

    @Resource
    MarshallingView safedocsMarshallingView;

    private static final Logger logger = LoggerFactory.getLogger(SafeDocsLicenseController.class);

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ModelAndView save(@RequestParam Map<String, String> params) throws Exception {
        String uuid;
        if (params.get("uuid") == null) {
            throw new Exception("uuid?  !!");

        } else {
            uuid = params.get("uuid");
        }
        String viewcnt = params.get("viewcnt") == null ? "0" : params.get("viewcnt");
        int nViewCnt = Integer.parseInt(viewcnt.equals("") ? "0" : viewcnt);

        String viewperiod = params.get("viewperiod") == null ? "0" : params.get("viewperiod");
        int nViewPeriod = Integer.parseInt(viewperiod.equals("") ? "0" : viewperiod);

        int nTargerCount = params.get("targetcnt") == null ? 0 : Integer.parseInt(params.get("targetcnt"));

        printInfo(params, "/save");

        SafeDocsLicenseModel model = new SafeDocsLicenseModel();

        model.setUuid(uuid);
        model.setViewcnt(nViewCnt);
        model.setViewperiod(nViewPeriod);

        return new ModelAndView(safedocsMarshallingView,
                (Map<String, ?>) licenseService.saveLicenseData(model, nTargerCount));

    }

    @RequestMapping(value = "/find", method = RequestMethod.POST)
    public ModelAndView find(@RequestParam Map<String, String> params) throws Exception {
        String uuid = params.get("uuid");
        boolean isFirstTimeOpen = Boolean.valueOf(params.get("firsttimeopen"));
        printInfo(params, "/find");

        SafeDocsLicenseModel model = new SafeDocsLicenseModel(uuid);
        if (isFirstTimeOpen) {
            model.setStatus(0);
        } else {
            model.setHardwareid(params.get("hardwareid"));
            model.setStatus(1);

        }

        return new ModelAndView(safedocsMarshallingView, (Map<String, ?>) licenseService.findLicenseData(model));

    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ModelAndView update(@RequestParam Map<String, String> params) throws Exception {

        String uuid = params.get("uuid");
        String hardwareid = params.get("hardwareid");

        String viewcnt = params.get("viewcnt") == null ? "0" : params.get("viewcnt");
        int nViewCnt = Integer.parseInt(viewcnt.equals("") ? "0" : viewcnt);
        int nId = Integer.parseInt(params.get("id"));
        printInfo(params, "/update");

        SafeDocsLicenseModel model = new SafeDocsLicenseModel(uuid);
        model.setHardwareid(hardwareid);
        model.setStatus(1);
        model.setViewcnt(nViewCnt);
        model.setId(nId);

        return new ModelAndView(safedocsMarshallingView, (Map<String, ?>) licenseService.updateLicenseData(model));

    }

    @RequestMapping(value = "/getdate", method = { RequestMethod.POST, RequestMethod.GET })
    public ModelAndView getDate(@RequestParam Map<String, String> params) throws Exception {

        Map<String, Object> retmap = new HashMap<String, Object>();
        retmap.put("safedocs", licenseService.getSystemDate());
        return new ModelAndView(safedocsMarshallingView, retmap);

    }

    private void printInfo(Map<String, String> params, String action) {
        logger.info("###############<<< " + action + " >>>###############");
        Iterator<String> iterator = params.keySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            logger.info("param key={},value={}", key, params.get(key));
        }

    }

}