Search in sources :

Example 1 with OpenmrsData

use of org.openmrs.OpenmrsData 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

Date (java.util.Date)1 Auditable (org.openmrs.Auditable)1 OpenmrsData (org.openmrs.OpenmrsData)1 OpenmrsMetadata (org.openmrs.OpenmrsMetadata)1 DAOException (org.openmrs.api.db.DAOException)1 SerializedObject (org.openmrs.api.db.SerializedObject)1 SerializationException (org.openmrs.serialization.SerializationException)1