Need a Java Action that logs every attribute ( name, value and type)

0
Hi, I am looking for a java action that logs every attribute ( name, value and type ) to the console of the entity passed. The input would be the entity. Even an example would be very helpful. Thanks,
asked
2 answers
2

I would recommend to take a look at the Java code from the Audit Trail module: https://appstore.home.mendix.com/link/app/138/Mendix/Audittrail

The commit action from the audit trail iterates over all attributes and references, the difference is that this module creates an entity for each member. But you can easily replace that with a log activity.

For reference the code is something like:

    Collection<? extends IMendixObjectMember<?>> members = inputObject.getMembers(sudoContext).values();
    for( IMendixObjectMember<?> member : members ) {
        if( member.isVirtual() ) {
            continue;   //Possible skip calculated attributes
        }

        if (member instanceof MendixObjectReference){
            if( !member.getName().startsWith("System.") ) {

            } //If the association starts with system it is the owner or changedby association
        }

        else if( member instanceof MendixObjectReferenceSet) {

        }
        else{                   
            String attributeName = member.getName();
            String value = getValue( member, getContext() );
        }
    }


//you want to have a function to format the values nicely in the log, String.valueof doesn't always give you the right info
private static String getValue( IMendixObjectMember<?> member, IContext context ) {
    Object value = member.getValue(context);

    if( value != null ) {

        if( value instanceof Date )
            return parseDate( (Date)value, context );

        else if( value instanceof String)
            return parseString( (String)value );

        return String.valueOf( value ).trim();
    }
    else
        return "";
}
answered
0

If you simply need to log entities then you don't actually need a java action. You could add a before commit event handler that simply uses the logging action to post everything to the console.

answered