Class JdbcGeneralNodeDatumDao

java.lang.Object
org.springframework.dao.support.DaoSupport
org.springframework.jdbc.core.support.JdbcDaoSupport
net.solarnetwork.node.dao.jdbc.AbstractJdbcDao<net.solarnetwork.node.domain.datum.NodeDatum>
net.solarnetwork.node.dao.jdbc.general.JdbcGeneralNodeDatumDao
All Implemented Interfaces:
net.solarnetwork.node.dao.DatumDao, JdbcDao, net.solarnetwork.service.PingTest, net.solarnetwork.settings.SettingSpecifierProvider, org.springframework.beans.factory.InitializingBean

public class JdbcGeneralNodeDatumDao extends AbstractJdbcDao<net.solarnetwork.node.domain.datum.NodeDatum> implements net.solarnetwork.node.dao.DatumDao, net.solarnetwork.settings.SettingSpecifierProvider, net.solarnetwork.service.PingTest
JDBC-based implementation of DatumDao for NodeDatum domain objects.
Version:
2.2
Author:
matt
  • Field Details

    • DEFAULT_TABLES_VERSION

      public static final int DEFAULT_TABLES_VERSION
      The default tables version.
      See Also:
    • MAX_SAMPLES_JSON_LENGTH

      public static final int MAX_SAMPLES_JSON_LENGTH
      The maximum allowed length of a datum samples when encoded as JSON.
      See Also:
    • TABLE_GENERAL_NODE_DATUM

      public static final String TABLE_GENERAL_NODE_DATUM
      The table name for datum.
      See Also:
    • DEFAULT_INIT_SQL

      public static final String DEFAULT_INIT_SQL
      The default classpath Resource for the initSqlResource.
      See Also:
    • DEFAULT_SQL_GET_TABLES_VERSION

      public static final String DEFAULT_SQL_GET_TABLES_VERSION
      The default value for the sqlGetTablesVersion property.
      See Also:
    • DEFAULT_MAX_FETCH_FOR_UPLOAD

      public static final int DEFAULT_MAX_FETCH_FOR_UPLOAD
      The default value for the maxFetchForUpload property.
      See Also:
    • DEFAULT_MAX_COUNT_PING_FAIL

      public static final int DEFAULT_MAX_COUNT_PING_FAIL
      The maxCountPingFail property default value.
      Since:
      2.1
      See Also:
    • SQL_RESOURCE_INSERT

      public static final String SQL_RESOURCE_INSERT
      The SQL resource to insert.
      See Also:
    • SQL_RESOURCE_DELETE_OLD

      public static final String SQL_RESOURCE_DELETE_OLD
      The SQL resource to delete old uploaded rows.
      See Also:
    • SQL_RESOURCE_FIND_FOR_UPLOAD

      public static final String SQL_RESOURCE_FIND_FOR_UPLOAD
      The SQL resource to find rows needing upload.
      See Also:
    • SQL_RESOURCE_FIND_FOR_PRIMARY_KEY

      public static final String SQL_RESOURCE_FIND_FOR_PRIMARY_KEY
      The SQL resource to fetch by primary key.
      See Also:
    • SQL_RESOURCE_UPDATE_UPLOADED

      public static final String SQL_RESOURCE_UPDATE_UPLOADED
      The SQL resource to update the upload date.
      See Also:
    • SQL_RESOURCE_UPDATE_DATA

      public static final String SQL_RESOURCE_UPDATE_DATA
      The SQL resource to update data.
      See Also:
    • SQL_RESOURCE_COUNT

      public static final String SQL_RESOURCE_COUNT
      The SQL resource to count.
      See Also:
    • LOG_SOURCE_ID

      public static final String LOG_SOURCE_ID
      A source ID for log messages posted as datum.
      Since:
      2.2
      See Also:
    • LOG_SOURCE_ID_PREFIX

      public static final String LOG_SOURCE_ID_PREFIX
      A source ID prefix for log messages posted as datum.
      Since:
      2.2
      See Also:
  • Constructor Details

    • JdbcGeneralNodeDatumDao

      public JdbcGeneralNodeDatumDao()
      Default constructor.
  • Method Details

    • storeDatum

      @Transactional(readOnly=false, propagation=REQUIRED, noRollbackFor=org.springframework.dao.DuplicateKeyException.class) public void storeDatum(net.solarnetwork.node.domain.datum.NodeDatum datum)
      Specified by:
      storeDatum in interface net.solarnetwork.node.dao.DatumDao
    • setUpdateStatementValues

      protected void setUpdateStatementValues(net.solarnetwork.node.domain.datum.NodeDatum datum, PreparedStatement ps) throws SQLException
      Description copied from class: AbstractJdbcDao
      Set PreparedStatement values for updating a domain object.

      Called from AbstractJdbcDao.updateDomainObject(Object, String) to persist changed values of a domain object.

      This implementation does not do anything. Extending classes should override this and set values on the PreparedStatement object as needed to persist the domain object.

      Overrides:
      setUpdateStatementValues in class AbstractJdbcDao<net.solarnetwork.node.domain.datum.NodeDatum>
      Parameters:
      datum - the domain object to persist
      ps - the PreparedStatement to persist with
      Throws:
      SQLException - if any SQL error occurs
    • setDatumUploaded

      @Transactional(readOnly=false, propagation=REQUIRED) public void setDatumUploaded(net.solarnetwork.node.domain.datum.NodeDatum datum, Instant date, String destination, String trackingId)
      Specified by:
      setDatumUploaded in interface net.solarnetwork.node.dao.DatumDao
    • deleteUploadedDataOlderThan

      @Transactional(readOnly=false, propagation=REQUIRED) public int deleteUploadedDataOlderThan(int hours)
      Specified by:
      deleteUploadedDataOlderThan in interface net.solarnetwork.node.dao.DatumDao
    • getDatumNotUploaded

      @Transactional(readOnly=true, propagation=REQUIRED) public List<net.solarnetwork.node.domain.datum.NodeDatum> getDatumNotUploaded(String destination)
      Specified by:
      getDatumNotUploaded in interface net.solarnetwork.node.dao.DatumDao
    • setStoreStatementValues

      protected void setStoreStatementValues(net.solarnetwork.node.domain.datum.NodeDatum datum, PreparedStatement ps) throws SQLException
      Description copied from class: AbstractJdbcDao
      Set PreparedStatement values for storing a domain object.

      Called from AbstractJdbcDao.storeDomainObject(Object, String) and AbstractJdbcDao.storeDomainObjectWithoutAutogeneratedKeys(Object, String) to persist values of a domain object.

      This implementation does not do anything. Extending classes should override this and set values on the PreparedStatement object as needed to persist the domain object.

      Overrides:
      setStoreStatementValues in class AbstractJdbcDao<net.solarnetwork.node.domain.datum.NodeDatum>
      Parameters:
      datum - the domain object to persist
      ps - the PreparedStatement to persist with
      Throws:
      SQLException - if any SQL error occurs
    • getObjectMapper

      public com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
      Get the object mapper.
      Returns:
      the mapper
    • setObjectMapper

      public void setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
      Set the object mapper.
      Parameters:
      objectMapper - the mapper to set
    • deleteUploadedDataOlderThanHours

      protected int deleteUploadedDataOlderThanHours(int hours)
      Execute a SQL update to delete data that has already been "uploaded" and is older than a specified number of hours.

      This executes SQL from the sqlDeleteOld property, setting a single timestamp parameter as the current time minus hours hours. The general idea is for the SQL to join to some "upload" table to find the rows in the "datum" table that have been uploaded and are older than the specified number of hours. For example:

       DELETE FROM solarnode.sn_some_datum p WHERE p.id IN 
       (SELECT pd.id FROM solarnode.sn_some_datum pd 
       INNER JOIN solarnode.sn_some_datum_upload u 
       ON u.power_datum_id = pd.id WHERE pd.created < ?)
       
      Parameters:
      hours - the number of hours hold to delete
      Returns:
      the number of rows deleted
    • findDatumNotUploaded

      protected List<net.solarnetwork.node.domain.datum.NodeDatum> findDatumNotUploaded(org.springframework.jdbc.core.RowMapper<net.solarnetwork.node.domain.datum.NodeDatum> rowMapper)
      Find datum entities that have not been uploaded to a specific destination.

      This executes SQL from the findForUploadSql property. It uses the maxFetchForUpload property to limit the number of rows returned, so the call may not return all rows available from the database (this is to conserve memory and process the data in small batches).

      Parameters:
      rowMapper - a RowMapper implementation to instantiate entities from found rows
      Returns:
      the matching rows, never null
    • findDatum

      protected List<net.solarnetwork.node.domain.datum.NodeDatum> findDatum(String sqlResource, org.springframework.jdbc.core.PreparedStatementSetter setter, org.springframework.jdbc.core.RowMapper<net.solarnetwork.node.domain.datum.NodeDatum> rowMapper)
      Find datum entities.
      Parameters:
      sqlResource - The name of the SQL resource to use. See AbstractJdbcDao.getSqlResource(String)
      setter - A prepared statement setter
      rowMapper - a RowMapper implementation to instantiate entities from found rows
      Returns:
      the matching rows, never null
      Since:
      1.2
    • preparedStatementSetterForPrimaryKey

      protected org.springframework.jdbc.core.PreparedStatementSetter preparedStatementSetterForPrimaryKey(Instant created, String sourceId)
      Create a PreparedStatementSetter that sets the primary key values on a statement.
      Parameters:
      created - The created date of the datum.
      sourceId - The source ID of the datum.
      Returns:
      The setter instance.
      Since:
      1.2
      See Also:
    • storeDomainObject

      protected void storeDomainObject(net.solarnetwork.node.domain.datum.NodeDatum datum)
      Store a new domain object using the SQL_RESOURCE_INSERT SQL.

      If isIgnoreMockData() returns true and datum is an instance of Mock then this method will not persist the object and will simply return -1.

      Parameters:
      datum - the datum to persist
    • updateDomainObject

      protected int updateDomainObject(net.solarnetwork.node.domain.datum.NodeDatum datum, String sqlUpdate)
      Update a domain object.
      Overrides:
      updateDomainObject in class AbstractJdbcDao<net.solarnetwork.node.domain.datum.NodeDatum>
      Parameters:
      datum - the domain object to update
      sqlUpdate - the SQL to persist the object with
      Returns:
      the updated row count
      Since:
      1.3
    • updateDatumUpload

      protected void updateDatumUpload(net.solarnetwork.node.domain.datum.NodeDatum datum, Instant timestamp)
      Mark a Datum as uploaded.

      This method will call updateDatumUpload(Instant, Object, Instant) passing in Datum.getTimestamp(), Datum.getSourceId(), and timestamp.

      Parameters:
      datum - the datum that was uploaded
      timestamp - the date the upload happened
    • updateDatumUpload

      protected int updateDatumUpload(Instant created, Object id, Instant timestamp)
      Mark a Datum as uploaded.

      This method will execute the SQL_RESOURCE_UPDATE_UPLOADED SQL setting the following parameters:

      1. Timestamp parameter based on timestamp
      2. Timestamp parameter based on created
      3. Object parameter based on id
      Parameters:
      created - the date the object was created
      id - the object's source or location ID
      timestamp - the date the upload happened
      Returns:
      the number of updated rows
    • postDatumStoredEvent

      protected final void postDatumStoredEvent(net.solarnetwork.node.domain.datum.NodeDatum datum)
      Post an Event for the DatumDao.EVENT_TOPIC_DATUM_STORED topic.
      Parameters:
      datum - the datum that was stored
      Since:
      1.3
    • createDatumStoredEvent

      protected org.osgi.service.event.Event createDatumStoredEvent(net.solarnetwork.node.domain.datum.NodeDatum datum)
      Create a new DatumDao.EVENT_TOPIC_DATUM_STORED Event object out of a Datum.

      This method uses the result of Datum.asSimpleMap() as the event properties.

      Parameters:
      datum - the datum to create the event for
      Returns:
      the new Event instance
      Since:
      1.3
    • getPingTestId

      public String getPingTestId()
      Specified by:
      getPingTestId in interface net.solarnetwork.service.PingTest
    • getPingTestName

      public String getPingTestName()
      Specified by:
      getPingTestName in interface net.solarnetwork.service.PingTest
    • getPingTestMaximumExecutionMilliseconds

      public long getPingTestMaximumExecutionMilliseconds()
      Specified by:
      getPingTestMaximumExecutionMilliseconds in interface net.solarnetwork.service.PingTest
    • performPingTest

      public net.solarnetwork.service.PingTest.Result performPingTest() throws Exception
      Specified by:
      performPingTest in interface net.solarnetwork.service.PingTest
      Throws:
      Exception
    • getSettingUid

      public String getSettingUid()
      Specified by:
      getSettingUid in interface net.solarnetwork.settings.SettingSpecifierProvider
    • getDisplayName

      public String getDisplayName()
      Specified by:
      getDisplayName in interface net.solarnetwork.settings.SettingSpecifierProvider
    • getSettingSpecifiers

      public List<net.solarnetwork.settings.SettingSpecifier> getSettingSpecifiers()
      Specified by:
      getSettingSpecifiers in interface net.solarnetwork.settings.SettingSpecifierProvider
    • getMaxFetchForUpload

      public int getMaxFetchForUpload()
      Get the maximum number of datum to fetch for upload at one time.
      Returns:
      the maximum number of datum rows to fetch
    • setMaxFetchForUpload

      public void setMaxFetchForUpload(int maxFetchForUpload)
      The maximum number of rows to return in the findDatumNotUploaded(RowMapper) method.

      Defaults to DEFAULT_MAX_FETCH_FOR_UPLOAD.

      Parameters:
      maxFetchForUpload - the maximum upload value
    • isIgnoreMockData

      public boolean isIgnoreMockData()
      Get the flag to ignore mock data.
      Returns:
      true to not store any mock data
    • setIgnoreMockData

      public void setIgnoreMockData(boolean ignoreMockData)
      Set a flag to not actually store any domain object that implements the Mock interface.

      This defaults to true, but during development it can be useful to configure this as false for testing.

      Parameters:
      ignoreMockData - the ignore mock data value
    • getMaxCountPingFail

      public int getMaxCountPingFail()
      Get the maximum number of messages to store before failing ping tests.
      Returns:
      the maximum count, or 0 to disable the test; defaults to DEFAULT_MAX_COUNT_PING_FAIL
      Since:
      2.1
    • setMaxCountPingFail

      public void setMaxCountPingFail(int maxCountPingFail)
      Set the maximum number of messages to store before failing ping tests.
      Parameters:
      maxCountPingFail - the maximum count, or 0 to disable the test
      Since:
      2.1