Class JdbcGeneralNodeDatumDao
- All Implemented Interfaces:
net.solarnetwork.node.dao.DatumDao,JdbcDao,net.solarnetwork.service.PingTest,net.solarnetwork.settings.SettingSpecifierProvider,org.springframework.beans.factory.InitializingBean
DatumDao for
NodeDatum domain objects.- Version:
- 2.2
- Author:
- matt
-
Nested Class Summary
Nested classes/interfaces inherited from interface net.solarnetwork.service.PingTest
net.solarnetwork.service.PingTest.Result -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe default classpath Resource for theinitSqlResource.static final intThemaxCountPingFailproperty default value.static final intThe default value for themaxFetchForUploadproperty.static final StringThe default value for thesqlGetTablesVersionproperty.static final intThe default tables version.static final StringA source ID for log messages posted as datum.static final StringA source ID prefix for log messages posted as datum.static final intThe maximum allowed length of a datum samples when encoded as JSON.static final StringThe SQL resource to count.static final StringThe SQL resource to delete old uploaded rows.static final StringThe SQL resource to fetch by primary key.static final StringThe SQL resource to find rows needing upload.static final StringThe SQL resource to insert.static final StringThe SQL resource to update data.static final StringThe SQL resource to update the upload date.static final StringThe table name for datum.Fields inherited from class net.solarnetwork.node.dao.jdbc.AbstractJdbcDao
logFields inherited from class org.springframework.dao.support.DaoSupport
loggerFields inherited from interface net.solarnetwork.node.dao.DatumDao
EVENT_TOPIC_DATUM_STORED -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected org.osgi.service.event.EventcreateDatumStoredEvent(net.solarnetwork.node.domain.datum.NodeDatum datum) Create a newDatumDao.EVENT_TOPIC_DATUM_STOREDEventobject out of aDatum.intdeleteUploadedDataOlderThan(int hours) protected intdeleteUploadedDataOlderThanHours(int hours) Execute a SQL update to delete data that has already been "uploaded" and is older than a specified number of hours.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.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.List<net.solarnetwork.node.domain.datum.NodeDatum>getDatumNotUploaded(String destination) intGet the maximum number of messages to store before failing ping tests.intGet the maximum number of datum to fetch for upload at one time.com.fasterxml.jackson.databind.ObjectMapperGet the object mapper.longList<net.solarnetwork.settings.SettingSpecifier>booleanGet the flag to ignore mock data.net.solarnetwork.service.PingTest.Resultprotected final voidpostDatumStoredEvent(net.solarnetwork.node.domain.datum.NodeDatum datum) Post anEventfor theDatumDao.EVENT_TOPIC_DATUM_STOREDtopic.protected org.springframework.jdbc.core.PreparedStatementSetterpreparedStatementSetterForPrimaryKey(Instant created, String sourceId) Create aPreparedStatementSetterthat sets the primary key values on a statement.voidsetDatumUploaded(net.solarnetwork.node.domain.datum.NodeDatum datum, Instant date, String destination, String trackingId) voidsetIgnoreMockData(boolean ignoreMockData) Set a flag to not actually store any domain object that implements theMockinterface.voidsetMaxCountPingFail(int maxCountPingFail) Set the maximum number of messages to store before failing ping tests.voidsetMaxFetchForUpload(int maxFetchForUpload) The maximum number of rows to return in thefindDatumNotUploaded(RowMapper)method.voidsetObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper) Set the object mapper.protected voidsetStoreStatementValues(net.solarnetwork.node.domain.datum.NodeDatum datum, PreparedStatement ps) SetPreparedStatementvalues for storing a domain object.protected voidsetUpdateStatementValues(net.solarnetwork.node.domain.datum.NodeDatum datum, PreparedStatement ps) SetPreparedStatementvalues for updating a domain object.voidstoreDatum(net.solarnetwork.node.domain.datum.NodeDatum datum) protected voidstoreDomainObject(net.solarnetwork.node.domain.datum.NodeDatum datum) Store a new domain object using theSQL_RESOURCE_INSERTSQL.protected intupdateDatumUpload(Instant created, Object id, Instant timestamp) Mark a Datum as uploaded.protected voidupdateDatumUpload(net.solarnetwork.node.domain.datum.NodeDatum datum, Instant timestamp) Mark a Datum as uploaded.protected intupdateDomainObject(net.solarnetwork.node.domain.datum.NodeDatum datum, String sqlUpdate) Update a domain object.Methods inherited from class net.solarnetwork.node.dao.jdbc.AbstractJdbcDao
getBatchSqlResource, getEventAdmin, getInitSqlResource, getMessageSource, getSchemaName, getSqlForUpdateSuffix, getSqlGetTablesVersion, getSqlResource, getSqlResource, getSqlResourcePrefix, getTableName, getTableNames, getTablesVersion, init, insertDomainObject, isUseAutogeneratedKeys, postEvent, schemaExists, setEventAdmin, setInitSqlResource, setMessageSource, setSchemaName, setSqlForUpdateSuffix, setSqlGetTablesVersion, setSqlResourcePrefix, setTableName, setTablesVersion, setUseAutogeneratedKeys, storeDomainObject, storeDomainObjectWithoutAutogeneratedKeys, tableExists, upgradeTablesVersion, verifyDatabaseExistsMethods inherited from class org.springframework.jdbc.core.support.JdbcDaoSupport
checkDaoConfig, createJdbcTemplate, getConnection, getDataSource, getExceptionTranslator, getJdbcTemplate, initTemplateConfig, releaseConnection, setDataSource, setJdbcTemplateMethods inherited from class org.springframework.dao.support.DaoSupport
afterPropertiesSet, initDaoMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface net.solarnetwork.settings.SettingSpecifierProvider
getMessageSource, templateSettingSpecifiers
-
Field Details
-
DEFAULT_TABLES_VERSION
public static final int DEFAULT_TABLES_VERSIONThe default tables version.- See Also:
-
MAX_SAMPLES_JSON_LENGTH
public static final int MAX_SAMPLES_JSON_LENGTHThe maximum allowed length of a datum samples when encoded as JSON.- See Also:
-
TABLE_GENERAL_NODE_DATUM
The table name for datum.- See Also:
-
DEFAULT_INIT_SQL
The default classpath Resource for theinitSqlResource.- See Also:
-
DEFAULT_SQL_GET_TABLES_VERSION
The default value for thesqlGetTablesVersionproperty.- See Also:
-
DEFAULT_MAX_FETCH_FOR_UPLOAD
public static final int DEFAULT_MAX_FETCH_FOR_UPLOADThe default value for themaxFetchForUploadproperty.- See Also:
-
DEFAULT_MAX_COUNT_PING_FAIL
public static final int DEFAULT_MAX_COUNT_PING_FAILThemaxCountPingFailproperty default value.- Since:
- 2.1
- See Also:
-
SQL_RESOURCE_INSERT
The SQL resource to insert.- See Also:
-
SQL_RESOURCE_DELETE_OLD
The SQL resource to delete old uploaded rows.- See Also:
-
SQL_RESOURCE_FIND_FOR_UPLOAD
The SQL resource to find rows needing upload.- See Also:
-
SQL_RESOURCE_FIND_FOR_PRIMARY_KEY
The SQL resource to fetch by primary key.- See Also:
-
SQL_RESOURCE_UPDATE_UPLOADED
The SQL resource to update the upload date.- See Also:
-
SQL_RESOURCE_UPDATE_DATA
The SQL resource to update data.- See Also:
-
SQL_RESOURCE_COUNT
The SQL resource to count.- See Also:
-
LOG_SOURCE_ID
A source ID for log messages posted as datum.- Since:
- 2.2
- See Also:
-
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:
storeDatumin interfacenet.solarnetwork.node.dao.DatumDao
-
setUpdateStatementValues
protected void setUpdateStatementValues(net.solarnetwork.node.domain.datum.NodeDatum datum, PreparedStatement ps) throws SQLException Description copied from class:AbstractJdbcDaoSetPreparedStatementvalues 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
PreparedStatementobject as needed to persist the domain object.- Overrides:
setUpdateStatementValuesin classAbstractJdbcDao<net.solarnetwork.node.domain.datum.NodeDatum>- Parameters:
datum- the domain object to persistps- 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:
setDatumUploadedin interfacenet.solarnetwork.node.dao.DatumDao
-
deleteUploadedDataOlderThan
@Transactional(readOnly=false, propagation=REQUIRED) public int deleteUploadedDataOlderThan(int hours) - Specified by:
deleteUploadedDataOlderThanin interfacenet.solarnetwork.node.dao.DatumDao
-
getDatumNotUploaded
@Transactional(readOnly=true, propagation=REQUIRED) public List<net.solarnetwork.node.domain.datum.NodeDatum> getDatumNotUploaded(String destination) - Specified by:
getDatumNotUploadedin interfacenet.solarnetwork.node.dao.DatumDao
-
setStoreStatementValues
protected void setStoreStatementValues(net.solarnetwork.node.domain.datum.NodeDatum datum, PreparedStatement ps) throws SQLException Description copied from class:AbstractJdbcDaoSetPreparedStatementvalues for storing a domain object.Called from
AbstractJdbcDao.storeDomainObject(Object, String)andAbstractJdbcDao.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
PreparedStatementobject as needed to persist the domain object.- Overrides:
setStoreStatementValuesin classAbstractJdbcDao<net.solarnetwork.node.domain.datum.NodeDatum>- Parameters:
datum- the domain object to persistps- 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
sqlDeleteOldproperty, setting a single timestamp parameter as the current time minushourshours. 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
findForUploadSqlproperty. It uses themaxFetchForUploadproperty 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- aRowMapperimplementation 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. SeeAbstractJdbcDao.getSqlResource(String)setter- A prepared statement setterrowMapper- aRowMapperimplementation 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 aPreparedStatementSetterthat 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 theSQL_RESOURCE_INSERTSQL.If
isIgnoreMockData()returns true anddatumis an instance ofMockthen 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:
updateDomainObjectin classAbstractJdbcDao<net.solarnetwork.node.domain.datum.NodeDatum>- Parameters:
datum- the domain object to updatesqlUpdate- 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 inDatum.getTimestamp(),Datum.getSourceId(), andtimestamp.- Parameters:
datum- the datum that was uploadedtimestamp- the date the upload happened
-
updateDatumUpload
Mark a Datum as uploaded.This method will execute the
SQL_RESOURCE_UPDATE_UPLOADEDSQL setting the following parameters:- Timestamp parameter based on
timestamp - Timestamp parameter based on
created - Object parameter based on
id
- Parameters:
created- the date the object was createdid- the object's source or location IDtimestamp- the date the upload happened- Returns:
- the number of updated rows
- Timestamp parameter based on
-
postDatumStoredEvent
protected final void postDatumStoredEvent(net.solarnetwork.node.domain.datum.NodeDatum datum) Post anEventfor theDatumDao.EVENT_TOPIC_DATUM_STOREDtopic.- 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 newDatumDao.EVENT_TOPIC_DATUM_STOREDEventobject out of aDatum.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
- Specified by:
getPingTestIdin interfacenet.solarnetwork.service.PingTest
-
getPingTestName
- Specified by:
getPingTestNamein interfacenet.solarnetwork.service.PingTest
-
getPingTestMaximumExecutionMilliseconds
public long getPingTestMaximumExecutionMilliseconds()- Specified by:
getPingTestMaximumExecutionMillisecondsin interfacenet.solarnetwork.service.PingTest
-
performPingTest
- Specified by:
performPingTestin interfacenet.solarnetwork.service.PingTest- Throws:
Exception
-
getSettingUid
- Specified by:
getSettingUidin interfacenet.solarnetwork.settings.SettingSpecifierProvider
-
getDisplayName
- Specified by:
getDisplayNamein interfacenet.solarnetwork.settings.SettingSpecifierProvider
-
getSettingSpecifiers
- Specified by:
getSettingSpecifiersin interfacenet.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 thefindDatumNotUploaded(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 theMockinterface.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
-