-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CustomAuditHandler for supporting OffsetDateTime and store in UTC
- Loading branch information
Showing
12 changed files
with
626 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...rc/main/java/org/apache/fineract/infrastructure/core/auditing/CustomDateTimeProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.fineract.infrastructure.core.auditing; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.OffsetDateTime; | ||
import java.time.ZoneId; | ||
import java.time.temporal.TemporalAccessor; | ||
import java.util.Optional; | ||
import org.apache.fineract.infrastructure.core.service.DateUtils; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.data.auditing.DateTimeProvider; | ||
|
||
public enum CustomDateTimeProvider implements DateTimeProvider { | ||
|
||
INSTANCE, TENANT; | ||
|
||
/* | ||
* (non-Javadoc) | ||
* | ||
* @see org.springframework.data.auditing.DateTimeProvider#getNow() | ||
*/ | ||
@NotNull | ||
@Override | ||
public Optional<TemporalAccessor> getNow() { | ||
|
||
switch (this) { | ||
case INSTANCE -> { | ||
return Optional.of(LocalDateTime.now(ZoneId.systemDefault())); | ||
} | ||
case TENANT -> { | ||
return Optional.of(OffsetDateTime.now(DateUtils.getDateTimeZoneOfTenant())); | ||
} | ||
} | ||
throw new UnsupportedOperationException(this + " is not supported!"); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...in/java/org/apache/fineract/infrastructure/core/auditing/JpaAuditingHandlerRegistrar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.fineract.infrastructure.core.auditing; | ||
|
||
import org.springframework.beans.factory.support.BeanDefinitionBuilder; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; | ||
import org.springframework.core.type.AnnotationMetadata; | ||
import org.springframework.data.auditing.CustomAuditingHandler; | ||
|
||
public class JpaAuditingHandlerRegistrar implements ImportBeanDefinitionRegistrar { | ||
|
||
@Override | ||
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { | ||
registry.registerBeanDefinition("jpaAuditingHandler", BeanDefinitionBuilder.rootBeanDefinition(CustomAuditingHandler.class) | ||
.addConstructorArgReference("jpaMappingContext").addConstructorArgReference("auditorAware").getBeanDefinition()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...rg/apache/fineract/infrastructure/core/domain/AbstractAuditableWithUTCDateTimeCustom.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.fineract.infrastructure.core.domain; | ||
|
||
import static org.apache.fineract.infrastructure.core.domain.AuditableFieldsConstants.CREATED_BY_DB_FIELD; | ||
import static org.apache.fineract.infrastructure.core.domain.AuditableFieldsConstants.CREATED_DATE_DB_FIELD; | ||
import static org.apache.fineract.infrastructure.core.domain.AuditableFieldsConstants.LAST_MODIFIED_BY_DB_FIELD; | ||
import static org.apache.fineract.infrastructure.core.domain.AuditableFieldsConstants.LAST_MODIFIED_DATE_DB_FIELD; | ||
|
||
import java.time.OffsetDateTime; | ||
import java.util.Optional; | ||
import javax.persistence.Column; | ||
import javax.persistence.MappedSuperclass; | ||
import org.apache.fineract.infrastructure.core.service.DateUtils; | ||
import org.springframework.data.domain.Auditable; | ||
import org.springframework.data.jpa.domain.AbstractAuditable; | ||
|
||
/** | ||
* A custom copy of {@link AbstractAuditable} to override the column names used on database. It also uses OffsetDateTime | ||
* for created and modified. The datetimes will be converted from tenant TZ to UTC before storing (automatically | ||
* happens) and converted from System TZ to tenant TZ after fetching from DB | ||
* | ||
* Abstract base class for auditable entities. Stores the audit values in persistent fields. | ||
*/ | ||
@MappedSuperclass | ||
public abstract class AbstractAuditableWithUTCDateTimeCustom extends AbstractPersistableCustom | ||
implements Auditable<Long, Long, OffsetDateTime> { | ||
|
||
private static final long serialVersionUID = 141481953116476081L; | ||
|
||
@Column(name = CREATED_BY_DB_FIELD, nullable = false) | ||
private Long createdBy; | ||
|
||
@Column(name = CREATED_DATE_DB_FIELD, nullable = false) | ||
private OffsetDateTime createdDate; | ||
|
||
@Column(name = LAST_MODIFIED_BY_DB_FIELD, nullable = false) | ||
private Long lastModifiedBy; | ||
|
||
@Column(name = LAST_MODIFIED_DATE_DB_FIELD, nullable = false) | ||
private OffsetDateTime lastModifiedDate; | ||
|
||
@Override | ||
public Optional<Long> getCreatedBy() { | ||
return Optional.ofNullable(this.createdBy); | ||
} | ||
|
||
@Override | ||
public void setCreatedBy(final Long createdBy) { | ||
this.createdBy = createdBy; | ||
} | ||
|
||
@Override | ||
public Optional<OffsetDateTime> getCreatedDate() { | ||
if (this.createdDate == null) { | ||
return Optional.empty(); | ||
} else { | ||
return Optional.of(this.createdDate | ||
.withOffsetSameInstant(DateUtils.getDateTimeZoneOfTenant().getRules().getOffset(this.createdDate.toInstant()))); | ||
} | ||
} | ||
|
||
@Override | ||
public void setCreatedDate(final OffsetDateTime createdDate) { | ||
this.createdDate = createdDate; | ||
} | ||
|
||
@Override | ||
public Optional<Long> getLastModifiedBy() { | ||
return Optional.ofNullable(this.lastModifiedBy); | ||
} | ||
|
||
@Override | ||
public void setLastModifiedBy(final Long lastModifiedBy) { | ||
this.lastModifiedBy = lastModifiedBy; | ||
} | ||
|
||
@Override | ||
public Optional<OffsetDateTime> getLastModifiedDate() { | ||
if (this.lastModifiedDate == null) { | ||
return Optional.empty(); | ||
} else { | ||
return Optional.of(this.lastModifiedDate | ||
.withOffsetSameInstant(DateUtils.getDateTimeZoneOfTenant().getRules().getOffset(this.lastModifiedDate.toInstant()))); | ||
} | ||
} | ||
|
||
@Override | ||
public void setLastModifiedDate(final OffsetDateTime lastModifiedDate) { | ||
this.lastModifiedDate = lastModifiedDate; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...rc/main/java/org/apache/fineract/infrastructure/core/domain/AuditableFieldsConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.fineract.infrastructure.core.domain; | ||
|
||
public final class AuditableFieldsConstants { | ||
|
||
public static final String CREATED_BY = "createdBy"; | ||
public static final String CREATED_DATE = "createdDate"; | ||
public static final String LAST_MODIFIED_BY = "lastModifiedBy"; | ||
public static final String LAST_MODIFIED_DATE = "lastModifiedDate"; | ||
|
||
public static final String CREATED_BY_DB_FIELD = "created_by"; | ||
public static final String CREATED_DATE_DB_FIELD = "created_on_utc"; | ||
public static final String LAST_MODIFIED_BY_DB_FIELD = "last_modified_by"; | ||
public static final String LAST_MODIFIED_DATE_DB_FIELD = "last_modified_on_utc"; | ||
|
||
private AuditableFieldsConstants() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.