mirror of
https://github.com/nsacyber/HIRS.git
synced 2025-03-10 14:34:27 +00:00
Continued changes remove and updating return types and parameter types
This commit is contained in:
parent
ebc903c78c
commit
51b9251e3f
@ -2,7 +2,6 @@ package hirs.attestationca.configuration;
|
|||||||
|
|
||||||
import hirs.persist.DBDeviceGroupManager;
|
import hirs.persist.DBDeviceGroupManager;
|
||||||
import hirs.persist.DBDeviceManager;
|
import hirs.persist.DBDeviceManager;
|
||||||
import hirs.persist.DBReferenceDigestManager;
|
|
||||||
import hirs.persist.DBReferenceEventManager;
|
import hirs.persist.DBReferenceEventManager;
|
||||||
import hirs.persist.DBReferenceManifestManager;
|
import hirs.persist.DBReferenceManifestManager;
|
||||||
import hirs.persist.DeviceGroupManager;
|
import hirs.persist.DeviceGroupManager;
|
||||||
|
@ -13,7 +13,6 @@ import hirs.data.persist.SupportReferenceManifest;
|
|||||||
import hirs.data.persist.certificate.Certificate;
|
import hirs.data.persist.certificate.Certificate;
|
||||||
import hirs.persist.CriteriaModifier;
|
import hirs.persist.CriteriaModifier;
|
||||||
import hirs.persist.DBManagerException;
|
import hirs.persist.DBManagerException;
|
||||||
import hirs.persist.DBReferenceDigestManager;
|
|
||||||
import hirs.persist.DBReferenceEventManager;
|
import hirs.persist.DBReferenceEventManager;
|
||||||
import hirs.persist.DBReferenceManifestManager;
|
import hirs.persist.DBReferenceManifestManager;
|
||||||
import hirs.persist.ReferenceDigestManager;
|
import hirs.persist.ReferenceDigestManager;
|
||||||
|
@ -9,7 +9,6 @@ import hirs.persist.DBDeviceGroupManager;
|
|||||||
import hirs.persist.DBDeviceManager;
|
import hirs.persist.DBDeviceManager;
|
||||||
import hirs.persist.DBManager;
|
import hirs.persist.DBManager;
|
||||||
import hirs.persist.DBPolicyManager;
|
import hirs.persist.DBPolicyManager;
|
||||||
import hirs.persist.DBReferenceDigestManager;
|
|
||||||
import hirs.persist.DBReferenceEventManager;
|
import hirs.persist.DBReferenceEventManager;
|
||||||
import hirs.persist.DBReferenceManifestManager;
|
import hirs.persist.DBReferenceManifestManager;
|
||||||
import hirs.persist.DeviceGroupManager;
|
import hirs.persist.DeviceGroupManager;
|
||||||
|
@ -1,102 +0,0 @@
|
|||||||
package hirs;
|
|
||||||
|
|
||||||
import static org.apache.logging.log4j.LogManager.getLogger;
|
|
||||||
|
|
||||||
import hirs.data.persist.Report;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
|
||||||
|
|
||||||
import org.apache.logging.log4j.Logger;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <code>IMAReportRequest</code> is an immutable class and extends
|
|
||||||
* <code>ReportRequest</code> to specify parameters which define
|
|
||||||
* the composition of the IMA report to be generated by the client.
|
|
||||||
* For example, <code>IMAReportRequest</code> specifies whether a full IMA
|
|
||||||
* report should be generated and submitted for appraisal or whether a delta
|
|
||||||
* report (containing new entries since last full report) should be generated
|
|
||||||
* and submitted. If a delta report is being requested, then an index must be
|
|
||||||
* provided which is used to inform the client which subset of measurements
|
|
||||||
* to include in the report.
|
|
||||||
*/
|
|
||||||
public final class IMAReportRequest implements ReportRequest {
|
|
||||||
|
|
||||||
private static final Logger LOGGER = getLogger(IMAReportRequest.class);
|
|
||||||
|
|
||||||
@XmlElement
|
|
||||||
private final String bootcycleId;
|
|
||||||
|
|
||||||
@XmlElement
|
|
||||||
private final int index;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Default constructor required to enable marshalling and unmarshalling
|
|
||||||
* to/from xml.
|
|
||||||
*/
|
|
||||||
public IMAReportRequest() {
|
|
||||||
this(null, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new <code>IMAReportRequest</code>. The boot-cycle ID and index
|
|
||||||
* are set. The boot-cycle ID may be null to indicate that it is unknown by
|
|
||||||
* the appraiser and a full report should be sent. The index is the index of
|
|
||||||
* the new IMA record to send. An index of zero indicates a full report
|
|
||||||
* should be sent.
|
|
||||||
* <p>
|
|
||||||
* If the boot-cycle is null then the index must be zero. Otherwise an
|
|
||||||
* <code>IllegalArgumentException</code> is thrown.
|
|
||||||
*
|
|
||||||
* @param bootcycleId
|
|
||||||
* boot-cycle ID
|
|
||||||
* @param i
|
|
||||||
* value to use as IMA index
|
|
||||||
*/
|
|
||||||
public IMAReportRequest(final String bootcycleId, final int i) {
|
|
||||||
LOGGER.debug("Entering constructor");
|
|
||||||
if (i >= 0) {
|
|
||||||
index = i;
|
|
||||||
} else {
|
|
||||||
String msg = "Cannot init IMAReportRequest with index less than 0";
|
|
||||||
LOGGER.error(msg);
|
|
||||||
throw new IllegalArgumentException(msg);
|
|
||||||
}
|
|
||||||
if (bootcycleId == null && i != 0) {
|
|
||||||
String msg = "null boot-cycle ID must have index of 0";
|
|
||||||
LOGGER.error(msg);
|
|
||||||
throw new IllegalArgumentException(msg);
|
|
||||||
}
|
|
||||||
this.bootcycleId = bootcycleId;
|
|
||||||
LOGGER.debug("Exiting constructor");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Class<? extends Report> getReportType() {
|
|
||||||
return IMAReport.class;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the boot-cycle ID. This is the last boot-cycle ID that was saved
|
|
||||||
* by the appraiser. If this is null or different than the boot-cycle ID for
|
|
||||||
* the collector then a full report shall be sent.
|
|
||||||
*
|
|
||||||
* @return boot-cycle ID
|
|
||||||
*/
|
|
||||||
public String getBootcycleId() {
|
|
||||||
return bootcycleId;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Method returns the IMA index.
|
|
||||||
* @return index used to specify start location of measurement entries to
|
|
||||||
* include in IMA report
|
|
||||||
*/
|
|
||||||
public int getIMAIndex() {
|
|
||||||
return index;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return String.format("(%s, %d)", bootcycleId, index);
|
|
||||||
}
|
|
||||||
}
|
|
@ -15,7 +15,7 @@ import javax.xml.bind.annotation.XmlTransient;
|
|||||||
* client and submitted to the <code>Appraiser</code> for processing.
|
* client and submitted to the <code>Appraiser</code> for processing.
|
||||||
*/
|
*/
|
||||||
@XmlTransient
|
@XmlTransient
|
||||||
@XmlSeeAlso(value = { IntegrityReportRequest.class, TPMReportRequest.class, IMAReportRequest.class,
|
@XmlSeeAlso(value = { IntegrityReportRequest.class, TPMReportRequest.class,
|
||||||
DeviceInfoReportRequest.class })
|
DeviceInfoReportRequest.class })
|
||||||
public interface ReportRequest {
|
public interface ReportRequest {
|
||||||
|
|
||||||
|
@ -47,19 +47,19 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
|||||||
private static final Logger LOGGER = LogManager.getLogger(AbstractDbManager.class);
|
private static final Logger LOGGER = LogManager.getLogger(AbstractDbManager.class);
|
||||||
private static final int MAX_CLASS_CACHE_ENTRIES = 500;
|
private static final int MAX_CLASS_CACHE_ENTRIES = 500;
|
||||||
|
|
||||||
private final ArchivableEntity entity;
|
private Class<T> clazz;
|
||||||
|
|
||||||
private SessionFactory factory;
|
private SessionFactory factory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new <code>AbstractDbManager</code>.
|
* Creates a new <code>AbstractDbManager</code>.
|
||||||
*
|
*
|
||||||
* @param entity Class to search for when doing Hibernate queries,
|
* @param clazz Class to search for when doing Hibernate queries,
|
||||||
* unfortunately class type of T cannot be determined using only T
|
* unfortunately class type of T cannot be determined using only T
|
||||||
* @param sessionFactory the session factory to use to interact with the database
|
* @param sessionFactory the session factory to use to interact with the database
|
||||||
*/
|
*/
|
||||||
public AbstractDbManager(final ArchivableEntity entity, final SessionFactory sessionFactory) {
|
public AbstractDbManager(final Class<T> clazz, final SessionFactory sessionFactory) {
|
||||||
if (entity == null) {
|
if (clazz == null) {
|
||||||
LOGGER.error("AbstractDbManager cannot be instantiated with a null class");
|
LOGGER.error("AbstractDbManager cannot be instantiated with a null class");
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"AbstractDbManager cannot be instantiated with a null class"
|
"AbstractDbManager cannot be instantiated with a null class"
|
||||||
@ -71,10 +71,15 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
|||||||
"AbstractDbManager cannot be instantiated with a null SessionFactory"
|
"AbstractDbManager cannot be instantiated with a null SessionFactory"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
this.entity = entity;
|
this.clazz = clazz;
|
||||||
this.factory = sessionFactory;
|
this.factory = sessionFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setClazz(final Class<T> classToSet) {
|
||||||
|
this.clazz = classToSet;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the currently configured database implementation.
|
* Return the currently configured database implementation.
|
||||||
*
|
*
|
||||||
@ -119,15 +124,13 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
|||||||
try {
|
try {
|
||||||
LOGGER.debug("retrieving object from db");
|
LOGGER.debug("retrieving object from db");
|
||||||
tx = session.beginTransaction();
|
tx = session.beginTransaction();
|
||||||
Object obj = session.get(entity, id);
|
Object obj = session.get(clazz, id);
|
||||||
if (obj instanceof hirs.data.persist.ArchivableEntity) {
|
|
||||||
hirs.data.persist.ArchivableEntity objectOfTypeT = (hirs.data.persist.ArchivableEntity) obj;
|
T objectOfTypeT = (T) obj;
|
||||||
LOGGER.debug("found object, deleting it");
|
LOGGER.debug("found object, deleting it");
|
||||||
session.delete(objectOfTypeT);
|
session.delete(objectOfTypeT);
|
||||||
deleted = true;
|
deleted = true;
|
||||||
} else {
|
|
||||||
LOGGER.debug("object not found");
|
|
||||||
}
|
|
||||||
tx.commit();
|
tx.commit();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
final String msg = "unable to retrieve object";
|
final String msg = "unable to retrieve object";
|
||||||
@ -164,16 +167,16 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
|||||||
Transaction tx = null;
|
Transaction tx = null;
|
||||||
Session session = factory.getCurrentSession();
|
Session session = factory.getCurrentSession();
|
||||||
CriteriaBuilder builder = session.getCriteriaBuilder();
|
CriteriaBuilder builder = session.getCriteriaBuilder();
|
||||||
CriteriaQuery<hirs.data.persist.ArchivableEntity> criteria = builder.createQuery(entity);
|
CriteriaQuery<T> criteria = builder.createQuery(clazz);
|
||||||
try {
|
try {
|
||||||
LOGGER.debug("retrieving object from db");
|
LOGGER.debug("retrieving object from db");
|
||||||
tx = session.beginTransaction();
|
tx = session.beginTransaction();
|
||||||
Root<hirs.data.persist.ArchivableEntity> myObjectRoot = criteria.from(clazz);
|
Root<T> myObjectRoot = criteria.from(this.clazz.getClass());
|
||||||
Join<hirs.data.persist.ArchivableEntity, JoinObject> joinObject = myObjectRoot.join("joinObject");
|
Join<T, JoinObject> joinObject = myObjectRoot.join("joinObject");
|
||||||
Object object = session.getSessionFactory().getCurrentSession().createCriteria(clazz)
|
Object object = session.getSessionFactory().getCurrentSession().createCriteria(this.clazz.getClass())
|
||||||
.add(Restrictions.eq("name", name)).uniqueResult();
|
.add(Restrictions.eq("name", name)).uniqueResult();
|
||||||
if (object instanceof hirs.data.persist.ArchivableEntity) {
|
if (object instanceof T) {
|
||||||
hirs.data.persist.ArchivableEntity objectOfTypeT = (hirs.data.persist.ArchivableEntity) object;
|
T objectOfTypeT = (T) object;
|
||||||
LOGGER.debug("found object, deleting it");
|
LOGGER.debug("found object, deleting it");
|
||||||
session.delete(objectOfTypeT);
|
session.delete(objectOfTypeT);
|
||||||
deleted = true;
|
deleted = true;
|
||||||
@ -240,29 +243,27 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
|||||||
Transaction tx = null;
|
Transaction tx = null;
|
||||||
Session session = factory.getCurrentSession();
|
Session session = factory.getCurrentSession();
|
||||||
try {
|
try {
|
||||||
LOGGER.debug("Deleting instances of class: {}", clazz);
|
LOGGER.debug("Deleting instances of class: {}", this.clazz.getClass());
|
||||||
tx = session.beginTransaction();
|
tx = session.beginTransaction();
|
||||||
CriteriaBuilder builder = session.getCriteriaBuilder();
|
CriteriaBuilder builder = session.getCriteriaBuilder();
|
||||||
CriteriaQuery<ArchivableEntity> criteriaQuery = builder.createQuery(clazz);
|
CriteriaQuery<T> criteriaQuery = builder.createQuery(this.clazz.getClass());
|
||||||
Root<hirs.data.persist.ArchivableEntity> root = criteriaQuery.from(clazz);
|
Root<T> root = criteriaQuery.from(this.clazz.getClass());
|
||||||
Predicate recordPredicate = builder.and(
|
Predicate recordPredicate = builder.and(
|
||||||
);
|
);
|
||||||
criteriaQuery.select(root).where(recordPredicate);
|
criteriaQuery.select(root).where(recordPredicate);
|
||||||
Query<ArchivableEntity> query = session.createQuery(criteriaQuery);
|
Query<T> query = session.createQuery(criteriaQuery);
|
||||||
List<ArchivableEntity> results = query.getResultList();
|
List<T> results = query.getResultList();
|
||||||
ArchivableEntity ret = null;
|
T ret = null;
|
||||||
if (results != null && !results.isEmpty()) {
|
if (results != null && !results.isEmpty()) {
|
||||||
ret = results.get(0);
|
ret = results.get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
List instances = session.createCriteria(clazz)
|
List instances = session.createCriteria(this.clazz.getClass())
|
||||||
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
|
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
|
||||||
for (Object instance : instances) {
|
for (Object instance : instances) {
|
||||||
if (instance instanceof ArchivableEntity) {
|
session.delete((T) instance);
|
||||||
session.delete((ArchivableEntity) instance);
|
|
||||||
numEntitiesDeleted++;
|
numEntitiesDeleted++;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
tx.commit();
|
tx.commit();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
final String msg = "unable to truncate class";
|
final String msg = "unable to truncate class";
|
||||||
@ -286,16 +287,16 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
|||||||
* @throws DBManagerException if an error is encountered while performing the query or creating
|
* @throws DBManagerException if an error is encountered while performing the query or creating
|
||||||
* the result objects
|
* the result objects
|
||||||
*/
|
*/
|
||||||
protected List<ArchivableEntity> doGetWithCriteria(final Collection<Criterion> criteriaCollection)
|
protected List<T> doGetWithCriteria(final Collection<Criterion> criteriaCollection)
|
||||||
throws DBManagerException {
|
throws DBManagerException {
|
||||||
return doGetWithCriteria(entity, criteriaCollection);
|
return doGetWithCriteria(clazz, criteriaCollection);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runs a Criteria query using the given collection of Criterion over the
|
* Runs a Criteria query using the given collection of Criterion over the
|
||||||
* associated class.
|
* associated class.
|
||||||
*
|
*
|
||||||
* @param <ArchivableEntity> the specific type of class to retrieve
|
* @param clazzToGet the specific type of class to retrieve
|
||||||
* (should extend this class' <T> parameter)
|
* (should extend this class' <T> parameter)
|
||||||
* @param clazzToGet the class of object to retrieve
|
* @param clazzToGet the class of object to retrieve
|
||||||
* @param criteriaCollection the collection of Criterion to apply
|
* @param criteriaCollection the collection of Criterion to apply
|
||||||
@ -304,8 +305,8 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
|||||||
* @throws DBManagerException if an error is encountered while performing the query or creating
|
* @throws DBManagerException if an error is encountered while performing the query or creating
|
||||||
* the result objects
|
* the result objects
|
||||||
*/
|
*/
|
||||||
protected final List<ArchivableEntity> doGetWithCriteria(
|
protected final List<T> doGetWithCriteria(
|
||||||
final Class<ArchivableEntity> clazzToGet,
|
final Class<T> clazzToGet,
|
||||||
final Collection<Criterion> criteriaCollection
|
final Collection<Criterion> criteriaCollection
|
||||||
) throws DBManagerException {
|
) throws DBManagerException {
|
||||||
LOGGER.debug("running criteria query over: {}", clazzToGet);
|
LOGGER.debug("running criteria query over: {}", clazzToGet);
|
||||||
@ -313,7 +314,7 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
|||||||
LOGGER.debug("null object argument");
|
LOGGER.debug("null object argument");
|
||||||
throw new NullPointerException("criteria or restrictions");
|
throw new NullPointerException("criteria or restrictions");
|
||||||
}
|
}
|
||||||
List<ArchivableEntity> ret = new ArrayList<>();
|
List<T> ret = new ArrayList<>();
|
||||||
Transaction tx = null;
|
Transaction tx = null;
|
||||||
Session session = factory.getCurrentSession();
|
Session session = factory.getCurrentSession();
|
||||||
try {
|
try {
|
||||||
@ -353,7 +354,7 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
|||||||
* @throws DBManagerException if object has previously been saved or an
|
* @throws DBManagerException if object has previously been saved or an
|
||||||
* error occurs while trying to save it to the database
|
* error occurs while trying to save it to the database
|
||||||
*/
|
*/
|
||||||
protected ArchivableEntity doSave(final ArchivableEntity object) throws DBManagerException {
|
protected T doSave(final T object) throws DBManagerException {
|
||||||
LOGGER.debug("saving object: {}", object);
|
LOGGER.debug("saving object: {}", object);
|
||||||
if (object == null) {
|
if (object == null) {
|
||||||
LOGGER.debug("null object argument");
|
LOGGER.debug("null object argument");
|
||||||
@ -366,9 +367,9 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
|||||||
LOGGER.debug("saving object in db");
|
LOGGER.debug("saving object in db");
|
||||||
tx = session.beginTransaction();
|
tx = session.beginTransaction();
|
||||||
final Serializable id = session.save(object);
|
final Serializable id = session.save(object);
|
||||||
Object o = session.get(object.getClass(), id);
|
T o = (T) session.get(object.getClass(), id);
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
return (ArchivableEntity) o;
|
return o;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
final String msg = "unable to save object";
|
final String msg = "unable to save object";
|
||||||
LOGGER.error(msg, e);
|
LOGGER.error(msg, e);
|
||||||
@ -387,7 +388,7 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
|||||||
* @param object object to update
|
* @param object object to update
|
||||||
* @throws DBManagerException if unable to update the record
|
* @throws DBManagerException if unable to update the record
|
||||||
*/
|
*/
|
||||||
protected void doUpdate(final ArchivableEntity object) throws DBManagerException {
|
protected void doUpdate(final T object) throws DBManagerException {
|
||||||
LOGGER.debug("updating object");
|
LOGGER.debug("updating object");
|
||||||
if (object == null) {
|
if (object == null) {
|
||||||
LOGGER.debug("null object argument");
|
LOGGER.debug("null object argument");
|
||||||
@ -422,7 +423,7 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
|||||||
* @throws DBManagerException if unable to search the database or recreate
|
* @throws DBManagerException if unable to search the database or recreate
|
||||||
* the <code>Object</code>
|
* the <code>Object</code>
|
||||||
*/
|
*/
|
||||||
protected ArchivableEntity doGet(final String name) throws DBManagerException {
|
protected T doGet(final String name) throws DBManagerException {
|
||||||
LOGGER.debug("getting object: {}", name);
|
LOGGER.debug("getting object: {}", name);
|
||||||
if (name == null) {
|
if (name == null) {
|
||||||
LOGGER.debug("null name argument");
|
LOGGER.debug("null name argument");
|
||||||
@ -432,17 +433,17 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
|||||||
Transaction tx = null;
|
Transaction tx = null;
|
||||||
Session session = factory.getCurrentSession();
|
Session session = factory.getCurrentSession();
|
||||||
try {
|
try {
|
||||||
LOGGER.debug("retrieving " + entity.toString() + " from db");
|
LOGGER.debug("retrieving " + clazz.toString() + " from db");
|
||||||
tx = session.beginTransaction();
|
tx = session.beginTransaction();
|
||||||
CriteriaBuilder builder = session.getCriteriaBuilder();
|
CriteriaBuilder builder = session.getCriteriaBuilder();
|
||||||
CriteriaQuery<ArchivableEntity> criteriaQuery = builder.createQuery(entity);
|
CriteriaQuery<T> criteriaQuery = builder.createQuery(clazz);
|
||||||
Root<ArchivableEntity> root = criteriaQuery.from(entity);
|
Root<T> root = criteriaQuery.from(clazz);
|
||||||
Predicate recordPredicate = builder.and(
|
Predicate recordPredicate = builder.and(
|
||||||
builder.equal(root.get("name"), name));
|
builder.equal(root.get("name"), name));
|
||||||
criteriaQuery.select(root).where(recordPredicate);
|
criteriaQuery.select(root).where(recordPredicate);
|
||||||
Query<ArchivableEntity> query = session.createQuery(criteriaQuery);
|
Query<T> query = session.createQuery(criteriaQuery);
|
||||||
List<ArchivableEntity> results = query.getResultList();
|
List<T> results = query.getResultList();
|
||||||
ArchivableEntity ret = null;
|
T ret = null;
|
||||||
if (results != null && !results.isEmpty()) {
|
if (results != null && !results.isEmpty()) {
|
||||||
ret = results.get(0);
|
ret = results.get(0);
|
||||||
}
|
}
|
||||||
@ -471,7 +472,7 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
|||||||
* @throws DBManagerException if unable to search the database or recreate
|
* @throws DBManagerException if unable to search the database or recreate
|
||||||
* the <code>Object</code>
|
* the <code>Object</code>
|
||||||
*/
|
*/
|
||||||
protected ArchivableEntity doGet(final Serializable id) throws DBManagerException {
|
protected T doGet(final Serializable id) throws DBManagerException {
|
||||||
LOGGER.debug("getting object: {}", id);
|
LOGGER.debug("getting object: {}", id);
|
||||||
if (id == null) {
|
if (id == null) {
|
||||||
LOGGER.debug("null id argument");
|
LOGGER.debug("null id argument");
|
||||||
@ -482,7 +483,7 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
|||||||
try {
|
try {
|
||||||
LOGGER.debug("retrieving object from db");
|
LOGGER.debug("retrieving object from db");
|
||||||
tx = session.beginTransaction();
|
tx = session.beginTransaction();
|
||||||
ArchivableEntity ret = (ArchivableEntity) session.get(entity, id);
|
T ret = (T) session.get(clazz, id);
|
||||||
tx.commit();
|
tx.commit();
|
||||||
return ret;
|
return ret;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -504,47 +505,45 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
|||||||
* managed. This class argument allows the caller to limit which types of
|
* managed. This class argument allows the caller to limit which types of
|
||||||
* <code>T</code> should be returned.
|
* <code>T</code> should be returned.
|
||||||
*
|
*
|
||||||
* @param clazz class type of <code>T</code>s to search for (may be null to
|
* @param entity class type of <code>T</code>s to search for (may be null to
|
||||||
* use Class<T>)
|
* use Class<T>)
|
||||||
* @param additionalRestriction - an added Criterion to use in the query, null for none
|
* @param additionalRestriction - an added Criterion to use in the query, null for none
|
||||||
* @return list of <code>T</code> names
|
* @return list of <code>T</code> names
|
||||||
* @throws DBManagerException if unable to search the database
|
* @throws DBManagerException if unable to search the database
|
||||||
*/
|
*/
|
||||||
protected List<ArchivableEntity> doGetList(final ArchivableEntity entity,
|
protected List<T> doGetList(final T entity,
|
||||||
final Criterion additionalRestriction)
|
final Criterion additionalRestriction)
|
||||||
throws DBManagerException {
|
throws DBManagerException {
|
||||||
LOGGER.debug("Getting object list");
|
LOGGER.debug("Getting object list");
|
||||||
ArchivableEntity searchClass = entity;
|
|
||||||
if (entity == null) {
|
if (entity == null) {
|
||||||
LOGGER.debug("entity is null");
|
LOGGER.debug("entity is null");
|
||||||
searchClass = this.entity;
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
List<ArchivableEntity> objects = new ArrayList<>();
|
T searchClass = entity;
|
||||||
|
List<T> objects = new ArrayList<>();
|
||||||
Transaction tx = null;
|
Transaction tx = null;
|
||||||
Session session = factory.getCurrentSession();
|
Session session = factory.getCurrentSession();
|
||||||
try {
|
try {
|
||||||
LOGGER.debug("Retrieving objects from db of class {}", searchClass.getName());
|
LOGGER.debug("Retrieving objects from db of class {}", searchClass);
|
||||||
tx = session.beginTransaction();
|
tx = session.beginTransaction();
|
||||||
CriteriaBuilder builder = session.getCriteriaBuilder();
|
CriteriaBuilder builder = session.getCriteriaBuilder();
|
||||||
CriteriaQuery<?> criteriaQuery = builder.createQuery(searchClass);
|
CriteriaQuery<T> criteriaQuery = builder.createQuery(searchClass);
|
||||||
Root<?> root = criteriaQuery.from(criteriaQuery.getResultType());
|
Root<T> root = criteriaQuery.from(criteriaQuery.getResultType());
|
||||||
Predicate recordPredicate = builder.and(
|
Predicate recordPredicate = builder.and(
|
||||||
|
|
||||||
);
|
);
|
||||||
criteriaQuery.select(root).where(recordPredicate).distinct(true);
|
criteriaQuery.select(root).where(recordPredicate).distinct(true);
|
||||||
Query<?> query = session.createQuery(criteriaQuery);
|
Query<T> query = session.createQuery(criteriaQuery);
|
||||||
List<?> results = query.getResultList();
|
List<T> results = query.getResultList();
|
||||||
|
|
||||||
// Criteria criteria = session.createCriteria(searchClass);
|
// Criteria criteria = session.createCriteria(searchClass);
|
||||||
if (additionalRestriction != null) {
|
// if (additionalRestriction != null) {
|
||||||
criteria.add(additionalRestriction);
|
// criteriaQuery.add(additionalRestriction);
|
||||||
}
|
// }
|
||||||
// List list = criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
|
// List list = criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
|
||||||
for (Object o : results) {
|
for (Object o : results) {
|
||||||
if (o instanceof ArchivableEntity) {
|
objects.add((T) o);
|
||||||
objects.add((ArchivableEntity) o);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
tx.commit();
|
tx.commit();
|
||||||
LOGGER.debug("Got {} objects", objects.size());
|
LOGGER.debug("Got {} objects", objects.size());
|
||||||
@ -580,12 +579,12 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
|||||||
* @throws DBManagerException if unable to create the list
|
* @throws DBManagerException if unable to create the list
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("checkstyle:parameternumber")
|
@SuppressWarnings("checkstyle:parameternumber")
|
||||||
protected FilteredRecordsList<ArchivableEntity> doGetOrderedList(final ArchivableEntity clazz,
|
protected FilteredRecordsList<T> doGetOrderedList(final Class<T> clazz,
|
||||||
final String columnToOrder, final boolean ascending, final int firstResult,
|
final String columnToOrder, final boolean ascending, final int firstResult,
|
||||||
final int maxResults, final String search, final Map<String, Boolean> searchableColumns,
|
final int maxResults, final String search, final Map<String, Boolean> searchableColumns,
|
||||||
final CriteriaModifier criteriaModifier) throws DBManagerException {
|
final CriteriaModifier criteriaModifier) throws DBManagerException {
|
||||||
LOGGER.debug("Getting object list");
|
LOGGER.debug("Getting object list");
|
||||||
ArchivableEntity searchClass = clazz;
|
Class<T> searchClass = clazz;
|
||||||
if (clazz == null) {
|
if (clazz == null) {
|
||||||
LOGGER.debug("clazz is null");
|
LOGGER.debug("clazz is null");
|
||||||
searchClass = this.clazz;
|
searchClass = this.clazz;
|
||||||
@ -598,9 +597,9 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Object that will store query values
|
//Object that will store query values
|
||||||
FilteredRecordsList<ArchivableEntity> aqr = new FilteredRecordsList<>();
|
FilteredRecordsList<T> aqr = new FilteredRecordsList<>();
|
||||||
|
|
||||||
List<ArchivableEntity> objects = new ArrayList<>();
|
List<T> objects = new ArrayList<>();
|
||||||
Transaction tx = null;
|
Transaction tx = null;
|
||||||
Session session = factory.getCurrentSession();
|
Session session = factory.getCurrentSession();
|
||||||
try {
|
try {
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package hirs.persist;
|
package hirs.persist;
|
||||||
|
|
||||||
import hirs.data.persist.AbstractEntity;
|
|
||||||
import org.hibernate.criterion.Criterion;
|
import org.hibernate.criterion.Criterion;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
@ -10,7 +9,7 @@ import java.util.List;
|
|||||||
* Interface defining database CRUD operations (Create, Read, Update, Delete).
|
* Interface defining database CRUD operations (Create, Read, Update, Delete).
|
||||||
* @param <T> the object type, T.
|
* @param <T> the object type, T.
|
||||||
*/
|
*/
|
||||||
public interface CrudManager<T extends Serializable> extends OrderedListQuerier<T> {
|
public interface CrudManager<T> extends OrderedListQuerier<T> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -27,7 +26,7 @@ public interface CrudManager<T extends Serializable> extends OrderedListQuerier<
|
|||||||
* @throws DBManagerException if object has previously been saved or an
|
* @throws DBManagerException if object has previously been saved or an
|
||||||
* error occurs while trying to save it to the database
|
* error occurs while trying to save it to the database
|
||||||
*/
|
*/
|
||||||
AbstractEntity save(AbstractEntity object) throws DBManagerException;
|
T save(T object) throws DBManagerException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates an object stored in the database. This updates the database
|
* Updates an object stored in the database. This updates the database
|
||||||
@ -36,7 +35,7 @@ public interface CrudManager<T extends Serializable> extends OrderedListQuerier<
|
|||||||
* @param object object to update
|
* @param object object to update
|
||||||
* @throws DBManagerException if an error occurs while trying to save it to the database
|
* @throws DBManagerException if an error occurs while trying to save it to the database
|
||||||
*/
|
*/
|
||||||
void update(AbstractEntity object) throws DBManagerException;
|
void update(T object) throws DBManagerException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the <code>Object</code> from the database. This searches the
|
* Retrieves the <code>Object</code> from the database. This searches the
|
||||||
@ -48,7 +47,7 @@ public interface CrudManager<T extends Serializable> extends OrderedListQuerier<
|
|||||||
* @throws DBManagerException if unable to search the database or recreate
|
* @throws DBManagerException if unable to search the database or recreate
|
||||||
* the <code>Object</code>
|
* the <code>Object</code>
|
||||||
*/
|
*/
|
||||||
AbstractEntity get(String name) throws DBManagerException;
|
T get(String name) throws DBManagerException;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -72,11 +71,12 @@ public interface CrudManager<T extends Serializable> extends OrderedListQuerier<
|
|||||||
* managed. This class argument allows the caller to limit which types of
|
* managed. This class argument allows the caller to limit which types of
|
||||||
* <code>T</code> should be returned.
|
* <code>T</code> should be returned.
|
||||||
*
|
*
|
||||||
|
* @param object class type of <code>T</code>s to search for (may be null to
|
||||||
|
* use Class<T>)
|
||||||
* @return list of <code>T</code> names
|
* @return list of <code>T</code> names
|
||||||
* @throws DBManagerException if unable to search the database
|
* @throws DBManagerException if unable to search the database
|
||||||
*/
|
*/
|
||||||
List<T> getList()
|
List<T> getList(T object) throws DBManagerException;
|
||||||
throws DBManagerException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of all <code>T</code>s of type <code>clazz</code> in the database, with an
|
* Returns a list of all <code>T</code>s of type <code>clazz</code> in the database, with an
|
||||||
@ -86,11 +86,13 @@ public interface CrudManager<T extends Serializable> extends OrderedListQuerier<
|
|||||||
* managed. This class argument allows the caller to limit which types of
|
* managed. This class argument allows the caller to limit which types of
|
||||||
* <code>T</code> should be returned.
|
* <code>T</code> should be returned.
|
||||||
*
|
*
|
||||||
|
* @param object class type of <code>T</code>s to search for (may be null to
|
||||||
|
* use Class<T>)
|
||||||
* @param additionalRestriction - an added Criterion to use in the query, null for none
|
* @param additionalRestriction - an added Criterion to use in the query, null for none
|
||||||
* @return list of <code>T</code> names
|
* @return list of <code>T</code> names
|
||||||
* @throws DBManagerException if unable to search the database
|
* @throws DBManagerException if unable to search the database
|
||||||
*/
|
*/
|
||||||
List<AbstractEntity> getList(Criterion additionalRestriction)
|
List<T> getList(T object, Criterion additionalRestriction)
|
||||||
throws DBManagerException;
|
throws DBManagerException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -119,7 +121,7 @@ public interface CrudManager<T extends Serializable> extends OrderedListQuerier<
|
|||||||
* @return true if successfully found and deleted the object
|
* @return true if successfully found and deleted the object
|
||||||
* @throws DBManagerException if unable to delete the object from the database
|
* @throws DBManagerException if unable to delete the object from the database
|
||||||
*/
|
*/
|
||||||
boolean delete(T entity) throws DBManagerException;
|
boolean delete(Class<T> entity) throws DBManagerException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes all instances of the associated class.
|
* Deletes all instances of the associated class.
|
||||||
|
@ -1,23 +1,37 @@
|
|||||||
package hirs.persist;
|
package hirs.persist;
|
||||||
|
|
||||||
|
import hirs.FilteredRecordsList;
|
||||||
import hirs.data.persist.certificate.Certificate;
|
import hirs.data.persist.certificate.Certificate;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
|
import org.hibernate.StaleObjectStateException;
|
||||||
|
import org.hibernate.criterion.Criterion;
|
||||||
|
import org.hibernate.exception.LockAcquisitionException;
|
||||||
|
import org.springframework.retry.RetryCallback;
|
||||||
|
import org.springframework.retry.RetryContext;
|
||||||
|
import org.springframework.retry.backoff.FixedBackOffPolicy;
|
||||||
|
import org.springframework.retry.policy.SimpleRetryPolicy;
|
||||||
|
import org.springframework.retry.support.RetryTemplate;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.io.Serializable;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class is used to persist and retrieve {@link Certificate}s into and from a database.
|
* This class is used to persist and retrieve {@link Certificate}s into and from a database.
|
||||||
*/
|
*/
|
||||||
public class DBCertificateManager extends DBManager<Certificate>
|
public class DBCertificateManager extends AbstractDbManager<Certificate>
|
||||||
implements CertificateManager {
|
implements CertificateManager {
|
||||||
|
|
||||||
private static final Logger LOGGER = LogManager.getLogger(DBCertificateManager.class);
|
private static final Logger LOGGER = LogManager.getLogger(DBCertificateManager.class);
|
||||||
|
|
||||||
|
// structure for retrying methods in the database
|
||||||
|
private RetryTemplate retryTemplate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new {@link DBCertificateManager} that uses the default
|
* Creates a new {@link DBCertificateManager} that uses the default
|
||||||
* database.
|
* database.
|
||||||
@ -51,11 +65,11 @@ public class DBCertificateManager extends DBManager<Certificate>
|
|||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <T extends Certificate> Set<T> get(final CertificateSelector certificateSelector) {
|
public <T extends Certificate> Set<T> get(final CertificateSelector certificateSelector) {
|
||||||
return new HashSet<>(
|
return new HashSet<>(0
|
||||||
(List<T>) getWithCriteria(
|
// (List<T>) getWithCriteria(
|
||||||
certificateSelector.getCertificateClass(),
|
// certificateSelector.getCertificateClass(),
|
||||||
Collections.singleton(certificateSelector.getCriterion())
|
// Collections.singleton(certificateSelector.getCriterion())
|
||||||
)
|
// )
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,4 +82,164 @@ public class DBCertificateManager extends DBManager<Certificate>
|
|||||||
public boolean deleteCertificate(final Certificate certificate) {
|
public boolean deleteCertificate(final Certificate certificate) {
|
||||||
return delete(certificate);
|
return delete(certificate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves the <code>Object</code> in the database. This creates a new
|
||||||
|
* database session and saves the object. If the <code>Object</code> had
|
||||||
|
* previously been saved then a <code>DBManagerException</code> is thrown.
|
||||||
|
*
|
||||||
|
* @param object object to save
|
||||||
|
* @return reference to saved object
|
||||||
|
* @throws DBManagerException if object has previously been saved or an
|
||||||
|
* error occurs while trying to save it to the database
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Certificate save(final Certificate object) throws DBManagerException {
|
||||||
|
return retryTemplate.execute(new RetryCallback<Certificate, DBManagerException>() {
|
||||||
|
@Override
|
||||||
|
public Certificate doWithRetry(final RetryContext context) throws DBManagerException {
|
||||||
|
return doSave(object);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes the object from the database. This removes all of the database
|
||||||
|
* entries that stored information with regards to the this object.
|
||||||
|
* <p>
|
||||||
|
* If the object is referenced by any other tables then this will throw a
|
||||||
|
* <code>DBManagerException</code>.
|
||||||
|
*
|
||||||
|
* @param object instance of the object to delete
|
||||||
|
* @return true if successfully found and deleted the object
|
||||||
|
* @throws DBManagerException if unable to find the baseline or delete it
|
||||||
|
* from the database
|
||||||
|
*/
|
||||||
|
public final boolean delete(final Certificate object) throws DBManagerException {
|
||||||
|
return retryTemplate.execute(new RetryCallback<Boolean, DBManagerException>() {
|
||||||
|
@Override
|
||||||
|
public Boolean doWithRetry(final RetryContext context) throws DBManagerException {
|
||||||
|
return doDelete(object);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates an object stored in the database. This updates the database
|
||||||
|
* entries to reflect the new values that should be set.
|
||||||
|
*
|
||||||
|
* @param object object to update
|
||||||
|
* @throws DBManagerException if an error occurs while trying to save it to the database
|
||||||
|
*/
|
||||||
|
public final void update(final Certificate object) throws DBManagerException {
|
||||||
|
retryTemplate.execute(new RetryCallback<Void, DBManagerException>() {
|
||||||
|
@Override
|
||||||
|
public Void doWithRetry(final RetryContext context) throws DBManagerException {
|
||||||
|
doUpdate(object);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Certificate get(String name) throws DBManagerException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the <code>Object</code> from the database. This searches the
|
||||||
|
* database for an entry whose name matches <code>name</code>. It then
|
||||||
|
* reconstructs the <code>Object</code> from the database entry.
|
||||||
|
*
|
||||||
|
* @param id id of the object
|
||||||
|
* @return object if found, otherwise null.
|
||||||
|
* @throws DBManagerException if unable to search the database or recreate
|
||||||
|
* the <code>Object</code>
|
||||||
|
*/
|
||||||
|
public final Certificate get(final Serializable id) throws DBManagerException {
|
||||||
|
return retryTemplate.execute(new RetryCallback<Certificate, DBManagerException>() {
|
||||||
|
@Override
|
||||||
|
public Certificate doWithRetry(final RetryContext context) throws DBManagerException {
|
||||||
|
return doGet(id);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Certificate> getList(Certificate object) throws DBManagerException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Certificate> getList(Certificate object, Criterion additionalRestriction) throws DBManagerException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean deleteById(Serializable id) throws DBManagerException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean delete(Class<Certificate> entity) throws DBManagerException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int deleteAll() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean archive(String name) throws DBManagerException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FilteredRecordsList getOrderedList(
|
||||||
|
Class<Certificate> clazz,
|
||||||
|
String columnToOrder,
|
||||||
|
boolean ascending,
|
||||||
|
int firstResult,
|
||||||
|
int maxResults,
|
||||||
|
String search,
|
||||||
|
Map<String, Boolean> searchableColumns) throws DBManagerException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FilteredRecordsList<Certificate> getOrderedList(
|
||||||
|
Class<Certificate> clazz,
|
||||||
|
String columnToOrder,
|
||||||
|
boolean ascending, int firstResult,
|
||||||
|
int maxResults, String search,
|
||||||
|
Map<String, Boolean> searchableColumns, CriteriaModifier criteriaModifier) throws DBManagerException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the parameters used to retry database transactions. The retry template will
|
||||||
|
* retry transactions that throw a LockAcquisitionException or StaleObjectStateException.
|
||||||
|
* @param maxTransactionRetryAttempts the maximum number of database transaction attempts
|
||||||
|
* @param retryWaitTimeMilliseconds the transaction retry wait time in milliseconds
|
||||||
|
*/
|
||||||
|
public final void setRetryTemplate(final int maxTransactionRetryAttempts,
|
||||||
|
final long retryWaitTimeMilliseconds) {
|
||||||
|
Map<Class<? extends Throwable>, Boolean> exceptionsToRetry = new HashMap<>();
|
||||||
|
exceptionsToRetry.put(LockAcquisitionException.class, true);
|
||||||
|
exceptionsToRetry.put(StaleObjectStateException.class, true);
|
||||||
|
|
||||||
|
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(
|
||||||
|
maxTransactionRetryAttempts,
|
||||||
|
exceptionsToRetry,
|
||||||
|
true,
|
||||||
|
false
|
||||||
|
);
|
||||||
|
|
||||||
|
FixedBackOffPolicy backoffPolicy = new FixedBackOffPolicy();
|
||||||
|
backoffPolicy.setBackOffPeriod(retryWaitTimeMilliseconds);
|
||||||
|
this.retryTemplate = new RetryTemplate();
|
||||||
|
this.retryTemplate.setRetryPolicy(retryPolicy);
|
||||||
|
this.retryTemplate.setBackOffPolicy(backoffPolicy);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package hirs.persist;
|
package hirs.persist;
|
||||||
|
|
||||||
import hirs.FilteredRecordsList;
|
import hirs.FilteredRecordsList;
|
||||||
import hirs.data.persist.AbstractEntity;
|
|
||||||
import hirs.data.persist.ArchivableEntity;
|
import hirs.data.persist.ArchivableEntity;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
@ -66,7 +65,7 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
|||||||
* @param sessionFactory the session factory to use to connect to the database
|
* @param sessionFactory the session factory to use to connect to the database
|
||||||
* unfortunately class type of T cannot be determined using only T
|
* unfortunately class type of T cannot be determined using only T
|
||||||
*/
|
*/
|
||||||
public DBManager(final AbstractEntity clazz, final SessionFactory sessionFactory) {
|
public DBManager(final Class<T> clazz, final SessionFactory sessionFactory) {
|
||||||
super(clazz, sessionFactory);
|
super(clazz, sessionFactory);
|
||||||
setRetryTemplate(DEFAULT_MAX_RETRY_ATTEMPTS, DEFAULT_RETRY_WAIT_TIME_MS);
|
setRetryTemplate(DEFAULT_MAX_RETRY_ATTEMPTS, DEFAULT_RETRY_WAIT_TIME_MS);
|
||||||
}
|
}
|
||||||
@ -115,12 +114,12 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
|||||||
* @throws DBManagerException if an error is encountered while performing the query or creating
|
* @throws DBManagerException if an error is encountered while performing the query or creating
|
||||||
* the result objects
|
* the result objects
|
||||||
*/
|
*/
|
||||||
public final List<hirs.data.persist.ArchivableEntity> getWithCriteria(final Collection<Criterion> criteriaCollection)
|
public final List<T> getWithCriteria(final Collection<Criterion> criteriaCollection)
|
||||||
throws DBManagerException {
|
throws DBManagerException {
|
||||||
return retryTemplate.execute(
|
return retryTemplate.execute(
|
||||||
new RetryCallback<List<AbstractEntity>, DBManagerException>() {
|
new RetryCallback<List<T>, DBManagerException>() {
|
||||||
@Override
|
@Override
|
||||||
public List<AbstractEntity> doWithRetry(final RetryContext context)
|
public List<T> doWithRetry(final RetryContext context)
|
||||||
throws DBManagerException {
|
throws DBManagerException {
|
||||||
return doGetWithCriteria(criteriaCollection);
|
return doGetWithCriteria(criteriaCollection);
|
||||||
}
|
}
|
||||||
@ -138,13 +137,13 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
|||||||
* @throws DBManagerException if an error is encountered while performing the query or creating
|
* @throws DBManagerException if an error is encountered while performing the query or creating
|
||||||
* the result objects
|
* the result objects
|
||||||
*/
|
*/
|
||||||
protected final List<hirs.data.persist.ArchivableEntity> getWithCriteria(
|
protected final List<T> getWithCriteria(
|
||||||
final Class<hirs.data.persist.ArchivableEntity> clazzToGet,
|
final Class<T> clazzToGet,
|
||||||
final Collection<Criterion> criteriaCollection) throws DBManagerException {
|
final Collection<Criterion> criteriaCollection) throws DBManagerException {
|
||||||
return retryTemplate.execute(
|
return retryTemplate.execute(
|
||||||
new RetryCallback<List<AbstractEntity>, DBManagerException>() {
|
new RetryCallback<List<T>, DBManagerException>() {
|
||||||
@Override
|
@Override
|
||||||
public List<hirs.data.persist.ArchivableEntity> doWithRetry(final RetryContext context)
|
public List<T> doWithRetry(final RetryContext context)
|
||||||
throws DBManagerException {
|
throws DBManagerException {
|
||||||
return doGetWithCriteria(clazzToGet, criteriaCollection);
|
return doGetWithCriteria(clazzToGet, criteriaCollection);
|
||||||
}
|
}
|
||||||
@ -178,10 +177,10 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
|||||||
* @throws DBManagerException if object has previously been saved or an
|
* @throws DBManagerException if object has previously been saved or an
|
||||||
* error occurs while trying to save it to the database
|
* error occurs while trying to save it to the database
|
||||||
*/
|
*/
|
||||||
public final AbstractEntity save(final AbstractEntity object) throws DBManagerException {
|
public final T save(final T object) throws DBManagerException {
|
||||||
return retryTemplate.execute(new RetryCallback<AbstractEntity, DBManagerException>() {
|
return retryTemplate.execute(new RetryCallback<T, DBManagerException>() {
|
||||||
@Override
|
@Override
|
||||||
public AbstractEntity doWithRetry(final RetryContext context) throws DBManagerException {
|
public T doWithRetry(final RetryContext context) throws DBManagerException {
|
||||||
return doSave(object);
|
return doSave(object);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -194,7 +193,7 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
|||||||
* @param object object to update
|
* @param object object to update
|
||||||
* @throws DBManagerException if an error occurs while trying to save it to the database
|
* @throws DBManagerException if an error occurs while trying to save it to the database
|
||||||
*/
|
*/
|
||||||
public final void update(final AbstractEntity object) throws DBManagerException {
|
public final void update(final T object) throws DBManagerException {
|
||||||
retryTemplate.execute(new RetryCallback<Void, DBManagerException>() {
|
retryTemplate.execute(new RetryCallback<Void, DBManagerException>() {
|
||||||
@Override
|
@Override
|
||||||
public Void doWithRetry(final RetryContext context) throws DBManagerException {
|
public Void doWithRetry(final RetryContext context) throws DBManagerException {
|
||||||
@ -214,10 +213,10 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
|||||||
* @throws DBManagerException if unable to search the database or recreate
|
* @throws DBManagerException if unable to search the database or recreate
|
||||||
* the <code>Object</code>
|
* the <code>Object</code>
|
||||||
*/
|
*/
|
||||||
public final ArchivableEntity get(final String name) throws DBManagerException {
|
public final T get(final String name) throws DBManagerException {
|
||||||
return retryTemplate.execute(new RetryCallback<ArchivableEntity, DBManagerException>() {
|
return retryTemplate.execute(new RetryCallback<T, DBManagerException>() {
|
||||||
@Override
|
@Override
|
||||||
public ArchivableEntity doWithRetry(final RetryContext context) throws DBManagerException {
|
public T doWithRetry(final RetryContext context) throws DBManagerException {
|
||||||
return doGet(name);
|
return doGet(name);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -233,10 +232,10 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
|||||||
* @throws DBManagerException if unable to search the database or recreate
|
* @throws DBManagerException if unable to search the database or recreate
|
||||||
* the <code>Object</code>
|
* the <code>Object</code>
|
||||||
*/
|
*/
|
||||||
public final AbstractEntity get(final Serializable id) throws DBManagerException {
|
public final T get(final Serializable id) throws DBManagerException {
|
||||||
return retryTemplate.execute(new RetryCallback<AbstractEntity, DBManagerException>() {
|
return retryTemplate.execute(new RetryCallback<T, DBManagerException>() {
|
||||||
@Override
|
@Override
|
||||||
public AbstractEntity doWithRetry(final RetryContext context) throws DBManagerException {
|
public T doWithRetry(final RetryContext context) throws DBManagerException {
|
||||||
return doGet(id);
|
return doGet(id);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -257,12 +256,13 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
|||||||
* @throws DBManagerException if unable to search the database or recreate
|
* @throws DBManagerException if unable to search the database or recreate
|
||||||
* the <code>Object</code>
|
* the <code>Object</code>
|
||||||
*/
|
*/
|
||||||
public final AbstractEntity getAndLoadLazyFields(final String name, final boolean recurse)
|
public final T getAndLoadLazyFields(final String name, final boolean recurse)
|
||||||
throws DBManagerException {
|
throws DBManagerException {
|
||||||
return retryTemplate.execute(new RetryCallback<AbstractEntity, DBManagerException>() {
|
return retryTemplate.execute(new RetryCallback<T, DBManagerException>() {
|
||||||
@Override
|
@Override
|
||||||
public AbstractEntity doWithRetry(final RetryContext context) throws DBManagerException {
|
public T doWithRetry(final RetryContext context) throws DBManagerException {
|
||||||
return doGetAndLoadLazyFields(name, recurse);
|
return null;
|
||||||
|
// return doGetAndLoadLazyFields(name, recurse);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -282,9 +282,10 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
|||||||
* @throws DBManagerException if unable to search the database or recreate
|
* @throws DBManagerException if unable to search the database or recreate
|
||||||
* the <code>Object</code>
|
* the <code>Object</code>
|
||||||
*/
|
*/
|
||||||
public final AbstractEntity getAndLoadLazyFields(final Serializable id, final boolean recurse)
|
public final T getAndLoadLazyFields(final Serializable id, final boolean recurse)
|
||||||
throws DBManagerException {
|
throws DBManagerException {
|
||||||
return doGetAndLoadLazyFields(id, recurse);
|
return null;
|
||||||
|
// return doGetAndLoadLazyFields(id, recurse);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -300,7 +301,7 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
|||||||
* @return list of <code>T</code> names
|
* @return list of <code>T</code> names
|
||||||
* @throws DBManagerException if unable to search the database
|
* @throws DBManagerException if unable to search the database
|
||||||
*/
|
*/
|
||||||
public List<AbstractEntity> getList(final AbstractEntity entity)
|
public List<T> getList(final T entity)
|
||||||
throws DBManagerException {
|
throws DBManagerException {
|
||||||
return getList(entity, null);
|
return getList(entity, null);
|
||||||
}
|
}
|
||||||
@ -320,11 +321,11 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
|||||||
* @throws DBManagerException if unable to search the database
|
* @throws DBManagerException if unable to search the database
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<AbstractEntity> getList(final AbstractEntity entity, final Criterion additionalRestriction)
|
public List<T> getList(final T entity, final Criterion additionalRestriction)
|
||||||
throws DBManagerException {
|
throws DBManagerException {
|
||||||
return retryTemplate.execute(new RetryCallback<List<AbstractEntity>, DBManagerException>() {
|
return retryTemplate.execute(new RetryCallback<List<T>, DBManagerException>() {
|
||||||
@Override
|
@Override
|
||||||
public List<AbstractEntity> doWithRetry(final RetryContext context) throws DBManagerException {
|
public List<T> doWithRetry(final RetryContext context) throws DBManagerException {
|
||||||
return doGetList(entity, additionalRestriction);
|
return doGetList(entity, additionalRestriction);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -350,7 +351,7 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public final FilteredRecordsList getOrderedList(
|
public final FilteredRecordsList getOrderedList(
|
||||||
final AbstractEntity clazz, final String columnToOrder,
|
final Class<T> clazz, final String columnToOrder,
|
||||||
final boolean ascending, final int firstResult,
|
final boolean ascending, final int firstResult,
|
||||||
final int maxResults, final String search,
|
final int maxResults, final String search,
|
||||||
final Map<String, Boolean> searchableColumns)
|
final Map<String, Boolean> searchableColumns)
|
||||||
@ -388,17 +389,17 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
|||||||
* @throws DBManagerException if unable to create the list
|
* @throws DBManagerException if unable to create the list
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("checkstyle:parameternumber")
|
@SuppressWarnings("checkstyle:parameternumber")
|
||||||
public final FilteredRecordsList<AbstractEntity> getOrderedList(
|
public final FilteredRecordsList<T> getOrderedList(
|
||||||
final AbstractEntity clazz, final String columnToOrder,
|
final Class<T> clazz, final String columnToOrder,
|
||||||
final boolean ascending, final int firstResult,
|
final boolean ascending, final int firstResult,
|
||||||
final int maxResults, final String search,
|
final int maxResults, final String search,
|
||||||
final Map<String, Boolean> searchableColumns, final CriteriaModifier criteriaModifier)
|
final Map<String, Boolean> searchableColumns, final CriteriaModifier criteriaModifier)
|
||||||
throws DBManagerException {
|
throws DBManagerException {
|
||||||
|
|
||||||
return retryTemplate.execute(
|
return retryTemplate.execute(
|
||||||
new RetryCallback<FilteredRecordsList<AbstractEntity>, DBManagerException>() {
|
new RetryCallback<FilteredRecordsList<T>, DBManagerException>() {
|
||||||
@Override
|
@Override
|
||||||
public FilteredRecordsList<AbstractEntity> doWithRetry(final RetryContext context)
|
public FilteredRecordsList<T> doWithRetry(final RetryContext context)
|
||||||
throws DBManagerException {
|
throws DBManagerException {
|
||||||
return doGetOrderedList(clazz, columnToOrder, ascending,
|
return doGetOrderedList(clazz, columnToOrder, ascending,
|
||||||
firstResult, maxResults,
|
firstResult, maxResults,
|
||||||
@ -419,7 +420,6 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
|||||||
* @throws DBManagerException if unable to find the baseline or delete it
|
* @throws DBManagerException if unable to find the baseline or delete it
|
||||||
* from the database
|
* from the database
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public final boolean delete(final String name) throws DBManagerException {
|
public final boolean delete(final String name) throws DBManagerException {
|
||||||
return retryTemplate.execute(new RetryCallback<Boolean, DBManagerException>() {
|
return retryTemplate.execute(new RetryCallback<Boolean, DBManagerException>() {
|
||||||
@Override
|
@Override
|
||||||
@ -464,7 +464,7 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
|||||||
* @throws DBManagerException if unable to delete the object from the database
|
* @throws DBManagerException if unable to delete the object from the database
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public final boolean delete(final AbstractEntity object) throws DBManagerException {
|
public final boolean delete(final Class<T> object) throws DBManagerException {
|
||||||
return retryTemplate.execute(new RetryCallback<Boolean, DBManagerException>() {
|
return retryTemplate.execute(new RetryCallback<Boolean, DBManagerException>() {
|
||||||
@Override
|
@Override
|
||||||
public Boolean doWithRetry(final RetryContext context) throws DBManagerException {
|
public Boolean doWithRetry(final RetryContext context) throws DBManagerException {
|
||||||
@ -489,7 +489,7 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ArchivableEntity target = get(name);
|
T target = get(name);
|
||||||
if (target == null) {
|
if (target == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -1,330 +0,0 @@
|
|||||||
package hirs.persist;
|
|
||||||
|
|
||||||
import hirs.data.persist.ReferenceDigestRecord;
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
|
||||||
import org.apache.logging.log4j.Logger;
|
|
||||||
import org.hibernate.Session;
|
|
||||||
import org.hibernate.SessionFactory;
|
|
||||||
import org.hibernate.Transaction;
|
|
||||||
import org.hibernate.query.Query;
|
|
||||||
|
|
||||||
import javax.persistence.criteria.CriteriaBuilder;
|
|
||||||
import javax.persistence.criteria.CriteriaQuery;
|
|
||||||
import javax.persistence.criteria.Predicate;
|
|
||||||
import javax.persistence.criteria.Root;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class is used to persist and retrieve {@link hirs.data.persist.ReferenceDigestRecord}s into
|
|
||||||
* and from the database.
|
|
||||||
*/
|
|
||||||
public class DBReferenceDigestManager extends DBManager<ReferenceDigestRecord>
|
|
||||||
implements ReferenceDigestManager {
|
|
||||||
|
|
||||||
private static final Logger LOGGER = LogManager.getLogger(DBReferenceDigestManager.class);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Default Constructor.
|
|
||||||
*
|
|
||||||
* @param sessionFactory session factory used to access database connections
|
|
||||||
*/
|
|
||||||
public DBReferenceDigestManager(final SessionFactory sessionFactory) {
|
|
||||||
super(ReferenceDigestRecord.class, sessionFactory);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ReferenceDigestRecord saveRecord(final ReferenceDigestRecord referenceDigestRecord) {
|
|
||||||
LOGGER.debug("saving digest record: {}", referenceDigestRecord);
|
|
||||||
try {
|
|
||||||
return save(referenceDigestRecord);
|
|
||||||
} catch (DBManagerException dbMEx) {
|
|
||||||
throw new RuntimeException(dbMEx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ReferenceDigestRecord getRecord(final ReferenceDigestRecord referenceDigestRecord) {
|
|
||||||
LOGGER.debug("Getting record for {}", referenceDigestRecord);
|
|
||||||
if (referenceDigestRecord == null) {
|
|
||||||
LOGGER.error("null referenceDigestRecord argument");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (referenceDigestRecord.getManufacturer() == null
|
|
||||||
|| referenceDigestRecord.getModel() == null) {
|
|
||||||
LOGGER.error("No reference to get record from db {}", referenceDigestRecord);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
ReferenceDigestRecord dbRecord = null;
|
|
||||||
Transaction transaction = null;
|
|
||||||
Session session = getFactory().getCurrentSession();
|
|
||||||
try {
|
|
||||||
LOGGER.debug("retrieving referenceDigestRecord from db");
|
|
||||||
transaction = session.beginTransaction();
|
|
||||||
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
|
||||||
CriteriaQuery<ReferenceDigestRecord> criteriaQuery = criteriaBuilder.createQuery(ReferenceDigestRecord.class);
|
|
||||||
Root<ReferenceDigestRecord> root = criteriaQuery.from(ReferenceDigestRecord.class);
|
|
||||||
Predicate recordPredicate = criteriaBuilder
|
|
||||||
.and(criteriaBuilder.equal(root.get("manufacturer"), referenceDigestRecord.getManufacturer()),
|
|
||||||
criteriaBuilder.equal(root.get("model"), referenceDigestRecord.getModel()));
|
|
||||||
criteriaQuery.select(root).where(recordPredicate);
|
|
||||||
Query<ReferenceDigestRecord> query = session.createQuery(criteriaQuery);
|
|
||||||
List<ReferenceDigestRecord> results = query.getResultList();
|
|
||||||
if (results != null && !results.isEmpty()) {
|
|
||||||
dbRecord = results.get(0);
|
|
||||||
}
|
|
||||||
// dbRecord = (ReferenceDigestRecord) session.createCriteria(ReferenceDigestRecord.class)
|
|
||||||
// .add(Restrictions.eq("manufacturer",
|
|
||||||
// referenceDigestRecord.getManufacturer())).add(Restrictions.eq("model",
|
|
||||||
// referenceDigestRecord.getModel())).uniqueResult();
|
|
||||||
transaction.commit();
|
|
||||||
} catch (Exception ex) {
|
|
||||||
final String msg = "unable to retrieve object";
|
|
||||||
LOGGER.error(msg, ex);
|
|
||||||
if (transaction != null) {
|
|
||||||
LOGGER.debug("rolling back transaction");
|
|
||||||
transaction.rollback();
|
|
||||||
}
|
|
||||||
throw new DBManagerException(msg, ex);
|
|
||||||
}
|
|
||||||
return dbRecord;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ReferenceDigestRecord getRecord(final String manufacturer, final String model) {
|
|
||||||
LOGGER.debug("Getting record for {} ~ {}", manufacturer, model);
|
|
||||||
if (manufacturer == null || model == null) {
|
|
||||||
LOGGER.error("No reference to get record from db {} ~ {}", manufacturer, model);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
ReferenceDigestRecord dbRecord = null;
|
|
||||||
Transaction tx = null;
|
|
||||||
Session session = getFactory().getCurrentSession();
|
|
||||||
try {
|
|
||||||
LOGGER.debug("retrieving referenceDigestRecord from db");
|
|
||||||
tx = session.beginTransaction();
|
|
||||||
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
|
||||||
CriteriaQuery<ReferenceDigestRecord> criteriaQuery = criteriaBuilder.createQuery(ReferenceDigestRecord.class);
|
|
||||||
Root<ReferenceDigestRecord> root = criteriaQuery.from(ReferenceDigestRecord.class);
|
|
||||||
Predicate recordPredicate = criteriaBuilder
|
|
||||||
.and(criteriaBuilder.equal(root.get("manufacturer"), manufacturer),
|
|
||||||
criteriaBuilder.equal(root.get("model"), model));
|
|
||||||
criteriaQuery.select(root).where(recordPredicate);
|
|
||||||
Query<ReferenceDigestRecord> query = session.createQuery(criteriaQuery);
|
|
||||||
List<ReferenceDigestRecord> results = query.getResultList();
|
|
||||||
if (results != null && !results.isEmpty()) {
|
|
||||||
dbRecord = results.get(0);
|
|
||||||
}
|
|
||||||
// dbRecord = (ReferenceDigestRecord) session.createCriteria(ReferenceDigestRecord.class)
|
|
||||||
// .add(Restrictions.eq("manufacturer",
|
|
||||||
// manufacturer)).add(Restrictions.eq("model",
|
|
||||||
// model)).uniqueResult();
|
|
||||||
tx.commit();
|
|
||||||
} catch (Exception ex) {
|
|
||||||
final String msg = "unable to retrieve object";
|
|
||||||
LOGGER.error(msg, ex);
|
|
||||||
if (tx != null) {
|
|
||||||
LOGGER.debug("rolling back transaction");
|
|
||||||
tx.rollback();
|
|
||||||
}
|
|
||||||
throw new DBManagerException(msg, ex);
|
|
||||||
}
|
|
||||||
return dbRecord;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<ReferenceDigestRecord> getRecordsByDeviceName(final String deviceName) {
|
|
||||||
LOGGER.debug("Getting record for {}", deviceName);
|
|
||||||
if (deviceName == null) {
|
|
||||||
LOGGER.error("No deviceName to get record from db");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
List<ReferenceDigestRecord> dbRecords = new ArrayList<>();
|
|
||||||
try {
|
|
||||||
List<ReferenceDigestRecord> dbTempList = super.getList(ReferenceDigestRecord.class);
|
|
||||||
for (ReferenceDigestRecord rdr : dbTempList) {
|
|
||||||
if (rdr.getDeviceName().equals(deviceName)) {
|
|
||||||
dbRecords.add(rdr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (DBManagerException dbMEx) {
|
|
||||||
throw new RuntimeException(dbMEx);
|
|
||||||
}
|
|
||||||
return dbRecords;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ReferenceDigestRecord getRecordById(final ReferenceDigestRecord referenceDigestRecord) {
|
|
||||||
LOGGER.debug("Getting record for {}", referenceDigestRecord);
|
|
||||||
if (referenceDigestRecord == null) {
|
|
||||||
LOGGER.error("null referenceDigestRecord argument");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (referenceDigestRecord.getId() == null) {
|
|
||||||
LOGGER.error("No id to get record from db {}", referenceDigestRecord);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
ReferenceDigestRecord dbRecord = null;
|
|
||||||
Transaction tx = null;
|
|
||||||
Session session = getFactory().getCurrentSession();
|
|
||||||
try {
|
|
||||||
LOGGER.debug("retrieving referenceDigestRecord from db");
|
|
||||||
tx = session.beginTransaction();
|
|
||||||
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
|
||||||
CriteriaQuery<ReferenceDigestRecord> criteriaQuery = criteriaBuilder.createQuery(ReferenceDigestRecord.class);
|
|
||||||
Root<ReferenceDigestRecord> root = criteriaQuery.from(ReferenceDigestRecord.class);
|
|
||||||
Predicate recordPredicate = criteriaBuilder
|
|
||||||
.and(criteriaBuilder.equal(root.get("id"), referenceDigestRecord.getId()));
|
|
||||||
criteriaQuery.select(root).where(recordPredicate);
|
|
||||||
Query<ReferenceDigestRecord> query = session.createQuery(criteriaQuery);
|
|
||||||
List<ReferenceDigestRecord> results = query.getResultList();
|
|
||||||
if (results != null && !results.isEmpty()) {
|
|
||||||
dbRecord = results.get(0);
|
|
||||||
}
|
|
||||||
// dbRecord = (ReferenceDigestRecord) session.createCriteria(ReferenceDigestRecord.class)
|
|
||||||
// .add(Restrictions.eq("id",
|
|
||||||
// referenceDigestRecord.getId())).uniqueResult();
|
|
||||||
tx.commit();
|
|
||||||
} catch (Exception ex) {
|
|
||||||
final String msg = "unable to retrieve object";
|
|
||||||
LOGGER.error(msg, ex);
|
|
||||||
if (tx != null) {
|
|
||||||
LOGGER.debug("rolling back transaction");
|
|
||||||
tx.rollback();
|
|
||||||
}
|
|
||||||
throw new DBManagerException(msg, ex);
|
|
||||||
}
|
|
||||||
return dbRecord;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ReferenceDigestRecord getRecordBySupportId(final UUID supportId) {
|
|
||||||
LOGGER.debug("Getting record for {}", supportId);
|
|
||||||
if (supportId == null) {
|
|
||||||
LOGGER.error("null supportId argument");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
ReferenceDigestRecord dbRecord = null;
|
|
||||||
Transaction tx = null;
|
|
||||||
Session session = getFactory().getCurrentSession();
|
|
||||||
try {
|
|
||||||
LOGGER.debug("retrieving referenceDigestRecord from db");
|
|
||||||
tx = session.beginTransaction();
|
|
||||||
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
|
||||||
CriteriaQuery<ReferenceDigestRecord> criteriaQuery = criteriaBuilder.createQuery(ReferenceDigestRecord.class);
|
|
||||||
Root<ReferenceDigestRecord> root = criteriaQuery.from(ReferenceDigestRecord.class);
|
|
||||||
Predicate recordPredicate = criteriaBuilder
|
|
||||||
.and(criteriaBuilder.equal(root.get("supportRim"), supportId));
|
|
||||||
criteriaQuery.select(root).where(recordPredicate);
|
|
||||||
Query<ReferenceDigestRecord> query = session.createQuery(criteriaQuery);
|
|
||||||
List<ReferenceDigestRecord> results = query.getResultList();
|
|
||||||
if (results != null && !results.isEmpty()) {
|
|
||||||
dbRecord = results.get(0);
|
|
||||||
}
|
|
||||||
// dbRecord = (ReferenceDigestRecord) session.createCriteria(ReferenceDigestRecord.class)
|
|
||||||
// .add(Restrictions.eq("supportRim", supportId)).uniqueResult();
|
|
||||||
tx.commit();
|
|
||||||
} catch (Exception ex) {
|
|
||||||
final String msg = "unable to retrieve object";
|
|
||||||
LOGGER.error(msg, ex);
|
|
||||||
if (tx != null) {
|
|
||||||
LOGGER.debug("rolling back transaction");
|
|
||||||
tx.rollback();
|
|
||||||
}
|
|
||||||
throw new DBManagerException(msg, ex);
|
|
||||||
}
|
|
||||||
return dbRecord;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<ReferenceDigestRecord> getRecordsByManufacturer(
|
|
||||||
final ReferenceDigestRecord referenceDigestRecord) {
|
|
||||||
LOGGER.debug("Getting records by manufacturer for {}", referenceDigestRecord);
|
|
||||||
if (referenceDigestRecord == null) {
|
|
||||||
LOGGER.error("null referenceDigestRecord argument");
|
|
||||||
throw new NullPointerException("null referenceDigestRecord");
|
|
||||||
}
|
|
||||||
if (referenceDigestRecord.getManufacturer() == null) {
|
|
||||||
LOGGER.error("null referenceDigestRecord manufacturer argument");
|
|
||||||
throw new NullPointerException("null referenceDigestRecord manufacturer");
|
|
||||||
}
|
|
||||||
|
|
||||||
List<ReferenceDigestRecord> dbRecords = new ArrayList<>();
|
|
||||||
String manufacturer = referenceDigestRecord.getManufacturer();
|
|
||||||
try {
|
|
||||||
List<ReferenceDigestRecord> dbTempList = super.getList(ReferenceDigestRecord.class);
|
|
||||||
for (ReferenceDigestRecord rdr : dbTempList) {
|
|
||||||
if (rdr.getManufacturer().equals(manufacturer)) {
|
|
||||||
dbRecords.add(rdr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (DBManagerException dbMEx) {
|
|
||||||
throw new RuntimeException(dbMEx);
|
|
||||||
}
|
|
||||||
return dbRecords;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<ReferenceDigestRecord> getRecordsByModel(
|
|
||||||
final ReferenceDigestRecord referenceDigestRecord) {
|
|
||||||
LOGGER.debug("Getting records by model for {}", referenceDigestRecord);
|
|
||||||
if (referenceDigestRecord == null) {
|
|
||||||
LOGGER.error("null referenceDigestRecord argument");
|
|
||||||
throw new NullPointerException("null referenceDigestRecord");
|
|
||||||
}
|
|
||||||
if (referenceDigestRecord.getModel() == null) {
|
|
||||||
LOGGER.error("null referenceDigestRecord model argument");
|
|
||||||
throw new NullPointerException("null referenceDigestRecord model");
|
|
||||||
}
|
|
||||||
|
|
||||||
List<ReferenceDigestRecord> dbRecords = new ArrayList<>();
|
|
||||||
String model = referenceDigestRecord.getModel();
|
|
||||||
try {
|
|
||||||
List<ReferenceDigestRecord> dbTempList = super.getList(ReferenceDigestRecord.class);
|
|
||||||
for (ReferenceDigestRecord rdr : dbTempList) {
|
|
||||||
if (rdr.getModel().equals(model)) {
|
|
||||||
dbRecords.add(rdr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (DBManagerException dbMEx) {
|
|
||||||
throw new RuntimeException(dbMEx);
|
|
||||||
}
|
|
||||||
return dbRecords;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void updateRecord(final ReferenceDigestRecord referenceDigestRecord) {
|
|
||||||
try {
|
|
||||||
super.update(referenceDigestRecord);
|
|
||||||
} catch (DBManagerException dbMEx) {
|
|
||||||
throw new RuntimeException(dbMEx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove a ReferenceDigestRecord from the database.
|
|
||||||
*
|
|
||||||
* @param referenceDigestRecord the referenceDigestRecord to delete
|
|
||||||
* @return true if deletion was successful, false otherwise
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean deleteRecord(final ReferenceDigestRecord referenceDigestRecord) {
|
|
||||||
boolean result = false;
|
|
||||||
LOGGER.info(String.format("Deleting reference to %s/%s",
|
|
||||||
referenceDigestRecord.getManufacturer(), referenceDigestRecord.getModel()));
|
|
||||||
try {
|
|
||||||
result = super.delete(referenceDigestRecord);
|
|
||||||
} catch (DBManagerException dbMEx) {
|
|
||||||
throw new RuntimeException(dbMEx);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
@ -30,7 +30,7 @@ public interface OrderedListQuerier<T> {
|
|||||||
* @throws DBManagerException if unable to create the list
|
* @throws DBManagerException if unable to create the list
|
||||||
*/
|
*/
|
||||||
FilteredRecordsList getOrderedList(
|
FilteredRecordsList getOrderedList(
|
||||||
Class<? extends T> clazz, String columnToOrder,
|
Class<T> clazz, String columnToOrder,
|
||||||
boolean ascending, int firstResult,
|
boolean ascending, int firstResult,
|
||||||
int maxResults, String search,
|
int maxResults, String search,
|
||||||
Map<String, Boolean> searchableColumns)
|
Map<String, Boolean> searchableColumns)
|
||||||
@ -59,7 +59,7 @@ public interface OrderedListQuerier<T> {
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("checkstyle:parameternumber")
|
@SuppressWarnings("checkstyle:parameternumber")
|
||||||
FilteredRecordsList<T> getOrderedList(
|
FilteredRecordsList<T> getOrderedList(
|
||||||
Class<? extends T> clazz, String columnToOrder,
|
Class<T> clazz, String columnToOrder,
|
||||||
boolean ascending, int firstResult,
|
boolean ascending, int firstResult,
|
||||||
int maxResults, String search,
|
int maxResults, String search,
|
||||||
Map<String, Boolean> searchableColumns, CriteriaModifier criteriaModifier)
|
Map<String, Boolean> searchableColumns, CriteriaModifier criteriaModifier)
|
||||||
|
@ -113,7 +113,7 @@ public class PersistenceConfiguration {
|
|||||||
@Bean
|
@Bean
|
||||||
public CertificateManager certificateManager() {
|
public CertificateManager certificateManager() {
|
||||||
DBCertificateManager manager = new DBCertificateManager(sessionFactory.getObject());
|
DBCertificateManager manager = new DBCertificateManager(sessionFactory.getObject());
|
||||||
setDbManagerRetrySettings(manager);
|
manager.setRetryTemplate(maxTransactionRetryAttempts, retryWaitTimeMilliseconds);
|
||||||
return manager;
|
return manager;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,19 +130,6 @@ public class PersistenceConfiguration {
|
|||||||
return manager;
|
return manager;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a {@link ReferenceDigestManager} ready to use.
|
|
||||||
*
|
|
||||||
* @return {@link ReferenceDigestManager}
|
|
||||||
*/
|
|
||||||
@Bean
|
|
||||||
public ReferenceDigestManager referenceDigestManager() {
|
|
||||||
DBReferenceDigestManager manager
|
|
||||||
= new DBReferenceDigestManager(sessionFactory.getObject());
|
|
||||||
setDbManagerRetrySettings(manager);
|
|
||||||
return manager;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a {@link ReferenceEventManager} ready to use.
|
* Creates a {@link ReferenceEventManager} ready to use.
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user