Материал из BiTel WiKi
package ru.bitel.bgbilling.modules.tv.dyn.addactions;
import java.util.Date;
import java.util.Set;
import ru.bitel.bgbilling.common.BGException;
import ru.bitel.bgbilling.kernel.container.managed.ServerContext;
import ru.bitel.bgbilling.kernel.contract.api.common.event.ContractModifiedEvent;
import ru.bitel.bgbilling.kernel.event.Event;
import ru.bitel.bgbilling.kernel.event.events.AdditionalActionEvent;
import ru.bitel.bgbilling.kernel.event.events.GetAdditionalWebActionListEvent;
import ru.bitel.bgbilling.kernel.script.server.dev.EventScriptBase;
import ru.bitel.bgbilling.modules.tv.api.common.bean.TvAccount;
import ru.bitel.bgbilling.modules.tv.api.common.bean.TvAccountSpec;
import ru.bitel.bgbilling.modules.tv.api.common.service.TvAccountService;
import ru.bitel.bgbilling.server.util.Setup;
import ru.bitel.common.Preferences;
import ru.bitel.common.sql.ConnectionSet;
import bitel.billing.server.contract.bean.ContractModuleManager;
public class AddModuleProcessor
extends EventScriptBase
{
/**
* ID дополнительного действия.
*/
private static final int ACTION_ID = 32677875;
/**
* ID модуля TV.
*/
private static final int MODULE_ID = 210;
/**
* ID типа аккаунта.
*/
private static final int TV_ACCOUNT_SPEC_ID = 3;
@Override
public void onEvent( Event event, Setup setup, ConnectionSet connectionSet )
throws Exception
{
if( event instanceof GetAdditionalWebActionListEvent )
{
GetAdditionalWebActionListEvent e = (GetAdditionalWebActionListEvent)event;
ContractModuleManager cmm = new ContractModuleManager( connectionSet.getConnection() );
Set<Integer> mids = cmm.getContractModuleSet( e.getContractId() );
if( mids.contains( MODULE_ID ) )
{
return;
}
((GetAdditionalWebActionListEvent)event).addAction( ACTION_ID, "Активировать IPTV" );
}
else if( event instanceof AdditionalActionEvent )
{
AdditionalActionEvent e = (AdditionalActionEvent)event;
ContractModuleManager cmm = new ContractModuleManager( connectionSet.getConnection() );
Set<Integer> mids = cmm.getContractModuleSet( e.getContractId() );
if( mids.contains( MODULE_ID ) )
{
return;
}
activate( connectionSet, cmm, e );
}
}
private void activate( final ConnectionSet connectionSet, final ContractModuleManager cmm, final AdditionalActionEvent e )
throws BGException
{
ServerContext context = ServerContext.get();
cmm.addContractModule( e.getContractId(), MODULE_ID );
context.publishAfterCommit( new ContractModifiedEvent( 0, e.getContractId() ) );
TvAccountService tvAccountService = context.getService( TvAccountService.class, MODULE_ID );
final TvAccountSpec servType = context.getDirectory( TvAccountSpec.class, MODULE_ID ).get( TV_ACCOUNT_SPEC_ID );
Preferences servTypeConfig = new Preferences( servType.getConfig(), "\n\r" );
int deviceId = servTypeConfig.getInt( "const.device.id", -1 );
final TvAccount serv = new TvAccount();
serv.setSpecId( TV_ACCOUNT_SPEC_ID );
serv.setContractId( e.getContractId() );
serv.setDateFrom( new Date() );
serv.setDateTo( null );
serv.setStatus( TvAccount.STATUS_ACTIVE );
if( deviceId > 0 )
{
serv.setDeviceId( deviceId );
}
else
{
e.addReport( "Не указан const.device.id в конфигурации типа аккаунта." );
return;
}
tvAccountService.tvAccountUpdate( serv, true, true, 0 );
e.addReport( "Услуга IPTV активирована. Ваш логин: " + serv.getLogin() + ", пароль: " + serv.getPassword() );
}
}