ActionQueryListNextWithFilter.java 10.4 KB
Newer Older
R
roo00 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
package com.x.cms.assemble.control.jaxrs.document;

import com.google.gson.JsonElement;
import com.x.base.core.entity.JpaObject;
import com.x.base.core.project.bean.WrapCopier;
import com.x.base.core.project.bean.WrapCopierFactory;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.base.core.project.tools.ListTools;
import com.x.cms.core.entity.Document;
import com.x.cms.core.entity.Review;
R
roo00 已提交
14 15 16
import com.x.cms.core.express.tools.filter.QueryFilter;
import com.x.cms.core.express.tools.filter.term.InTerm;
import com.x.cms.core.express.tools.filter.term.NotInTerm;
R
update  
roo00 已提交
17 18 19 20 21
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
R
roo00 已提交
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

public class ActionQueryListNextWithFilter extends BaseAction {

	private static  Logger logger = LoggerFactory.getLogger(ActionQueryListNextWithFilter.class);

	protected ActionResult<List<Wo>> execute( HttpServletRequest request, String id, Integer count, JsonElement jsonElement, EffectivePerson effectivePerson ) {
		ActionResult<List<Wo>> result = new ActionResult<>();		
		Long total = 0L;
		Wi wi = null;
		List<Wo> wos = new ArrayList<>();
		List<Document> documentList = null;
		List<Document> searchResultList = new ArrayList<>();
		List<Review> reviewList =  null;
		Boolean check = true;
		Boolean isManager = false;
		String personName = effectivePerson.getDistinguishedName();
		QueryFilter queryFilter = null;
		
		if ( count == 0 ) { count = 20; }
		if ( StringUtils.isEmpty( id ) || "(0)".equals( id ) ) { id = null; }
		
		try {
			wi = this.convertToWrapIn( jsonElement, Wi.class );
		} catch (Exception e ) {
			check = false;
			Exception exception = new ExceptionDocumentInfoProcess( e, "系统在将JSON信息转换为对象时发生异常。JSON:" + jsonElement.toString() );
			result.error( exception );
			logger.error( e, effectivePerson, request, null);
		}
		if ( wi == null ) { wi = new Wi(); }
		
		if( StringUtils.isEmpty( wi.getDocumentType() )) {
			wi.setDocumentType( "信息" );
		}
		
R
update  
roo00 已提交
57
		if( StringUtils.isEmpty( wi.getOrderField() )) {
R
roo00 已提交
58 59 60
			wi.setOrderField( "createTime" );
		}
		
R
update  
roo00 已提交
61 62
		if( StringUtils.isEmpty( wi.getOrderType() )) {
			wi.setOrderType( "DESC" );
R
roo00 已提交
63 64
		}
		
R
update  
roo00 已提交
65
		if( ListTools.isEmpty( wi.getStatusList() )) {
R
roo00 已提交
66 67 68 69 70 71 72 73 74 75 76 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 125 126 127 128 129 130 131 132 133 134 135 136
			List<String> status = new ArrayList<>();
			status.add( "published" );
			wi.setStatusList( status );
		}
		
		if (check) {
			try {
				queryFilter = wi.getQueryFilter();
			} catch (Exception e) {
				check = false;
				Exception exception = new ExceptionDocumentInfoProcess(e, "系统在获取查询条件信息时发生异常。");
				result.error(exception);
				logger.error(e, effectivePerson, request, null);
			}
		}
		
		if( check ) {
			try {
				if( effectivePerson.isManager() || userManagerService.isHasPlatformRole( effectivePerson.getDistinguishedName(), "CMSManager" )) {
					isManager = true;
				}
			} catch (Exception e) {
				check = false;
				Exception exception = new ExceptionDocumentInfoProcess(e, "系统在判断用户是否是管理时发生异常。");
				result.error(exception);
				logger.error(e, effectivePerson, request, null);
			}
		}
		
		if( check ) {
			//查询是否已读,需要使用相应的ID进行IN操作,效率有一些低
			List<String> readDocIds = null;
			if( "READ".equalsIgnoreCase( wi.getReadFlag() )) { //只查询阅读过的
				//查询出该用户所有已经阅读过的文档ID列表
				try {
					readDocIds = documentViewRecordServiceAdv.listDocIdsByPerson( personName, 2000 );
					if( ListTools.isEmpty( readDocIds )) {
						readDocIds = new ArrayList<>();
						readDocIds.add( "no Document readed" );
					}
					if( isManager ) {
						queryFilter.addInTerm( new InTerm( "id", new ArrayList<>( readDocIds ) ) );
					}else {
						queryFilter.addInTerm( new InTerm( "docId", new ArrayList<>( readDocIds ) ) );
					}
				} catch (Exception e) {
					check = false;
					Exception exception = new ExceptionDocumentInfoProcess(e, "系统在查询用户已经阅读过的文档ID列表时发生异常。");
					result.error(exception);
					logger.error(e, effectivePerson, request, null);
				}
			}else if("UNREAD".equalsIgnoreCase( wi.getReadFlag() )) { //只查询未阅读过的
				//查询出该用户所有已经阅读过的文档ID列表
				try {
					readDocIds = documentViewRecordServiceAdv.listDocIdsByPerson( personName, 2000 );
					if( ListTools.isNotEmpty( readDocIds )) {
						if( isManager ) {
							queryFilter.addNotInTerm( new NotInTerm( "id", new ArrayList<>( readDocIds ) ) );
						}else {
							queryFilter.addNotInTerm( new NotInTerm( "docId", new ArrayList<>( readDocIds ) ) );
						}
					}
				} catch (Exception e) {
					check = false;
					Exception exception = new ExceptionDocumentInfoProcess(e, "系统在查询用户已经阅读过的文档ID列表时发生异常。");
					result.error(exception);
					logger.error(e, effectivePerson, request, null);
				}
			}
		}
		
R
update  
roo00 已提交
137
		if (check) { // 从Review表中查询符合条件的对象总数
R
roo00 已提交
138
			try {
R
update  
roo00 已提交
139
				if( isManager ) { //直接从Document忽略权限查询
R
roo00 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
					total = documentQueryService.countWithConditionOutofPermission( queryFilter );
				}else {
					total = documentQueryService.countWithConditionInReview( personName, queryFilter );
				}
			} catch (Exception e) {
				check = false;
				Exception exception = new ExceptionDocumentInfoProcess(e, "系统在获取用户可查询到的文档数据条目数量时发生异常。");
				result.error(exception);
				logger.error(e, effectivePerson, request, null);
			}
		}
		
		if (check) {
			//document和Review除了sequence还有5个排序列支持title, appAlias, categoryAlias, categoryName, creatorUnitName的分页查询
			//除了sequence和title, appAlias, categoryAlias, categoryName, creatorUnitName之外,其他的列排序全部在内存进行分页
			try {
				if( isManager ) {
R
update  
roo00 已提交
157 158
					//判断排序列是不是已经在Document表里做了sequence索引列,如果是,则只需要从document表查询 即可。
					if( Document.isFieldInSequence( wi.getOrderField()) ) { //直接从Document忽略权限查询
R
roo00 已提交
159 160
						searchResultList = documentQueryService.listNextWithConditionOutofPermission( id, count, wi.getOrderField(), wi.getOrderType(), queryFilter );
					}else {
R
update  
roo00 已提交
161
						//根据人员权限查询出2000条文档的完整信息,然后对某属性进行排序,在内存中进行分页
R
roo00 已提交
162 163 164 165 166 167 168 169 170
						documentList = documentQueryService.listNextWithConditionOutofPermission( wi.getOrderField(), wi.getOrderType(), queryFilter, 2000 );
						//循环分页,查询传入的ID所在的位置,向后再查询N条
						if( ListTools.isNotEmpty( documentList )) {
							Boolean add2List = false;
							//放一页到searchResultList中进行返回
							for( Document document : documentList ) {
								if( StringUtils.isEmpty( id ) || document.getId().equalsIgnoreCase( id ) ) {
									add2List = true;
								}
R
update  
roo00 已提交
171 172
								if( add2List ) { searchResultList.add( document ); }
								if( searchResultList.size() >= count ) { break; }
R
roo00 已提交
173 174 175 176
							}
						}
					}
				}else {
R
update  
roo00 已提交
177
					if( Document.isFieldInSequence(wi.getOrderField()) ) { // 从Review表中查询符合条件的对象,并且转换为Document对象列表
R
roo00 已提交
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 205 206 207 208 209 210
						searchResultList = documentQueryService.listNextWithConditionInReview( id, count, wi.getOrderField(), wi.getOrderType(), personName, queryFilter );
					}else {
						reviewList =  documentQueryService.listNextWithConditionInReview( wi.getOrderField(), wi.getOrderType(), personName, queryFilter, 2000 );
						//循环分页,查询传入的ID所在的位置,向后再查询N条,转换为Document放到searchResultList
						searchResultList = new ArrayList<>();
						if( ListTools.isNotEmpty( reviewList )) {
							Boolean add2List = false;
							//放一页到searchResultList中进行返回
							for( Review review : reviewList ) {
								if( StringUtils.isEmpty( id ) || review.getDocId().equalsIgnoreCase( id ) ) {
									add2List = true;
								}
								if( add2List ) {
									searchResultList.add(documentQueryService.get( review.getDocId() ) );
								}
								if( searchResultList.size() >= count ) {
									break;
								}
							}
						}
					}
				}
			} catch (Exception e) {
				check = false;
				Exception exception = new ExceptionDocumentInfoProcess(e, "系统在根据用户可访问的文档ID列表对文档进行分页查询时发生异常。");
				result.error(exception);
				logger.error(e, effectivePerson, request, null);
			}
		}
		
		if (check) {
			if ( searchResultList != null ) {
				Wo wo = null;
R
update  
roo00 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
				for( Document document : searchResultList ) {
					if( document != null ){
						try {
							wo = Wo.copier.copy( document );
							if( wo.getCreatorPerson() != null && !wo.getCreatorPerson().isEmpty() ) {
								wo.setCreatorPersonShort( wo.getCreatorPerson().split( "@" )[0]);
							}
							if( wo.getCreatorUnitName() != null && !wo.getCreatorUnitName().isEmpty() ) {
								wo.setCreatorUnitNameShort( wo.getCreatorUnitName().split( "@" )[0]);
							}
							if( wo.getCreatorTopUnitName() != null && !wo.getCreatorTopUnitName().isEmpty() ) {
								wo.setCreatorTopUnitNameShort( wo.getCreatorTopUnitName().split( "@" )[0]);
							}
							if( wi.getNeedData() ) {
								//需要组装数据
								wo.setData( documentQueryService.getDocumentData( document ) );
							}
						} catch (Exception e) {
							check = false;
							Exception exception = new ExceptionDocumentInfoProcess(e, "系统获取文档数据内容信息时发生异常。Id:" + document.getCategoryId());
							result.error(exception);
							logger.error(e, effectivePerson, request, null);
R
roo00 已提交
233
						}
R
update  
roo00 已提交
234
						wos.add( wo );
R
roo00 已提交
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 273 274 275 276 277
					}
				}
			}
		}
		result.setCount(total);
		result.setData(wos);
		return result;
	}	

	public class DocumentCacheForFilter {

		private Long total = 0L;		
		private List<Wo> documentList = null;

		public Long getTotal() {
			return total;
		}

		public void setTotal(Long total) {
			this.total = total;
		}

		public List<Wo> getDocumentList() {
			return documentList;
		}

		public void setDocumentList(List<Wo> documentList) {
			this.documentList = documentList;
		}	
	}
	
	public static class Wi extends WrapInDocumentFilter{
		
	}
	
	public static class Wo extends WrapOutDocumentList {
		
		public static List<String> Excludes = new ArrayList<String>();
		
		public static WrapCopier<Document, Wo> copier = WrapCopierFactory.wo( Document.class, Wo.class, null,JpaObject.FieldsInvisible);
		
	}
}