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.text.SimpleDateFormat; import java.util.ConcurrentModificationException; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; import jp.co.nemuzuka.dao.MilestoneDao; import jp.co.nemuzuka.entity.MilestoneModelEx; import jp.co.nemuzuka.form.MilestoneForm; import jp.co.nemuzuka.model.MilestoneModel; import jp.co.nemuzuka.service.MilestoneService; import jp.co.nemuzuka.utils.ConvertUtils; import jp.co.nemuzuka.utils.DateTimeUtils; import org.apache.commons.lang.StringUtils; import org.slim3.datastore.Datastore; import com.google.appengine.api.datastore.Key; /** * MilestoneService?. * @author kazumune */ public class MilestoneServiceImpl implements MilestoneService { MilestoneDao milestoneDao = MilestoneDao.getInstance(); private static MilestoneServiceImpl impl = new MilestoneServiceImpl(); /** * ?. * @return */ public static MilestoneServiceImpl getInstance() { return impl; } /** * . */ private MilestoneServiceImpl() { } /* (non-Javadoc) * @see jp.co.nemuzuka.service.MilestoneService#get(java.lang.String) */ @Override public MilestoneForm get(String keyString) { MilestoneForm form = new MilestoneForm(); if (StringUtils.isNotEmpty(keyString)) { MilestoneModel model = milestoneDao.get(keyString); setForm(form, model); } return form; } /* (non-Javadoc) * @see jp.co.nemuzuka.service.MilestoneService#put(jp.co.nemuzuka.form.MilestoneForm, java.lang.String) */ @Override public void put(MilestoneForm form, String projectKeyToString) { MilestoneModel model = null; if (StringUtils.isNotEmpty(form.keyToString)) { //?? Key key = Datastore.stringToKey(form.keyToString); Long version = ConvertUtils.toLong(form.versionNo); //version?Key?projectKey?? model = milestoneDao.get(key, version, projectKeyToString); if (model == null) { //??????Exceptionthrow throw new ConcurrentModificationException(); } } else { //??? model = new MilestoneModel(); } setModel(model, form, projectKeyToString); milestoneDao.put(model); } /* (non-Javadoc) * @see jp.co.nemuzuka.service.MilestoneService#delete(jp.co.nemuzuka.form.MilestoneForm, java.lang.String) */ @Override public void delete(MilestoneForm form, String projectKeyToString) { Key key = Datastore.stringToKey(form.keyToString); Long version = ConvertUtils.toLong(form.versionNo); //version?Key?projectKey?? MilestoneModel model = milestoneDao.get(key, version, projectKeyToString); if (model == null) { //??????Exceptionthrow throw new ConcurrentModificationException(); } milestoneDao.delete(model.getKey()); } /* (non-Javadoc) * @see jp.co.nemuzuka.service.MilestoneService#getAllList(java.lang.String) */ @Override public List<MilestoneModelEx> getAllList(String projectKeyToString) { return milestoneDao.getAllList(projectKeyToString); } /* (non-Javadoc) * @see jp.co.nemuzuka.service.MilestoneService#updateSortNum(java.lang.String[], java.lang.String) */ @Override public void updateSortNum(String[] sortedKeyToString, String projectKeyToString) { //?Key? Set<Key> keys = new LinkedHashSet<Key>(); for (String target : sortedKeyToString) { keys.add(Datastore.stringToKey(target)); } if (keys.size() == 0) { return; } Map<Key, MilestoneModel> map = milestoneDao.getMap(projectKeyToString, keys.toArray(new Key[0])); //1???? long sortNum = 1; for (Key target : keys) { MilestoneModel model = map.get(target); if (model != null) { model.setSortNum(sortNum); milestoneDao.put(model); sortNum++; } } } /** * Form. * @param form Form * @param model Model */ private void setForm(MilestoneForm form, MilestoneModel model) { if (model == null) { return; } SimpleDateFormat sdf = DateTimeUtils.createSdf("yyyyMMdd"); form.keyToString = model.getKeyToString(); form.milestoneName = model.getMilestoneName(); form.startDate = ConvertUtils.toString(model.getStartDate(), sdf); form.endDate = ConvertUtils.toString(model.getEndDate(), sdf); form.versionNo = ConvertUtils.toString(model.getVersion()); } /** * Model. * @param model Model * @param form Form * @param projectKeyToString Key */ private void setModel(MilestoneModel model, MilestoneForm form, String projectKeyToString) { if (model.getKey() == null) { //??? Key projectKey = Datastore.stringToKey(projectKeyToString); model.setProjectKey(projectKey); model.setSortNum(Long.MAX_VALUE); } SimpleDateFormat sdf = DateTimeUtils.createSdf("yyyyMMdd"); model.setMilestoneName(form.milestoneName); model.setStartDate(ConvertUtils.toDate(form.startDate, sdf)); model.setEndDate(ConvertUtils.toDate(form.endDate, sdf)); } }