Class DBTagStore

  • All Implemented Interfaces:
    TagConfigProvider, TagStore

    public class DBTagStore
    extends AbstractTagStore
    This class represents a DB tag storage mechanism. Right now it's written with the external legacy db in mind, in could be broken up into a proper class hierarchy that supported that along w/ the internal db and others. Also, it implements SQLTagErrorLogger and SQLTagValueLogger, though both of those could be broken out into separate classes.
    • Constructor Detail

      • DBTagStore

        public DBTagStore​(GatewayContext context,
                          java.lang.String managerName,
                          java.lang.String dataSource,
                          int pollRate,
                          int pollOverlap,
                          boolean isDriving,
                          java.lang.String driverName)
    • Method Detail

      • getBatchSize

        protected int getBatchSize()
        Returns the size that should be used for batching. If 0, batching won't be used.
      • getDriverName

        public java.lang.String getDriverName()
      • getDataSource

        public java.lang.String getDataSource()
      • setDataSource

        public void setDataSource​(java.lang.String value)
      • isDatasourceAvailable

        protected boolean isDatasourceAvailable()
      • isConfigPaused

        protected boolean isConfigPaused()
      • setConfigPaused

        protected void setConfigPaused​(boolean value)
      • setWriteHandler

        public void setWriteHandler​(DBWriteHandler writeHandler)
      • setConfigPoll

        public void setConfigPoll​(int value)
      • setPollOverlap

        public void setPollOverlap​(int value)
      • getLastUpdateDate

        protected java.sql.Timestamp getLastUpdateDate​(boolean overlapAdjusted)
      • setLastUpdateDate

        protected void setLastUpdateDate​(java.sql.Timestamp value)
      • getCurrentUpdateDate

        public java.sql.Timestamp getCurrentUpdateDate()
      • setCurrentUpdateDate

        public void setCurrentUpdateDate​(java.sql.Timestamp value)
      • addLeaseChangeListener

        public void addLeaseChangeListener​(LeaseEventListener listener)
      • removeLeaseChangeListener

        public void removeLeaseChangeListener​(LeaseEventListener listener)
      • notifyLeasesModified

        protected void notifyLeasesModified​(java.util.Collection<LeaseInfo> leases)
      • startup

        public void startup()
                     throws java.lang.Exception
        Start up the tag store, checks the tables and starts polling.
        Throws:
        java.lang.Exception
      • shutdown

        public void shutdown()
        Description copied from interface: TagStore
        Shuts down the tag store, finalizing any writes and preventing further operations.
      • getConnection

        protected SRConnection getConnection()
                                      throws java.sql.SQLException
        Returns a new connection. Used to return read_uncommitted, but now returns normal connection.

        11/3/2010 - I've changed the isolation level to read_uncommitted in hopes of reducing deadlock errors in SQL Server. The stuff we do here doesn't really need strong isolation anyhow- there's not really anything that gets rolled back (value updates are performed in a transaction, but only for performance, we may not even be necessary in Java). 3/31/2011 - Commented that out, since oracle doesn't seem to support it.

        Throws:
        java.sql.SQLException
      • getDatasource

        protected Datasource getDatasource()
      • getQuery

        protected java.lang.String getQuery​(java.lang.String key,
                                            java.lang.String rawQuery)
        Gets a quoted query from the cache, inserting it if not already there. If key is null, it won't be stored, just quoted and returned.
      • getSCName

        protected java.lang.String getSCName​(EntityId id)
      • configPoll

        protected final void configPoll()
      • internalConfigPoll

        protected void internalConfigPoll()
                                   throws java.lang.Exception
        This is where the work is done. It can be overridden by inheriting classes in order to do their own thing, but they should always call this super function first.
        Throws:
        java.lang.Exception
      • reloadTags

        public void reloadTags​(java.util.Set<EntityId> ids)
      • loadTags

        protected void loadTags​(java.util.List<TagStoreObject<TagConfig>> modifiedTags,
                                java.util.List<EntityId> deletedTags)
                         throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • loadTags

        protected void loadTags​(java.util.List<TagStoreObject<TagConfig>> modifiedTags,
                                java.util.List<EntityId> deletedTags,
                                boolean cfgLoad,
                                java.lang.Object param)
                         throws java.sql.SQLException
        Loads the tags from the db. It supports 2 load methodologies: by id, and by config change. The later is used if cfgLoad is true. Both methods take 1 param: for config, the last time, and for id, the tag id.
        Throws:
        java.sql.SQLException
      • loadTagsFromResultset

        protected void loadTagsFromResultset​(Dataset resultSet,
                                             java.util.List<TagStoreObject<TagConfig>> modifiedTags,
                                             java.util.List<EntityId> deletedTags,
                                             java.util.Map<java.lang.Integer,​TagDefinition> tagMap,
                                             java.util.Map<java.lang.Integer,​ComplexTagStoreObject> ownerLookup)
        This function does slightly different things based on the parameters. First, we load simple tags, and udt parents. In this case deleted tags will be marked in the passed in list, and udt parents will be put in the ownerLookup. On second pass, we'll be loading udt children. Deleted tags are ignored (deleted list should be null). In both cases, all tags are put in the tagLookup. Only in the first pass are tags put in the modified map.
      • loadMetaProperties

        protected void loadMetaProperties​(java.util.Map<java.lang.Integer,​TagDefinition> tagDefs)
                                   throws java.lang.Exception
        Throws:
        java.lang.Exception
      • loadAlarmStates

        protected void loadAlarmStates​(java.util.Map<java.lang.Integer,​TagDefinition> tagDefs,
                                       boolean cfgLoad,
                                       java.lang.Object param)
                                throws java.lang.Exception
        Throws:
        java.lang.Exception
      • loadPermissions

        protected void loadPermissions​(java.util.Map<java.lang.Integer,​TagDefinition> tagDefs,
                                       boolean cfgLoad,
                                       java.lang.Object param)
                                throws java.lang.Exception
        Throws:
        java.lang.Exception
      • loadEventScripts

        protected void loadEventScripts​(java.util.Map<java.lang.Integer,​TagDefinition> tagDefs,
                                        boolean cfgLoad,
                                        java.lang.Object param)
                                 throws java.lang.Exception
        Throws:
        java.lang.Exception
      • loadScanClasses

        protected void loadScanClasses​(java.util.List<TagStoreObject<ScanClass>> modifiedSCIs,
                                       java.util.List<EntityId> deletedSCIs)
                                throws java.sql.SQLException
        Returns a map of scan classes loaded from the database. Key = SCID.
        Throws:
        java.sql.SQLException
      • loadWriteQueueRequests

        protected java.util.List<DBWriteRequest> loadWriteQueueRequests()
                                                                 throws java.sql.SQLException
        Scans the write queue for write commands. Only called if the provider is Driving.
        Throws:
        java.sql.SQLException
      • saveWriteResults

        protected void saveWriteResults​(java.util.List<DBTagStore.DBWriteResult> results)
                                 throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getTagValueReadQuery

        protected java.lang.String getTagValueReadQuery()
      • loadTagValueChanges

        protected java.util.List<DBTagValue> loadTagValueChanges()
        This function reads the value changes for "external tags" (the definition of which depends on what mode we're in). It should be called periodically, normally from the configPoll function because it uses the same timestamp to track changes.
      • storeTagValues

        public void storeTagValues​(java.util.Collection<TagPropertyValue> tagVals)
        Description copied from interface: TagStore
        Writes the given values to the store. The "value" is not necessarily the "tag value"- it can be the value of any property of the tag. The tag store may not store all of the properties- it will only store what it believes is necessary.
      • internalStoreTagValues

        protected void internalStoreTagValues​(java.util.List<TagPropertyValue> tagVals)
        Stores the values. If tagVals is null, will attempt to write the valuesToWrite cache. *
      • cacheValuesToWrite

        protected void cacheValuesToWrite​(java.util.List<TagPropertyValue> values)
        NOTE: Only call after already having sychronized on valuesToWrite *
      • updateExternalScanclasses

        protected void updateExternalScanclasses()
        Reads the full scanclassinstance table and returns the rows as SCI objects.
      • addTags

        public void addTags​(TagPath parentFolder,
                            java.util.List<? extends Tag> tags)
                     throws java.lang.Exception
        Description copied from interface: TagStore
        Saves the specified tag definitions to the store. Should result in the listeners being notified about the addition/removal of tags. Will result in itemsAdded notification.
        Throws:
        java.lang.Exception
      • addTagsV2

        protected int addTagsV2​(TagPath parentFolder,
                                java.util.List<? extends Tag> tags)
                         throws java.lang.Exception
        Throws:
        java.lang.Exception
      • insertComplexTag

        protected void insertComplexTag​(java.lang.Integer ownerId,
                                        ComplexTag tag)
                                 throws java.lang.Exception
        Throws:
        java.lang.Exception
      • insertTags

        protected java.util.Map<java.lang.Integer,​GeneralTagConfig<Tag,​java.lang.String>> insertTags​(java.util.List<GeneralTagConfig<Tag,​java.lang.String>> tags,
                                                                                                                 java.lang.Integer ownerId)
                                                                                                          throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getIdsForNewTags

        protected java.util.Map<java.lang.String,​java.lang.Integer> getIdsForNewTags​(SRConnection conn)
                                                                                    throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • getTagPropValues

        protected java.util.Iterator<PropertyValue> getTagPropValues​(Tag tag)
      • modifyTags

        public void modifyTags​(TagDiff diff,
                               java.util.Collection<EntityId> tagIds)
        Description copied from interface: TagStore
        Applies the tagdiff to the given tags. Will result in itemsModified/itemsRemoved notification (removed if tag path changed)
      • modifyTagPaths

        public void modifyTagPaths​(java.util.List<EntityId> ids,
                                   java.util.List<TagPath> newPaths)
                            throws java.lang.Exception
        Description copied from interface: TagStore
        Writes new tag paths to the store. The entity ids must map 1-to-1 with the new tag paths. The new paths should be the full path of the tag, including the tag name. It will change the tag name as well, but won't check for collisions.
        Throws:
        java.lang.Exception
      • getMetaInsertStatement

        protected java.sql.PreparedStatement getMetaInsertStatement​(SRConnection conn)
                                                             throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • bulkInsertTagMeta

        protected void bulkInsertTagMeta​(BatchStatement stmt,
                                         int tagId,
                                         java.util.Iterator<PropertyValue> itr)
                                  throws java.lang.Exception
        Throws:
        java.lang.Exception
      • addPropertyToBatch

        protected void addPropertyToBatch​(int tagId,
                                          BatchStatement propStmt,
                                          java.lang.String name,
                                          java.lang.Object value,
                                          DataTypeClass dtype)
                                   throws java.lang.Exception
        Throws:
        java.lang.Exception
      • addPropertySetToBatch

        protected void addPropertySetToBatch​(int tagId,
                                             BatchStatement stmt,
                                             java.lang.String subPath,
                                             PropertySet props)
                                      throws java.lang.Exception
        Throws:
        java.lang.Exception
      • addPropertyToBatch

        protected void addPropertyToBatch​(int tagId,
                                          BatchStatement stmt,
                                          java.lang.String name,
                                          java.lang.Object value,
                                          DataTypeClass dtype,
                                          DataType type,
                                          java.lang.String subPath,
                                          boolean isCustom)
                                   throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • deleteTagPropertiesForUpdate

        protected void deleteTagPropertiesForUpdate​(java.util.Collection<EntityId> ids,
                                                    TagDiff diff)
                                             throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • deleteTagPropertiesForUpdate

        protected void deleteTagPropertiesForUpdate​(EntityId id,
                                                    TagDiff diff,
                                                    BatchStatement stmt)
                                             throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • deletePropertiesForUpdate

        protected void deletePropertiesForUpdate​(EntityId id,
                                                 PropertySet ps,
                                                 java.lang.String overridePath,
                                                 BatchStatement stmt)
                                          throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • deletePropertyForUpdate

        protected void deletePropertyForUpdate​(EntityId owner,
                                               Property<?> prop,
                                               java.lang.String overridePath,
                                               BatchStatement stmt)
                                        throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • getAlarmStateStatement

        protected java.sql.PreparedStatement getAlarmStateStatement​(SRConnection conn)
                                                             throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • modifyComplexTag

        protected void modifyComplexTag​(EntityId tagId,
                                        ComplexTagDiff diff)
                                 throws java.lang.Exception
        Throws:
        java.lang.Exception
      • storeTagEventScripts

        protected void storeTagEventScripts​(SRConnection conn,
                                            java.lang.Long tagId,
                                            TagEventScripts scripts,
                                            boolean tryUpdate)
                                     throws java.lang.Exception
        Throws:
        java.lang.Exception
      • storeAlarmConfiguration

        protected void storeAlarmConfiguration​(SRConnection conn,
                                               java.lang.Long tagId,
                                               AlarmConfiguration config,
                                               java.lang.String overridePath,
                                               boolean tryUpdate)
                                        throws java.lang.Exception
        Throws:
        java.lang.Exception
      • insertTagEventScripts

        protected void insertTagEventScripts​(SRConnection conn,
                                             java.lang.Long tagId,
                                             TagEventScripts scripts,
                                             java.lang.String overridePath)
      • insertTagAlarmConfig

        protected void insertTagAlarmConfig​(SRConnection conn,
                                            java.lang.Long tagId,
                                            java.util.List<AlarmDefinition> config,
                                            java.lang.String overridePath)
                                     throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • insertTagAlarmProperties

        protected void insertTagAlarmProperties​(SRConnection conn,
                                                java.util.Map<java.lang.Long,​AlarmDefinition> defs)
                                         throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • getPermissionStatment

        protected java.sql.PreparedStatement getPermissionStatment​(SRConnection conn)
                                                            throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • writePermissions

        protected void writePermissions​(SRConnection conn,
                                        int tagid,
                                        TagPermissionsModel permissions,
                                        boolean deletePrevious)
                                 throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • writePermissions

        protected void writePermissions​(BatchStatement stmt,
                                        int tagid,
                                        TagPermissionsModel permissions)
                                 throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • findScid

        protected int findScid​(java.lang.String scName)
      • deleteTags

        public void deleteTags​(java.util.Collection<EntityId> tagIds)
                        throws java.lang.Exception
        Description copied from interface: TagStore
        Deletes the given tags from the store. Will result in the itemsRemoved notification.
        Specified by:
        deleteTags in interface TagStore
        Overrides:
        deleteTags in class AbstractTagStore
        Throws:
        java.lang.Exception
      • addScanClasses

        public void addScanClasses​(java.util.Collection<ScanClass> scanclasses)
                            throws java.lang.Exception
        Description copied from interface: TagStore
        Same as with tags, but for scanclasses.
        Throws:
        java.lang.Exception
      • modifyScanClass

        public void modifyScanClass​(EntityId scanclassId,
                                    ScanClass newDefinition)
                             throws java.lang.Exception
        Throws:
        java.lang.Exception
      • deleteScanClasses

        public void deleteScanClasses​(java.util.Collection<EntityId> scanclassIds)
      • getRequiredTables

        protected java.util.List<DBTableSchema> getRequiredTables()
      • runTableVerification

        protected void runTableVerification()
      • verifyTables

        protected final void verifyTables​(boolean createIfAbsent)
                                   throws java.lang.Exception
        Throws:
        java.lang.Exception
      • checkVersion

        protected void checkVersion()
      • setDriverInformation

        public void setDriverInformation​(java.lang.String ipAddress,
                                         int port)
      • updateDriverTable

        protected void updateDriverTable()
        This function updates the driver information in the database, inserting it if necessary.
      • loadLeaseChanges

        protected java.util.List<LeaseInfo> loadLeaseChanges()
      • processScanClassLeases

        protected void processScanClassLeases()
        ************************** START - Scanclass leasing ***************************
      • updateLeasedScanclassSet

        public void updateLeasedScanclassSet​(java.util.Collection<EntityId> scanClasseIds)
      • readDriverInformation

        public java.util.Map<java.lang.String,​DriverInformation> readDriverInformation()
      • cleanupTables

        protected void cleanupTables()
                              throws java.lang.Exception
        Cleans up tables that might have "abandoned" rows. Throws an exception, but only for serious connection level stuff. Otherwise captures and logs errors in the individual operations.
        Throws:
        java.lang.Exception
      • upgradeAlerts

        protected void upgradeAlerts()
                              throws java.lang.Exception
        Throws:
        java.lang.Exception
      • writeAgainstExternalProvider

        public void writeAgainstExternalProvider​(java.util.List<DBTagStore.TagWriteTask> tasks)
        ************************** START - Value write code ***************************
      • readExternalWriteResults

        protected void readExternalWriteResults()
        This function looks for results of write tasks that we've entered.