Search in sources :

Example 1 with IVoucherGenerator

use of org.jaffa.components.voucher.IVoucherGenerator in project jaffa-framework by jaffa-projects.

the class FlexField method generateKey.

/**
 * Generate the technical-key, if required.
 */
public void generateKey() throws ApplicationExceptions, FrameworkException {
    if (getFlexFieldId() == null) {
        try {
            IVoucherGenerator vg = VoucherGeneratorFactory.instance();
            vg.setUow(getUOW());
            vg.setDomainClassName(getClass().getName());
            vg.setFieldName(FlexFieldMeta.FLEX_FIELD_ID);
            vg.setLabelToken(FlexFieldMeta.META_FLEX_FIELD_ID.getLabelToken());
            setFlexFieldId(vg.generate());
        } catch (ValidationException e) {
            throw new ApplicationExceptions(e);
        }
    }
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) IVoucherGenerator(org.jaffa.components.voucher.IVoucherGenerator)

Example 2 with IVoucherGenerator

use of org.jaffa.components.voucher.IVoucherGenerator in project jaffa-framework by jaffa-projects.

the class AuditTransactionObject method generateKey.

/**
 * Generate the technical-key, if required.
 */
public void generateKey() throws ApplicationExceptions, FrameworkException {
    if (getObjectId() == null) {
        try {
            IVoucherGenerator vg = VoucherGeneratorFactory.instance();
            vg.setUow(getUOW());
            vg.setDomainClassName(getClass().getName());
            vg.setFieldName(AuditTransactionObjectMeta.OBJECT_ID);
            vg.setLabelToken(AuditTransactionObjectMeta.META_OBJECT_ID.getLabelToken());
            setObjectId(vg.generate());
        } catch (ValidationException e) {
            throw new ApplicationExceptions(e);
        }
    }
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) IVoucherGenerator(org.jaffa.components.voucher.IVoucherGenerator)

Example 3 with IVoucherGenerator

use of org.jaffa.components.voucher.IVoucherGenerator 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 4 with IVoucherGenerator

use of org.jaffa.components.voucher.IVoucherGenerator in project jaffa-framework by jaffa-projects.

the class JDBCLoggerWithAttachment method append.

/**
 * Invokes the append method of the base class.
 * Additionally it'll create an attachment, if provided in the MDC.
 * @param event the LoggingEvent to log
 * @param layout layout to use for message
 * @exception Exception Description of Exception
 */
public void append(LoggingEvent event, Layout layout) throws Exception {
    // If attachment is not provided, simply invoke the append() method of the base class and return
    if (event.getMDC(m_attachmentMDCKey) == null) {
        super.append(event, layout);
        return;
    }
    // The following is a copy of the original method, but with the ability to obtain the auto-generated key.
    if (!ready())
        throw new Exception("JDBCLogger::append(), Not ready to append !");
    boolean errorOccurred = false;
    PreparedStatement prepStmt = null;
    try {
        prepareConnectionForAttachment();
        int paramIndex = 1;
        prepStmt = (PreparedStatement) c_stmt.get(this);
        String logId = null;
        for (int i = 0; i < ((Number) c_num.get(this)).intValue(); i++) {
            JDBCLogColumn logcol = (JDBCLogColumn) ((List) c_logcols.get(this)).get(i);
            if (((Boolean) c_ignore.get(logcol)).booleanValue())
                continue;
            int logtype = ((Number) c_logtype.get(logcol)).intValue();
            int sqlType = ((Number) c_sqlType.get(logcol)).intValue();
            Object value = c_value.get(logcol);
            Object parameter = null;
            if (logtype == JDBCLogType.MSG) {
                parameter = event.getRenderedMessage();
                if (parameter == null) {
                    prepStmt.setNull(paramIndex, sqlType);
                } else {
                    prepStmt.setObject(paramIndex, parameter);
                }
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.LAYOUT) {
                parameter = layout.format(event);
                // default: use first pattern
                int layoutIndex = ((Integer) value).intValue();
                // LAYOUT~x. use tokens in layout string
                List tokenList = getTokenList((String) parameter);
                String token = getTokenFromList(tokenList, layoutIndex);
                if (parameter == null) {
                    prepStmt.setNull(paramIndex, sqlType);
                } else {
                    prepStmt.setString(paramIndex, token);
                }
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.PRIO) {
                parameter = event.getLevel().toString();
                if (parameter == null) {
                    prepStmt.setNull(paramIndex, sqlType);
                } else {
                    prepStmt.setObject(paramIndex, parameter);
                }
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.CAT) {
                parameter = event.getLoggerName();
                if (parameter == null) {
                    prepStmt.setNull(paramIndex, sqlType);
                } else {
                    prepStmt.setObject(paramIndex, parameter);
                }
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.THREAD) {
                parameter = event.getThreadName();
                if (parameter == null) {
                    prepStmt.setNull(paramIndex, sqlType);
                } else {
                    prepStmt.setObject(paramIndex, parameter);
                }
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.ID) {
                parameter = ((JDBCIDHandler) c_idhandler.get(logcol)).getID();
                if (parameter == null) {
                    prepStmt.setNull(paramIndex, sqlType);
                } else {
                    prepStmt.setObject(paramIndex, parameter);
                }
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.STATIC) {
                parameter = value;
                if (parameter == null) {
                    prepStmt.setNull(paramIndex, sqlType);
                } else {
                    prepStmt.setObject(paramIndex, parameter);
                }
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.TIMESTAMP) {
                parameter = new Timestamp((new java.util.Date()).getTime());
                if (parameter == null) {
                    prepStmt.setNull(paramIndex, sqlType);
                } else {
                    prepStmt.setObject(paramIndex, parameter);
                }
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.INC) {
                parameter = ((Number) c_inc.get(this));
                c_inc.set(this, ((Number) parameter).longValue() + 1);
                prepStmt.setObject(paramIndex, parameter);
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.DYNAMIC) {
                parameter = ((JDBCColumnHandler) c_columnHandler.get(logcol)).getObject(event, (String) c_table.get(this), (String) c_name.get(logcol));
                if (parameter == null) {
                    prepStmt.setNull(paramIndex, sqlType);
                } else {
                    prepStmt.setObject(paramIndex, parameter);
                }
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.THROWABLE) {
                // extract throwable information from loggingEvent if available
                parameter = (String) c_quotedString.invoke(this, getThrowableRepresentationFromLoggingEvent(event));
                if (parameter == null) {
                    prepStmt.setNull(paramIndex, sqlType);
                } else {
                    prepStmt.setObject(paramIndex, parameter);
                }
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.NDC) {
                parameter = event.getNDC();
                if (parameter == null) {
                    prepStmt.setNull(paramIndex, sqlType);
                } else {
                    prepStmt.setObject(paramIndex, parameter);
                }
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.MDC) {
                Object mdcObject = event.getMDC(value.toString());
                parameter = mdcObject == null ? null : mdcObject.toString();
                if (parameter == null) {
                    prepStmt.setNull(paramIndex, sqlType);
                } else {
                    prepStmt.setObject(paramIndex, parameter);
                }
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.IPRIO) {
                parameter = event.getLevel().toInt();
                prepStmt.setObject(paramIndex, parameter);
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.ORACLE_SEQUENCE) {
            // do nothing
            }
            // Obtain the logId
            if (parameter != null && "log_id".equalsIgnoreCase((String) c_name.get(logcol)))
                logId = parameter.toString();
        }
        prepStmt.executeUpdate();
        // Obtain the auto-generated key
        if (logId == null)
            throw new Exception("JDBCLogger::append(), LogId was not determined for the Log. Cannot add the attachment");
        prepStmt.close();
        // Now create the attachment
        Object attachment = event.getMDC(m_attachmentMDCKey);
        prepStmt = m_stmtForAttachment;
        IVoucherGenerator vg = VoucherGeneratorFactory.instance();
        vg.setConnection((Connection) c_con.get(this));
        vg.setDomainClassName(AttachmentMeta.getName());
        vg.setFieldName(AttachmentMeta.ATTACHMENT_ID);
        vg.setLabelToken(AttachmentMeta.META_ATTACHMENT_ID.getLabelToken());
        prepStmt.setString(1, vg.generate());
        prepStmt.setString(2, "org.jaffa.modules.messaging.domain.BusinessEventLog;" + logId);
        prepStmt.setString(3, AttachmentService.createAttachmentName(attachment));
        prepStmt.setString(4, "E");
        prepStmt.setString(5, AttachmentService.createContentType(attachment));
        prepStmt.setTimestamp(6, new Timestamp((new java.util.Date()).getTime()));
        if (SecurityManager.getPrincipal() != null && SecurityManager.getPrincipal().getName() != null)
            prepStmt.setString(7, SecurityManager.getPrincipal().getName());
        else
            prepStmt.setNull(7, Types.VARCHAR);
        byte[] bytes = AttachmentService.createAttachmentData(attachment);
        if ("oracle".equalsIgnoreCase(m_engine)) {
            Blob blob = (Blob) c_createBlob.invoke(null, c_con.get(this), bytes);
            prepStmt.setBlob(8, blob);
        } else {
            InputStream stream = new BufferedInputStream(new ByteArrayInputStream(bytes));
            prepStmt.setBinaryStream(8, stream, bytes.length);
        }
        prepStmt.executeUpdate();
    } catch (Exception e) {
        // e.printStackTrace();
        errorOccurred = true;
        throw (e);
    } finally {
        try {
            if (prepStmt != null) {
                prepStmt.close();
            }
            freeConnection();
        } catch (Exception exception) {
            if (errorOccurred) {
            // consume exception
            } else {
                throw (exception);
            }
        }
    }
}
Also used : JDBCIDHandler(org.apache.log4j.jdbcplus.JDBCIDHandler) Blob(java.sql.Blob) JDBCColumnHandler(org.apache.log4j.jdbcplus.JDBCColumnHandler) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JDBCLogColumn(org.apache.log4j.jdbcplus.JDBCLogColumn) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) IVoucherGenerator(org.jaffa.components.voucher.IVoucherGenerator) SQLException(java.sql.SQLException) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) List(java.util.List)

Example 5 with IVoucherGenerator

use of org.jaffa.components.voucher.IVoucherGenerator in project jaffa-framework by jaffa-projects.

the class SOAEvent method validate.

// .//GEN-END:preAdd_2_be
// All the custom code goes here//GEN-FIRST:custom
/**
 * {@inheritDoc}
 */
@Override
public void validate() throws ApplicationExceptions, FrameworkException {
    // Generate the technical-key, if required
    if (getEventId() == null) {
        try {
            IVoucherGenerator vg = VoucherGeneratorFactory.instance();
            vg.setUow(getUOW());
            vg.setDomainClassName(getClass().getName());
            vg.setFieldName(SOAEventMeta.EVENT_ID);
            vg.setLabelToken(SOAEventMeta.META_EVENT_ID.getLabelToken());
            setEventId(vg.generate());
        } catch (ValidationException e) {
            throw new ApplicationExceptions(e);
        }
    }
    // Log a WARNing message if the event is not documented in soa-events.xml
    if (getEventName() != null && isModified(SOAEventMeta.EVENT_NAME) && ConfigurationService.getInstance().getSoaEventInfo(getEventName()) == null)
        log.warn("SOA Event '" + getEventName() + "' should be documented in soa-events.xml");
    super.validate();
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) IVoucherGenerator(org.jaffa.components.voucher.IVoucherGenerator)

Aggregations

IVoucherGenerator (org.jaffa.components.voucher.IVoucherGenerator)11 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)9 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Blob (java.sql.Blob)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 Timestamp (java.sql.Timestamp)1 Date (java.util.Date)1 List (java.util.List)1 JDBCColumnHandler (org.apache.log4j.jdbcplus.JDBCColumnHandler)1 JDBCIDHandler (org.apache.log4j.jdbcplus.JDBCIDHandler)1 JDBCLogColumn (org.apache.log4j.jdbcplus.JDBCLogColumn)1 IAttachmentData (org.jaffa.components.attachment.apis.IAttachmentData)1 DateTime (org.jaffa.datatypes.DateTime)1 MandatoryFieldException (org.jaffa.datatypes.exceptions.MandatoryFieldException)1