Search in sources :

Example 1 with EntityException

use of alma.acs.entityutil.EntityException in project ACS by ACS-Community.

the class CastorMarshalMapper method translate.

/**
	 * Converts a Castor generated entity object to the marshalled representation as
	 * an <code>XmlEntityStruct</code>.
	 *  
	 * @see alma.acs.component.dynwrapper.TypeMapper#translate(java.lang.Object, java.lang.Object, java.lang.Class,
	 * alma.acs.component.dynwrapper.ComponentInvocationHandler)
	 */
@SuppressWarnings("unchecked")
public Object translate(Object oldObject, Object newObjectTemplate, Class newObjectClass, ComponentInvocationHandler invHandler) throws DynWrapperException {
    if (oldObject == null) {
        return null;
    }
    EntityT entityMeta;
    try {
        entityMeta = m_entityTFinder.extractEntityT(oldObject);
    } catch (EntityException e) {
        String extractionErrMsg = "failed to extract EntityT object from " + oldObject.getClass().getName();
        throw new DynWrapperException(extractionErrMsg, e);
    }
    if (entityMeta == null) {
        String extractionNullErrMsg = "entity object of type " + oldObject.getClass().getName() + " must have a non-null child assignable to " + EntityT.class.getName() + " to be serialized.";
        throw new DynWrapperException(extractionNullErrMsg);
    }
    XmlEntityStruct entStruct = null;
    // dealt with using a Holder class. Just confused right now...
    if (newObjectTemplate != null) {
        if (!XmlEntityStruct.class.isInstance(newObjectTemplate)) {
            throw new DynWrapperException("invalid template for XmlEntityStruct object");
        }
        entStruct = (XmlEntityStruct) newObjectTemplate;
    } else {
        entStruct = new XmlEntityStruct();
    }
    entStruct.entityId = entityMeta.getEntityId();
    entStruct.entityTypeName = entityMeta.getEntityTypeName();
    entStruct.schemaVersion = (entityMeta.getSchemaVersion() != null ? entityMeta.getSchemaVersion() : "");
    try {
        StringWriter wr = new StringWriter();
        Marshaller marsh = new Marshaller(wr);
        marsh.setValidation(false);
        marsh.marshal(oldObject);
        entStruct.xmlString = wr.toString();
    } catch (Exception e) {
        String msg = "failed to marshal entity object with id='" + entityMeta.getEntityId() + "' using the Castor Marshaller without validation.";
        throw new DynWrapperException(msg);
    }
    if (m_verbose) {
        m_logger.finer("successfully translated from '" + oldObject.getClass().getName() + "' to '" + entStruct.getClass().getName() + "'.");
    }
    return entStruct;
}
Also used : Marshaller(org.exolab.castor.xml.Marshaller) StringWriter(java.io.StringWriter) EntityT(alma.entities.commonentity.EntityT) EntityException(alma.acs.entityutil.EntityException) XmlEntityStruct(alma.xmlentity.XmlEntityStruct) EntityException(alma.acs.entityutil.EntityException)

Aggregations

EntityException (alma.acs.entityutil.EntityException)1 EntityT (alma.entities.commonentity.EntityT)1 XmlEntityStruct (alma.xmlentity.XmlEntityStruct)1 StringWriter (java.io.StringWriter)1 Marshaller (org.exolab.castor.xml.Marshaller)1