TaskListService.java 10.7 KB
Newer Older
R
roo00 已提交
1
package com.x.teamwork.assemble.control.service;
R
fix  
roo00 已提交
2 3 4 5 6 7 8 9 10 11 12

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.entity.annotation.CheckPersistType;
import com.x.base.core.entity.annotation.CheckRemoveType;
import com.x.base.core.project.tools.ListTools;
import com.x.teamwork.assemble.control.Business;
13 14 15
import com.x.teamwork.assemble.control.jaxrs.list.ActionListWithTaskGroup.Wo;
import com.x.teamwork.core.entity.Project;
import com.x.teamwork.core.entity.ProjectTemplate;
R
roo00 已提交
16
import com.x.teamwork.core.entity.TaskGroup;
R
fix  
roo00 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
import com.x.teamwork.core.entity.TaskList;
import com.x.teamwork.core.entity.TaskListRele;

class TaskListService {

	/**
	 * 根据工作任务列表的标识查询工作任务列表的信息
	 * @param emc
	 * @param flag  主要是ID
	 * @return
	 * @throws Exception 
	 */
	protected TaskList get(EntityManagerContainer emc, String flag) throws Exception {
		Business business = new Business( emc );
		return business.taskListFactory().get( flag );
	}
	
	/**
	 * 根据用户和项目ID查询工作任务列表
	 * @param emc
	 * @param person
	 * @return
	 * @throws Exception
	 */
	protected List<TaskList> listWithProject( EntityManagerContainer emc,  String person, String projectId ) throws Exception {
		Business business = new Business( emc );	
		return business.taskListFactory().listWithPersonAndProject( person, projectId );
	}
	
	/**
	 * 根据工作任务组ID查询工作任务列表
	 * @param emc
	 * @param taskGroupId
	 * @return
	 * @throws Exception
	 */
	protected List<TaskList> listWithTaskGroup( EntityManagerContainer emc, String taskGroupId ) throws Exception {
		Business business = new Business( emc );	
		return business.taskListFactory().listWithTaskGroup( taskGroupId );
	}

R
roo00 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71
	public List<String> listTaskIdsWithTaskListId( EntityManagerContainer emc, String taskListId) throws Exception {
		Business business = new Business( emc );	
		return business.taskListFactory().listTaskIdWithTaskListId(taskListId);
	}
	
	public List<String> listTaskIdsWithTaskListIds( EntityManagerContainer emc, List<String> taskListIds) throws Exception {
		Business business = new Business( emc );	
		return business.taskListFactory().listTaskIdWithTaskListIds(taskListIds);
	}
	
	public List<TaskListRele> listTaskListWithTask(EntityManagerContainer emc, String taskId, String taskGroupId ) throws Exception {
		Business business = new Business( emc );	
		return business.taskListFactory().listTaskListWithTask( taskId, taskGroupId );
	}
liyi_hz2008's avatar
liyi_hz2008 已提交
72 73 74 75 76

	public List<String> listTaskListIdWithTask(EntityManagerContainer emc, String taskId, String taskGroupId ) throws Exception {
		Business business = new Business( emc );
		return business.taskListFactory().listTaskListIdWithTask( taskId, taskGroupId );
	}
R
roo00 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	
	public List<String> listTaskListIdsWithGroup(EntityManagerContainer emc, String taskGroupId, String person) throws Exception {
		Business business = new Business( emc );	
		return business.taskListFactory().listTaskListIdsWithGroup( taskGroupId, person );
	}	
	
	public List<TaskListRele> listReleWithTaskAndListId(EntityManagerContainer emc, List<String> taskIds, String taskListId) throws Exception {
		Business business = new Business( emc );	
		return business.taskListFactory().listReleWithTaskAndListId( taskIds, taskListId );
	}
	
	public Long countTaskWithTaskListId(EntityManagerContainer emc, String taskListId) throws Exception {
		Business business = new Business( emc );	
		return business.taskListFactory().countTaskWithTaskListId( taskListId );
	}
	
	public Long countTaskWithOutoTaskListInGroup(EntityManagerContainer emc, String personName, String taskGroupId) throws Exception {
		Business business = new Business(emc);
		TaskGroup taskGroup = emc.find( taskGroupId, TaskGroup.class );
		//查询所有未归类的任务列表
		List<String> taskIds_all = new ArrayList<>();
		List<String> taskIds_allViewable = null;
		List<String> taskIds_forGroup = null;
		List<String> taskListIds_forGroup = null;
		
		if( taskGroup == null ) {
			return 0L;
		}
		
		//查询在指定项目里所有可见的工作任务列表
		taskIds_allViewable = business.reviewFactory().listTaskIdsWithPersonAndProject( personName,  taskGroup.getProject() );
		if( ListTools.isNotEmpty( taskIds_allViewable ) ) {
			for( String str : taskIds_allViewable ) {
				//先全部添加到输出的工作任务ID里,然后将所有已经归过类的任务排除
				taskIds_all.add( str );
			}
		}
		
		//查询用户在该taskGroup里所有已经有关联的任务ID列表
		taskListIds_forGroup = business.taskListFactory().listTaskListIdsWithGroup( taskGroupId, personName );
		taskIds_forGroup = business.taskListFactory().listTaskIdsWithTaskGroupId( taskListIds_forGroup );
		if( ListTools.isNotEmpty( taskIds_forGroup )) {
			taskIds_all.removeAll( taskIds_forGroup );
		}
		
		return Long.parseLong( taskIds_all.size()+"" );
	}
	
R
fix  
roo00 已提交
125 126 127
	/**
	 * 向数据库持久化工作任务列表列表信息
	 * @param emc
liyi_hz2008's avatar
liyi_hz2008 已提交
128
	 * @param object
R
fix  
roo00 已提交
129 130 131 132 133 134 135 136 137 138
	 * @return
	 * @throws Exception 
	 */
	protected TaskList save( EntityManagerContainer emc, TaskList object ) throws Exception {
		TaskList taskList = null;
		if( StringUtils.isEmpty( object.getId() )  ){
			object.setId( TaskList.createId() );
		}
		
		taskList = emc.find( object.getId(), TaskList.class );
139
		Business business = new Business(emc);
R
fix  
roo00 已提交
140 141
		emc.beginTransaction( TaskList.class );		
		if( taskList == null ){ // 保存一个新的对象
R
roo00 已提交
142 143 144 145 146 147
			TaskGroup taskGroup = emc.find( object.getTaskGroup(), TaskGroup.class );
			if( taskGroup == null ) {
				throw new Exception("TaskGroup not exists!ID:" + object.getTaskGroup() );
			}
			object.setProject( taskGroup.getProject() );
			
R
fix  
roo00 已提交
148 149 150 151
			taskList = new TaskList();
			object.copyTo( taskList );
			if( StringUtils.isNotEmpty( object.getId() ) ){
				taskList.setId( object.getId() );
152 153 154 155 156
			}

			//查询最大的排序号
			Integer maxListOrder = business.taskListFactory().maxListOrder( taskGroup.getId() ) + 1;
			taskList.setOrder( maxListOrder );
R
fix  
roo00 已提交
157 158
			emc.persist( taskList, CheckPersistType.all);
		}else{ //对象已经存在,更新对象信息
R
roo00 已提交
159 160 161 162 163 164 165
			TaskGroup taskGroup = emc.find( taskList.getTaskGroup(), TaskGroup.class );
			if( taskGroup == null ) {
				throw new Exception("TaskGroup not exists!ID:" + object.getTaskGroup() );
			}
			taskList.setMemo( object.getMemo() );
			if( StringUtils.isNotEmpty( object.getName() )) {
				taskList.setName( object.getName() );
R
fix  
roo00 已提交
166
			}
R
roo00 已提交
167 168 169
			if( object.getOrder() != null ) {
				taskList.setOrder( object.getOrder() );
			}			
R
fix  
roo00 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
			emc.check( taskList, CheckPersistType.all );	
		}
		emc.commit();
		
		return taskList;
	}
	
	/**
	 * 根据工作任务列表标识删除工作任务列表信息(逻辑删除,加上delete标识)
	 * @param emc
	 * @param id
	 * @throws Exception 
	 */
	protected void delete( EntityManagerContainer emc, String id ) throws Exception {
		emc.beginTransaction( TaskList.class );
		TaskList taskList = emc.find( id, TaskList.class );
		if( taskList != null ) {
			taskList.setDeleted( true );
			emc.check( taskList , CheckPersistType.all );
		}
		emc.commit();
	}
	
	/**
	 * 根据工作任务列表标识删除工作任务列表信息(物理删除)
	 * @param emc
	 * @param id
	 * @throws Exception 
	 */
	protected void remove( EntityManagerContainer emc, String id ) throws Exception {
		Business business = new Business( emc );
		emc.beginTransaction( TaskList.class );
		emc.beginTransaction( TaskListRele.class );
		TaskList taskList = emc.find( id, TaskList.class );
		//还需删除TaskList所有的关联信息
R
roo00 已提交
205
		List<TaskListRele> lists = business.taskListFactory().listReleWithListId( id );
R
fix  
roo00 已提交
206 207 208 209 210 211 212 213 214 215
		if( ListTools.isNotEmpty( lists )) {
			for( TaskListRele listRele: lists ) {
				emc.remove( listRele , CheckRemoveType.all );
			}
		}
		if( taskList != null ) {
			emc.remove( taskList , CheckRemoveType.all );
		}
		emc.commit();
	}
R
roo00 已提交
216
	
R
fix  
roo00 已提交
217
	/**
NoSubject's avatar
NoSubject 已提交
218
	 * 创建默认的工作任务列表(四个)
R
fix  
roo00 已提交
219 220 221 222 223 224 225 226 227 228 229
	 * @param emc
	 * @param person
	 * @param taskGroupId
	 * @return
	 * @throws Exception
	 */
	public List<TaskList> createDefaultTaskListForTaskGroup( EntityManagerContainer emc, String person, String taskGroupId) throws Exception {
		List<TaskList> taskLists = new ArrayList<>();		
		TaskGroup taskGroup = emc.find( taskGroupId, TaskGroup.class );
		
		TaskList taskList = null;
NoSubject's avatar
NoSubject 已提交
230
		
R
fix  
roo00 已提交
231
		if( taskGroup != null ) {
232
			Project project = emc.find( taskGroup.getProject(), Project.class );
R
fix  
roo00 已提交
233
			
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
			emc.beginTransaction( TaskList.class );
			if(project != null && StringUtils.isNotEmpty( project.getTemplateId() )){
				//如果有模板id,则按模板创建泳道,否则创建默认的四个泳道
				ProjectTemplate projectTemplate = emc.find( project.getTemplateId(), ProjectTemplate.class );
				if(projectTemplate != null){
					List<String> taskListNames = null;
					taskListNames = projectTemplate.getTaskList();
					if(ListTools.isNotEmpty( taskListNames )){
						int nameCount = 0;
						for( String taskListName : taskListNames ) {
							nameCount++;
							taskList = composeTaskListObject( taskGroup.getProject(), taskGroupId, taskListName, nameCount, "SYSTEM", person, "" );
							emc.persist( taskList, CheckPersistType.all );
							taskLists.add( taskList );
						}
					}else{
						throw new Exception("TaskListTemplate not exists!ProjectTemplateID:" + project.getTemplateId() );
					}
				}else{
					throw new Exception("ProjectTemplate not exists!ID:" + project.getTemplateId() );
				}
				
			}else{
				taskList = composeTaskListObject( taskGroup.getProject(), taskGroupId, "已规划的工作", 1, "SYSTEM", person, "" );
				emc.persist( taskList, CheckPersistType.all );
				taskLists.add( taskList );
				
				taskList = composeTaskListObject( taskGroup.getProject(), taskGroupId, "已分解的任务", 2, "SYSTEM", person, "" );
				emc.persist( taskList, CheckPersistType.all );
				taskLists.add( taskList );
				
				taskList = composeTaskListObject( taskGroup.getProject(), taskGroupId, "进行中的任务", 3, "SYSTEM", person, "" );
				emc.persist( taskList, CheckPersistType.all );
				taskLists.add( taskList );
				
				taskList = composeTaskListObject( taskGroup.getProject(), taskGroupId, "已完成的任务", 4, "SYSTEM", person, "" );
				emc.persist( taskList, CheckPersistType.all );
				taskLists.add( taskList );
			}
NoSubject's avatar
NoSubject 已提交
273
			
R
fix  
roo00 已提交
274 275 276
			
			emc.commit();
		}
NoSubject's avatar
NoSubject 已提交
277
		
R
fix  
roo00 已提交
278 279
		return taskLists;
	}
NoSubject's avatar
NoSubject 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292
	
	private TaskList composeTaskListObject( String projectId, String taskGroupId, String listName, int orderNum, String creatorName, String owner, String memo ) {
		TaskList taskList = new TaskList();		
		taskList.setId( TaskList.createId() );
		taskList.setProject( projectId );
		taskList.setTaskGroup( taskGroupId );
		taskList.setMemo( memo );
		taskList.setName( listName );
		taskList.setOrder( orderNum );
		taskList.setCreatorPerson( creatorName );
		taskList.setOwner( owner );
		return taskList;
	}
R
fix  
roo00 已提交
293
}