Skip to content

Commit 434110d

Browse files
authored
Merge pull request #41 from mendix/dat/timezone-logging-9
Fix timezone logging 9
2 parents e265c96 + e43989e commit 434110d

File tree

3 files changed

+68
-42
lines changed

3 files changed

+68
-42
lines changed

Audit trail.mpr

0 Bytes
Binary file not shown.

javasource/audittrail/log/CreateLogObject.java

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.Date;
1212
import java.util.HashMap;
1313
import java.util.LinkedHashMap;
14+
import java.util.LinkedList;
1415
import java.util.List;
1516
import java.util.Map;
1617
import java.util.Objects;
@@ -83,6 +84,7 @@ public static IMendixObject createAuditLogItems(final IMendixObject auditableObj
8384
+ auditableObject.getId().toLong() + "), state: " + auditableObject.getState() + "/" + logType);
8485

8586
final IContext sudoContext = Core.createSystemContext();
87+
sudoContext.getSession().setTimeZone(getTimeZone(context));
8688
final IMendixObject logObject = Core.instantiate(sudoContext, Log.getType());
8789

8890
IMendixIdentifier userObjectId = null;
@@ -190,6 +192,13 @@ public static IMendixObject createAuditLogItems(final IMendixObject auditableObj
190192
}
191193
}
192194

195+
private static String getTimeZone(final IContext context) {
196+
final TimeZone tz = context.getSession().getTimeZone();
197+
if (tz != null) return tz.getID();
198+
if (Constants.getServerTimeZone() == null || Constants.getServerTimeZone().isEmpty()) return "GMT";
199+
return Constants.getServerTimeZone();
200+
}
201+
193202
private static int createLogLines(final IMendixObject inputObject, final IMendixObject logObject, final IContext sudoContext,
194203
final IContext currentContext, final TypeOfLog logType, final String skipAssociation) throws CoreException {
195204
boolean isNew = false;
@@ -248,10 +257,11 @@ else if (member instanceof MendixObjectReferenceSet)
248257

249258
private static List<IMendixObject> createSingleLogLine(final IMendixObject logObject, final IMendixObjectMember<?> member,
250259
final String memberType, final boolean isNew, final IContext context) throws CoreException {
251-
final String oldValue = getMemberValueString(member, false, context), newValue = getMemberValueString(member, true, context);
260+
final String oldValue = getMemberValueString(member, false, context);
261+
final String newValue = getMemberValueString(member, true, context);
252262

253-
final boolean newOrChangedObject = !oldValue.equals(newValue) || isNew;
254-
if (!Constants.getIncludeOnlyChangedAttributes() || newOrChangedObject) {
263+
final boolean newOrChangedAttribute = isNew || !oldValue.equals(newValue);
264+
if (newOrChangedAttribute || !Constants.getIncludeOnlyChangedAttributes()) {
255265
final IMendixObject logLine = Core.instantiate(context, LogLine.getType());
256266

257267
logLine.setValue(context, LogLine.MemberNames.Member.toString(), member.getName());
@@ -264,7 +274,7 @@ private static List<IMendixObject> createSingleLogLine(final IMendixObject logOb
264274
else
265275
logLine.setValue(context, LogLine.MemberNames.OldValue.toString(), oldValue);
266276

267-
if (newOrChangedObject)
277+
if (newOrChangedAttribute)
268278
incNumberOfChangedMembers(logObject, context, context, isNew, member.getName());
269279

270280
return Collections.singletonList(logLine);
@@ -428,52 +438,48 @@ private static List<IMendixObject> createReferenceSetLogLine(final IMendixObject
428438
}
429439

430440
private static String getMemberValueString(final IMendixObjectMember<?> member, final boolean fromCache, final IContext context) {
431-
Object value = null;
432-
433-
if (fromCache == true) {
434-
// Values from cache
435-
value = member.getValue(context);
436-
} else {
437-
// Values form DB
438-
value = member.getOriginalValue(context);
441+
final Object value = (fromCache) ? member.getValue(context) : member.getOriginalValue(context);
442+
443+
if (value == null) {
444+
return "";
439445
}
440446

441-
if (value != null) {
442-
if (value instanceof Date) {
443-
return parseDate((Date) value, context);
444-
} else if (value instanceof BigDecimal) {
445-
return String.valueOf(((BigDecimal) value).stripTrailingZeros()).trim();
446-
} else if (value instanceof String) {
447-
return (String) value;
448-
} else {
449-
return String.valueOf(value).trim();
450-
}
447+
if (value instanceof Date) {
448+
return parseDate((Date) value, context);
451449
}
452-
453-
return "";
450+
451+
if (value instanceof BigDecimal) {
452+
return String.valueOf(((BigDecimal) value).stripTrailingZeros()).trim();
453+
}
454+
455+
if (value instanceof String) {
456+
return (String) value;
457+
}
458+
459+
return String.valueOf(value).trim();
454460
}
455461

456462
private static String parseDate(final Date date, final IContext context) {
457-
String dateOutput = "";
458-
if (date != null) {
459-
final DateFormat dateFormat = new SimpleDateFormat(Constants.getLogLineDateFormat());
460-
if (Constants.getLogServerTimeZoneDateNotation()) {
461-
final TimeZone zone = TimeZone.getTimeZone(Constants.getServerTimeZone());
462-
dateFormat.setTimeZone(zone);
463-
dateOutput = dateFormat.format(date) + " (UTC) ";
464-
}
463+
if (date == null) {
464+
return "";
465+
}
465466

466-
if (Constants.getLogSessionTimeZoneDateNotation() && context.getSession() != null
467-
&& context.getSession().getTimeZone() != null) {
468-
if (!"".equals(dateOutput))
469-
dateOutput += " / ";
467+
List<String> dateFormats = new LinkedList<String>();
470468

471-
final TimeZone zone = context.getSession().getTimeZone();
472-
dateFormat.setTimeZone(zone);
473-
dateOutput += dateFormat.format(date) + " (" + zone.getDisplayName() + ") ";
474-
}
469+
if (Constants.getLogServerTimeZoneDateNotation()) {
470+
dateFormats.add(dateInZone(date, TimeZone.getTimeZone(Constants.getServerTimeZone())));
471+
}
472+
473+
if (Constants.getLogSessionTimeZoneDateNotation() && context.getSession() != null && context.getSession().getTimeZone() != null) {
474+
dateFormats.add(dateInZone(date, context.getSession().getTimeZone()));
475475
}
476476

477-
return dateOutput;
477+
return dateFormats.stream().collect(Collectors.joining(" / "));
478+
}
479+
480+
private static String dateInZone(final Date date, final TimeZone zone) {
481+
final DateFormat dateFormat = new SimpleDateFormat(Constants.getLogLineDateFormat());
482+
dateFormat.setTimeZone(zone);
483+
return dateFormat.format(date) + " (" + zone.getID() + ")";
478484
}
479485
}

javasource/test_crm/tests/TestAuditInheritance.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package test_crm.tests;
22

33
import java.util.Arrays;
4+
import java.util.Calendar;
45
import java.util.Collections;
6+
import java.util.Date;
7+
import java.util.TimeZone;
58

69
import com.mendix.core.CoreException;
710
import com.mendix.systemwideinterfaces.core.IMendixObject;
@@ -17,6 +20,7 @@
1720

1821
import static test_crm.proxies.Company.MemberNames.CompanyNr;
1922
import static test_crm.proxies.Company.MemberNames.Dec;
23+
import static test_crm.proxies.Company.MemberNames.Founded;
2024
import static test_crm.proxies.Company.MemberNames.Name;
2125
import static test_crm.proxies.Company.MemberNames.Number;
2226
import static test_crm.proxies.Company.MemberNames.InternNr;
@@ -53,10 +57,14 @@ public void testChangeRecord() throws CoreException {
5357

5458
company.setName(NAME2);
5559
company.setDec(new java.math.BigDecimal("0.00000000")); // should not be considered changed
60+
company.setFounded(FOUNDED_DATE);
5661
company.commit();
5762

5863
// Assert log was created
59-
final ExpectedLog expectedLog = createExpectedLog(TypeOfLog.Change, company).changeAttribute(Name, NAME, NAME2);
64+
final ExpectedLog expectedLog =
65+
createExpectedLog(TypeOfLog.Change, company)
66+
.changeAttribute(Name, NAME, NAME2)
67+
.changeAttribute(Founded, "", "11/04/1991 (UTC)");
6068

6169
final ActualLog actualLog = ActualLog.getLastLog(context, company.getMendixObject().getId().toLong());
6270

@@ -175,17 +183,29 @@ private ExpectedLog createExpectedLog(final TypeOfLog typeOfLog, final Company c
175183
.addAttribute(Name, NAME).addAttribute(CompanyNr, COMPANY_NR)
176184
.addAttribute(InternNr, company.getInternNr())
177185
.addAttribute(Dec, 0).addAttribute(Number, 0)
186+
.addAttribute(Founded, "")
178187
.addReferences(Company_Group, context, MemberType.ReferenceSet, groupObjects);
179188
} else {
180189
return new ExpectedLog(typeOfLog, Company.entityName, admin, initialDate, Company.entityName)
181190
.keepAttribute(Name, NAME).keepAttribute(CompanyNr, COMPANY_NR)
182191
.keepAttribute(InternNr, company.getInternNr())
183192
.keepAttribute(Dec, 0).keepAttribute(Number, 0)
193+
.keepAttribute(Founded, "")
184194
.keepReferences(Company_Group, context, MemberType.ReferenceSet, groupObjects);
185195
}
186196
}
187197

198+
private static Date createDate() {
199+
Calendar calendar = Calendar.getInstance();
200+
calendar.set(Calendar.YEAR, 1991);
201+
calendar.set(Calendar.MONTH, 10);
202+
calendar.set(Calendar.DATE, 4);
203+
calendar.set(Calendar.HOUR, 2);
204+
return calendar.getTime();
205+
}
206+
188207
private static final String NAME = "Company";
189208
private static final String NAME2 = "Company2";
190209
private static final String COMPANY_NR = "123";
210+
private static final Date FOUNDED_DATE = createDate();
191211
}

0 commit comments

Comments
 (0)