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

Java tutorial

Introduction

Here is the source code for jp.co.nemuzuka.service.impl.TodoServiceImpl.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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

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.common.PeriodStatus;
import jp.co.nemuzuka.common.TodoStatus;
import jp.co.nemuzuka.dao.TodoDao;
import jp.co.nemuzuka.dao.TodoDao.Param;
import jp.co.nemuzuka.entity.TodoModelEx;
import jp.co.nemuzuka.form.TodoCommentForm;
import jp.co.nemuzuka.form.TodoDetailForm;
import jp.co.nemuzuka.form.TodoForm;
import jp.co.nemuzuka.model.TodoModel;
import jp.co.nemuzuka.service.CommentService;
import jp.co.nemuzuka.service.MemberService;
import jp.co.nemuzuka.service.TodoService;
import jp.co.nemuzuka.service.TodoTagService;
import jp.co.nemuzuka.utils.ConvertUtils;
import jp.co.nemuzuka.utils.CurrentDateUtils;
import jp.co.nemuzuka.utils.DateTimeUtils;

/**
 * TodoService?.
 * @author k-katagiri
 */
public class TodoServiceImpl implements TodoService {

    TodoDao todoDao = TodoDao.getInstance();
    CommentService commentService = CommentServiceImpl.getInstance();
    MemberService memberService = MemberServiceImpl.getInstance();
    TodoTagService todoTagService = TodoTagServiceImpl.getInstance();

    private static TodoServiceImpl impl = new TodoServiceImpl();

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

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

    /* (non-Javadoc)
     * @see jp.co.nemuzuka.service.TodoService#getList(jp.co.nemuzuka.dao.TodoDao.Param)
     */
    @Override
    public List<TodoModelEx> getList(Param param, boolean isDashboard) {

        List<TodoModel> modelList = null;
        if (isDashboard) {
            modelList = todoDao.getDashbordList(param.limit, param.targetMemberKeyString);
        } else {
            modelList = todoDao.getList(param);
        }

        List<TodoModelEx> list = new ArrayList<TodoModelEx>();
        Date today = CurrentDateUtils.getInstance().getCurrentDate();
        SimpleDateFormat sdf = DateTimeUtils.createSdf("yyyyMMdd");
        SimpleDateFormat sdf2 = DateTimeUtils.createSdf("yyyy/MM/dd HH:mm");
        for (TodoModel target : modelList) {
            TodoModelEx entity = new TodoModelEx();
            entity.setModel(target);
            entity.setPeriod(ConvertUtils.toString(target.getPeriod(), sdf));
            entity.setCreatedAt(ConvertUtils.toString(target.getCreatedAt(), sdf2));

            //??????
            if (target.getPeriod() != null) {
                entity.setPeriodStatus(createPeriodStatus(today, target.getPeriod(), target.getStatus()));
            }
            entity.setTodoStatus(target.getStatus().getLabel());
            list.add(entity);
        }
        return list;
    }

    /* (non-Javadoc)
     * @see jp.co.nemuzuka.service.TodoService#get(java.lang.String, java.lang.String)
     */
    @Override
    public TodoForm get(String keyString, String mail) {

        TodoForm form = new TodoForm();
        if (StringUtils.isNotEmpty(keyString)) {
            //Key??????
            Key key = Datastore.stringToKey(keyString);
            Key memberKey = memberService.getKey(mail);
            TodoModel model = todoDao.getWithMemberKey(key, memberKey);
            setForm(form, model);
        }
        //TODO
        form.setTagList(todoTagService.getList(mail));
        return form;
    }

    /* (non-Javadoc)
     * @see jp.co.nemuzuka.service.TodoService#getDetail(java.lang.String, java.lang.String)
     */
    @Override
    public TodoDetailForm getDetail(String keyString, String mail) {

        TodoForm form = get(keyString, mail);
        if (StringUtils.isEmpty(form.keyToString)) {
            return null;
        }

        TodoDetailForm detailForm = new TodoDetailForm();
        detailForm.setForm(form);
        detailForm.commentList = commentService.getList(Datastore.stringToKey(form.keyToString));

        return detailForm;
    }

    /* (non-Javadoc)
     * @see jp.co.nemuzuka.service.TodoService#updateTodoStatus(jp.co.nemuzuka.form.TodoForm, java.lang.String)
     */
    @Override
    public void updateTodoStatus(TodoForm form, String email) {
        TodoModel model = null;
        Key memberKey = memberService.getKey(email);
        Key key = Datastore.stringToKey(form.keyToString);
        Long version = ConvertUtils.toLong(form.versionNo);
        //key????Key??
        model = todoDao.get(key, version, memberKey);
        if (model == null) {
            //??????Exceptionthrow
            throw new ConcurrentModificationException();
        }

        TodoStatus status = TodoStatus.fromCode(form.todoStatus);
        if (status == null) {
            status = TodoStatus.nothing;
        }
        model.setStatus(status);
        todoDao.put(model);
    }

    /* (non-Javadoc)
     * @see jp.co.nemuzuka.service.TodoService#put(jp.co.nemuzuka.form.TodoForm, java.lang.String)
     */
    @Override
    public void put(TodoForm form, String mail) {

        TodoModel model = null;
        Key memberKey = memberService.getKey(mail);
        if (StringUtils.isNotEmpty(form.keyToString)) {
            //??
            Key key = Datastore.stringToKey(form.keyToString);
            Long version = ConvertUtils.toLong(form.versionNo);
            //key????Key??
            model = todoDao.get(key, version, memberKey);
            if (model == null) {
                //??????Exceptionthrow
                throw new ConcurrentModificationException();
            }
        } else {
            //????
            model = new TodoModel();
            model.setCreateMemberKey(memberKey);
        }
        //??????
        setModel(model, form);
        todoDao.put(model);

        //??TODO
        putTodoTag(form.tag, mail);
    }

    /**
     * TODO.
     * ???TODO???
     * @param tag (?)
     * @param mail 
     */
    private void putTodoTag(String tag, String mail) {

        if (StringUtils.isEmpty(tag)) {
            return;
        }

        String[] tags = tag.split(",");
        Set<String> tagSet = new LinkedHashSet<String>();
        for (String target : tags) {
            String tagName = StringUtils.trimToEmpty(target);
            if (StringUtils.isEmpty(tagName)) {
                continue;
            }
            tagSet.add(tagName);
        }

        if (tagSet.size() == 0) {
            return;
        }
        todoTagService.put(tagSet.toArray(new String[0]), mail);
    }

    /* (non-Javadoc)
     * @see jp.co.nemuzuka.service.TodoService#delete(jp.co.nemuzuka.form.TodoForm, java.lang.String)
     */
    @Override
    public void delete(TodoForm form, String mail) {
        Key key = Datastore.stringToKey(form.keyToString);
        Key memberKey = memberService.getKey(mail);
        Long version = ConvertUtils.toLong(form.versionNo);
        //key????Key??
        TodoModel model = todoDao.get(key, version, memberKey);
        if (model == null) {
            //??????Exceptionthrow
            throw new ConcurrentModificationException();
        }
        todoDao.delete(key);
    }

    /* (non-Javadoc)
     * @see jp.co.nemuzuka.service.TodoService#putComment(jp.co.nemuzuka.form.TodoCommentForm, java.lang.String)
     */
    @Override
    public void putComment(TodoCommentForm form, String email) {
        //?????????
        Key todoModelKey = Datastore.stringToKey(form.keyToString);
        Key memberKey = memberService.getKey(email);

        //Key??Key??
        TodoModel model = todoDao.getWithMemberKey(todoModelKey, memberKey);
        if (model == null) {
            //??????Exceptionthrow
            throw new ConcurrentModificationException();
        }

        //???????
        TodoStatus status = TodoStatus.fromCode(form.status);
        if (status == null) {
            status = TodoStatus.nothing;
        }
        if (model.getStatus().equals(status) == false) {
            Long versonNo = ConvertUtils.toLong(form.versionNo);
            //??????
            if (model.getVersion().equals(versonNo) == false) {
                //???????????Exceptionthrow
                throw new ConcurrentModificationException();
            }
            model.setStatus(status);
            todoDao.put(model);
        }

        //
        commentService.put(todoModelKey, form.comment, email);
    }

    /* (non-Javadoc)
     * @see jp.co.nemuzuka.service.TodoService#deleteComment(java.lang.String, java.lang.String, java.lang.Long, java.lang.String)
     */
    @Override
    public void deleteComment(String keyString, String commentKeyString, Long commentVersionNo, String email) {

        //TODO??
        Key todoModelKey = Datastore.stringToKey(keyString);
        Key memberKey = memberService.getKey(email);

        //Key??Key??
        TodoModel model = todoDao.getWithMemberKey(todoModelKey, memberKey);
        if (model == null) {
            //??????Exceptionthrow
            throw new ConcurrentModificationException();
        }

        //
        commentService.delete(todoModelKey, commentKeyString, commentVersionNo);
    }

    /**
     * ?.
     * @param today 
     * @param targetDate ?
     * @param todoStatus TODO
     * @return ?
     */
    PeriodStatus createPeriodStatus(Date today, Date targetDate, TodoStatus todoStatus) {
        long toDayTime = today.getTime();
        long periodTime = targetDate.getTime();

        //???
        PeriodStatus periodStatus = null;
        switch (todoStatus) {
        case doing:
        case nothing:
            if (toDayTime == periodTime) {
                periodStatus = PeriodStatus.today;
            } else if (toDayTime > periodTime) {
                periodStatus = PeriodStatus.periodDate;
            }
            break;
        }
        return periodStatus;
    }

    /**
     * Form.
     * @param form Form
     * @param model Model
     */
    private void setForm(TodoForm form, TodoModel model) {
        if (model == null) {
            return;
        }
        SimpleDateFormat sdf = DateTimeUtils.createSdf("yyyyMMdd");
        form.keyToString = model.getKeyToString();
        form.todoStatus = model.getStatus().getCode();
        form.title = model.getTitle();
        form.tag = StringUtils.defaultString(model.getTag());
        form.content = model.getContent().getValue();
        form.period = ConvertUtils.toString(model.getPeriod(), sdf);
        form.versionNo = ConvertUtils.toString(model.getVersion());
    }

    /**
     * Model.
     * @param model Model
     * @param form Form
     */
    private void setModel(TodoModel model, TodoForm form) {
        SimpleDateFormat sdf = DateTimeUtils.createSdf("yyyyMMdd");

        TodoStatus status = TodoStatus.fromCode(form.todoStatus);
        if (status == null) {
            status = TodoStatus.nothing;
        }
        model.setStatus(status);
        model.setTitle(form.title);
        model.setTag(reCreateTag(form.tag));
        model.setContent(new Text(StringUtils.defaultString(form.content)));
        model.setPeriod(ConvertUtils.toDate(form.period, sdf));
        model.setVersion(ConvertUtils.toLong(form.versionNo));
    }

    /**
     * Tag?.
     * ?Tag????
     * @param tag Tag
     * @return ?Tag
     */
    private String reCreateTag(String tag) {

        if (StringUtils.isEmpty(tag)) {
            return "";
        }

        String[] array = ConvertUtils.toStringArray(tag, ",");
        StringBuilder sb = new StringBuilder();
        for (String target : array) {
            if (sb.length() != 0) {
                sb.append(",");
            }
            sb.append(target);
        }
        return sb.toString();
    }
}