📄 eqlmanagerejb.java
字号:
public void update( LogonSession ls, EQLERes res, EQLResRecord record ) throws EQLException { // Initialization long time = System.currentTimeMillis(); EQLSession eqlSession = getEQLSession( ls ); try { __update( eqlSession, res, record ); } catch( EQLException ex ) { // Make explicit rollback on checked exception. setRollbackOnly(); throw ex; } catch(EQLConstraintViolationException e) { throw e; } catch( Throwable t ) { ErrorHelper.throwSystemException( t, this ); } // Ok. INFO( "EQL UPDATE completed. Time (ms) = " + ( System.currentTimeMillis() - time ) ); } /** * EQL <code>DELETE</code> function. * * @param ls user logon session * @param entity base entity * @param compoundPkeys compound primary keys array * @throws EQLException */ public void delete( LogonSession ls, Entity entity, CompoundKey[] compoundPkeys ) throws EQLException { // Check arguments. if( entity == null || compoundPkeys == null ) { throw new IllegalStateException(); } EQLERes res = new EQLERes( entity ); // Add records. int fields = entity.getEfieldCount(); for( int i = 0; i < compoundPkeys.length; i++ ) { CompoundKey compoundPkey = compoundPkeys[i]; int size = compoundPkey.size(); // Create EQLResRecord object. EQLResRecord resRecord = new EQLResRecord( false, fields ); res.addRecord( resRecord ); int key_num = 0; for( int j = 0; j < fields; j++ ) { Efield field = entity.getEfield( j ); if( !field.getPkey().booleanValue() ) { continue; } // Pimary key found... if( key_num >= size ) { break; } // Create EQLResCell object for concrete field. EQLReqField reqField = new EQLReqField( entity, field ); EQLObject o = EQLObject.getInstance( compoundPkey.getKey( key_num ) ); EQLResCell resCell = new EQLResCell( reqField, o ); resRecord.addData( resCell, key_num ); key_num++; } } // Do delete. res.markAllAsDelete(); update( ls, res ); } // --------------- // SERVICE METHODS // --------------- /** * Access method to cache manager. * * @param ls logon session * @param entity Entity object * @return EQLResCacheObject object * @throws EQLException */ public EQLResCacheObject getCache( LogonSession ls, Entity entity ) throws EQLException { EQLResCacheObject cache = EQLResCacheManager.getInstance().getCacheObject( entity ); if( cache == null ) { select( ls, entity ); cache = EQLResCacheManager.getInstance().getCacheObject( entity ); if( cache == null ) { throw new EQLSystemException( "Entity '" + entity.getName() + "' can't be cached." ); } } return cache; } // ========================================================= Helper methods // // Call EQL SELECT NEW query. // void __selectNew( EQLSession eqlSession, Entity entity, EQLERes res ) throws EQLException { // Create new record. int size = entity.getEfieldCount(); EQLResRecord record = new EQLResRecord( true, size ); res.addRecord( record ); // Initialize fields. for( int i = 0; i < size; i++ ) { Efield field = entity.getEfield( i ); EQLReqField reqField = new EQLReqField( entity, field ); // .. check 'eql-defsrc' attribute to boost perfomance EQLRes __res = null; if( field.getEqlDefsrc() != null ) { __res = __selectNew( eqlSession, field ); } EQLResCell cell; if( __res != null && __res.size() > 0 ) { // .. found default data cell = __res.getRecord( 0 ).getResCell( 0 ); } else { // .. default data is empty cell = new EQLResCell( reqField, EQLNullObject.getInstance() ); } // .. add data cell record.addData( cell, i ); } // Initialize datasets. size = entity.getDatasetCount(); for( int i = 0; i < size; i++ ) { Dataset ds = entity.getDataset( i ); Entity dsEntity = eqlSession.findEntity( ds.getEntity() ); EQLReqDataset reqDataset = new EQLReqDataset( entity, ds, dsEntity ); EQLDRes dsRes = new EQLDRes( reqDataset ); // check required // and select new record for dataset if( ds.getRequired().booleanValue() ) { __selectNew( eqlSession, dsEntity, dsRes ); } record.addDRes( dsRes ); } // Run post-new scripts. boolean hasScripts = ( entity.getScripts() != null ); if( hasScripts ) { Afternew[] afterNews = entity.getScripts().getAfternew(); if( afterNews != null ) { for( int j = 0; j < afterNews.length; j++ ) { doEntityUpdate( eqlSession, res, record, afterNews[j] ); } } } } // // Call EQL SELECT NEW query. // EQLRes __selectNew( EQLSession eqlSession, Efield field ) throws EQLException { String eqlQuery = "NEW " + field.getId(); return doSelect( eqlSession, eqlQuery, null, null ); } // // Call EQL UPDATE query (update / insert / delete). // void __update( EQLSession eqlSession, EQLERes res, EQLResRecord record ) throws EQLException { // Unlock records first. getLockManagerLocal().unlock( res, eqlSession.getLogonSession(), record ); // Get array of records, because records can be removed from Res object. int records = 0; EQLResRecord[] recordsArray; if( record == null ) { records = res.size(); recordsArray = ( EQLResRecord[] ) res.getRecords().toArray( new EQLResRecord[0] ); } else { records = 1; recordsArray = new EQLResRecord[] {record}; } // Main updates cycle. for( int i = 0; i < records; i++ ) { EQLResRecord resRecord = recordsArray[i]; if( getLogger().isDebugEnabled() ) { DEBUG( "Updating record #" + i + "\n\t base entity: " + res.getEntity().getName() + "\n\t delete? - " + resRecord.doDelete() + "\n\t new? - " + resRecord.isNew() + "\n\t changed? - " + resRecord.isChanged() ); } int status; if( resRecord.doDelete() ) { status = doDelete( eqlSession, res, resRecord ); } else { status = doUpdate( eqlSession, res, resRecord ); } if( status == SKIP ) { break; } } } // ========================================================= Agent's methods // // Does EQL COUNT query. // public int doCount( EQLSession eqlSession, EQLRes res ) throws EQLException { EQLAgent agent = EQLAgentFactory.getInstance().getEQLAgent( res ); return agent.doCount( eqlSession, res ); } // // Does EQL SELECT query. // private EQLRes doSelect( EQLSession eqlSession, String eqlQuery, EQLIntPreparedStatement eqlPS, EQLReqMetaData meta ) throws EQLException { EQLRes res = null; if( getLogger().isInfoEnabled() ) { INFO( "...EQL query: " + eqlQuery );// INFO( "...EQL meta: " + meta ); } // 1. Parse EQL query and build EQLReq EQLInterpreter interpreter = EQLFactory.getInstance().getEqlInterpreter( eqlSession ); interpreter.setPreparedStatement( eqlPS ); interpreter.setEQLReqMetaData( meta ); EQLReq req = interpreter.build( eqlQuery );// INFO( "...build (parse) EQL request completed. Class: " + req.getClass() ); // 2. Detect can we take response from cache boolean needToCache = false; Entity baseEntity = ( req instanceof EQLEReq ) ? ( ( EQLEReq ) req ).getEntity() : null; if( baseEntity != null && baseEntity.getCache().booleanValue() ) { needToCache = true; }// INFO( "...detect 'need to store in cache' flag: " + needToCache ); if( needToCache ) { // 3. Try to find response in cache res = EQLResCacheManager.getInstance().findEQLResInCache( baseEntity ); } if( res != null ) { DEBUG( "..response found in the cache!" ); } if( res == null ) { if( ( req instanceof EQLEReq ) && ( meta == null || !meta.isIgnoreAllDatasets() ) ) { // 4. Add datasets in EQL request for( int i = 0; i < baseEntity.getDatasetCount(); i++ ) { Dataset dataset = baseEntity.getDataset( i ); if( meta != null && meta.isLazyLoadDataset( dataset ) ) { // .. ignore this dataset continue; } INFO( "...add dataset '" + dataset.getName() + "' in EQL request" ); Entity datasetEntity = eqlSession.findEntity( dataset.getEntity() ); EQLReqDataset reqDataset = new EQLReqDataset( baseEntity, dataset, datasetEntity ); EQLDReq dReq = new EQLDReq( reqDataset ); EQLReq __req = EQLFactory.getInstance().getEqlInterpreter( eqlSession ).build( dataset.getEql() ); dReq.copyOf( __req ); // add EQLDReq in request req.addDReq( dReq ); } } // 5. Call EQLAgent EQLAgent agent = EQLAgentFactory.getInstance().getEQLAgent( req ); res = agent.doSelect( eqlSession, req ); } if( needToCache ) { // 6. Put respone to cache. EQLResCacheManager.getInstance().saveEQLResInCache( ( EQLERes ) res ); } // 7. Ok return res; } // // Does the record insert/update. // private int doUpdate( EQLSession eqlSession, EQLERes res, EQLResRecord resRecord ) throws EQLException { // Get base entity. Entity entity = res.getEntity(); // Is record changed? boolean isChanged = resRecord.isChanged(); if( !isChanged ) { return CONTINUE; } try { // Run pre-update scripts. boolean hasScripts = ( entity.getScripts() != null ); if( hasScripts ) { Beforeupdate[] beforeUpdates = entity.getScripts().getBeforeupdate(); if( beforeUpdates != null ) { for( int j = 0; j < beforeUpdates.length; j++ ) { int status = doEntityUpdate( eqlSession, res, resRecord, beforeUpdates[j] ); if( status == EntityUpdate.SKIP_ALL ) { return SKIP; } if( status == EntityUpdate.SKIP_RECORD ) { return CONTINUE; } } } } // Write history./* if( entity.getHasHistoryFields().booleanValue() ) { resRecord = getHistoryLocalLocal().logHistory( eqlSession, res, resRecord ); }*/ // Need to fix this. ILadnev. if( !entity.getDbobject().equals("QX_HIS_TBLFIELD") ) { resRecord = getHistoryLocalLocal().logHistory( eqlSession, res, resRecord ); } // Update. Update update; if( hasScripts && ( update = entity.getScripts().getUpdate() ) != null ) { // Run custom update script. int status = doEntityUpdate( eqlSession, res, resRecord, update ); if( status == EntityUpdate.SKIP_ALL ) { return SKIP; } if( status == EntityUpdate.SKIP_RECORD ) { return CONTINUE; } } else { // Call EQLAgent. EQLAgent agent = EQLAgentFactory.getInstance().getEQLAgent( res ); agent.doUpdate( eqlSession, res, resRecord );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -