Java tutorial
/* * 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 jp.co.nemuzuka.core.entity.LabelValueBean; import jp.co.nemuzuka.dao.CategoryDao; import jp.co.nemuzuka.form.CategoryForm; import jp.co.nemuzuka.model.CategoryModel; import jp.co.nemuzuka.service.CategoryService; import jp.co.nemuzuka.utils.ConvertUtils; import jp.co.nemuzuka.utils.LabelValueBeanUtils; 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; /** * CategoryService?. * @author kazumune */ public class CategoryServiceImpl implements CategoryService { CategoryDao categoryDao = CategoryDao.getInstance(); private static CategoryServiceImpl impl = new CategoryServiceImpl(); /** * ?. * @return */ public static CategoryServiceImpl getInstance() { return impl; } /** * . */ private CategoryServiceImpl() { } /* (non-Javadoc) * @see jp.co.nemuzuka.service.CategoryService#get(java.lang.String) */ @Override public CategoryForm get(String projectKeyString) { CategoryForm form = new CategoryForm(); if (StringUtils.isNotEmpty(projectKeyString)) { CategoryModel model = categoryDao.get4ProjectKey(projectKeyString); setForm(form, model); } return form; } /* (non-Javadoc) * @see jp.co.nemuzuka.service.CategoryService#put(jp.co.nemuzuka.form.CategoryForm, java.lang.String) */ @Override public void put(CategoryForm form, String projectKeyString) { CategoryModel model = null; Long version = ConvertUtils.toLong(form.versionNo); if (version != null) { //?? //version?Key?? Key key = Datastore.stringToKey(form.keyToString); model = categoryDao.get(key, version); if (model == null) { //??????Exceptionthrow throw new ConcurrentModificationException(); } } else { //??? model = new CategoryModel(); //Key?Key? Key key = Datastore.createKey(CategoryModel.class, projectKeyString); model.setKey(key); } setModel(model, form); categoryDao.put(model); } /* (non-Javadoc) * @see jp.co.nemuzuka.service.CategoryService#getList(java.lang.String) */ @Override public List<LabelValueBean> getList(String projectKeyString) { CategoryForm form = get(projectKeyString); return LabelValueBeanUtils.createList(form.categoryName, true); } /** * Form. * @param form Form * @param model Model */ private void setForm(CategoryForm form, CategoryModel model) { if (model == null) { return; } form.keyToString = model.getKeyToString(); form.categoryName = model.getCategoryName().getValue(); form.versionNo = ConvertUtils.toString(model.getVersion()); } /** * Model. * @param model Model * @param form Form */ private void setModel(CategoryModel model, CategoryForm form) { model.setCategoryName(new Text(form.categoryName)); } }