Search in sources :

Example 11 with DAOException

use of org.openmrs.api.db.DAOException 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

DAOException (org.openmrs.api.db.DAOException)11 User (org.openmrs.User)3 LoginCredential (org.openmrs.api.db.LoginCredential)3 Date (java.util.Date)2 Criteria (org.hibernate.Criteria)2 ConceptMapType (org.openmrs.ConceptMapType)2 SerializationException (org.openmrs.serialization.SerializationException)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 FlushMode (org.hibernate.FlushMode)1 Auditable (org.openmrs.Auditable)1 ConceptStopWord (org.openmrs.ConceptStopWord)1 Drug (org.openmrs.Drug)1 OpenmrsData (org.openmrs.OpenmrsData)1 OpenmrsMetadata (org.openmrs.OpenmrsMetadata)1 OpenmrsObject (org.openmrs.OpenmrsObject)1 SerializedObject (org.openmrs.api.db.SerializedObject)1