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.PriorityDao; import jp.co.nemuzuka.form.PriorityForm; import jp.co.nemuzuka.model.PriorityModel; import jp.co.nemuzuka.service.PriorityService; 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; /** * PriorityService?. * @author kazumune */ public class PriorityServiceImpl implements PriorityService { PriorityDao priorityDao = PriorityDao.getInstance(); private static PriorityServiceImpl impl = new PriorityServiceImpl(); /** * ?. * @return */ public static PriorityServiceImpl getInstance() { return impl; } /** * . */ private PriorityServiceImpl() { } /* (? Javadoc) * @see jp.co.nemuzuka.service.PriorityService#get(java.lang.String) */ @Override public PriorityForm get(String projectKeyString) { PriorityForm form = new PriorityForm(); if (StringUtils.isNotEmpty(projectKeyString)) { PriorityModel model = priorityDao.get4ProjectKey(projectKeyString); setForm(form, model); } return form; } /* (? Javadoc) * @see jp.co.nemuzuka.service.PriorityService#put(jp.co.nemuzuka.form.PriorityForm, java.lang.String) */ @Override public void put(PriorityForm form, String projectKeyString) { PriorityModel model = null; Long version = ConvertUtils.toLong(form.versionNo); if (version != null) { //?? //version?Key?? Key key = Datastore.stringToKey(form.keyToString); model = priorityDao.get(key, version); if (model == null) { //??????Exceptionthrow throw new ConcurrentModificationException(); } } else { //??? model = new PriorityModel(); //Key?Key? Key key = Datastore.createKey(PriorityModel.class, projectKeyString); model.setKey(key); } setModel(model, form); priorityDao.put(model); } /* (? Javadoc) * @see jp.co.nemuzuka.service.PriorityService#getList(java.lang.String) */ @Override public List<LabelValueBean> getList(String projectKeyString) { PriorityForm form = get(projectKeyString); return LabelValueBeanUtils.createList(form.priorityName, true); } /** * Form. * @param form Form * @param model Model */ private void setForm(PriorityForm form, PriorityModel model) { if (model == null) { return; } form.keyToString = model.getKeyToString(); form.priorityName = model.getPriorityName().getValue(); form.versionNo = ConvertUtils.toString(model.getVersion()); } /** * Model. * @param model Model * @param form Form */ private void setModel(PriorityModel model, PriorityForm form) { model.setPriorityName(new Text(form.priorityName)); } }