Search in sources :

Example 1 with IAttachmentData

use of org.jaffa.components.attachment.apis.IAttachmentData in project jaffa-framework by jaffa-projects.

the class AttachmentHandler method endBeanLoad.

/**
 * Called after a bean has been loaded.
 * @param path This is the source path of this graph, used when processing a more complex tree, where this is the path to get to this root object being processed.
 * @param source the domain object that was just queried.
 * @param target the graph object being molded.
 * @param filter Filter object that it is used to control what fields are populated or the target objects.
 * @param originalCriteria the original graph criteria.
 * @throws ApplicationException if any Application error occurs.
 * @throws ApplicationExceptions if multiple Application errors occur.
 * @throws FrameworkException if an internal error occurs.
 */
@Override
public void endBeanLoad(String path, Object source, Object target, MappingFilter filter, GraphCriteria originalCriteria) throws ApplicationException, ApplicationExceptions, FrameworkException {
    checkFunctionAccess((Attachment) source, false);
    if (source instanceof Attachment && target instanceof AttachmentGraph) {
        AttachmentGraph attachmentGraph = (AttachmentGraph) target;
        Attachment attachment = (Attachment) source;
        IAttachmentData attachmentDataImpl = AttachmentFactory.getAttachmentDataImpl();
        if (attachmentDataImpl != null && attachment.getDocumentReferenceId() != null && (originalCriteria.getResultGraphRules() == null || originalCriteria.getResultGraphRules().length == 0 || !Arrays.asList(originalCriteria.getResultGraphRules()).contains("-data"))) {
            byte[] data = attachmentDataImpl.read(attachment.getDocumentReferenceId());
            if (data != null) {
                attachmentGraph.setData(data);
            }
        }
    }
}
Also used : AttachmentGraph(org.jaffa.components.attachment.apis.data.AttachmentGraph) Attachment(org.jaffa.components.attachment.domain.Attachment) IAttachmentData(org.jaffa.components.attachment.apis.IAttachmentData)

Example 2 with IAttachmentData

use of org.jaffa.components.attachment.apis.IAttachmentData in project jaffa-framework by jaffa-projects.

the class AttachmentHandler method endBeanClone.

@Override
public void endBeanClone(String path, Object source, Object target, Object newGraph) throws ApplicationException, ApplicationExceptions, FrameworkException {
    if (source instanceof AttachmentGraph && target instanceof Attachment) {
        AttachmentGraph attachmentGraph = (AttachmentGraph) source;
        Attachment attachment = (Attachment) target;
        IAttachmentData attachmentDataImpl = AttachmentFactory.getAttachmentDataImpl();
        if (attachmentDataImpl != null && attachment.getDocumentReferenceId() != null) {
            if (attachmentDataImpl.read(attachment.getDocumentReferenceId()) == null) {
                attachmentDataImpl.create(attachment.getDocumentReferenceId(), attachmentGraph.getData());
            }
        }
    }
}
Also used : AttachmentGraph(org.jaffa.components.attachment.apis.data.AttachmentGraph) Attachment(org.jaffa.components.attachment.domain.Attachment) IAttachmentData(org.jaffa.components.attachment.apis.IAttachmentData)

Example 3 with IAttachmentData

use of org.jaffa.components.attachment.apis.IAttachmentData in project jaffa-framework by jaffa-projects.

the class AttachmentHandler method endBeanAdd.

public void endBeanAdd(String path, Object source, Object target) throws ApplicationException, ApplicationExceptions, FrameworkException {
    if (source instanceof AttachmentGraph && target instanceof Attachment) {
        AttachmentGraph attachmentGraph = (AttachmentGraph) source;
        Attachment attachment = (Attachment) target;
        IAttachmentData attachmentDataImpl = AttachmentFactory.getAttachmentDataImpl();
        if (attachmentDataImpl != null && attachment.getDocumentReferenceId() != null) {
            if (attachmentDataImpl.read(attachment.getDocumentReferenceId()) == null) {
                attachmentDataImpl.create(attachment.getDocumentReferenceId(), attachmentGraph.getData());
            }
        }
    }
}
Also used : AttachmentGraph(org.jaffa.components.attachment.apis.data.AttachmentGraph) Attachment(org.jaffa.components.attachment.domain.Attachment) IAttachmentData(org.jaffa.components.attachment.apis.IAttachmentData)

Example 4 with IAttachmentData

use of org.jaffa.components.attachment.apis.IAttachmentData in project jaffa-framework by jaffa-projects.

the class Attachment method validate.

// .//GEN-END:preUpdate_2_be
// All the custom code goes here//GEN-FIRST:custom
/**
 * {@inheritDoc}
 */
@Override
public void validate() throws ApplicationExceptions, FrameworkException {
    IAttachmentData attachmentDataImpl = AttachmentFactory.getAttachmentDataImpl();
    // Ensure that the attachment is specified
    if (attachmentDataImpl == null) {
        if (getOriginalFileName() == null || ("E".equals(getAttachmentType()) && (getData() == null || getData().length == 0)))
            throw new ApplicationExceptions(new MandatoryFieldException(AttachmentMeta.META_ORIGINAL_FILE_NAME.getLabelToken()));
        // Blank out the uploaded data, if any, for a non-embedded attachment
        if (!"E".equals(getAttachmentType()) && getData() != null) {
            if (log.isDebugEnabled())
                log.debug("Ensuring that the uploaded data, if any, is blanked out for a non-embedded attachment");
            try {
                setData(null);
            } catch (ValidationException e) {
                throw new ApplicationExceptions(e);
            }
        }
    }
    // Generate the technical-key, if required
    if (getAttachmentId() == null) {
        try {
            IVoucherGenerator vg = VoucherGeneratorFactory.instance();
            vg.setUow(getUOW());
            vg.setDomainClassName(getClass().getName());
            vg.setFieldName(AttachmentMeta.ATTACHMENT_ID);
            vg.setLabelToken(AttachmentMeta.META_ATTACHMENT_ID.getLabelToken());
            setAttachmentId(vg.generate());
        } catch (ValidationException e) {
            throw new ApplicationExceptions(e);
        }
    }
    if (getDocumentReferenceId() == null && attachmentDataImpl != null) {
        if (log.isDebugEnabled())
            log.debug("Attachment is stored in external data repository");
        try {
            IVoucherGenerator vg = VoucherGeneratorFactory.instance();
            vg.setUow(getUOW());
            vg.setDomainClassName(getClass().getName());
            vg.setFieldName(AttachmentMeta.DOCUMENT_REFERENCE_ID);
            vg.setLabelToken(AttachmentMeta.META_DOCUMENT_REFERENCE_ID.getLabelToken());
            setDocumentReferenceId(vg.generate());
        } catch (ValidationException e) {
            throw new ApplicationExceptions(e);
        }
    }
    if (getDocumentReferenceId() != null && attachmentDataImpl != null) {
        try {
            setAttachmentType("D");
            // Null out attachment data as it is stored in document repository
            setData(null);
        } catch (ValidationException e) {
            throw new ApplicationExceptions(e);
        }
    }
    super.validate();
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) MandatoryFieldException(org.jaffa.datatypes.exceptions.MandatoryFieldException) IVoucherGenerator(org.jaffa.components.voucher.IVoucherGenerator) IAttachmentData(org.jaffa.components.attachment.apis.IAttachmentData)

Example 5 with IAttachmentData

use of org.jaffa.components.attachment.apis.IAttachmentData in project jaffa-framework by jaffa-projects.

the class AttachmentHandler method endBeanDelete.

public void endBeanDelete(String path, Object source, Object target) throws ApplicationException, ApplicationExceptions, FrameworkException {
    if (source instanceof AttachmentGraph && target instanceof Attachment) {
        AttachmentGraph attachmentGraph = (AttachmentGraph) source;
        Attachment attachment = (Attachment) target;
        IAttachmentData attachmentDataImpl = AttachmentFactory.getAttachmentDataImpl();
        if (attachmentDataImpl != null && attachment.getDocumentReferenceId() != null) {
            Criteria c = new Criteria();
            c.setTable(AttachmentMeta.getName());
            c.addCriteria(AttachmentMeta.DOCUMENT_REFERENCE_ID, attachment.getDocumentReferenceId());
            c.addCriteria(AttachmentMeta.ATTACHMENT_ID, Criteria.RELATIONAL_NOT_EQUALS, attachment.getAttachmentId());
            c.addFunction(Criteria.FUNCTION_COUNT, null, Criteria.ID_FUNCTION_COUNT);
            Iterator<Attachment> iterator = attachment.getUOW().query(c).iterator();
            if (iterator.hasNext()) {
                Number count = (Number) ((Map) iterator.next()).get(Criteria.ID_FUNCTION_COUNT);
                if (count == null || count.intValue() == 0) {
                    attachmentDataImpl.delete(attachment.getDocumentReferenceId());
                }
            }
        }
    }
}
Also used : AttachmentGraph(org.jaffa.components.attachment.apis.data.AttachmentGraph) Attachment(org.jaffa.components.attachment.domain.Attachment) GraphCriteria(org.jaffa.soa.graph.GraphCriteria) Criteria(org.jaffa.persistence.Criteria) IAttachmentData(org.jaffa.components.attachment.apis.IAttachmentData)

Aggregations

IAttachmentData (org.jaffa.components.attachment.apis.IAttachmentData)5 AttachmentGraph (org.jaffa.components.attachment.apis.data.AttachmentGraph)4 Attachment (org.jaffa.components.attachment.domain.Attachment)4 IVoucherGenerator (org.jaffa.components.voucher.IVoucherGenerator)1 MandatoryFieldException (org.jaffa.datatypes.exceptions.MandatoryFieldException)1 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)1 Criteria (org.jaffa.persistence.Criteria)1 GraphCriteria (org.jaffa.soa.graph.GraphCriteria)1