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

Java tutorial

Introduction

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

import jp.co.nemuzuka.common.Authority;
import jp.co.nemuzuka.common.ProjectAuthority;
import jp.co.nemuzuka.core.entity.LabelValueBean;
import jp.co.nemuzuka.core.entity.UserInfo;
import jp.co.nemuzuka.dao.ProjectDao;
import jp.co.nemuzuka.dao.ProjectMemberDao;
import jp.co.nemuzuka.entity.ProjectModelEx;
import jp.co.nemuzuka.form.ProjectForm;
import jp.co.nemuzuka.model.MemberModel;
import jp.co.nemuzuka.model.ProjectMemberModel;
import jp.co.nemuzuka.model.ProjectModel;
import jp.co.nemuzuka.service.MemberService;
import jp.co.nemuzuka.service.ProjectService;
import jp.co.nemuzuka.utils.ConvertUtils;
import jp.co.nemuzuka.utils.HtmlStringUtils;

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;

public class ProjectServiceImpl implements ProjectService {

    ProjectDao projectDao = ProjectDao.getInstance();
    ProjectMemberDao projectMemberDao = ProjectMemberDao.getInstance();
    MemberService memberService = MemberServiceImpl.getInstance();

    private static ProjectServiceImpl impl = new ProjectServiceImpl();

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

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

    /* (? Javadoc)
     * @see jp.co.nemuzuka.service.ProjectService#get(java.lang.String)
     */
    @Override
    public ProjectForm get(String keyString) {

        ProjectForm form = new ProjectForm();

        if (StringUtils.isNotEmpty(keyString)) {
            ProjectModel model = projectDao.get(keyString);
            setForm(form, model);
        }
        //Form?????
        setFormCommon(form);
        return form;
    }

    /* (? Javadoc)
     * @see jp.co.nemuzuka.service.ProjectService#put(jp.co.nemuzuka.form.ProjectForm)
     */
    @Override
    public void put(ProjectForm form) {
        ProjectModel model = null;
        boolean isAdd = false;
        if (StringUtils.isNotEmpty(form.keyToString)) {
            //??
            Key key = Datastore.stringToKey(form.keyToString);
            Long version = ConvertUtils.toLong(form.versionNo);
            //version?Key??
            model = projectDao.get(key, version);
            if (model == null) {
                //??????Exceptionthrow
                throw new ConcurrentModificationException();
            }
        } else {
            //???
            model = new ProjectModel();
            isAdd = true;
        }
        setModel(model, form);
        projectDao.put(model);

        //??????????
        if (isAdd) {
            addProjectMember4Admin(model.getKey(), form.adminMemberId);
        }
    }

    /* (? Javadoc)
     * @see jp.co.nemuzuka.service.ProjectService#delete(jp.co.nemuzuka.form.ProjectForm)
     */
    @Override
    public void delete(ProjectForm form) {
        //version?Key??
        Key key = Datastore.stringToKey(form.keyToString);
        Long version = ConvertUtils.toLong(form.versionNo);
        ProjectModel model = projectDao.get(key, version);
        if (model == null) {
            //??????Exceptionthrow
            throw new ConcurrentModificationException();
        }
        //
        projectDao.delete(model.getKey());

        //?
        List<ProjectMemberModel> list = projectMemberDao.getList(form.keyToString, null);
        List<Key> keyList = new ArrayList<Key>();
        for (ProjectMemberModel target : list) {
            keyList.add(target.getKey());
        }
        projectMemberDao.delete(keyList.toArray(new Key[0]));
    }

    /* (? Javadoc)
     * @see jp.co.nemuzuka.service.ProjectService#getList(java.lang.String)
     */
    @Override
    public List<ProjectModelEx> getList(String projectName) {

        List<ProjectModel> list = projectDao.getList(projectName);
        List<ProjectModelEx> retList = new ArrayList<ProjectModelEx>();
        for (ProjectModel target : list) {
            retList.add(createProjectModelEx(target));
        }
        return retList;
    }

    /* (? Javadoc)
     * @see jp.co.nemuzuka.service.ProjectService#getUserProjectList(java.lang.String, boolean)
     */
    @Override
    public TargetProjectResult getUserProjectList(String email, boolean gaeAdmin) {

        TargetProjectResult result = new TargetProjectResult();

        MemberModel model = memberService.getModel(email);
        if (model == null) {
            return result;
        }

        if (gaeAdmin || model.getAuthority() == Authority.admin) {
            result.admin = true;

            //????????
            List<ProjectModel> list = projectDao.getAllList();
            result.projectList = createUserProjectList(list);
        } else {
            //?????????
            List<ProjectMemberModel> list = projectMemberDao.getList(null, model.getKeyToString());

            Set<Key> projectKeySet = new LinkedHashSet<Key>();
            for (ProjectMemberModel target : list) {
                projectKeySet.add(target.getProjectKey());
            }
            result.projectList = createUserProjectList(projectKeySet);
        }
        return result;
    }

    /* (non-Javadoc)
     * @see jp.co.nemuzuka.service.ProjectService#getUserProjectList(java.lang.String)
     */
    @Override
    public List<ProjectModelEx> getUserProjectList(String email) {

        List<ProjectModelEx> retList = new ArrayList<ProjectModelEx>();
        MemberModel model = memberService.getModel(email);
        if (model == null) {
            return retList;
        }

        //??????
        List<ProjectMemberModel> list = projectMemberDao.getList(null, model.getKeyToString());
        Set<Key> projectKeySet = new LinkedHashSet<Key>();
        for (ProjectMemberModel target : list) {
            projectKeySet.add(target.getProjectKey());
        }
        if (projectKeySet.size() == 0) {
            return retList;
        }

        //List??
        List<ProjectModel> projectList = projectDao.getList(projectKeySet.toArray(new Key[0]));
        for (ProjectModel target : projectList) {
            retList.add(createProjectModelEx(target));
        }
        return retList;
    }

    /* (? Javadoc)
     * @see jp.co.nemuzuka.service.ProjectService#getAllList()
     */
    @Override
    public List<ProjectModel> getAllList() {
        return projectDao.getAllList();
    }

    /* (? Javadoc)
     * @see jp.co.nemuzuka.service.ProjectService#setUserInfo(java.lang.String, java.lang.String, jp.co.nemuzuka.core.entity.UserInfo)
     */
    @Override
    public void setUserInfo(String projectKeyString, String mail, UserInfo userInfo) {
        //?
        userInfo.initProjectInfo();
        userInfo.setSelectedProject(projectKeyString);

        if (StringUtils.isEmpty(projectKeyString)) {
            //???????????
            return;
        }

        MemberModel memberModel = memberService.getModel(mail);
        if (memberModel == null) {
            return;
        }
        //??????????
        if (userInfo.isSystemManager()) {
            userInfo.setProjectManager(true);
        }

        Key projectKey = Datastore.stringToKey(projectKeyString);
        //???????
        ProjectMemberModel projectMemberModel = projectMemberDao.get(projectKey, memberModel.getKey());
        if (projectMemberModel == null) {
            return;
        }

        //????????????
        userInfo.setProjectMember(true);

        //GAE???????????????
        //????
        if (userInfo.isProjectManager() == false) {
            if (projectMemberModel.getProjectAuthority() == ProjectAuthority.type1) {
                userInfo.setProjectManager(true);
            }
        }
        userInfo.setProjectAuthority(projectMemberModel.getProjectAuthority());
    }

    /**
     * LabelValueBean?.
     * @param projectKeySet KeySet
     * @return LabelValueBeanList(???)
     */
    protected List<LabelValueBean> createUserProjectList(Set<Key> projectKeySet) {

        List<ProjectModel> projectList = projectDao.getList(projectKeySet.toArray(new Key[0]));
        return createUserProjectList(projectList);
    }

    /**
     * LabelValueBeanList?.
     * @param projectList List
     * @return ?List
     */
    private List<LabelValueBean> createUserProjectList(List<ProjectModel> projectList) {
        List<LabelValueBean> list = new ArrayList<LabelValueBean>();
        list.add(new LabelValueBean("--?--", ""));
        for (ProjectModel target : projectList) {
            list.add(new LabelValueBean(target.getProjectName(), target.getKeyToString()));
        }
        return list;
    }

    /**
     * Form.
     * @param form Form
     * @param model Model
     */
    private void setForm(ProjectForm form, ProjectModel model) {
        if (model == null) {
            return;
        }

        form.keyToString = model.getKeyToString();
        form.projectName = model.getProjectName();
        form.projectId = model.getProjectId();
        form.projectSummary = model.getProjectSummary().getValue();
        form.versionNo = ConvertUtils.toString(model.getVersion());
    }

    /**
     * Form.
     * ????
     * @param form Form
     */
    private void setFormCommon(ProjectForm form) {
        List<LabelValueBean> targetList = new ArrayList<LabelValueBean>();

        //?????
        List<MemberModel> list = memberService.getAllList();
        for (MemberModel target : list) {
            targetList.add(new LabelValueBean(target.getName(), target.getKeyToString()));
        }
        form.setMemberList(targetList);
    }

    /**
     * Model.
     * @param model Model
     * @param form Form
     */
    private void setModel(ProjectModel model, ProjectForm form) {
        model.setProjectName(form.projectName);
        model.setProjectId(form.projectId);
        model.setProjectSummary(new Text(form.projectSummary));
    }

    /**
     * ??.
     * ?????????
     * @param projectKey Key
     * @param adminMemberId ?Key
     */
    private void addProjectMember4Admin(Key projectKey, String adminMemberId) {

        Key memberKey = Datastore.stringToKey(adminMemberId);
        ProjectMemberModel model = new ProjectMemberModel();
        model.createKey(projectKey, memberKey);
        model.setProjectAuthority(ProjectAuthority.type1);
        projectMemberDao.put(model);
    }

    /**
     * ProjectModelEx?.
     * ????Model?????
     * @param target ?Model
     * @return ?Model
     */
    private ProjectModelEx createProjectModelEx(ProjectModel target) {
        ProjectModelEx model = new ProjectModelEx();
        model.setModel(target);
        String summaryString = "";
        Text summary = target.getProjectSummary();
        if (summary != null) {
            summaryString = summary.getValue();
        }
        model.setProjectSummaryView(HtmlStringUtils.escapeTextAreaString(summaryString));
        target.setProjectSummary(null);
        return model;
    }
}