diff --git a/Audit trail.mpr b/Audit trail.mpr index 7b748e1..12fae10 100644 Binary files a/Audit trail.mpr and b/Audit trail.mpr differ diff --git a/javasource/audittrail/log/CreateLogObject.java b/javasource/audittrail/log/CreateLogObject.java index 4ef1328..d4716f8 100644 --- a/javasource/audittrail/log/CreateLogObject.java +++ b/javasource/audittrail/log/CreateLogObject.java @@ -11,6 +11,7 @@ import java.util.Date; import java.util.HashMap; import java.util.LinkedHashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -83,6 +84,7 @@ public static IMendixObject createAuditLogItems(final IMendixObject auditableObj + auditableObject.getId().toLong() + "), state: " + auditableObject.getState() + "/" + logType); final IContext sudoContext = Core.createSystemContext(); + sudoContext.getSession().setTimeZone(getTimeZone(context)); final IMendixObject logObject = Core.instantiate(sudoContext, Log.getType()); IMendixIdentifier userObjectId = null; @@ -190,6 +192,13 @@ public static IMendixObject createAuditLogItems(final IMendixObject auditableObj } } + private static String getTimeZone(final IContext context) { + final TimeZone tz = context.getSession().getTimeZone(); + if (tz != null) return tz.getID(); + if (Constants.getServerTimeZone() == null || Constants.getServerTimeZone().isEmpty()) return "GMT"; + return Constants.getServerTimeZone(); + } + private static int createLogLines(final IMendixObject inputObject, final IMendixObject logObject, final IContext sudoContext, final IContext currentContext, final TypeOfLog logType, final String skipAssociation) throws CoreException { boolean isNew = false; @@ -248,10 +257,11 @@ else if (member instanceof MendixObjectReferenceSet) private static List createSingleLogLine(final IMendixObject logObject, final IMendixObjectMember member, final String memberType, final boolean isNew, final IContext context) throws CoreException { - final String oldValue = getMemberValueString(member, false, context), newValue = getMemberValueString(member, true, context); + final String oldValue = getMemberValueString(member, false, context); + final String newValue = getMemberValueString(member, true, context); - final boolean newOrChangedObject = !oldValue.equals(newValue) || isNew; - if (!Constants.getIncludeOnlyChangedAttributes() || newOrChangedObject) { + final boolean newOrChangedAttribute = isNew || !oldValue.equals(newValue); + if (newOrChangedAttribute || !Constants.getIncludeOnlyChangedAttributes()) { final IMendixObject logLine = Core.instantiate(context, LogLine.getType()); logLine.setValue(context, LogLine.MemberNames.Member.toString(), member.getName()); @@ -264,7 +274,7 @@ private static List createSingleLogLine(final IMendixObject logOb else logLine.setValue(context, LogLine.MemberNames.OldValue.toString(), oldValue); - if (newOrChangedObject) + if (newOrChangedAttribute) incNumberOfChangedMembers(logObject, context, context, isNew, member.getName()); return Collections.singletonList(logLine); @@ -428,52 +438,48 @@ private static List createReferenceSetLogLine(final IMendixObject } private static String getMemberValueString(final IMendixObjectMember member, final boolean fromCache, final IContext context) { - Object value = null; - - if (fromCache == true) { - // Values from cache - value = member.getValue(context); - } else { - // Values form DB - value = member.getOriginalValue(context); + final Object value = (fromCache) ? member.getValue(context) : member.getOriginalValue(context); + + if (value == null) { + return ""; } - if (value != null) { - if (value instanceof Date) { - return parseDate((Date) value, context); - } else if (value instanceof BigDecimal) { - return String.valueOf(((BigDecimal) value).stripTrailingZeros()).trim(); - } else if (value instanceof String) { - return (String) value; - } else { - return String.valueOf(value).trim(); - } + if (value instanceof Date) { + return parseDate((Date) value, context); } - - return ""; + + if (value instanceof BigDecimal) { + return String.valueOf(((BigDecimal) value).stripTrailingZeros()).trim(); + } + + if (value instanceof String) { + return (String) value; + } + + return String.valueOf(value).trim(); } private static String parseDate(final Date date, final IContext context) { - String dateOutput = ""; - if (date != null) { - final DateFormat dateFormat = new SimpleDateFormat(Constants.getLogLineDateFormat()); - if (Constants.getLogServerTimeZoneDateNotation()) { - final TimeZone zone = TimeZone.getTimeZone(Constants.getServerTimeZone()); - dateFormat.setTimeZone(zone); - dateOutput = dateFormat.format(date) + " (UTC) "; - } + if (date == null) { + return ""; + } - if (Constants.getLogSessionTimeZoneDateNotation() && context.getSession() != null - && context.getSession().getTimeZone() != null) { - if (!"".equals(dateOutput)) - dateOutput += " / "; + List dateFormats = new LinkedList(); - final TimeZone zone = context.getSession().getTimeZone(); - dateFormat.setTimeZone(zone); - dateOutput += dateFormat.format(date) + " (" + zone.getDisplayName() + ") "; - } + if (Constants.getLogServerTimeZoneDateNotation()) { + dateFormats.add(dateInZone(date, TimeZone.getTimeZone(Constants.getServerTimeZone()))); + } + + if (Constants.getLogSessionTimeZoneDateNotation() && context.getSession() != null && context.getSession().getTimeZone() != null) { + dateFormats.add(dateInZone(date, context.getSession().getTimeZone())); } - return dateOutput; + return dateFormats.stream().collect(Collectors.joining(" / ")); + } + + private static String dateInZone(final Date date, final TimeZone zone) { + final DateFormat dateFormat = new SimpleDateFormat(Constants.getLogLineDateFormat()); + dateFormat.setTimeZone(zone); + return dateFormat.format(date) + " (" + zone.getID() + ")"; } } diff --git a/javasource/test_crm/tests/TestAuditInheritance.java b/javasource/test_crm/tests/TestAuditInheritance.java index 22329cd..4d1f91e 100644 --- a/javasource/test_crm/tests/TestAuditInheritance.java +++ b/javasource/test_crm/tests/TestAuditInheritance.java @@ -1,7 +1,10 @@ package test_crm.tests; import java.util.Arrays; +import java.util.Calendar; import java.util.Collections; +import java.util.Date; +import java.util.TimeZone; import com.mendix.core.CoreException; import com.mendix.systemwideinterfaces.core.IMendixObject; @@ -17,6 +20,7 @@ import static test_crm.proxies.Company.MemberNames.CompanyNr; import static test_crm.proxies.Company.MemberNames.Dec; +import static test_crm.proxies.Company.MemberNames.Founded; import static test_crm.proxies.Company.MemberNames.Name; import static test_crm.proxies.Company.MemberNames.Number; import static test_crm.proxies.Company.MemberNames.InternNr; @@ -53,10 +57,14 @@ public void testChangeRecord() throws CoreException { company.setName(NAME2); company.setDec(new java.math.BigDecimal("0.00000000")); // should not be considered changed + company.setFounded(FOUNDED_DATE); company.commit(); // Assert log was created - final ExpectedLog expectedLog = createExpectedLog(TypeOfLog.Change, company).changeAttribute(Name, NAME, NAME2); + final ExpectedLog expectedLog = + createExpectedLog(TypeOfLog.Change, company) + .changeAttribute(Name, NAME, NAME2) + .changeAttribute(Founded, "", "11/04/1991 (UTC)"); final ActualLog actualLog = ActualLog.getLastLog(context, company.getMendixObject().getId().toLong()); @@ -175,17 +183,29 @@ private ExpectedLog createExpectedLog(final TypeOfLog typeOfLog, final Company c .addAttribute(Name, NAME).addAttribute(CompanyNr, COMPANY_NR) .addAttribute(InternNr, company.getInternNr()) .addAttribute(Dec, 0).addAttribute(Number, 0) + .addAttribute(Founded, "") .addReferences(Company_Group, context, MemberType.ReferenceSet, groupObjects); } else { return new ExpectedLog(typeOfLog, Company.entityName, admin, initialDate, Company.entityName) .keepAttribute(Name, NAME).keepAttribute(CompanyNr, COMPANY_NR) .keepAttribute(InternNr, company.getInternNr()) .keepAttribute(Dec, 0).keepAttribute(Number, 0) + .keepAttribute(Founded, "") .keepReferences(Company_Group, context, MemberType.ReferenceSet, groupObjects); } } + private static Date createDate() { + Calendar calendar = Calendar.getInstance(); + calendar.set(Calendar.YEAR, 1991); + calendar.set(Calendar.MONTH, 10); + calendar.set(Calendar.DATE, 4); + calendar.set(Calendar.HOUR, 2); + return calendar.getTime(); + } + private static final String NAME = "Company"; private static final String NAME2 = "Company2"; private static final String COMPANY_NR = "123"; + private static final Date FOUNDED_DATE = createDate(); }