com.baidu.cc.web.action.VersionSaveAction.java Source code

Java tutorial

Introduction

Here is the source code for com.baidu.cc.web.action.VersionSaveAction.java

Source

/*
 * Copyright 2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.baidu.cc.web.action;

import com.baidu.cc.common.SysUtils;
import com.baidu.cc.configuration.bo.Version;
import com.baidu.cc.configuration.service.EnvironmentService;
import com.baidu.cc.configuration.service.ProjectService;
import com.baidu.cc.configuration.service.VersionService;
import com.baidu.cc.web.action.VersionSaveActionBase.DataResult;
import com.baidu.cc.web.action.VersionSaveActionBase.ReqParam;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

/**
 * ?.
 * 
 * @author BJF
 */
public class VersionSaveAction extends VersionSaveActionBase<ReqParam, DataResult> {

    /** serial Version UID. */
    private static final long serialVersionUID = 1L;

    /** VersionService. */
    @Resource
    private VersionService versionService;

    /** ProjectService. */
    @Resource
    private ProjectService projectService;

    /** EnvironmentService. */
    @Resource
    private EnvironmentService environmentService;

    // step1 validate parameters. could be ignore
    /**
     * <pre>
     * ???????
     * ???????
     * </pre>
     * 
     * .
     * 
     * @param reqParam
     *            request param
     * @return req parameters valid result.
     */
    protected Map<String, String> doValidate(ReqParam reqParam) {
        Map<String, String> result = new HashMap<String, String>();
        Version version = reqParam.getVersion();
        if (version == null) {
            result.put("envNull", "");
        } else {
            if (StringUtils.isBlank(version.getName()) || version.getName().length() > 50) {
                result.put("nameLengthError", "??150");
            }
            Long versionId = versionService.findIdByName(version.getName());
            if (versionId != null && !versionId.equals(version.getId())) {
                result.put("nameUniqError", "????");
            }

            if (StringUtils.isNotBlank(version.getMemo()) && version.getMemo().length() > 255) {
                result.put("memoLengthError", "255");
            }
        }

        return result;

    }

    // step2 customize parse parameters. could be ignore
    /**
     * ???????.
     * 
     * @param reqParam
     *            request param
     * @return request parameters after user process.
     */
    protected ReqParam doParseParam(ReqParam reqParam) {
        return reqParam;
    }

    // step3 call service component
    /**
     * ?.
     * 
     * @param reqParam
     *            request param
     * @return 
     */
    @Override
    protected DataResult doExecuteService(ReqParam reqParam) {
        Version version = reqParam.getVersion();
        Long versionId = version.getId();
        Date now = new Date();
        version.setUpdateTime(now);
        // ID
        if (versionId == null) {
            version.setCheckSum(SysUtils.uuid());
            version.setCheckSumDate(now);
            versionService.saveEntitySelective(version);
        } else {
            versionService.updateEntitySelective(version);
        }

        return null;
    }

    // step4 wrap result return to font page
    /**
     * .
     * 
     * @param dr
     *            
     * @return result after user process.
     */
    protected Object doWrapResult(DataResult dr) {
        return dr;
    }

}