Search in sources :

Example 1 with SerializedObject

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;
}
Also used : SerializedObject(org.openmrs.api.db.SerializedObject) ArrayList(java.util.ArrayList)

Example 2 with SerializedObject

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);
    }
}
Also used : SerializedObject(org.openmrs.api.db.SerializedObject) ReportDesign(org.openmrs.module.reporting.report.ReportDesign) IOException(java.io.IOException) ReportDefinition(org.openmrs.module.reporting.report.definition.ReportDefinition)

Example 3 with SerializedObject

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;
}
Also used : SerializedObject(org.openmrs.api.db.SerializedObject) ArrayList(java.util.ArrayList)

Example 4 with SerializedObject

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;
}
Also used : SerializedObject(org.openmrs.api.db.SerializedObject) Criteria(org.hibernate.Criteria)

Example 5 with SerializedObject

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;
}
Also used : DAOException(org.openmrs.api.db.DAOException) Auditable(org.openmrs.Auditable) SerializedObject(org.openmrs.api.db.SerializedObject) SerializationException(org.openmrs.serialization.SerializationException) OpenmrsData(org.openmrs.OpenmrsData) OpenmrsMetadata(org.openmrs.OpenmrsMetadata) Date(java.util.Date)

Aggregations

SerializedObject (org.openmrs.api.db.SerializedObject)6 ArrayList (java.util.ArrayList)2 IOException (java.io.IOException)1 Date (java.util.Date)1 Criteria (org.hibernate.Criteria)1 Auditable (org.openmrs.Auditable)1 OpenmrsData (org.openmrs.OpenmrsData)1 OpenmrsMetadata (org.openmrs.OpenmrsMetadata)1 DAOException (org.openmrs.api.db.DAOException)1 ReportDesign (org.openmrs.module.reporting.report.ReportDesign)1 ReportDefinition (org.openmrs.module.reporting.report.definition.ReportDefinition)1 SerializationException (org.openmrs.serialization.SerializationException)1