Calendar_SettingService.java 6.4 KB
Newer Older
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
package com.x.calendar.assemble.control.service;

import java.util.List;

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.calendar.assemble.control.Business;
import com.x.calendar.core.entity.Calendar_Setting;
import com.x.calendar.core.entity.Calendar_SettingLobValue;


/**
 * 日程管理系统设置服务类
 * @author O2LEE
 *
 */
public class Calendar_SettingService {
	
	//private Logger logger = LoggerFactory.getLogger( Calendar_SettingService.class );

	/**
	 * 获取所有的系统设置信息
	 * @return
	 * @throws Exception
	 */
	public List<Calendar_Setting> listAll( EntityManagerContainer emc ) throws Exception {
		Business business =  new Business( emc );
		return business.calendar_SettingFactory().listAll();
	}
	
	/**
	 * 根据ID获取指定的系统设置信息
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public Calendar_Setting get( EntityManagerContainer emc, String id ) throws Exception {
		return emc.find(id, Calendar_Setting.class);
	}

	/**
	 * 根据ID删除指定的系统设置信息
	 * @param id
	 * @throws Exception
	 */
	public void delete( EntityManagerContainer emc, String id ) throws Exception {
		Calendar_Setting report_S_Setting = null;
		if( id == null || id.isEmpty() ){
			throw new Exception( "id is null, system can not delete any object." );
		}
		report_S_Setting = emc.find( id, Calendar_Setting.class );
		if ( null == report_S_Setting ) {
			throw new Exception( "object is not exist {'id':'"+ id +"'}" );
		}else{
			emc.beginTransaction( Calendar_Setting.class );
			emc.remove( report_S_Setting, CheckRemoveType.all );
			emc.commit();
		}
	}
	/**
	 * 根据设置编码获取指定的设置信息
	 * @param code
	 * @return
	 * @throws Exception
	 */
	public Calendar_Setting getByCode( EntityManagerContainer emc, String code ) throws Exception {
		List<Calendar_Setting> report_S_SettingList = null;
		Calendar_Setting report_S_Setting = null;
		List<String> ids = null;
		Business business = new Business( emc );
		if( code == null || code.isEmpty() ){
			throw new Exception( "code is null, system can not query any object." );
		}
		
		ids = business.calendar_SettingFactory().listIdsByCode(code);
		if( ids != null && !ids.isEmpty() ){
			report_S_SettingList =  business.calendar_SettingFactory().list( ids );
		}
		
		if( report_S_SettingList != null && !report_S_SettingList.isEmpty() ){
			for( int i=0; i< report_S_SettingList.size(); i++  ){
				if( i == 0 ){
					report_S_Setting = report_S_SettingList.get( i );
				}else{
					emc.beginTransaction( Calendar_Setting.class );
					emc.remove( report_S_SettingList.get( i ), CheckRemoveType.all );
					emc.commit();
				}
			}
		}
		return report_S_Setting;
	}

	/**
	 * 创建系统设置信息
	 * @param report_S_Setting
	 * @return
	 * @throws Exception
	 */
	public Calendar_Setting create( EntityManagerContainer emc, Calendar_Setting report_S_Setting ) throws Exception {
		Calendar_Setting report_S_Setting_old = null;
		Calendar_SettingLobValue reportSettingLobValue = null;
		
		report_S_Setting_old = emc.find( report_S_Setting.getId(), Calendar_Setting.class );
		if( report_S_Setting_old != null ){
			throw new Exception("report_S_Setting{'id':' "+ report_S_Setting.getId() +" '} exists, can not create new object");
		}else{
			emc.beginTransaction( Calendar_Setting.class );

			//对长文本记录进行操作
			if( report_S_Setting.getIsLob() ) {
				emc.beginTransaction( Calendar_SettingLobValue.class );
				reportSettingLobValue = emc.find( report_S_Setting.getId(), Calendar_SettingLobValue.class );
				if( reportSettingLobValue != null ) {
					reportSettingLobValue.setLobValue( report_S_Setting.getConfigValue() );//更新值
					emc.check( reportSettingLobValue, CheckPersistType.all );
				}else {
					//没有,就创建一个LOB值记录
					reportSettingLobValue = new Calendar_SettingLobValue();
					reportSettingLobValue.setId( report_S_Setting.getId() );
					reportSettingLobValue.setLobValue( report_S_Setting.getConfigValue() );
					emc.persist( reportSettingLobValue, CheckPersistType.all );
				}
				report_S_Setting.setConfigValue( "LobValue" );
			}
			
			emc.persist( report_S_Setting, CheckPersistType.all);
			emc.commit();
		}
		return report_S_Setting;
	}
	
	/**
	 * 更新系统设置信息
	 * @param report_S_Setting
	 * @return
	 * @throws Exception
	 */
	public Calendar_Setting update( EntityManagerContainer emc, Calendar_Setting report_S_Setting ) throws Exception {
		if( report_S_Setting == null ){
			throw new Exception("report_S_Setting is null, can not update object!");
		}
		Calendar_Setting report_S_Setting_old = null;
		Calendar_SettingLobValue reportSettingLobValue = null;
		Business business = new Business(emc);
		report_S_Setting_old = business.calendar_SettingFactory().getWithConfigCode(report_S_Setting.getConfigCode());
		if( report_S_Setting_old != null ){
			emc.beginTransaction( Calendar_Setting.class );
			report_S_Setting_old.setConfigCode( report_S_Setting.getConfigCode() );
			report_S_Setting_old.setConfigName( report_S_Setting.getConfigName() );
			report_S_Setting_old.setConfigValue( report_S_Setting.getConfigValue() );
			report_S_Setting_old.setOrderNumber( report_S_Setting.getOrderNumber() );
			report_S_Setting_old.setIsLob( report_S_Setting.getIsLob() );
			
			//对长文本记录进行操作
			if( report_S_Setting_old.getIsLob() ) {
				emc.beginTransaction( Calendar_SettingLobValue.class );
				reportSettingLobValue = emc.find( report_S_Setting.getId(), Calendar_SettingLobValue.class );
				if( reportSettingLobValue != null ) {
					reportSettingLobValue.setLobValue( report_S_Setting.getConfigValue() );//更新值
					emc.check( reportSettingLobValue, CheckPersistType.all );
				}else {
					//没有,就创建一个LOB值记录
					reportSettingLobValue = new Calendar_SettingLobValue();
					reportSettingLobValue.setId( report_S_Setting.getId() );
					reportSettingLobValue.setLobValue( report_S_Setting.getConfigValue() );
					emc.persist( reportSettingLobValue, CheckPersistType.all );
				}
				report_S_Setting_old.setConfigValue( "LobValue" );
			}
			emc.check( report_S_Setting_old, CheckPersistType.all);
			emc.commit();
		}else{
			throw new Exception("old object report_S_Setting{'id':' "+ report_S_Setting.getId() +" '} is not exists. ");
		}
		
		return report_S_Setting_old;
	}

}