Commit 2846c0f4 authored by Matija Obreza's avatar Matija Obreza Committed by Artem Hrybeniuk
Browse files

Updated to java.time

parent d9bd0f45
......@@ -20,6 +20,10 @@ import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.Format;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
......@@ -305,9 +309,13 @@ public class AuditTrailInterceptor extends EmptyInterceptor implements Initializ
}
final Class<?> returnedClass = type.getReturnedClass();
if (Date.class.equals(returnedClass) || Calendar.class.equals(returnedClass)) {
TemporalType temporalType = TemporalType.TIMESTAMP;
if (Instant.class.equals(returnedClass)) {
return DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneOffset.systemDefault()).format((Instant)someValue);
} else if (LocalDate.class.equals(returnedClass)) {
return DateTimeFormatter.ISO_DATE.format((LocalDate)someValue);
} else if (Date.class.equals(returnedClass) || Calendar.class.equals(returnedClass)) {
TemporalType temporalType = TemporalType.TIMESTAMP;
try {
final Field field = entityClass.getDeclaredField(propertyName);
if ((field != null) && field.isAnnotationPresent(Temporal.class)) {
......@@ -319,12 +327,12 @@ public class AuditTrailInterceptor extends EmptyInterceptor implements Initializ
}
switch (temporalType) {
case TIMESTAMP:
return dateTimeFormatter.format(someValue);
case DATE:
return dateFormatter.format(someValue);
case TIME:
return timeFormatter.format(someValue);
case TIMESTAMP:
return dateTimeFormatter.format(someValue);
case DATE:
return dateFormatter.format(someValue);
case TIME:
return timeFormatter.format(someValue);
}
}
......@@ -427,16 +435,16 @@ public class AuditTrailInterceptor extends EmptyInterceptor implements Initializ
LOG.trace("onCollectionRemove is empty, no change", key);
return;
}
if (pc.getValue() instanceof Collection<?>) {
deleted.addAll((Collection<?>) pc.getValue());
}
if (deleted.size() == 0) {
LOG.trace("onCollectionRemove is empty, no change", key);
return;
}
Serializable snap = pc.getStoredSnapshot();
LOG.trace("Collection remove: key={} coll={} snap={}", key, collection, snap);
......@@ -511,7 +519,7 @@ public class AuditTrailInterceptor extends EmptyInterceptor implements Initializ
recordChange(pc.getOwner(), (Long) key, propertyName, collectionToStringSorted(previous), collectionToStringSorted(remaining), referencedEntity, previous, remaining);
}
/**
* Produce a string representation of the collection. Set elements are sorted,
* other collection types return `#toString`.
......@@ -614,7 +622,9 @@ public class AuditTrailInterceptor extends EmptyInterceptor implements Initializ
return true;
} else if (Boolean.class.equals(class1)) {
return true;
} else if (Date.class.equals(class1)) {
} else if (Instant.class.equals(class1)) {
return true;
} else if (LocalDate.class.equals(class1)) {
return true;
} else if (UUID.class.equals(class1)) {
return true;
......@@ -634,7 +644,7 @@ public class AuditTrailInterceptor extends EmptyInterceptor implements Initializ
* @param previousState the previous state
* @param currentState the current state
* @param referencedEntity the referenced entity
* @param curr previousEntit
* @param curr previousEntit
* @param prev currentEntity
*/
private void recordChange(final Object entity, final Long id, final String propertyName, final String previousState, final String currentState,
......
......@@ -15,8 +15,6 @@
*/
package org.genesys.blocks.auditlog.model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
......@@ -29,8 +27,6 @@ import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
......@@ -43,6 +39,8 @@ import org.springframework.data.annotation.CreatedBy;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.time.Instant;
/**
* The Class AuditLog.
*/
......@@ -67,9 +65,8 @@ public class AuditLog extends EmptyModel {
private Long createdBy;
/** The log date. */
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "logdate", nullable = false)
private Date logDate;
private Instant logDate;
/** Class name of the referenced entity. */
@ManyToOne(optional = false)
......@@ -184,7 +181,7 @@ public class AuditLog extends EmptyModel {
*
* @return the log date
*/
public Date getLogDate() {
public Instant getLogDate() {
return logDate;
}
......@@ -193,7 +190,7 @@ public class AuditLog extends EmptyModel {
*
* @param logDate the log date
*/
public void setLogDate(final Date logDate) {
public void setLogDate(final Instant logDate) {
this.logDate = logDate;
}
......
......@@ -24,8 +24,8 @@ import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.auditlog.model.AuditAction;
import org.genesys.blocks.auditlog.model.AuditLog;
import org.genesys.blocks.auditlog.model.QAuditLog;
import org.genesys.blocks.model.filters.DateFilter;
import org.genesys.blocks.model.filters.EmptyModelFilter;
import org.genesys.blocks.model.filters.InstantFilter;
import org.genesys.blocks.model.filters.NumberFilter;
/**
......@@ -51,7 +51,7 @@ public class AuditLogFilter extends EmptyModelFilter<AuditLogFilter, AuditLog> {
public Set<String> propertyName;
/** The log date. */
public DateFilter logDate;
public InstantFilter logDate;
/**
* Builds the query.
......
......@@ -15,8 +15,8 @@
*/
package org.genesys.blocks.auditlog.service.impl;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -113,7 +113,7 @@ public class AuditTrailServiceImpl implements AuditTrailService {
*/
private AuditLog toAuditLog(final TransactionAuditLog tlog) {
final AuditLog auditLog = new AuditLog();
auditLog.setLogDate(new Date());
auditLog.setLogDate(Instant.now());
auditLog.setAction(tlog.getAction());
auditLog.setClassPk(tlog.getClassPk());
auditLog.setEntityId(tlog.getEntityId());
......
......@@ -18,7 +18,8 @@ package org.genesys.blocks.auditlog.component;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import java.util.Date;
import java.time.Instant;
import java.time.LocalDate;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
......@@ -84,7 +85,8 @@ public class AuditTrailInterceptorTest {
assertThat(auditTrailInterceptor.isPrimitiveType(Boolean.class), is(true));
assertThat(auditTrailInterceptor.isPrimitiveType(boolean.class), is(true));
assertThat(auditTrailInterceptor.isPrimitiveType(TestEnum.class), is(true));
assertThat(auditTrailInterceptor.isPrimitiveType(Date.class), is(true));
assertThat(auditTrailInterceptor.isPrimitiveType(Instant.class), is(true));
assertThat(auditTrailInterceptor.isPrimitiveType(LocalDate.class), is(true));
assertThat(auditTrailInterceptor.isPrimitiveType(UUID.class), is(true));
}
......
......@@ -21,8 +21,6 @@ import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import java.util.Date;
import org.genesys.blocks.auditlog.persistence.AuditLogRepository;
import org.genesys.blocks.auditlog.test.EntityTest;
import org.genesys.blocks.model.ClassPK;
......@@ -31,6 +29,8 @@ import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import java.time.Instant;
/**
* The Class AuditLogEntityTest.
*/
......@@ -62,7 +62,7 @@ public class AuditLogEntityTest extends EntityTest<AuditLog, AuditLogRepository>
public void successfulPersist() {
final AuditLog log = makeDefault();
log.setAction(AuditAction.UPDATE);
log.setLogDate(new Date());
log.setLogDate(Instant.now());
log.setClassPk(makeClassPk(Long.class));
log.setEntityId(1);
log.setNewState("New");
......
......@@ -20,9 +20,9 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
......@@ -83,9 +83,9 @@ public class CopyableTest {
@LastModifiedBy
private Long myLastModifiedBy;
@CreatedDate
private Date myCreatedDate;
private Instant myCreatedDate;
@LastModifiedDate
private Date myLastModifiedDate;
private Instant myLastModifiedDate;
@NotCopyable
private String myNotCopyable;
private int myInt;
......@@ -128,8 +128,8 @@ public class CopyableTest {
source.myId = 1L;
source.myCreatedBy = 2L;
source.myLastModifiedBy = 3L;
source.myCreatedDate = new Date();
source.myLastModifiedDate = new Date();
source.myCreatedDate = Instant.now();
source.myLastModifiedDate = Instant.now();
source.myNotCopyable = "test string";
source.setPrivateInt(6);
source.myDefaultInt = 7;
......@@ -181,8 +181,8 @@ public class CopyableTest {
source.myId = 1L;
source.myCreatedBy = 2L;
source.myLastModifiedBy = 3L;
source.myCreatedDate = new Date();
source.myLastModifiedDate = new Date();
source.myCreatedDate = Instant.now();
source.myLastModifiedDate = Instant.now();
source.myNotCopyable = "test string";
source.setPrivateInt(6);
source.myDefaultInt = 7;
......
......@@ -16,8 +16,8 @@
package org.genesys.blocks.security.lockout;
import java.io.Serializable;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
......@@ -126,7 +126,7 @@ public class AccountLockoutManager {
if (stats != null) {
stats.count++;
stats.lastAttempt = new Date();
stats.lastAttempt = Instant.now();
loginAttempts.put(userName, stats);
LOG.info("Updated failed login statistics for username=" + userName + " " + stats);
......@@ -157,7 +157,7 @@ public class AccountLockoutManager {
}
final List<String> userNames = new ArrayList<>(loginAttempts.keySet());
final long now = new Date().getTime();
final long now = Instant.now().toEpochMilli();
for (final String userName : userNames) {
final AttemptStatistics stats = loginAttempts.get(userName);
......@@ -166,7 +166,7 @@ public class AccountLockoutManager {
continue;
}
if ((now - stats.lastAttempt.getTime()) >= lockoutTime) {
if ((now - stats.lastAttempt.toEpochMilli()) >= lockoutTime) {
loginAttempts.remove(userName);
LOG.info("Removed expired failed login statistics for " + userName + " " + stats);
}
......@@ -192,7 +192,7 @@ public class AccountLockoutManager {
int count = 0;
/** The last attempt. */
Date lastAttempt = new Date();
Instant lastAttempt = Instant.now();
/*
* (non-Javadoc)
......
......@@ -16,9 +16,9 @@
package org.genesys.blocks.security.model;
import java.text.MessageFormat;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
......@@ -36,8 +36,6 @@ import javax.persistence.JoinColumn;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import com.fasterxml.jackson.annotation.JsonIgnore;
......@@ -109,18 +107,15 @@ public abstract class BasicUser<R extends GrantedAuthority> extends AclSid imple
/** Account control. */
@JsonView(JsonViews.Internal.class)
@Temporal(TemporalType.TIMESTAMP)
private Date accountExpires;
private Instant accountExpires;
/** The locked until. */
@JsonView(JsonViews.Internal.class)
@Temporal(TemporalType.TIMESTAMP)
private Date lockedUntil;
private Instant lockedUntil;
/** The password expires. */
@JsonView(JsonViews.Internal.class)
@Temporal(TemporalType.TIMESTAMP)
private Date passwordExpires;
private Instant passwordExpires;
/** The roles. */
@JsonView(JsonViews.Protected.class)
......@@ -138,8 +133,7 @@ public abstract class BasicUser<R extends GrantedAuthority> extends AclSid imple
/** The date of last successful login. */
@JsonView(JsonViews.Internal.class)
@Temporal(TemporalType.TIMESTAMP)
private Date lastLogin;
private Instant lastLogin;
@Transient
@JsonIgnore
......@@ -226,7 +220,7 @@ public abstract class BasicUser<R extends GrantedAuthority> extends AclSid imple
*
* @return the account expires
*/
public Date getAccountExpires() {
public Instant getAccountExpires() {
return accountExpires;
}
......@@ -235,7 +229,7 @@ public abstract class BasicUser<R extends GrantedAuthority> extends AclSid imple
*
* @param accountExpires the new account expires
*/
public void setAccountExpires(final Date accountExpires) {
public void setAccountExpires(final Instant accountExpires) {
this.accountExpires = accountExpires;
}
......@@ -244,7 +238,7 @@ public abstract class BasicUser<R extends GrantedAuthority> extends AclSid imple
*
* @return the locked until
*/
public Date getLockedUntil() {
public Instant getLockedUntil() {
return lockedUntil;
}
......@@ -253,7 +247,7 @@ public abstract class BasicUser<R extends GrantedAuthority> extends AclSid imple
*
* @param lockedUntil the new locked until
*/
public void setLockedUntil(final Date lockedUntil) {
public void setLockedUntil(final Instant lockedUntil) {
this.lockedUntil = lockedUntil;
}
......@@ -262,7 +256,7 @@ public abstract class BasicUser<R extends GrantedAuthority> extends AclSid imple
*
* @return the password expires
*/
public Date getPasswordExpires() {
public Instant getPasswordExpires() {
return passwordExpires;
}
......@@ -271,7 +265,7 @@ public abstract class BasicUser<R extends GrantedAuthority> extends AclSid imple
*
* @param passwordExpires the new password expires
*/
public void setPasswordExpires(final Date passwordExpires) {
public void setPasswordExpires(final Instant passwordExpires) {
this.passwordExpires = passwordExpires;
}
......@@ -391,7 +385,7 @@ public abstract class BasicUser<R extends GrantedAuthority> extends AclSid imple
@Override
@JsonView(JsonViews.Protected.class)
public boolean isAccountNonLocked() {
return (lockedUntil == null) || !lockedUntil.after(new Date());
return (lockedUntil == null) || !lockedUntil.isAfter(Instant.now());
}
/**
......@@ -412,7 +406,7 @@ public abstract class BasicUser<R extends GrantedAuthority> extends AclSid imple
@Override
@JsonView(JsonViews.Protected.class)
public boolean isCredentialsNonExpired() {
return (passwordExpires == null) || !passwordExpires.before(new Date());
return (passwordExpires == null) || !passwordExpires.isBefore(Instant.now());
}
/*
......@@ -448,7 +442,7 @@ public abstract class BasicUser<R extends GrantedAuthority> extends AclSid imple
*
* @return the last login
*/
public Date getLastLogin() {
public Instant getLastLogin() {
return lastLogin;
}
......@@ -457,7 +451,7 @@ public abstract class BasicUser<R extends GrantedAuthority> extends AclSid imple
*
* @param lastLogin the new last login
*/
public void setLastLogin(Date lastLogin) {
public void setLastLogin(Instant lastLogin) {
this.lastLogin = lastLogin;
}
......
......@@ -15,12 +15,17 @@
*/
package org.genesys.blocks.security.service.impl;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.persistence.EntityNotFoundException;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.security.NoUserFoundException;
import org.genesys.blocks.security.NotUniqueUserException;
......@@ -43,8 +48,6 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityNotFoundException;
/**
* The Class BasicUserServiceImpl.
*
......@@ -276,10 +279,8 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
if (user.getAccountType() == AccountType.LOCAL) {
setPassword(user, password);
// Set account to expire 1 month after change password
Calendar accountExpires = Calendar.getInstance();
accountExpires.add(Calendar.MONTH, 1);
user.setAccountExpires(accountExpires.getTime());
// Set account to expire 12 months after change password
user.setAccountExpires(LocalDateTime.now().plusMonths(12).toInstant(ZoneOffset.UTC));
return deepLoad(_repository.save(user));
} else {
......@@ -330,7 +331,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
final T user = getUser(userId);
if (locked) {
// Lock for account until some time
user.setLockedUntil(new Date(System.currentTimeMillis() + accountLockoutTime));
user.setLockedUntil(Instant.now().plus(accountLockoutTime, ChronoUnit.MILLIS));
LOG.warn("Locking user account for user=" + user.getEmail() + " until=" + user.getLockedUntil());
} else {
LOG.warn("Unlocking user account for user=" + user.getEmail());
......@@ -373,13 +374,13 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
throw new NoUserFoundException("No such user.");
}
u.setLastLogin(new Date());
u.setLastLogin(Instant.now());
// Set account to expire 1 year after last login
Calendar accountExpires = Calendar.getInstance();
accountExpires.add(Calendar.YEAR, 1);
u.setAccountExpires(accountExpires.getTime());
u.setAccountExpires(accountExpires.toInstant());
_repository.save(u);
}
}
......@@ -19,7 +19,8 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.util.Calendar;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.util.stream.Collectors;
import org.genesys.blocks.security.NoUserFoundException;
......@@ -56,15 +57,13 @@ public class BasicUserServiceTest extends ServiceTest {
assertThat(user.getAccountType(), is(AccountType.LOCAL));
TestUser updatedUser = testUserService.changePassword(user, "newPassword2#");
Calendar accountExpires = Calendar.getInstance();
accountExpires.add(Calendar.MONTH, 1);
LocalDate accountExpires = LocalDate.now().plusMonths(12);
Calendar updatedAccountExpires = Calendar.getInstance();
updatedAccountExpires.setTime(updatedUser.getAccountExpires());
LocalDate updatedAccountExpires = updatedUser.getAccountExpires().atZone(ZoneOffset.systemDefault()).toLocalDate();
assertThat(accountExpires.get(Calendar.YEAR), is(updatedAccountExpires.get(Calendar.YEAR)));
assertThat(accountExpires.get(Calendar.MONTH), is(updatedAccountExpires.get(Calendar.MONTH)));
assertThat(accountExpires.get(Calendar.DAY_OF_MONTH), is(updatedAccountExpires.get(Calendar.DAY_OF_MONTH)));
assertThat(accountExpires.getYear(), is(updatedAccountExpires.getYear()));
assertThat(accountExpires.getMonthValue(), is(updatedAccountExpires.getMonthValue()));
assertThat(accountExpires.getDayOfMonth(), is(updatedAccountExpires.getDayOfMonth()));
}
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment