jp.co.nemuzuka.service.impl.StatusServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for jp.co.nemuzuka.service.impl.StatusServiceImpl.java

Source

/*
 * Copyright 2012 Kazumune Katagiri. (http://d.hatena.ne.jp/nemuzuka)
 *
 * 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 jp.co.nemuzuka.service.impl;

import java.util.ConcurrentModificationException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slim3.datastore.Datastore;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Text;

import jp.co.nemuzuka.core.entity.LabelValueBean;
import jp.co.nemuzuka.dao.StatusDao;
import jp.co.nemuzuka.form.StatusForm;
import jp.co.nemuzuka.model.StatusModel;
import jp.co.nemuzuka.service.StatusService;
import jp.co.nemuzuka.utils.ConvertUtils;
import jp.co.nemuzuka.utils.LabelValueBeanUtils;

/**
 * StatusService?.
 * @author k-katagiri
 */
public class StatusServiceImpl implements StatusService {

    StatusDao statusDao = StatusDao.getInstance();

    private static StatusServiceImpl impl = new StatusServiceImpl();

    /**
     * ?.
     * @return 
     */
    public static StatusServiceImpl getInstance() {
        return impl;
    }

    /**
     * .
     */
    private StatusServiceImpl() {
    }

    /* (non-Javadoc)
     * @see jp.co.nemuzuka.service.StatusService#get(java.lang.String)
     */
    @Override
    public StatusForm get(String projectKeyString) {
        StatusForm form = new StatusForm();
        if (StringUtils.isNotEmpty(projectKeyString)) {
            StatusModel model = statusDao.get4ProjectKey(projectKeyString);
            setForm(form, model);
        }
        return form;
    }

    /* (non-Javadoc)
     * @see jp.co.nemuzuka.service.StatusService#put(jp.co.nemuzuka.form.StatusForm, java.lang.String)
     */
    @Override
    public void put(StatusForm form, String projectKeyString) {
        StatusModel model = null;
        Long version = ConvertUtils.toLong(form.versionNo);

        if (version != null) {
            //??
            //version?Key??
            Key key = Datastore.stringToKey(form.keyToString);
            model = statusDao.get(key, version);
            if (model == null) {
                //??????Exceptionthrow
                throw new ConcurrentModificationException();
            }
        } else {
            //???
            model = new StatusModel();
            //Key?Key?
            Key key = Datastore.createKey(StatusModel.class, projectKeyString);
            model.setKey(key);
        }
        setModel(model, form);
        statusDao.put(model);
    }

    /* (non-Javadoc)
     * @see jp.co.nemuzuka.service.StatusService#getList(java.lang.String)
     */
    @Override
    public List<LabelValueBean> getList(String projectKeyString) {
        StatusForm form = get(projectKeyString);
        return LabelValueBeanUtils.createList(form.statusName, true);
    }

    /**
     * Form.
     * @param form Form
     * @param model Model
     */
    private void setForm(StatusForm form, StatusModel model) {
        if (model == null) {
            return;
        }
        form.keyToString = model.getKeyToString();
        form.statusName = model.getStatusName().getValue();
        form.closeStatusName = model.getCloseStatusName().getValue();
        form.versionNo = ConvertUtils.toString(model.getVersion());
    }

    /**
     * Model.
     * @param model Model
     * @param form Form
     */
    private void setModel(StatusModel model, StatusForm form) {
        model.setStatusName(new Text(form.statusName));
        model.setCloseStatusName(new Text(StringUtils.defaultString(form.closeStatusName, "")));
    }

}