DocumentPersistService.java 11.2 KB
Newer Older
Z
Zhou Rui 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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 58 59 60 61 62 63 64 65 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 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 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
package com.x.cms.assemble.control.service;

import com.google.gson.JsonElement;
import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
import com.x.base.core.entity.annotation.CheckPersistType;
import com.x.base.core.entity.dataitem.ItemCategory;
import com.x.base.core.project.tools.ListTools;
import com.x.cms.assemble.control.DocumentDataHelper;
import com.x.cms.assemble.control.jaxrs.document.ActionPersistBatchModifyData.WiDataChange;
import com.x.cms.assemble.control.jaxrs.document.ActionPersistBatchModifyData.Wo;
import com.x.cms.assemble.control.jaxrs.permission.element.PermissionInfo;
import com.x.cms.core.entity.CategoryInfo;
import com.x.cms.core.entity.Document;
import com.x.cms.core.entity.content.Data;
import com.x.query.core.entity.Item;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 对文档信息进行持久化服务类
 */
public class DocumentPersistService {
	private DocumentInfoService documentInfoService = new DocumentInfoService();
	private PermissionOperateService permissionService = new PermissionOperateService();
	
	public Document save( Document document, JsonElement jsonElement ) throws Exception {
		if( document == null ){
			throw new Exception("document is null!");
		}
		if (document.getPictureList() != null && !document.getPictureList().isEmpty()) {
			document.setHasIndexPic(true);
		}
		try ( EntityManagerContainer emc = EntityManagerContainerFactory.instance().create() ) {
			document.setModifyTime( new Date());	
			document.setSequenceAppAlias( document.getAppAlias() + document.getId() );
			document.setSequenceCategoryAlias( document.getCategoryAlias() + document.getId() );
			if( document.getTitle().length() > 30 ) {
				document.setSequenceTitle( document.getTitle().substring(0, 30) + document.getId() );
			}else {
				document.setSequenceTitle( document.getTitle() + document.getId() );
			}
			if( document.getCreatorPerson().length() > 50 ) {
				document.setSequenceCreatorPerson( document.getCreatorPerson().substring(0, 50) + document.getId() );
			}else {
				document.setSequenceCreatorPerson( document.getCreatorPerson() + document.getId() );
			}
			if( document.getCreatorUnitName().length() > 50 ) {
				document.setSequenceCreatorUnitName( document.getCreatorUnitName().substring(0, 50) + document.getId() );
			}else {
				document.setSequenceCreatorUnitName( document.getCreatorUnitName() + document.getId() );
			}			
			
			document =  documentInfoService.save( emc, document );
			//如果有数据信息,则保存数据信息
			
			DocumentDataHelper documentDataHelper = new DocumentDataHelper( emc, document );
			if( jsonElement != null ) {
				documentDataHelper.update( jsonElement );
			}
			emc.commit();

			Data data = documentDataHelper.get();
			data.setDocument( document );
			documentDataHelper.update( data );
			
			emc.commit();
			return document;
		} catch ( Exception e ) {
			throw e;
		}
	}
	
	/**
	 * 根据文档信息更新数据内容中的文档信息内容
	 * @param document
	 * @return
	 * @throws Exception
	 */
	public Document refreshDocInfoData( Document document ) throws Exception {
		if( document == null ){
			throw new Exception("document is null!");
		}
		try ( EntityManagerContainer emc = EntityManagerContainerFactory.instance().create() ) {
			document =  documentInfoService.save( emc, document );
			//如果有数据信息,则保存数据信息
			DocumentDataHelper documentDataHelper = new DocumentDataHelper( emc, document );
			Data data = documentDataHelper.get();
			data.setDocument( document );
			documentDataHelper.update(data);
			emc.commit();
			return document;
		} catch ( Exception e ) {
			throw e;
		}
	}

	void fill(Item o, Document document) {
		/** 将DateItem与Document放在同一个分区 */
		o.setDistributeFactor(document.getDistributeFactor());
		o.setBundle(document.getId());
		o.setItemCategory(ItemCategory.cms);
	}
	
	

	/**
	 * 变更一个文档的分类信息
	 * @param document
	 * @param categoryInfo
	 * @throws Exception
	 */
	public Boolean changeCategory( Document document, CategoryInfo categoryInfo) throws Exception {
		if( document == null ){
			throw new Exception("document is empty!");
		}		
		if( categoryInfo == null ){
			throw new Exception("categoryInfo is empty!");
		}
		Data data = null;
		DocumentDataHelper documentDataHelper = null;
		try ( EntityManagerContainer emc = EntityManagerContainerFactory.instance().create() ) {
			emc.beginTransaction( Document.class );
			emc.beginTransaction(  Item.class );
			
			document = emc.find( document.getId(), Document.class );
			
			//更新document分类相关信息
			document.setAppId( categoryInfo.getAppId() );
			document.setAppName( categoryInfo.getAppName() );
			document.setCategoryId( categoryInfo.getId() );
			document.setCategoryName( categoryInfo.getCategoryName() );
			document.setCategoryAlias( categoryInfo.getCategoryAlias() );
			document.setForm( categoryInfo.getReadFormId() );
			document.setFormName( categoryInfo.getReadFormName() );
			document.setModifyTime( new Date());
			
			document.setSequenceAppAlias( document.getAppAlias() + document.getId() );
			document.setSequenceCategoryAlias( document.getCategoryAlias() + document.getId() );
			if( document.getTitle().length() > 30 ) {
				document.setSequenceTitle( document.getTitle().substring(0, 30) + document.getId() );
			}else {
				document.setSequenceTitle( document.getTitle() + document.getId() );
			}
			if( document.getCreatorPerson().length() > 50 ) {
				document.setSequenceCreatorPerson( document.getCreatorPerson().substring(0, 50) + document.getId() );
			}else {
				document.setSequenceCreatorPerson( document.getCreatorPerson() + document.getId() );
			}
			if( document.getCreatorUnitName().length() > 50 ) {
				document.setSequenceCreatorUnitName( document.getCreatorUnitName().substring(0, 50) + document.getId() );
			}else {
				document.setSequenceCreatorUnitName( document.getCreatorUnitName() + document.getId() );
			}
			
			emc.check( document, CheckPersistType.all );
			
			//更新数据里的document对象信息
			documentDataHelper = new DocumentDataHelper( emc, document );
			data = documentDataHelper.get();
			data.setDocument( document );
			documentDataHelper.update( data );

			emc.commit();
			return true;
		} catch ( Exception e ) {
			throw e;
		}
	}
	
	public Wo changeData(List<String> docIds, List<WiDataChange> dataChanges) throws Exception {
		if( ListTools.isEmpty( docIds )){
			throw new Exception("docIds is empty!");
		}
		if( ListTools.isEmpty( dataChanges )){
			throw new Exception("dataChanges is empty!");
		}
		Wo wo = new Wo();
		Data data = null;
		Document document_entity = null;
		DocumentDataHelper documentDataHelper = null;
		for( String docId : docIds ) {
			try ( EntityManagerContainer emc = EntityManagerContainerFactory.instance().create() ) {
				
				emc.beginTransaction( Document.class );
				emc.beginTransaction( Item.class );
				
				document_entity = emc.find( docId, Document.class );
				document_entity.setModifyTime( new Date());
				
				documentDataHelper = new DocumentDataHelper( emc, document_entity );
				data = documentDataHelper.get();
				for( WiDataChange dataChange : dataChanges ) {
					if( "Integer".equals(dataChange.getDataType() )) {
						data.put( dataChange.getDataPath(), dataChange.getDataInteger());
					}else if( "String".equals(dataChange.getDataType() )) {
						data.put( dataChange.getDataPath(), dataChange.getDataString());
					}else if( "Date".equals(dataChange.getDataType() )) {
						data.put( dataChange.getDataPath(), dataChange.getDataDate());
					}else if( "Boolean".equals(dataChange.getDataType() )) {
						data.put( dataChange.getDataPath(), dataChange.getDataBoolean());
					}else {
						data.put( dataChange.getDataPath(), dataChange.getDataString());
					}
				}
				data.setDocument( document_entity );
				documentDataHelper.update( data );
				emc.commit();
				wo.increaseSuccess_count(1);
			} catch ( Exception e ) {
				wo.appendErorrId(docId);
				wo.increaseError_count(1);
				e.printStackTrace();
			}
		}
		return wo;
	}

	public Boolean inReview(String documentId) throws Exception {
		if( StringUtils.isEmpty( documentId ) ){
			throw new Exception("documentId is empty!");
		}
		try ( EntityManagerContainer emc = EntityManagerContainerFactory.instance().create() ) {
			return documentInfoService.inReview( emc, documentId );
		} catch ( Exception e ) {
			throw e;
		}
	}

	public void topDocument(String id) throws Exception {
		if( StringUtils.isEmpty( id ) ){
			throw new Exception("id is empty!");
		}
		try ( EntityManagerContainer emc = EntityManagerContainerFactory.instance().create() ) {
			Document document = emc.find( id, Document.class );
			if( document != null ) {
//				emc.beginTransaction( Item.class );
				emc.beginTransaction( Document.class );
				document.setIsTop( true );			
				
				DocumentDataHelper documentDataHelper = new DocumentDataHelper( emc, document );
				Data data = documentDataHelper.get();
				data.setDocument( document );
				documentDataHelper.update( data );
				
				emc.commit();
			}
			
		} catch ( Exception e ) {
			throw e;
		}
	}
	
	public void unTopDocument(String id) throws Exception {
		if( StringUtils.isEmpty( id ) ){
			throw new Exception("id is empty!");
		}
		try ( EntityManagerContainer emc = EntityManagerContainerFactory.instance().create() ) {
			Document document = emc.find( id, Document.class );
			if( document != null ) {
				emc.beginTransaction( Document.class );
				document.setIsTop( false );
				DocumentDataHelper documentDataHelper = new DocumentDataHelper( emc, document );
				Data data = documentDataHelper.get();
				data.setDocument( document );
				documentDataHelper.update( data );
				emc.commit();
			}
			
		} catch ( Exception e ) {
			throw e;
		}
	}
	
	/**
	 * 删除指定ID的文档
	 * @param docId
	 * @throws Exception
	 */
	public void delete(  String docId ) throws Exception {
		if( StringUtils.isEmpty(  docId ) ){
			throw new Exception("docId is empty!");
		}
		try ( EntityManagerContainer emc = EntityManagerContainerFactory.instance().create() ) {
			documentInfoService.delete(emc, docId);
		} catch ( Exception e ) {
			throw e;
		}
	}

	/**
	 * 根据读者作者列表更新文档所有的权限信息
	 * @param docId
	 * @param readerList
	 * @param authorList
	 * @throws Exception
	 */
	public void refreshDocumentPermission( String docId, List<PermissionInfo> readerList, List<PermissionInfo> authorList ) throws Exception {
		if( StringUtils.isEmpty( docId ) ){
			throw new Exception("docId is empty!");
		}
		permissionService.refreshDocumentPermission( docId, readerList, authorList);
		
		new CmsBatchOperationPersistService().addOperation( 
				CmsBatchOperationProcessService.OPT_OBJ_DOCUMENT, 
				CmsBatchOperationProcessService.OPT_TYPE_PERMISSION,  docId,  docId, "刷新文档权限:ID=" +  docId );
	}

	/**
	 * 根据组织好的权限信息列表更新指定文档的权限信息
	 * @param docId
	 * @param permissionList
	 * @throws Exception
	 */
	public void refreshDocumentPermission( String docId, List<PermissionInfo> permissionList ) throws Exception {
		if( StringUtils.isEmpty( docId ) ){
			throw new Exception("docId is empty!");
		}
		permissionService.refreshDocumentPermission(docId, permissionList);
	}
}