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

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.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.base.core.project.organization.Person;
import com.x.base.core.project.tools.ListTools;
import com.x.teamwork.assemble.control.Business;
import com.x.teamwork.core.entity.Project;
import com.x.teamwork.core.entity.Review;
import com.x.teamwork.core.entity.Task;
R
update  
roo00 已提交
14 15 16 17
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
R
roo00 已提交
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 58 59 60 61 62 63 64

public class ReviewService {

	private UserManagerService userManagerService = new UserManagerService();
	private static  Logger logger = LoggerFactory.getLogger( ReviewService.class );
		
	/**
	 * 根据指定文档的权限信息重置或者刷新所有的Review信息
	 * @param emc
	 * @param docId
	 * @throws Exception
	 */
	public void refreshTaskReview( EntityManagerContainer emc, String taskId ) throws Exception {
		Task task = emc.find( taskId, Task.class );
		Project project = null;
		if( task != null ) {
			project = emc.find( task.getProject(), Project.class );
		}
		 if( task != null ) {//正常发布后的工作需要计算工作的可见范围
				logger.info( "refreshTaskReview -> refresh review for [published] task: " + task.getName() );
				List<String> persons = addTaskAllPermission( emc, new ArrayList<>(), taskId );
				logger.info( "refreshTaskReview -> there are "+ persons.size() +" permission in this task: " + task.getName() );
				refreshTaskReview( emc, project, task, persons );
		}
	}
	
	/**
	 * 根据指定文档删除所有的Review信息
	 * @param emc
	 * @param docId
	 * @throws Exception
	 */
	public void deleteTaskReview( EntityManagerContainer emc, String taskId ) throws Exception {
		Business business = new Business(emc);
		Integer maxQueryCount = 1000;
		Long count = business.reviewFactory().countByTask(taskId);
		Long maxTimes = count/maxQueryCount + 1 + 1; //多补偿一次		
		List<Review> reviewList = null;
		List<String> ids = null;
		for( int i = 0 ; i <= maxTimes; i++ ) {
			ids = business.reviewFactory().listReviewByTask(taskId, maxQueryCount );
			if( ListTools.isNotEmpty( ids )) {
				reviewList = emc.list( Review.class, ids);
			}
			if( ListTools.isNotEmpty( reviewList )) {
				emc.beginTransaction( Review.class );
				for( Review review : reviewList ) {
L
luojing 已提交
65 66 67 68
					//emc.remove( review );
					//改为软删除-lj
					review.setDeleted(true);
					emc.check( review , CheckPersistType.all );
R
roo00 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
				}				
				emc.commit();
			}
		}
	}

	/**
	 * 将指定工作任务的Review刷新为指定的人员可见
	 * @param emc
	 * @param project
	 * @param task
	 * @param permissionPersons
	 * @throws Exception
	 */
	private void refreshTaskReview( EntityManagerContainer emc, Project project, Task task, List<String> permissionPersons) throws Exception {
		Business business = new Business(emc);
		Review review = null;
R
roo00 已提交
86
		Project projectEntity = null;
R
roo00 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
		List<Review> reviews = null;
		List<Review> reviews_tmp = null;
		//先检查该文档是否存在Review信息
		List<String> oldReviewIds = business.reviewFactory().listReviewByTask( task.getId(), 10000 );
		
		List<String> oldPermissionPersons = new ArrayList<>();
		if( permissionPersons == null ) {
			permissionPersons = new ArrayList<>();
		}
		
		emc.beginTransaction( Review.class );
		if( ListTools.isNotEmpty( oldReviewIds )) {
			reviews = emc.list( Review.class, oldReviewIds ); //查询该文档所有的Review列表,收集原来的Review人员名称oldPermissionPersons
			if( ListTools.isNotEmpty( reviews )) {
				emc.beginTransaction( Review.class );
				for( Review review_tmp : reviews ) {
					oldPermissionPersons.add( review_tmp.getPermissionObj() );
					//对比是否有需要删除的Review数据
					if( ListTools.isNotEmpty( permissionPersons) && permissionPersons.contains( review_tmp.getPermissionObj() ) ) {
						//说明存在的,保留,不需要处理,检查一下,是否有重复的需要删除
						reviews_tmp = business.reviewFactory().listByTaskAndPerson( task.getId(), review_tmp.getPermissionObj() );
R
roo00 已提交
108 109
						if( ListTools.isNotEmpty( reviews_tmp) ) {
							//如果有数据不一致,就更新,把重复的数据删除掉
R
roo00 已提交
110 111 112 113
							for( int i=0; i<reviews_tmp.size(); i++  ) {
								if( i > 0 ) {
									//删除多余的Review
									emc.remove( reviews_tmp.get(i), CheckRemoveType.all );
R
roo00 已提交
114 115 116
								}else {
									//只取一个,对比一下,把数据更新一下
									if( taskInfoChanged( task, reviews_tmp.get(0))) {
R
update  
roo00 已提交
117
//										System.out.println("任务内容变更过了,需要更新所有的Review");
R
roo00 已提交
118 119 120
										 updateWithTask( reviews_tmp.get(0), task );
										 emc.check( reviews_tmp.get(0), CheckPersistType.all );
									}else {
R
update  
roo00 已提交
121
//										System.out.println("任务内容没有变更,不需要更新Review");
R
roo00 已提交
122
									}
R
roo00 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
								}
							}
						}
					}else {
						//不存在的Review需要删除
						emc.remove( review_tmp, CheckRemoveType.all );
					}
				}
				emc.commit();
			}
		}		
		
		//再判断是否需要添加新的Review信息
		Person personObj = null;
		String personName = null;
R
roo00 已提交
138 139 140 141
		projectEntity = emc.find( project.getId(), Project.class );
		if( projectEntity == null ) {
			throw new Exception("project not exist! id:" + project.getId());
		}
R
roo00 已提交
142 143 144 145 146 147 148
		permissionPersons = ListTools.trim( permissionPersons, true, true, new String[0] ); //去重复
		for( String person : permissionPersons ) {
			if( !person.equalsIgnoreCase( "*" )) {
				//检查一下个人是否存在,防止姓名或者唯一标识变更过了导致权限不正确
				personObj = userManagerService.getPerson( person );
				if( personObj != null ) {
					personName = personObj.getDistinguishedName();
149 150
				}else{
					personName = userManagerService.getPersonNameWithIdentity(person);
R
roo00 已提交
151 152 153 154 155 156 157 158 159 160 161 162
				}
			}
			if( StringUtils.isNotEmpty( personName )) {
				//查询一下,数据库里, 是否有相同的数据,如果有,就不再添加了
				 oldReviewIds = business.reviewFactory().listIdsByTaskAndPerson( task.getId(), personName );
				 if( ListTools.isEmpty( oldReviewIds )) {
					 review = createReviewWithTask( project, task, personName );
					 emc.beginTransaction( Review.class );
					 emc.persist( review, CheckPersistType.all );
					 emc.commit();
				 }
			}
R
roo00 已提交
163 164
			
			//判断所有的人员是否全部加入到Project的参与者里
165 166
			//projectEntity.addParticipantPerson( person );		
			projectEntity.addParticipantPerson( personName );	
R
roo00 已提交
167
		}
R
roo00 已提交
168 169 170
		emc.beginTransaction( Project.class );
		emc.check( projectEntity, CheckPersistType.all);
		emc.commit();
R
update  
roo00 已提交
171
//		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>refreshTaskReview over!");
R
roo00 已提交
172 173
	}
	
R
roo00 已提交
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
	/**
	 * 使用Task信息更新已存在的Review信息
	 * @param review
	 * @param task
	 */
	private void updateWithTask( Review review, Task task) {
		review.setTaskId( task.getId() );
		review.setParent( task.getParent() );
		review.setName( task.getName() );
		review.setProject( task.getProject() );
		review.setProjectName( task.getProjectName() );		
		review.setCreatorPerson( task.getCreatorPerson() );
		review.setExecutor( task.getExecutor() );
		review.setExecutorIdentity( task.getExecutorIdentity() );
		review.setExecutorUnit( task.getExecutorUnit() );
		review.setStartTime( task.getStartTime() );
		review.setEndTime( task.getEndTime() );		
		review.setArchive( task.getArchive() );
		review.setCompleted( task.getCompleted() );
		review.setOvertime( task.getOvertime() );
		review.setDeleted( task.getDeleted() );
		review.setWorkStatus( task.getWorkStatus() );		
		review.setPriority( task.getPriority() );
		review.setProgress( task.getProgress() );
		review.setOrder( task.getOrder() );		
		review.setTaskSequence( task.getSequence() );		
		review.setClaimed( task.getClaimed() );
		review.setRemindRelevance( task.getRemindRelevance());
	}

	private boolean taskInfoChanged(Task task, Review review) {
R
roo00 已提交
205 206 207 208 209 210
		if( review.getExecutor() == null ) {
			review.setExecutor( "" );
		}
		if( task.getExecutor() == null ) {
			task.setExecutor( "" );
		}
R
roo00 已提交
211 212 213 214 215 216 217
		if( !review.getTaskId().equalsIgnoreCase( task.getId() )) { return true; }
		if( !review.getParent().equalsIgnoreCase( task.getParent() )) { return true; }
		if( !review.getProject().equalsIgnoreCase( task.getProject() )) { return true; }
		if( !review.getName().equalsIgnoreCase( task.getName() )) { return true; }
		if( !review.getProjectName().equalsIgnoreCase( task.getProjectName() )) { return true; }
		if( !review.getCreatorPerson().equalsIgnoreCase( task.getCreatorPerson() )) { return true; }
		if( !review.getExecutor().equalsIgnoreCase( task.getExecutor() )) { return true; }
R
roo00 已提交
218 219
//		if( !review.getExecutorIdentity().equalsIgnoreCase( task.getExecutorIdentity() )) { return true; }
//		if( !review.getExecutorUnit().equalsIgnoreCase( task.getExecutorUnit() )) { return true; }
R
roo00 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
		if( review.getStartTime().getTime() != task.getStartTime().getTime() ) { return true; }
		if( review.getEndTime().getTime() != task.getEndTime().getTime() ) { return true; }
		if( review.getArchive() != task.getArchive() ) { return true; }
		if( review.getCompleted() != task.getCompleted() ) { return true; }
		if( review.getOvertime() != task.getOvertime() ) { return true; }
		if( review.getDeleted() != task.getDeleted() ) { return true; }
		if( review.getArchive() != task.getArchive() ) { return true; }
		if( review.getArchive() != task.getArchive() ) { return true; }
		if( !review.getWorkStatus().equalsIgnoreCase( task.getWorkStatus() )) { return true; }
		if( !review.getPriority().equalsIgnoreCase( task.getPriority() )) { return true; }
		if( review.getProgress() != task.getProgress() ) { return true; }
		if( review.getOrder() == task.getOrder() ) { return true; }
		if( !review.getSequence().equalsIgnoreCase( task.getSequence() )) { return true; }
		if( review.getClaimed() != task.getClaimed() ) { return true; }
		if( review.getRemindRelevance() != task.getRemindRelevance() ) { return true; }
		return false;
	}

R
roo00 已提交
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
	/**
	 * 将一个任务涉及到的所有权限转换为人员
	 * @param emc
	 * @param permissionObjs
	 * @param taskId
	 * @return
	 * @throws Exception 
	 */
	private List<String> addTaskAllPermission( EntityManagerContainer emc, List<String> permissionObjs, String taskId ) throws Exception {
		if( permissionObjs == null ) {
			permissionObjs = new ArrayList<>();
		}
		Task task = emc.find( taskId, Task.class );
		Project project = null;
		if( task != null ) {			
			if( StringUtils.isNotBlank( task.getCreatorPerson() ) && !permissionObjs.contains( task.getCreatorPerson() )) {
				permissionObjs.add( task.getCreatorPerson() );
			}
			if( StringUtils.isNotBlank( task.getExecutor() ) && !permissionObjs.contains( task.getExecutor() )) {
				permissionObjs.add( task.getExecutor() );
			}
			if( ListTools.isNotEmpty( task.getParticipantList() )) {
				permissionObjs = addPermissionObj( permissionObjs, task.getParticipantList() );
			}
			//工作所在项目的创建者和负责人也应该可以看见这个任务
			project = emc.find( task.getProject(), Project.class );
			if( StringUtils.isNotBlank( project.getCreatorPerson() ) && !permissionObjs.contains( project.getCreatorPerson() )) {
				permissionObjs.add( project.getCreatorPerson() );
			}
			if( StringUtils.isNotBlank( project.getExecutor() ) && !permissionObjs.contains( project.getExecutor() )) {
				permissionObjs.add( project.getExecutor() );
			}
			
			//查查该工作是否有上级工作,如果有上级工作,那上级工作的可见人员也应该加入到该工作的可见人员中
liyi_hz2008's avatar
liyi_hz2008 已提交
272
			if( StringUtils.isNotEmpty( task.getParent() ) && !StringUtils.equals( task.getParent(), task.getId() ) ) {
R
roo00 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
				permissionObjs = addTaskAllPermission( emc, permissionObjs, task.getParent() );
			}
		}
		return permissionObjs;
	}

	/**
	 * 将指定的权限名称拆解人员,添加到permissionObjs,并且返回
	 * @param permissionObjs
	 * @param objNames
	 * @return
	 * @throws Exception 
	 */
	private List<String> addPermissionObj(List<String> permissionObjs, List<String> objNames ) throws Exception {
		String result = null;
		List<String> persons  = null;
		if( permissionObjs == null ) {
			permissionObjs = new ArrayList<>();
		}
		for( String objName : objNames ) {
			if( objName.endsWith( "@P" ) ) {
				if( !permissionObjs.contains( objName )) {
					permissionObjs.add( objName );
				}
			}else if( objName.endsWith( "@I" ) ) {//将Identity转换为人员
				result = userManagerService.getPersonNameWithIdentity( objName );
				permissionObjs = addStringToList( permissionObjs, result );
			}else if( objName.endsWith( "@U" ) ) {//将组织拆解为人员
				//判断一下,如果不是顶层组织,就或者顶层组织不唯一,才将组织解析为人员
				if( !userManagerService.isTopUnit( objName ) || userManagerService.countTopUnit() > 1 ) {
					persons  = userManagerService.listPersonWithUnit( objName );
					permissionObjs = addListToList( permissionObjs, persons );
				}
			}else if( objName.endsWith( "@G" ) ) {//将群组拆解为人员
				persons  = userManagerService.listPersonWithGroup( objName );
				permissionObjs = addListToList( permissionObjs, persons );
			}else if( objName.endsWith( "@R" ) ) {
				persons  = userManagerService.listPersonWithRole( objName );
				permissionObjs = addListToList( permissionObjs, persons );
			}else if( "*".equals( objName ) ) {
				permissionObjs = addStringToList( permissionObjs, objName );
			}
		}
		return permissionObjs;
	}

	/**
	 * 根据工作任务信息以及可见权限来组织一个Review对象
	 * @param project
	 * @param task
	 * @param person
	 * @return
	 */
	private Review createReviewWithTask( Project project, Task task, String person ) {
		Review review = new Review();
		
		review.setId( Review.createId() );
		review.setTaskId( task.getId() );
		review.setParent( task.getParent() );
		review.setName( task.getName() );
		review.setProject(project.getId() );
		review.setProjectName( project.getTitle() );
		
		review.setCreatorPerson( task.getCreatorPerson() );
		review.setExecutor( task.getExecutor() );
		review.setExecutorIdentity( task.getExecutorIdentity() );
		review.setExecutorUnit( task.getExecutorUnit() );
		review.setPermissionObj( person );
		review.setPermissionObjType( "PERSON" );
		
		review.setStartTime( task.getStartTime() );
		review.setEndTime( task.getEndTime() );
		review.setCreateTime( task.getCreateTime() );		
		review.setUpdateTime( task.getUpdateTime() );
		
		review.setArchive( task.getArchive() );
		review.setCompleted( task.getCompleted() );
		review.setOvertime( task.getOvertime() );
		review.setDeleted( task.getDeleted() );
		review.setWorkStatus( task.getWorkStatus() );
		
		review.setPriority( task.getPriority() );
		review.setProgress( task.getProgress() );
		review.setOrder( task.getOrder() );
		
		review.setTaskSequence( task.getSequence() );
		
		review.setClaimed( task.getClaimed() );
		review.setRemindRelevance( task.getRemindRelevance());
		
		review.setPermissionObj( person );
		if( "*".equals( person ) ) {
			review.setPermissionObjType( "*" );
		}else {
			review.setPermissionObjType( "PERSON" );
		}
		return review;
	}
	
	/**
	 * 将字符串添加到集合里,去重
	 * @param list
	 * @param string
	 * @return list
	 */
	private List<String> addStringToList( List<String> list, String string ) {
		if( list == null ) {
			list = new ArrayList<>();
		}
		if( !list.contains( string )) {
			list.add( string);
		}
		return list;
	}
	
	/**
	 * 将字符串列表添加到集合里,去重
	 * @param list
	 * @param list2
	 * @return list
	 */
	private List<String> addListToList( List<String> list, List<String> list2 ) {
		if( list == null ) {
			list = new ArrayList<>();
		}
		if( ListTools.isEmpty( list2 ) ) {
			return list;
		}else {
			for( String string : list2 ) {
				if( !list.contains( string )) {
					list.add( string);
				}
			}
		}
		return list;
	}

	/**
	 * 检查传入的taskIds中有多少是指定用户可见的ID
	 * @param taskIds
	 * @param personName
	 * @return
	 * @throws Exception 
	 */
	public List<String> checkTaskIdsWithPermission(EntityManagerContainer emc, List<String> taskIds, String personName) throws Exception {
		if( ListTools.isEmpty( taskIds )) {
			return null;
		}
		if( StringUtils.isEmpty( personName )) {
			return null;
		}
		Business business = new Business(emc);
		return business.reviewFactory().checkTaskIdsWithPermission( taskIds, personName );
	}

	/**
	 * 查询用户在指定项目下所有可见的工作任务ID列表
	 * @param emc
	 * @param person
	 * @param project
	 * @return
	 * @throws Exception
	 */
	public List<String> listTaskIdsWithPerson(EntityManagerContainer emc, String person, String project ) throws Exception {
		if( StringUtils.isEmpty( person )) {
			return null;
		}
		if( StringUtils.isEmpty( project )) {
			return null;
		}
		Business business = new Business(emc);
		return business.reviewFactory().listTaskIdsWithPersonAndProject( person, project );
	}

	public List<Review> listTaskWithPersonAndParentId(EntityManagerContainer emc, String person, String taskId) throws Exception {
		if( StringUtils.isEmpty( person )) {
			return null;
		}
		if( StringUtils.isEmpty( taskId )) {
			return null;
		}
		Business business = new Business(emc);
		return business.reviewFactory().listTaskWithPersonAndParentId( person, taskId );
	}
}