📄 alerteqlagent.java
字号:
/* * Copyright 2006-2007 Queplix Corp. * * Licensed under the Queplix Public License, Version 1.1.1 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.queplix.com/solutions/commercial-open-source/queplix-public-license/ * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */package com.queplix.core.modules.alert.eql;import com.queplix.core.error.GenericSystemException;import com.queplix.core.integrator.security.AccessRightsManager;import com.queplix.core.integrator.security.LogonSession;import com.queplix.core.integrator.security.NoSuchGroupException;import com.queplix.core.integrator.security.NoSuchUserException;import com.queplix.core.integrator.security.User;import com.queplix.core.integrator.security.WorkGroup;import com.queplix.core.jxb.entity.Entity;import com.queplix.core.modules.alert.Alert;import com.queplix.core.modules.alert.AlertData;import com.queplix.core.modules.alert.ejb.AlertManagerLocal;import com.queplix.core.modules.alert.ejb.AlertManagerLocalHome;import com.queplix.core.modules.alert.utils.AlertSelectorCriteria;import com.queplix.core.modules.eql.CompoundKey;import com.queplix.core.modules.eql.EQLEReq;import com.queplix.core.modules.eql.EQLERes;import com.queplix.core.modules.eql.EQLObject;import com.queplix.core.modules.eql.EQLReq;import com.queplix.core.modules.eql.EQLReqField;import com.queplix.core.modules.eql.EQLReqSelect;import com.queplix.core.modules.eql.EQLRes;import com.queplix.core.modules.eql.EQLResCell;import com.queplix.core.modules.eql.EQLResMetaData;import com.queplix.core.modules.eql.EQLResRecord;import com.queplix.core.modules.eql.EQLSession;import com.queplix.core.modules.eql.error.EQLException;import com.queplix.core.modules.eql.parser.EQLAgentStub;import com.queplix.core.modules.eql.parser.generic.SQLExecutorGenericImpl;import com.queplix.core.utils.JNDINames;import com.queplix.core.utils.log.AbstractLogger;import com.queplix.core.utils.log.Log;import javax.ejb.CreateException;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.SortedMap;/** * <p>Default EQL agent class to search Alerts as an entity</p> * @author Baranov Andrey [ALB] * @version $Revision: 1.2 $ $Date: 2005/10/26 14:03:51 $ */public abstract class AlertEQLAgent extends EQLAgentStub { // ----------------------------------------------------- fields/constants // 'Last Read Alert ID' session parameter name. protected final static String LAST_READ_ALERT_ID = "__lastAlertId"; // Logger. protected final AbstractLogger logger = Log.getLog( getClass() ); // Map for severity key-name pairs. // !!! Severity must be taken from read-only entity. protected final Map severityMap = new HashMap(); // ----------------------------------------------------- interface implementation /** * Returns last read alert ID for current user. * @param ls LogonSession * @return Alert ID or NULL */ public static Long getLastReadAlert( LogonSession ls ) { Object attr = ls.getParameter( LAST_READ_ALERT_ID ); return( Long ) attr; } /** * Remembers last read alert ID for current user. * @param ls LogonSession * @param lastAlertId Alert ID */ public static void setLastReadAlert( LogonSession ls, Long lastAlertId ) { ls.addParameter( LAST_READ_ALERT_ID, lastAlertId ); } /* * No javadoc * @see EQLAgent#doCount */ public int doCount( EQLSession eqlSession, EQLRes res ) throws EQLException { long time = System.currentTimeMillis(); // Create criteria and set mandatory parameters. AlertSelectorCriteria criteria = new AlertSelectorCriteria(); criteria.setLogonSession( eqlSession.getLogonSession() ); // Search result. SortedMap map = callAlertManager( eqlSession, criteria ); int ret; if( map != null ) { ret = map.size(); } else { ret = 0; } // Ok. if( logger.getLogger().isDebugEnabled() ) { logger.DEBUG( "Alert count operation completed. Found: " + ret + ". Time(ms): " + ( System.currentTimeMillis() - time ) ); } return ret; } /* * No javadoc * @see EQLAgent#doSelect */ public EQLRes doSelect( EQLSession eqlSession, EQLReq req ) throws EQLException { long time = System.currentTimeMillis(); // Probably 'new' request was received... if( ! ( req instanceof EQLEReq ) ) { return parseNewRequest( req ); } try { // Init List of entity field names. EQLReqSelect reqSelect = req.getSelect(); int columns = reqSelect.size(); List columNames = new ArrayList( columns ); for( int i = 0; i < columns; i++ ) { columNames.add( reqSelect.getAttr( i ).getReqField().getField().getName() ); } // Get Last Alert ID. Long lastAlertId = getLastReadAlert( eqlSession.getLogonSession() ); // Get page and page size. Integer page = SQLExecutorGenericImpl.getPage( req ); Integer pageSize = SQLExecutorGenericImpl.getPageSize( req ); // Create criteria and set mandatory parameters. AlertSelectorCriteria criteria = new AlertSelectorCriteria(); criteria.setLogonSession( eqlSession.getLogonSession() ); criteria.setPage( page ); criteria.setPageSize( pageSize ); // Search result. SortedMap ret = callAlertManager( eqlSession, criteria ); // Build EQLERes. Entity entity = ( ( EQLEReq ) req ).getEntity(); EQLERes res = new EQLERes( entity ); EQLResMetaData metaData = new EQLResMetaData(); metaData.setReq( req ); res.setMetaData( metaData ); // Fill EQLRes. if( ret != null ) { // Check if Available More record. res.getMetaData().setParam( SQLExecutorGenericImpl.HAS_MORE_PARAM, Boolean.valueOf( criteria.isAvailableMore() ) ); Long maxAlertID = null; for( Iterator it = ret.keySet().iterator(); it.hasNext(); ) { Long key = ( Long ) it.next(); if( maxAlertID == null ) { // All keys must be sorted in DESC order! maxAlertID = key; } Alert alert = ( Alert ) ret.get( key ); // Build new EQLResRecord with size = <code>columns</code>. EQLResRecord record = new EQLResRecord( false, columns ); res.addRecord( record ); // Populate EQLResRecord. addCells( eqlSession, record, entity, alert, columNames ); } // Remember max alert ID in logon session. // All alerts must be sorted by ID in ASC order! if( logger.getLogger().isDebugEnabled() ) { logger.DEBUG( "Try to remember last alert ID: " + maxAlertID + ". Prevous is: " + lastAlertId ); } if( maxAlertID != null ) { if( lastAlertId == null || maxAlertID.longValue() > lastAlertId.longValue() ) { setLastReadAlert( eqlSession.getLogonSession(), maxAlertID ); } } } // Ok. if( logger.getLogger().isDebugEnabled() ) { logger.DEBUG( "Alert search operation completed. Time(ms): " + ( System.currentTimeMillis() - time ) ); } return res; } catch( EQLException ex ) { logger.ERROR( ex ); throw ex; } catch( Throwable t ) { logger.ERROR( t ); // Show user 'normal' exception. throw new EQLException( "Problems retrieving the alerts, please retry later..." ); } } /* * No javadoc * @see EQLAgent#doUpdate */ public int doUpdate( EQLSession eqlSession, EQLERes res, EQLResRecord resRecord ) throws EQLException { long time = System.currentTimeMillis(); int updated; if( resRecord.isNew() ) { // Do Insert. updated = __doInsert( eqlSession, res, resRecord ); } else { // Do Update. updated = __doUpdate( eqlSession, res, resRecord ); } // Ok. if( logger.getLogger().isDebugEnabled() ) { logger.DEBUG( "Alert update operation completed. Updated " + updated + " records. Time(ms): " + ( System.currentTimeMillis() - time ) ); } return updated; } /* * No javadoc * @see EQLAgent#doDelete */ public int doDelete( EQLSession eqlSession, EQLERes res, EQLResRecord resRecord ) throws EQLException { long time = System.currentTimeMillis(); // Read alert_id Entity entity = res.getEntity(); EQLResCell resCell = resRecord.getResCell( getReqField( entity, "alert_id" ) ); Long alertID = ( resCell != null ) ? resCell.getLong() : null; if( alertID == null ) { throwUserQueryParseException( eqlSession, res, "alert_id", null, null ); } AlertManagerLocal local = null; long[] ret; try { // Get local reference. local = getAlertManagerLocal( eqlSession ); // Set mandatory attributes. local.setId( alertID.longValue() ); // Call Alert Manager ret = local.deleteAlerts(); } finally { try { if( local != null ) { local.remove(); } } catch( Exception ex ) {} } // Ok. int updated = ( ret != null ) ? ret.length : 0; if( logger.getLogger().isDebugEnabled() ) { logger.DEBUG( "Alert deletion operation completed. Removed " + updated + " records. Time(ms): " + ( System.currentTimeMillis() - time ) ); } return updated; } /* * No javadoc * @see EQLAgentStub#addSelectConstraint */ protected void addSelectConstraint( Object result, EQLReqField field, EQLObject value ) { // [ALB] constraints not supported } // ----------------------------------------------------- abstract methods /** * AlertManagerEJB caller. * @param eqlSession EQLSession * @param criteria search criteria value object * @return SortedMap search result * @throws EQLException */ abstract SortedMap callAlertManager( EQLSession eqlSession, AlertSelectorCriteria criteria ) throws EQLException; // ----------------------------------------------------- protected methods // // Build and add EQLResCell objects upon on <code>alert</code> to <code>record</code>. // protected void addCells( EQLSession eqlSession, EQLResRecord record, Entity entity, Alert alert, List columNames ) throws EQLException { AlertData alertData = alert.getData(); CompoundKey recordKey = ( alertData != null ) ? alertData.getRecordKey() : null; // 1) alert_id EQLResCell cell = getNumberCell( entity, "alert_id", new Long( alert.getAlertID() ) ); if( cell != null ) { record.addData( cell, columNames.indexOf( "alert_id" ) ); } // 2) creator_id cell = getNumberCell( entity, "creator_id", new Long( alert.getCreatorID() ) ); if( cell != null ) { record.addData( cell, columNames.indexOf( "creator_id" ) ); _addUserListCell( eqlSession, cell, alert.getCreatorID(), alert.getCreatorType() ); } // 3) creator_type cell = getNumberCell( entity, "creator_type", new Integer( alert.getCreatorType() ) ); if( cell != null ) { record.addData( cell, columNames.indexOf( "creator_type" ) ); } // 4) sender_id cell = getNumberCell( entity, "sender_id", new Long( alert.getSenderID() ) ); if( cell != null ) { record.addData( cell, columNames.indexOf( "sender_id" ) ); _addUserListCell( eqlSession, cell, alert.getSenderID(), alert.getSenderType() ); } // 5) sender_type cell = getNumberCell( entity, "sender_type", new Integer( alert.getSenderType() ) ); if( cell != null ) { record.addData( cell, columNames.indexOf( "sender_type" ) ); } // 6) message cell = getStringCell( entity, "message", alert.getMessage() ); if( cell != null ) { record.addData( cell, columNames.indexOf( "message" ) ); } // 7) severity cell = getNumberCell( entity, "severity", new Integer( alert.getSeverity() ) ); if( cell != null ) { record.addData( cell, columNames.indexOf( "severity" ) ); _addSeverityListCell( eqlSession, record, cell ); } // 8) dateposted cell = getDateCell( entity, "dateposted", alert.getDateposted() ); if( cell != null ) { record.addData( cell, columNames.indexOf( "dateposted" ) ); } // 9) recipient_id cell = getNumberCell( entity, "recipient_id", alert.getRecipientID() ); if( cell != null ) { record.addData( cell, columNames.indexOf( "recipient_id" ) ); if( alert.getRecipientID() != null ) { _addUserListCell( eqlSession, cell, alert.getRecipientID().longValue(), alert.getRecipientType().intValue() ); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -