@Test public void testBuildUpdateSystemCodesMessage() { final SystemCodes oldsc = new SystemCodes("categoryold","keyold","old"); final SystemCodes newsc = new SystemCodes("categorynew","keynew","new"); final Ninja oldtc = new Ninja(); oldtc.setFirstName("Samurai"); oldtc.setLastName("Ken"); oldtc.setStatus(oldsc); final Ninja newtc = new Ninja(); newtc.setStatus(newsc); newtc.setFirstName("Samurai"); newtc.setLastName("Ken"); final Ninja sametc = new Ninja(); sametc.setStatus(oldsc); sametc.setFirstName("Samurai"); sametc.setLastName("Ken"); final String expected = "<p class='change-message'>Changed Ninja with Name:<span class='primary-field'>Samurai Ken</span> with the following: " + "Status from <span class='field-value-from'>KEYOLD:old</span> to <span class='field-value-to'>KEYNEW:new</span> </p>"; Assert.assertEquals(expected, CrudUtil.buildUpdateMessage(oldtc, newtc)); Assert.assertEquals("", CrudUtil.buildUpdateMessage(oldtc, sametc)); }
@SuppressWarnings({ "rawtypes", "unchecked" }) public static String buildUpdateMessage(final BaseEntity oldObject, final BaseEntity newObject) { final StringBuilder message = new StringBuilder( "<p class='change-message'>Changed "); final AuditableField pf = CacheUtil.getPrimaryField(oldObject); message.append(buildPrimaryField(pf, oldObject)).append( " with the following: "); final List<AuditableField> auditableFields = CacheUtil .getAuditable(oldObject); int count = 0; for (final AuditableField property : auditableFields) { _log.debug("Building update message for field " + property.getFieldName()); Object oldValue = retrieveNullableObjectValue(oldObject, property.getFieldName()); Object newValue = retrieveNullableObjectValue(newObject, property.getFieldName()); oldValue = normalizeValue(oldValue); newValue = normalizeValue(newValue); if (!oldValue.getClass().getCanonicalName().startsWith( newValue.getClass().getCanonicalName()) ) { _log.debug("Old object: " + oldValue); _log.warn("Unable to compare [" + property.getFieldName() + "] for audit logging due to difference in datatype. " + "oldValue is [" + oldValue.getClass() + "] and newValue is [" + newValue.getClass() + "]"); continue; } if (Collection.class.isAssignableFrom(oldValue.getClass()) && Collection.class.isAssignableFrom(newValue.getClass())) { if (((Collection) oldValue).isEmpty() && ((Collection) newValue).isEmpty()) { _log.debug("Old and New values are empty"); continue; } _log.debug("Old and New values are not empty"); final List addedList = new ArrayList((Collection) newValue); addedList.removeAll(new ArrayList((Collection) oldValue)); final List removedList = new ArrayList((Collection) oldValue); removedList.removeAll(new ArrayList((Collection) newValue)); if (!addedList.isEmpty() || !removedList.isEmpty()) { if (count > 0) { message.append("and "); } } if (!addedList.isEmpty()) { message.append("added ").append(property.getTitle()) .append(" <span class='field-values-added'>") .append(addedList).append("</span> "); } if (!removedList.isEmpty()) { if (!addedList.isEmpty()) { message.append("and "); } message.append("removed ").append(property.getTitle()) .append(" <span class='field-values-removed'>") .append(removedList).append("</span> "); count++; } } else { if (!oldValue.equals(newValue)) { if (count > 0) { message.append("and "); } if (StringUtil.isEmpty(newValue.toString())) { message.append(property.getTitle()) .append(" <span class='field-value-removed'>") .append(oldValue.toString()) .append("</span> is removed "); } else { message.append(property.getTitle()); if (!StringUtil.isEmpty(oldValue.toString())) { message.append( " from <span class='field-value-from'>") .append(oldValue.toString()) .append("</span> "); } else { message.append(" is set "); } message.append("to <span class='field-value-to'>") .append(newValue.toString()).append("</span> "); } count++; } } } message.append("</p>"); if (count == 0) { return ""; } else { return message.toString(); } }
CrudUtil { @SuppressWarnings({ "rawtypes", "unchecked" }) public static String buildUpdateMessage(final BaseEntity oldObject, final BaseEntity newObject) { final StringBuilder message = new StringBuilder( "<p class='change-message'>Changed "); final AuditableField pf = CacheUtil.getPrimaryField(oldObject); message.append(buildPrimaryField(pf, oldObject)).append( " with the following: "); final List<AuditableField> auditableFields = CacheUtil .getAuditable(oldObject); int count = 0; for (final AuditableField property : auditableFields) { _log.debug("Building update message for field " + property.getFieldName()); Object oldValue = retrieveNullableObjectValue(oldObject, property.getFieldName()); Object newValue = retrieveNullableObjectValue(newObject, property.getFieldName()); oldValue = normalizeValue(oldValue); newValue = normalizeValue(newValue); if (!oldValue.getClass().getCanonicalName().startsWith( newValue.getClass().getCanonicalName()) ) { _log.debug("Old object: " + oldValue); _log.warn("Unable to compare [" + property.getFieldName() + "] for audit logging due to difference in datatype. " + "oldValue is [" + oldValue.getClass() + "] and newValue is [" + newValue.getClass() + "]"); continue; } if (Collection.class.isAssignableFrom(oldValue.getClass()) && Collection.class.isAssignableFrom(newValue.getClass())) { if (((Collection) oldValue).isEmpty() && ((Collection) newValue).isEmpty()) { _log.debug("Old and New values are empty"); continue; } _log.debug("Old and New values are not empty"); final List addedList = new ArrayList((Collection) newValue); addedList.removeAll(new ArrayList((Collection) oldValue)); final List removedList = new ArrayList((Collection) oldValue); removedList.removeAll(new ArrayList((Collection) newValue)); if (!addedList.isEmpty() || !removedList.isEmpty()) { if (count > 0) { message.append("and "); } } if (!addedList.isEmpty()) { message.append("added ").append(property.getTitle()) .append(" <span class='field-values-added'>") .append(addedList).append("</span> "); } if (!removedList.isEmpty()) { if (!addedList.isEmpty()) { message.append("and "); } message.append("removed ").append(property.getTitle()) .append(" <span class='field-values-removed'>") .append(removedList).append("</span> "); count++; } } else { if (!oldValue.equals(newValue)) { if (count > 0) { message.append("and "); } if (StringUtil.isEmpty(newValue.toString())) { message.append(property.getTitle()) .append(" <span class='field-value-removed'>") .append(oldValue.toString()) .append("</span> is removed "); } else { message.append(property.getTitle()); if (!StringUtil.isEmpty(oldValue.toString())) { message.append( " from <span class='field-value-from'>") .append(oldValue.toString()) .append("</span> "); } else { message.append(" is set "); } message.append("to <span class='field-value-to'>") .append(newValue.toString()).append("</span> "); } count++; } } } message.append("</p>"); if (count == 0) { return ""; } else { return message.toString(); } } }
CrudUtil { @SuppressWarnings({ "rawtypes", "unchecked" }) public static String buildUpdateMessage(final BaseEntity oldObject, final BaseEntity newObject) { final StringBuilder message = new StringBuilder( "<p class='change-message'>Changed "); final AuditableField pf = CacheUtil.getPrimaryField(oldObject); message.append(buildPrimaryField(pf, oldObject)).append( " with the following: "); final List<AuditableField> auditableFields = CacheUtil .getAuditable(oldObject); int count = 0; for (final AuditableField property : auditableFields) { _log.debug("Building update message for field " + property.getFieldName()); Object oldValue = retrieveNullableObjectValue(oldObject, property.getFieldName()); Object newValue = retrieveNullableObjectValue(newObject, property.getFieldName()); oldValue = normalizeValue(oldValue); newValue = normalizeValue(newValue); if (!oldValue.getClass().getCanonicalName().startsWith( newValue.getClass().getCanonicalName()) ) { _log.debug("Old object: " + oldValue); _log.warn("Unable to compare [" + property.getFieldName() + "] for audit logging due to difference in datatype. " + "oldValue is [" + oldValue.getClass() + "] and newValue is [" + newValue.getClass() + "]"); continue; } if (Collection.class.isAssignableFrom(oldValue.getClass()) && Collection.class.isAssignableFrom(newValue.getClass())) { if (((Collection) oldValue).isEmpty() && ((Collection) newValue).isEmpty()) { _log.debug("Old and New values are empty"); continue; } _log.debug("Old and New values are not empty"); final List addedList = new ArrayList((Collection) newValue); addedList.removeAll(new ArrayList((Collection) oldValue)); final List removedList = new ArrayList((Collection) oldValue); removedList.removeAll(new ArrayList((Collection) newValue)); if (!addedList.isEmpty() || !removedList.isEmpty()) { if (count > 0) { message.append("and "); } } if (!addedList.isEmpty()) { message.append("added ").append(property.getTitle()) .append(" <span class='field-values-added'>") .append(addedList).append("</span> "); } if (!removedList.isEmpty()) { if (!addedList.isEmpty()) { message.append("and "); } message.append("removed ").append(property.getTitle()) .append(" <span class='field-values-removed'>") .append(removedList).append("</span> "); count++; } } else { if (!oldValue.equals(newValue)) { if (count > 0) { message.append("and "); } if (StringUtil.isEmpty(newValue.toString())) { message.append(property.getTitle()) .append(" <span class='field-value-removed'>") .append(oldValue.toString()) .append("</span> is removed "); } else { message.append(property.getTitle()); if (!StringUtil.isEmpty(oldValue.toString())) { message.append( " from <span class='field-value-from'>") .append(oldValue.toString()) .append("</span> "); } else { message.append(" is set "); } message.append("to <span class='field-value-to'>") .append(newValue.toString()).append("</span> "); } count++; } } } message.append("</p>"); if (count == 0) { return ""; } else { return message.toString(); } } private  CrudUtil(); }
CrudUtil { @SuppressWarnings({ "rawtypes", "unchecked" }) public static String buildUpdateMessage(final BaseEntity oldObject, final BaseEntity newObject) { final StringBuilder message = new StringBuilder( "<p class='change-message'>Changed "); final AuditableField pf = CacheUtil.getPrimaryField(oldObject); message.append(buildPrimaryField(pf, oldObject)).append( " with the following: "); final List<AuditableField> auditableFields = CacheUtil .getAuditable(oldObject); int count = 0; for (final AuditableField property : auditableFields) { _log.debug("Building update message for field " + property.getFieldName()); Object oldValue = retrieveNullableObjectValue(oldObject, property.getFieldName()); Object newValue = retrieveNullableObjectValue(newObject, property.getFieldName()); oldValue = normalizeValue(oldValue); newValue = normalizeValue(newValue); if (!oldValue.getClass().getCanonicalName().startsWith( newValue.getClass().getCanonicalName()) ) { _log.debug("Old object: " + oldValue); _log.warn("Unable to compare [" + property.getFieldName() + "] for audit logging due to difference in datatype. " + "oldValue is [" + oldValue.getClass() + "] and newValue is [" + newValue.getClass() + "]"); continue; } if (Collection.class.isAssignableFrom(oldValue.getClass()) && Collection.class.isAssignableFrom(newValue.getClass())) { if (((Collection) oldValue).isEmpty() && ((Collection) newValue).isEmpty()) { _log.debug("Old and New values are empty"); continue; } _log.debug("Old and New values are not empty"); final List addedList = new ArrayList((Collection) newValue); addedList.removeAll(new ArrayList((Collection) oldValue)); final List removedList = new ArrayList((Collection) oldValue); removedList.removeAll(new ArrayList((Collection) newValue)); if (!addedList.isEmpty() || !removedList.isEmpty()) { if (count > 0) { message.append("and "); } } if (!addedList.isEmpty()) { message.append("added ").append(property.getTitle()) .append(" <span class='field-values-added'>") .append(addedList).append("</span> "); } if (!removedList.isEmpty()) { if (!addedList.isEmpty()) { message.append("and "); } message.append("removed ").append(property.getTitle()) .append(" <span class='field-values-removed'>") .append(removedList).append("</span> "); count++; } } else { if (!oldValue.equals(newValue)) { if (count > 0) { message.append("and "); } if (StringUtil.isEmpty(newValue.toString())) { message.append(property.getTitle()) .append(" <span class='field-value-removed'>") .append(oldValue.toString()) .append("</span> is removed "); } else { message.append(property.getTitle()); if (!StringUtil.isEmpty(oldValue.toString())) { message.append( " from <span class='field-value-from'>") .append(oldValue.toString()) .append("</span> "); } else { message.append(" is set "); } message.append("to <span class='field-value-to'>") .append(newValue.toString()).append("</span> "); } count++; } } } message.append("</p>"); if (count == 0) { return ""; } else { return message.toString(); } } private  CrudUtil(); static String buildCreateMessage(final BaseEntity obj); static List<String> getUpdatedFields(final BaseEntity oldObject,
			final BaseEntity newObject); static String convertAuditableFieldsToJson(BaseEntity obj); @SuppressWarnings({ "rawtypes", "unchecked" }) static String buildUpdateMessage(final BaseEntity oldObject,
			final BaseEntity newObject); static String buildDeleteMessage(final BaseEntity obj); static Map<String, Object> buildMapValues(final BaseEntity object); @SuppressWarnings("rawtypes") static Map<String, Object> buildMapValues(final BaseEntity object,
			final List<String> fields); static String buildURLParameters(final BaseEntity object); @SuppressWarnings("rawtypes") static String buildJpaQueryString(final BaseEntity example,
			final boolean exactMatch); @SuppressWarnings("rawtypes") static Object retrieveObjectValue(final Object obj,
			final String property); static Object retrieveNullableObjectValue(final Object obj,
			final String property); static Class<?> retrieveObjectType(final Object obj,
			final String property); static Boolean evaluateExpression(final Object obj,
			final String expression); @SuppressWarnings("unchecked") static String replaceSQLParameters(String sql, final Object obj); @SuppressWarnings({ "rawtypes" }) static List<Field> getAllFields(final Class clazz); @SuppressWarnings({ "rawtypes" }) static List<Field> getAllFields(final Class clazz,
			final boolean includeParent); @SuppressWarnings({ "rawtypes" }) static List<Method> getAllMethods(final Class clazz); @SuppressWarnings({ "rawtypes" }) static List<Method> getAllMethods(final Class clazz,
			final boolean includeParent); static List<MessageResponse> convertErrorMessage(
			final BindingResult bindingResult, final Locale locale,
			final MessageSource messageSource); static List<MessageResponse> buildSuccessMessage(
			final BaseEntity object, final String code, final Locale locale,
			final MessageSource messageSource); static boolean isCollection(final Object ob); static BaseEntity clone(final BaseEntity source); }
CrudUtil { @SuppressWarnings({ "rawtypes", "unchecked" }) public static String buildUpdateMessage(final BaseEntity oldObject, final BaseEntity newObject) { final StringBuilder message = new StringBuilder( "<p class='change-message'>Changed "); final AuditableField pf = CacheUtil.getPrimaryField(oldObject); message.append(buildPrimaryField(pf, oldObject)).append( " with the following: "); final List<AuditableField> auditableFields = CacheUtil .getAuditable(oldObject); int count = 0; for (final AuditableField property : auditableFields) { _log.debug("Building update message for field " + property.getFieldName()); Object oldValue = retrieveNullableObjectValue(oldObject, property.getFieldName()); Object newValue = retrieveNullableObjectValue(newObject, property.getFieldName()); oldValue = normalizeValue(oldValue); newValue = normalizeValue(newValue); if (!oldValue.getClass().getCanonicalName().startsWith( newValue.getClass().getCanonicalName()) ) { _log.debug("Old object: " + oldValue); _log.warn("Unable to compare [" + property.getFieldName() + "] for audit logging due to difference in datatype. " + "oldValue is [" + oldValue.getClass() + "] and newValue is [" + newValue.getClass() + "]"); continue; } if (Collection.class.isAssignableFrom(oldValue.getClass()) && Collection.class.isAssignableFrom(newValue.getClass())) { if (((Collection) oldValue).isEmpty() && ((Collection) newValue).isEmpty()) { _log.debug("Old and New values are empty"); continue; } _log.debug("Old and New values are not empty"); final List addedList = new ArrayList((Collection) newValue); addedList.removeAll(new ArrayList((Collection) oldValue)); final List removedList = new ArrayList((Collection) oldValue); removedList.removeAll(new ArrayList((Collection) newValue)); if (!addedList.isEmpty() || !removedList.isEmpty()) { if (count > 0) { message.append("and "); } } if (!addedList.isEmpty()) { message.append("added ").append(property.getTitle()) .append(" <span class='field-values-added'>") .append(addedList).append("</span> "); } if (!removedList.isEmpty()) { if (!addedList.isEmpty()) { message.append("and "); } message.append("removed ").append(property.getTitle()) .append(" <span class='field-values-removed'>") .append(removedList).append("</span> "); count++; } } else { if (!oldValue.equals(newValue)) { if (count > 0) { message.append("and "); } if (StringUtil.isEmpty(newValue.toString())) { message.append(property.getTitle()) .append(" <span class='field-value-removed'>") .append(oldValue.toString()) .append("</span> is removed "); } else { message.append(property.getTitle()); if (!StringUtil.isEmpty(oldValue.toString())) { message.append( " from <span class='field-value-from'>") .append(oldValue.toString()) .append("</span> "); } else { message.append(" is set "); } message.append("to <span class='field-value-to'>") .append(newValue.toString()).append("</span> "); } count++; } } } message.append("</p>"); if (count == 0) { return ""; } else { return message.toString(); } } private  CrudUtil(); static String buildCreateMessage(final BaseEntity obj); static List<String> getUpdatedFields(final BaseEntity oldObject,
			final BaseEntity newObject); static String convertAuditableFieldsToJson(BaseEntity obj); @SuppressWarnings({ "rawtypes", "unchecked" }) static String buildUpdateMessage(final BaseEntity oldObject,
			final BaseEntity newObject); static String buildDeleteMessage(final BaseEntity obj); static Map<String, Object> buildMapValues(final BaseEntity object); @SuppressWarnings("rawtypes") static Map<String, Object> buildMapValues(final BaseEntity object,
			final List<String> fields); static String buildURLParameters(final BaseEntity object); @SuppressWarnings("rawtypes") static String buildJpaQueryString(final BaseEntity example,
			final boolean exactMatch); @SuppressWarnings("rawtypes") static Object retrieveObjectValue(final Object obj,
			final String property); static Object retrieveNullableObjectValue(final Object obj,
			final String property); static Class<?> retrieveObjectType(final Object obj,
			final String property); static Boolean evaluateExpression(final Object obj,
			final String expression); @SuppressWarnings("unchecked") static String replaceSQLParameters(String sql, final Object obj); @SuppressWarnings({ "rawtypes" }) static List<Field> getAllFields(final Class clazz); @SuppressWarnings({ "rawtypes" }) static List<Field> getAllFields(final Class clazz,
			final boolean includeParent); @SuppressWarnings({ "rawtypes" }) static List<Method> getAllMethods(final Class clazz); @SuppressWarnings({ "rawtypes" }) static List<Method> getAllMethods(final Class clazz,
			final boolean includeParent); static List<MessageResponse> convertErrorMessage(
			final BindingResult bindingResult, final Locale locale,
			final MessageSource messageSource); static List<MessageResponse> buildSuccessMessage(
			final BaseEntity object, final String code, final Locale locale,
			final MessageSource messageSource); static boolean isCollection(final Object ob); static BaseEntity clone(final BaseEntity source);  }