use of org.openmrs.api.db.SerializedObject in project openmrs-core by openmrs.
the class HibernateSerializedObjectDAO method getAllObjects.
/**
* @see SerializedObjectDAO#getAllObjects(Class, boolean)
*/
@Override
public <T extends OpenmrsObject> List<T> getAllObjects(Class<T> type, boolean includeRetired) throws DAOException {
List<T> ret = new ArrayList<>();
List<SerializedObject> objects = getAllSerializedObjects(type, includeRetired);
for (SerializedObject serializedObject : objects) {
ret.add(convertSerializedObject(type, serializedObject));
}
return ret;
}
use of org.openmrs.api.db.SerializedObject in project openmrs-module-mirebalais by PIH.
the class ReportSetup method setupReport.
/**
* This is only public for testing
* @param manager
*/
public static void setupReport(ReportManager manager, ReportService reportService, ReportDefinitionService reportDefinitionService, AdministrationService administrationService, SerializedObjectDAO serializedObjectDAO) {
if (alreadyAtLatestVersion(manager, administrationService)) {
return;
}
ReportDefinition reportDefinition = manager.constructReportDefinition();
log.info("Saving new definition of " + reportDefinition.getName());
ReportDefinition existing = reportDefinitionService.getDefinitionByUuid(reportDefinition.getUuid());
if (existing != null) {
// we need to overwrite the existing, rather than purge-and-recreate, to avoid deleting old ReportRequests
log.debug("overwriting existing ReportDefinition");
reportDefinition.setId(existing.getId());
// Context.evictFromSession(existing);
} else {
// incompatible class changes for a serialized object could mean that getting the definition return null
// and some serialization error gets logged. In that case we want to overwrite the invalid serialized definition
SerializedObject invalidSerializedObject = serializedObjectDAO.getSerializedObjectByUuid(reportDefinition.getUuid());
if (invalidSerializedObject != null) {
reportDefinition.setId(invalidSerializedObject.getId());
// Context.evictFromSession(invalidSerializedObject);
}
// serializedObjectDAO.purgeObject(invalidSerializedObject.getId());
}
reportDefinitionService.saveDefinition(reportDefinition);
// purging a ReportDesign doesn't trigger any extra logic, so we can just purge-and-recreate here
List<ReportDesign> existingDesigns = reportService.getReportDesigns(reportDefinition, null, true);
if (existingDesigns.size() > 0) {
log.debug("Deleting " + existingDesigns.size() + " old designs for " + reportDefinition.getName());
for (ReportDesign design : existingDesigns) {
reportService.purgeReportDesign(design);
}
}
try {
List<ReportDesign> designs = manager.constructReportDesigns(reportDefinition);
for (ReportDesign design : designs) {
reportService.saveReportDesign(design);
}
administrationService.setGlobalProperty(globalPropertyFor(manager), manager.getVersion());
} catch (IOException ex) {
log.error("Error constructing report design for " + reportDefinition.getName(), ex);
}
}
use of org.openmrs.api.db.SerializedObject in project openmrs-core by openmrs.
the class HibernateSerializedObjectDAO method getAllObjectsByName.
/**
* @see SerializedObjectDAO#getAllObjectsByName(Class, String, boolean)
*/
@Override
public <T extends OpenmrsMetadata> List<T> getAllObjectsByName(Class<T> type, String name, boolean exactMatchOnly) throws DAOException {
List<T> ret = new ArrayList<>();
List<SerializedObject> objects = getAllSerializedObjectsByName(type, name, exactMatchOnly);
for (SerializedObject serializedObject : objects) {
ret.add(convertSerializedObject(type, serializedObject));
}
return ret;
}
use of org.openmrs.api.db.SerializedObject in project openmrs-core by openmrs.
the class HibernateSerializedObjectDAO method getSerializedObjectByUuid.
/**
* @see SerializedObjectDAO#getSerializedObjectByUuid(String)
*/
@Override
public SerializedObject getSerializedObjectByUuid(String uuid) throws DAOException {
SerializedObject ret = null;
if (uuid != null) {
Criteria c = sessionFactory.getCurrentSession().createCriteria(SerializedObject.class);
c.add(Restrictions.eq("uuid", uuid));
ret = (SerializedObject) c.uniqueResult();
}
return ret;
}
use of org.openmrs.api.db.SerializedObject in project openmrs-core by openmrs.
the class HibernateSerializedObjectDAO method saveObject.
/**
* @see SerializedObjectDAO#saveObject(OpenmrsObject, OpenmrsSerializer)
*/
@Override
public <T extends OpenmrsObject> T saveObject(T object, OpenmrsSerializer serializer) throws DAOException {
Class<? extends OpenmrsObject> baseType = getRegisteredTypeForObject(object);
if (baseType == null) {
throw new DAOException("SerializedObjectDAO does not support saving objects of type <" + object.getClass() + ">");
}
SerializedObject serializedObject = getSerializedObject(object.getId());
if (serializedObject == null) {
serializedObject = new SerializedObject();
}
if (serializer == null) {
serializer = getSerializer(serializedObject);
}
if (object instanceof Auditable) {
Auditable auditableObj = (Auditable) object;
if (auditableObj.getCreator() == null) {
auditableObj.setCreator(Context.getAuthenticatedUser());
}
serializedObject.setCreator(auditableObj.getCreator());
if (auditableObj.getDateCreated() == null) {
auditableObj.setDateCreated(new Date());
}
serializedObject.setDateCreated(auditableObj.getDateCreated());
serializedObject.setChangedBy(auditableObj.getChangedBy());
serializedObject.setDateChanged(auditableObj.getDateChanged());
}
String data;
try {
data = serializer.serialize(object);
} catch (SerializationException e) {
throw new DAOException("Unable to save object <" + object + "> because serialization failed.", e);
}
serializedObject.setUuid(object.getUuid());
serializedObject.setType(baseType.getName());
serializedObject.setSubtype(object.getClass().getName());
serializedObject.setSerializationClass(serializer.getClass());
serializedObject.setSerializedData(data);
if (object instanceof OpenmrsMetadata) {
OpenmrsMetadata metaObj = (OpenmrsMetadata) object;
serializedObject.setName(metaObj.getName());
serializedObject.setDescription(metaObj.getDescription());
serializedObject.setRetired(metaObj.getRetired());
serializedObject.setRetiredBy(metaObj.getRetiredBy());
serializedObject.setDateRetired(metaObj.getDateRetired());
serializedObject.setRetireReason(metaObj.getRetireReason());
}
if (object instanceof OpenmrsData) {
OpenmrsData dataObj = (OpenmrsData) object;
serializedObject.setRetired(dataObj.getVoided());
serializedObject.setRetiredBy(dataObj.getVoidedBy());
serializedObject.setDateRetired(dataObj.getDateVoided());
serializedObject.setRetireReason(dataObj.getVoidReason());
}
sessionFactory.getCurrentSession().saveOrUpdate(serializedObject);
object.setId(serializedObject.getId());
return object;
}
Aggregations