Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified Audit trail.mpr
Binary file not shown.
88 changes: 47 additions & 41 deletions javasource/audittrail/log/CreateLogObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -248,10 +257,11 @@ else if (member instanceof MendixObjectReferenceSet)

private static List<IMendixObject> 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());
Expand All @@ -264,7 +274,7 @@ private static List<IMendixObject> 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);
Expand Down Expand Up @@ -428,52 +438,48 @@ private static List<IMendixObject> 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<String> dateFormats = new LinkedList<String>();

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() + ")";
}
}
22 changes: 21 additions & 1 deletion javasource/test_crm/tests/TestAuditInheritance.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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();
}