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.DBDeviceManager;
|
||||
import hirs.persist.DBReferenceDigestManager;
|
||||
import hirs.persist.DBReferenceEventManager;
|
||||
import hirs.persist.DBReferenceManifestManager;
|
||||
import hirs.persist.DeviceGroupManager;
|
||||
|
@ -13,7 +13,6 @@ import hirs.data.persist.SupportReferenceManifest;
|
||||
import hirs.data.persist.certificate.Certificate;
|
||||
import hirs.persist.CriteriaModifier;
|
||||
import hirs.persist.DBManagerException;
|
||||
import hirs.persist.DBReferenceDigestManager;
|
||||
import hirs.persist.DBReferenceEventManager;
|
||||
import hirs.persist.DBReferenceManifestManager;
|
||||
import hirs.persist.ReferenceDigestManager;
|
||||
|
@ -9,7 +9,6 @@ import hirs.persist.DBDeviceGroupManager;
|
||||
import hirs.persist.DBDeviceManager;
|
||||
import hirs.persist.DBManager;
|
||||
import hirs.persist.DBPolicyManager;
|
||||
import hirs.persist.DBReferenceDigestManager;
|
||||
import hirs.persist.DBReferenceEventManager;
|
||||
import hirs.persist.DBReferenceManifestManager;
|
||||
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.
|
||||
*/
|
||||
@XmlTransient
|
||||
@XmlSeeAlso(value = { IntegrityReportRequest.class, TPMReportRequest.class, IMAReportRequest.class,
|
||||
@XmlSeeAlso(value = { IntegrityReportRequest.class, TPMReportRequest.class,
|
||||
DeviceInfoReportRequest.class })
|
||||
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 int MAX_CLASS_CACHE_ENTRIES = 500;
|
||||
|
||||
private final ArchivableEntity entity;
|
||||
private Class<T> clazz;
|
||||
|
||||
private SessionFactory factory;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param sessionFactory the session factory to use to interact with the database
|
||||
*/
|
||||
public AbstractDbManager(final ArchivableEntity entity, final SessionFactory sessionFactory) {
|
||||
if (entity == null) {
|
||||
public AbstractDbManager(final Class<T> clazz, final SessionFactory sessionFactory) {
|
||||
if (clazz == null) {
|
||||
LOGGER.error("AbstractDbManager cannot be instantiated with a null class");
|
||||
throw new IllegalArgumentException(
|
||||
"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"
|
||||
);
|
||||
}
|
||||
this.entity = entity;
|
||||
this.clazz = clazz;
|
||||
this.factory = sessionFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setClazz(final Class<T> classToSet) {
|
||||
this.clazz = classToSet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the currently configured database implementation.
|
||||
*
|
||||
@ -119,15 +124,13 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
||||
try {
|
||||
LOGGER.debug("retrieving object from db");
|
||||
tx = session.beginTransaction();
|
||||
Object obj = session.get(entity, id);
|
||||
if (obj instanceof hirs.data.persist.ArchivableEntity) {
|
||||
hirs.data.persist.ArchivableEntity objectOfTypeT = (hirs.data.persist.ArchivableEntity) obj;
|
||||
Object obj = session.get(clazz, id);
|
||||
|
||||
T objectOfTypeT = (T) obj;
|
||||
LOGGER.debug("found object, deleting it");
|
||||
session.delete(objectOfTypeT);
|
||||
deleted = true;
|
||||
} else {
|
||||
LOGGER.debug("object not found");
|
||||
}
|
||||
|
||||
tx.commit();
|
||||
} catch (Exception e) {
|
||||
final String msg = "unable to retrieve object";
|
||||
@ -164,16 +167,16 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
||||
Transaction tx = null;
|
||||
Session session = factory.getCurrentSession();
|
||||
CriteriaBuilder builder = session.getCriteriaBuilder();
|
||||
CriteriaQuery<hirs.data.persist.ArchivableEntity> criteria = builder.createQuery(entity);
|
||||
CriteriaQuery<T> criteria = builder.createQuery(clazz);
|
||||
try {
|
||||
LOGGER.debug("retrieving object from db");
|
||||
tx = session.beginTransaction();
|
||||
Root<hirs.data.persist.ArchivableEntity> myObjectRoot = criteria.from(clazz);
|
||||
Join<hirs.data.persist.ArchivableEntity, JoinObject> joinObject = myObjectRoot.join("joinObject");
|
||||
Object object = session.getSessionFactory().getCurrentSession().createCriteria(clazz)
|
||||
Root<T> myObjectRoot = criteria.from(this.clazz.getClass());
|
||||
Join<T, JoinObject> joinObject = myObjectRoot.join("joinObject");
|
||||
Object object = session.getSessionFactory().getCurrentSession().createCriteria(this.clazz.getClass())
|
||||
.add(Restrictions.eq("name", name)).uniqueResult();
|
||||
if (object instanceof hirs.data.persist.ArchivableEntity) {
|
||||
hirs.data.persist.ArchivableEntity objectOfTypeT = (hirs.data.persist.ArchivableEntity) object;
|
||||
if (object instanceof T) {
|
||||
T objectOfTypeT = (T) object;
|
||||
LOGGER.debug("found object, deleting it");
|
||||
session.delete(objectOfTypeT);
|
||||
deleted = true;
|
||||
@ -240,29 +243,27 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
||||
Transaction tx = null;
|
||||
Session session = factory.getCurrentSession();
|
||||
try {
|
||||
LOGGER.debug("Deleting instances of class: {}", clazz);
|
||||
LOGGER.debug("Deleting instances of class: {}", this.clazz.getClass());
|
||||
tx = session.beginTransaction();
|
||||
CriteriaBuilder builder = session.getCriteriaBuilder();
|
||||
CriteriaQuery<ArchivableEntity> criteriaQuery = builder.createQuery(clazz);
|
||||
Root<hirs.data.persist.ArchivableEntity> root = criteriaQuery.from(clazz);
|
||||
CriteriaQuery<T> criteriaQuery = builder.createQuery(this.clazz.getClass());
|
||||
Root<T> root = criteriaQuery.from(this.clazz.getClass());
|
||||
Predicate recordPredicate = builder.and(
|
||||
);
|
||||
criteriaQuery.select(root).where(recordPredicate);
|
||||
Query<ArchivableEntity> query = session.createQuery(criteriaQuery);
|
||||
List<ArchivableEntity> results = query.getResultList();
|
||||
ArchivableEntity ret = null;
|
||||
Query<T> query = session.createQuery(criteriaQuery);
|
||||
List<T> results = query.getResultList();
|
||||
T ret = null;
|
||||
if (results != null && !results.isEmpty()) {
|
||||
ret = results.get(0);
|
||||
}
|
||||
|
||||
List instances = session.createCriteria(clazz)
|
||||
List instances = session.createCriteria(this.clazz.getClass())
|
||||
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
|
||||
for (Object instance : instances) {
|
||||
if (instance instanceof ArchivableEntity) {
|
||||
session.delete((ArchivableEntity) instance);
|
||||
session.delete((T) instance);
|
||||
numEntitiesDeleted++;
|
||||
}
|
||||
}
|
||||
tx.commit();
|
||||
} catch (Exception e) {
|
||||
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
|
||||
* the result objects
|
||||
*/
|
||||
protected List<ArchivableEntity> doGetWithCriteria(final Collection<Criterion> criteriaCollection)
|
||||
protected List<T> doGetWithCriteria(final Collection<Criterion> criteriaCollection)
|
||||
throws DBManagerException {
|
||||
return doGetWithCriteria(entity, criteriaCollection);
|
||||
return doGetWithCriteria(clazz, criteriaCollection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a Criteria query using the given collection of Criterion over the
|
||||
* 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)
|
||||
* @param clazzToGet the class of object to retrieve
|
||||
* @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
|
||||
* the result objects
|
||||
*/
|
||||
protected final List<ArchivableEntity> doGetWithCriteria(
|
||||
final Class<ArchivableEntity> clazzToGet,
|
||||
protected final List<T> doGetWithCriteria(
|
||||
final Class<T> clazzToGet,
|
||||
final Collection<Criterion> criteriaCollection
|
||||
) throws DBManagerException {
|
||||
LOGGER.debug("running criteria query over: {}", clazzToGet);
|
||||
@ -313,7 +314,7 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
||||
LOGGER.debug("null object argument");
|
||||
throw new NullPointerException("criteria or restrictions");
|
||||
}
|
||||
List<ArchivableEntity> ret = new ArrayList<>();
|
||||
List<T> ret = new ArrayList<>();
|
||||
Transaction tx = null;
|
||||
Session session = factory.getCurrentSession();
|
||||
try {
|
||||
@ -353,7 +354,7 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
||||
* @throws DBManagerException if object has previously been saved or an
|
||||
* 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);
|
||||
if (object == null) {
|
||||
LOGGER.debug("null object argument");
|
||||
@ -366,9 +367,9 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
||||
LOGGER.debug("saving object in db");
|
||||
tx = session.beginTransaction();
|
||||
final Serializable id = session.save(object);
|
||||
Object o = session.get(object.getClass(), id);
|
||||
T o = (T) session.get(object.getClass(), id);
|
||||
session.getTransaction().commit();
|
||||
return (ArchivableEntity) o;
|
||||
return o;
|
||||
} catch (Exception e) {
|
||||
final String msg = "unable to save object";
|
||||
LOGGER.error(msg, e);
|
||||
@ -387,7 +388,7 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
||||
* @param object object to update
|
||||
* @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");
|
||||
if (object == null) {
|
||||
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
|
||||
* 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);
|
||||
if (name == null) {
|
||||
LOGGER.debug("null name argument");
|
||||
@ -432,17 +433,17 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
||||
Transaction tx = null;
|
||||
Session session = factory.getCurrentSession();
|
||||
try {
|
||||
LOGGER.debug("retrieving " + entity.toString() + " from db");
|
||||
LOGGER.debug("retrieving " + clazz.toString() + " from db");
|
||||
tx = session.beginTransaction();
|
||||
CriteriaBuilder builder = session.getCriteriaBuilder();
|
||||
CriteriaQuery<ArchivableEntity> criteriaQuery = builder.createQuery(entity);
|
||||
Root<ArchivableEntity> root = criteriaQuery.from(entity);
|
||||
CriteriaQuery<T> criteriaQuery = builder.createQuery(clazz);
|
||||
Root<T> root = criteriaQuery.from(clazz);
|
||||
Predicate recordPredicate = builder.and(
|
||||
builder.equal(root.get("name"), name));
|
||||
criteriaQuery.select(root).where(recordPredicate);
|
||||
Query<ArchivableEntity> query = session.createQuery(criteriaQuery);
|
||||
List<ArchivableEntity> results = query.getResultList();
|
||||
ArchivableEntity ret = null;
|
||||
Query<T> query = session.createQuery(criteriaQuery);
|
||||
List<T> results = query.getResultList();
|
||||
T ret = null;
|
||||
if (results != null && !results.isEmpty()) {
|
||||
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
|
||||
* 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);
|
||||
if (id == null) {
|
||||
LOGGER.debug("null id argument");
|
||||
@ -482,7 +483,7 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
||||
try {
|
||||
LOGGER.debug("retrieving object from db");
|
||||
tx = session.beginTransaction();
|
||||
ArchivableEntity ret = (ArchivableEntity) session.get(entity, id);
|
||||
T ret = (T) session.get(clazz, id);
|
||||
tx.commit();
|
||||
return ret;
|
||||
} 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
|
||||
* <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>)
|
||||
* @param additionalRestriction - an added Criterion to use in the query, null for none
|
||||
* @return list of <code>T</code> names
|
||||
* @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)
|
||||
throws DBManagerException {
|
||||
LOGGER.debug("Getting object list");
|
||||
ArchivableEntity searchClass = entity;
|
||||
if (entity == 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;
|
||||
Session session = factory.getCurrentSession();
|
||||
try {
|
||||
LOGGER.debug("Retrieving objects from db of class {}", searchClass.getName());
|
||||
LOGGER.debug("Retrieving objects from db of class {}", searchClass);
|
||||
tx = session.beginTransaction();
|
||||
CriteriaBuilder builder = session.getCriteriaBuilder();
|
||||
CriteriaQuery<?> criteriaQuery = builder.createQuery(searchClass);
|
||||
Root<?> root = criteriaQuery.from(criteriaQuery.getResultType());
|
||||
CriteriaQuery<T> criteriaQuery = builder.createQuery(searchClass);
|
||||
Root<T> root = criteriaQuery.from(criteriaQuery.getResultType());
|
||||
Predicate recordPredicate = builder.and(
|
||||
|
||||
);
|
||||
criteriaQuery.select(root).where(recordPredicate).distinct(true);
|
||||
Query<?> query = session.createQuery(criteriaQuery);
|
||||
List<?> results = query.getResultList();
|
||||
Query<T> query = session.createQuery(criteriaQuery);
|
||||
List<T> results = query.getResultList();
|
||||
|
||||
// Criteria criteria = session.createCriteria(searchClass);
|
||||
if (additionalRestriction != null) {
|
||||
criteria.add(additionalRestriction);
|
||||
}
|
||||
// if (additionalRestriction != null) {
|
||||
// criteriaQuery.add(additionalRestriction);
|
||||
// }
|
||||
// List list = criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
|
||||
for (Object o : results) {
|
||||
if (o instanceof ArchivableEntity) {
|
||||
objects.add((ArchivableEntity) o);
|
||||
}
|
||||
objects.add((T) o);
|
||||
}
|
||||
tx.commit();
|
||||
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
|
||||
*/
|
||||
@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 int maxResults, final String search, final Map<String, Boolean> searchableColumns,
|
||||
final CriteriaModifier criteriaModifier) throws DBManagerException {
|
||||
LOGGER.debug("Getting object list");
|
||||
ArchivableEntity searchClass = clazz;
|
||||
Class<T> searchClass = clazz;
|
||||
if (clazz == null) {
|
||||
LOGGER.debug("clazz is null");
|
||||
searchClass = this.clazz;
|
||||
@ -598,9 +597,9 @@ public abstract class AbstractDbManager<T> implements CrudManager<T> {
|
||||
}
|
||||
|
||||
//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;
|
||||
Session session = factory.getCurrentSession();
|
||||
try {
|
||||
|
@ -1,6 +1,5 @@
|
||||
package hirs.persist;
|
||||
|
||||
import hirs.data.persist.AbstractEntity;
|
||||
import org.hibernate.criterion.Criterion;
|
||||
|
||||
import java.io.Serializable;
|
||||
@ -10,7 +9,7 @@ import java.util.List;
|
||||
* Interface defining database CRUD operations (Create, Read, Update, Delete).
|
||||
* @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
|
||||
* 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
|
||||
@ -36,7 +35,7 @@ public interface CrudManager<T extends Serializable> extends OrderedListQuerier<
|
||||
* @param object object to update
|
||||
* @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
|
||||
@ -48,7 +47,7 @@ public interface CrudManager<T extends Serializable> extends OrderedListQuerier<
|
||||
* @throws DBManagerException if unable to search the database or recreate
|
||||
* 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
|
||||
* <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
|
||||
* @throws DBManagerException if unable to search the database
|
||||
*/
|
||||
List<T> getList()
|
||||
throws DBManagerException;
|
||||
List<T> getList(T object) throws DBManagerException;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* <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
|
||||
* @return list of <code>T</code> names
|
||||
* @throws DBManagerException if unable to search the database
|
||||
*/
|
||||
List<AbstractEntity> getList(Criterion additionalRestriction)
|
||||
List<T> getList(T object, Criterion additionalRestriction)
|
||||
throws DBManagerException;
|
||||
|
||||
/**
|
||||
@ -119,7 +121,7 @@ public interface CrudManager<T extends Serializable> extends OrderedListQuerier<
|
||||
* @return true if successfully found and deleted the object
|
||||
* @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.
|
||||
|
@ -1,23 +1,37 @@
|
||||
package hirs.persist;
|
||||
|
||||
import hirs.FilteredRecordsList;
|
||||
import hirs.data.persist.certificate.Certificate;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
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.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
|
||||
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
|
||||
* database.
|
||||
@ -51,11 +65,11 @@ public class DBCertificateManager extends DBManager<Certificate>
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends Certificate> Set<T> get(final CertificateSelector certificateSelector) {
|
||||
return new HashSet<>(
|
||||
(List<T>) getWithCriteria(
|
||||
certificateSelector.getCertificateClass(),
|
||||
Collections.singleton(certificateSelector.getCriterion())
|
||||
)
|
||||
return new HashSet<>(0
|
||||
// (List<T>) getWithCriteria(
|
||||
// certificateSelector.getCertificateClass(),
|
||||
// Collections.singleton(certificateSelector.getCriterion())
|
||||
// )
|
||||
);
|
||||
}
|
||||
|
||||
@ -68,4 +82,164 @@ public class DBCertificateManager extends DBManager<Certificate>
|
||||
public boolean deleteCertificate(final Certificate 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;
|
||||
|
||||
import hirs.FilteredRecordsList;
|
||||
import hirs.data.persist.AbstractEntity;
|
||||
import hirs.data.persist.ArchivableEntity;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
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
|
||||
* 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);
|
||||
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
|
||||
* 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 {
|
||||
return retryTemplate.execute(
|
||||
new RetryCallback<List<AbstractEntity>, DBManagerException>() {
|
||||
new RetryCallback<List<T>, DBManagerException>() {
|
||||
@Override
|
||||
public List<AbstractEntity> doWithRetry(final RetryContext context)
|
||||
public List<T> doWithRetry(final RetryContext context)
|
||||
throws DBManagerException {
|
||||
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
|
||||
* the result objects
|
||||
*/
|
||||
protected final List<hirs.data.persist.ArchivableEntity> getWithCriteria(
|
||||
final Class<hirs.data.persist.ArchivableEntity> clazzToGet,
|
||||
protected final List<T> getWithCriteria(
|
||||
final Class<T> clazzToGet,
|
||||
final Collection<Criterion> criteriaCollection) throws DBManagerException {
|
||||
return retryTemplate.execute(
|
||||
new RetryCallback<List<AbstractEntity>, DBManagerException>() {
|
||||
new RetryCallback<List<T>, DBManagerException>() {
|
||||
@Override
|
||||
public List<hirs.data.persist.ArchivableEntity> doWithRetry(final RetryContext context)
|
||||
public List<T> doWithRetry(final RetryContext context)
|
||||
throws DBManagerException {
|
||||
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
|
||||
* error occurs while trying to save it to the database
|
||||
*/
|
||||
public final AbstractEntity save(final AbstractEntity object) throws DBManagerException {
|
||||
return retryTemplate.execute(new RetryCallback<AbstractEntity, DBManagerException>() {
|
||||
public final T save(final T object) throws DBManagerException {
|
||||
return retryTemplate.execute(new RetryCallback<T, DBManagerException>() {
|
||||
@Override
|
||||
public AbstractEntity doWithRetry(final RetryContext context) throws DBManagerException {
|
||||
public T doWithRetry(final RetryContext context) throws DBManagerException {
|
||||
return doSave(object);
|
||||
}
|
||||
});
|
||||
@ -194,7 +193,7 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
||||
* @param object object to update
|
||||
* @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>() {
|
||||
@Override
|
||||
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
|
||||
* the <code>Object</code>
|
||||
*/
|
||||
public final ArchivableEntity get(final String name) throws DBManagerException {
|
||||
return retryTemplate.execute(new RetryCallback<ArchivableEntity, DBManagerException>() {
|
||||
public final T get(final String name) throws DBManagerException {
|
||||
return retryTemplate.execute(new RetryCallback<T, DBManagerException>() {
|
||||
@Override
|
||||
public ArchivableEntity doWithRetry(final RetryContext context) throws DBManagerException {
|
||||
public T doWithRetry(final RetryContext context) throws DBManagerException {
|
||||
return doGet(name);
|
||||
}
|
||||
});
|
||||
@ -233,10 +232,10 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
||||
* @throws DBManagerException if unable to search the database or recreate
|
||||
* the <code>Object</code>
|
||||
*/
|
||||
public final AbstractEntity get(final Serializable id) throws DBManagerException {
|
||||
return retryTemplate.execute(new RetryCallback<AbstractEntity, DBManagerException>() {
|
||||
public final T get(final Serializable id) throws DBManagerException {
|
||||
return retryTemplate.execute(new RetryCallback<T, DBManagerException>() {
|
||||
@Override
|
||||
public AbstractEntity doWithRetry(final RetryContext context) throws DBManagerException {
|
||||
public T doWithRetry(final RetryContext context) throws DBManagerException {
|
||||
return doGet(id);
|
||||
}
|
||||
});
|
||||
@ -257,12 +256,13 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
||||
* @throws DBManagerException if unable to search the database or recreate
|
||||
* 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 {
|
||||
return retryTemplate.execute(new RetryCallback<AbstractEntity, DBManagerException>() {
|
||||
return retryTemplate.execute(new RetryCallback<T, DBManagerException>() {
|
||||
@Override
|
||||
public AbstractEntity doWithRetry(final RetryContext context) throws DBManagerException {
|
||||
return doGetAndLoadLazyFields(name, recurse);
|
||||
public T doWithRetry(final RetryContext context) throws DBManagerException {
|
||||
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
|
||||
* 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 {
|
||||
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
|
||||
* @throws DBManagerException if unable to search the database
|
||||
*/
|
||||
public List<AbstractEntity> getList(final AbstractEntity entity)
|
||||
public List<T> getList(final T entity)
|
||||
throws DBManagerException {
|
||||
return getList(entity, null);
|
||||
}
|
||||
@ -320,11 +321,11 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
||||
* @throws DBManagerException if unable to search the database
|
||||
*/
|
||||
@Override
|
||||
public List<AbstractEntity> getList(final AbstractEntity entity, final Criterion additionalRestriction)
|
||||
public List<T> getList(final T entity, final Criterion additionalRestriction)
|
||||
throws DBManagerException {
|
||||
return retryTemplate.execute(new RetryCallback<List<AbstractEntity>, DBManagerException>() {
|
||||
return retryTemplate.execute(new RetryCallback<List<T>, DBManagerException>() {
|
||||
@Override
|
||||
public List<AbstractEntity> doWithRetry(final RetryContext context) throws DBManagerException {
|
||||
public List<T> doWithRetry(final RetryContext context) throws DBManagerException {
|
||||
return doGetList(entity, additionalRestriction);
|
||||
}
|
||||
});
|
||||
@ -350,7 +351,7 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
||||
*/
|
||||
@Override
|
||||
public final FilteredRecordsList getOrderedList(
|
||||
final AbstractEntity clazz, final String columnToOrder,
|
||||
final Class<T> clazz, final String columnToOrder,
|
||||
final boolean ascending, final int firstResult,
|
||||
final int maxResults, final String search,
|
||||
final Map<String, Boolean> searchableColumns)
|
||||
@ -388,17 +389,17 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
||||
* @throws DBManagerException if unable to create the list
|
||||
*/
|
||||
@SuppressWarnings("checkstyle:parameternumber")
|
||||
public final FilteredRecordsList<AbstractEntity> getOrderedList(
|
||||
final AbstractEntity clazz, final String columnToOrder,
|
||||
public final FilteredRecordsList<T> getOrderedList(
|
||||
final Class<T> clazz, final String columnToOrder,
|
||||
final boolean ascending, final int firstResult,
|
||||
final int maxResults, final String search,
|
||||
final Map<String, Boolean> searchableColumns, final CriteriaModifier criteriaModifier)
|
||||
throws DBManagerException {
|
||||
|
||||
return retryTemplate.execute(
|
||||
new RetryCallback<FilteredRecordsList<AbstractEntity>, DBManagerException>() {
|
||||
new RetryCallback<FilteredRecordsList<T>, DBManagerException>() {
|
||||
@Override
|
||||
public FilteredRecordsList<AbstractEntity> doWithRetry(final RetryContext context)
|
||||
public FilteredRecordsList<T> doWithRetry(final RetryContext context)
|
||||
throws DBManagerException {
|
||||
return doGetOrderedList(clazz, columnToOrder, ascending,
|
||||
firstResult, maxResults,
|
||||
@ -419,7 +420,6 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
||||
* @throws DBManagerException if unable to find the baseline or delete it
|
||||
* from the database
|
||||
*/
|
||||
|
||||
public final boolean delete(final String name) throws DBManagerException {
|
||||
return retryTemplate.execute(new RetryCallback<Boolean, DBManagerException>() {
|
||||
@Override
|
||||
@ -464,7 +464,7 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
||||
* @throws DBManagerException if unable to delete the object from the database
|
||||
*/
|
||||
@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>() {
|
||||
@Override
|
||||
public Boolean doWithRetry(final RetryContext context) throws DBManagerException {
|
||||
@ -489,7 +489,7 @@ public class DBManager<T> extends AbstractDbManager<T> {
|
||||
return false;
|
||||
}
|
||||
|
||||
ArchivableEntity target = get(name);
|
||||
T target = get(name);
|
||||
if (target == null) {
|
||||
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
|
||||
*/
|
||||
FilteredRecordsList getOrderedList(
|
||||
Class<? extends T> clazz, String columnToOrder,
|
||||
Class<T> clazz, String columnToOrder,
|
||||
boolean ascending, int firstResult,
|
||||
int maxResults, String search,
|
||||
Map<String, Boolean> searchableColumns)
|
||||
@ -59,7 +59,7 @@ public interface OrderedListQuerier<T> {
|
||||
*/
|
||||
@SuppressWarnings("checkstyle:parameternumber")
|
||||
FilteredRecordsList<T> getOrderedList(
|
||||
Class<? extends T> clazz, String columnToOrder,
|
||||
Class<T> clazz, String columnToOrder,
|
||||
boolean ascending, int firstResult,
|
||||
int maxResults, String search,
|
||||
Map<String, Boolean> searchableColumns, CriteriaModifier criteriaModifier)
|
||||
|
@ -113,7 +113,7 @@ public class PersistenceConfiguration {
|
||||
@Bean
|
||||
public CertificateManager certificateManager() {
|
||||
DBCertificateManager manager = new DBCertificateManager(sessionFactory.getObject());
|
||||
setDbManagerRetrySettings(manager);
|
||||
manager.setRetryTemplate(maxTransactionRetryAttempts, retryWaitTimeMilliseconds);
|
||||
return manager;
|
||||
}
|
||||
|
||||
@ -130,19 +130,6 @@ public class PersistenceConfiguration {
|
||||
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.
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user