Commit 15e685c2 authored by Maxym Borodenko's avatar Maxym Borodenko Committed by Matija Obreza

AuditLog#createdBy returns a name of a Cooperator

parent 7e8e9ea5
......@@ -18,9 +18,11 @@ package org.gringlobal.api.v1.impl;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.validation.Valid;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.apache.commons.lang3.ArrayUtils;
import org.genesys.blocks.auditlog.model.AuditLog;
import org.genesys.blocks.auditlog.service.AuditTrailService;
......@@ -31,6 +33,7 @@ import org.gringlobal.api.v1.FilteredCRUDController;
import org.gringlobal.api.v1.FilteredPage;
import org.gringlobal.api.v1.Pagination;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.custom.json.converter.JsonCooperatorConverter;
import org.gringlobal.model.AccessionInvAttach;
import org.gringlobal.model.Inventory;
import org.gringlobal.model.InventoryAction;
......@@ -114,8 +117,8 @@ public class InventoryController extends FilteredCRUDController<Inventory, Inven
* @return the list of all log entries
*/
@GetMapping("/auditlog/{id}")
public List<AuditLog> inventoryAuditLogs(@PathVariable(value = "id") final Long inventoryId) {
return auditService.listAuditLogs(crudService.get(inventoryId));
public List<WrappedAuditLog> inventoryAuditLogs(@PathVariable(value = "id") final Long inventoryId) {
return auditService.listAuditLogs(crudService.get(inventoryId)).stream().map(WrappedAuditLog::new).collect(Collectors.toList());
}
@Override
......@@ -195,4 +198,16 @@ public class InventoryController extends FilteredCRUDController<Inventory, Inven
return attachmentFileService.removeFile(crudService.get(inventoryId), attachmentId);
}
private static class WrappedAuditLog extends AuditLog {
public WrappedAuditLog(AuditLog auditLog) {
super(auditLog);
}
@JsonSerialize(converter = JsonCooperatorConverter.class)
@Override
public Long getCreatedBy() {
return super.getCreatedBy();
}
}
}
......@@ -21,6 +21,10 @@ import org.genesys.blocks.auditlog.service.ClassPKService;
import org.genesys.blocks.auditlog.service.impl.AuditTrailServiceImpl;
import org.genesys.blocks.auditlog.service.impl.ClassPKServiceImpl;
import org.genesys.blocks.security.SpringSecurityAuditorAware;
import org.gringlobal.custom.json.converter.JsonCooperatorConverter;
import org.gringlobal.service.CooperatorService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
......@@ -31,7 +35,15 @@ import org.springframework.data.domain.AuditorAware;
* @author Matija Obreza
*/
@Configuration
public class AuditConfig {
public class AuditConfig implements InitializingBean {
@Autowired
private CooperatorService cooperatorService;
@Override
public void afterPropertiesSet() throws Exception {
JsonCooperatorConverter.setCooperatorProvider(cooperatorService);
}
@Bean
public AuditorAware<?> auditorAware() {
......
/*
* Copyright 2020 Global Crop Diversity Trust
*
* Licensed 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.gringlobal.custom.json.converter;
import com.fasterxml.jackson.databind.util.StdConverter;
/**
* The JsonCooperatorConverter converts Cooperator IDs to Cooperator names.
*/
public class JsonCooperatorConverter extends StdConverter<Long, String> {
private static CooperatorProvider COOPERATOR_PROVIDER;
public interface CooperatorProvider {
String getCooperatorName(long id);
}
/**
* Sets the cooperator provider.
*
* @param cooperatorProvider the new cooperator provider
*/
public static void setCooperatorProvider(CooperatorProvider cooperatorProvider) {
COOPERATOR_PROVIDER = cooperatorProvider;
}
/**
* Convert Cooperator ID to Cooperator name using COOPERATOR_PROVIDER (when available)
*/
@Override
public String convert(Long value) {
if (value == null) {
return null;
} else {
if (COOPERATOR_PROVIDER == null) {
return Long.toString(value);
} else {
return COOPERATOR_PROVIDER.getCooperatorName(value);
}
}
}
}
......@@ -15,13 +15,14 @@
*/
package org.gringlobal.service;
import org.gringlobal.custom.json.converter.JsonCooperatorConverter;
import org.gringlobal.model.Cooperator;
import org.gringlobal.service.filter.CooperatorFilter;
/**
* The Interface CooperatorService.
*/
public interface CooperatorService extends FilteredCRUDService<Cooperator, CooperatorFilter> {
public interface CooperatorService extends FilteredCRUDService<Cooperator, CooperatorFilter>, JsonCooperatorConverter.CooperatorProvider {
}
......@@ -15,6 +15,7 @@
*/
package org.gringlobal.service.impl;
import org.apache.commons.lang3.StringUtils;
import org.gringlobal.api.InvalidApiUsageException;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.Cooperator;
......@@ -94,4 +95,13 @@ public class CooperatorServiceImpl extends FilteredCRUDServiceImpl<Cooperator, C
return QCooperator.cooperator.id;
}
@Override
public String getCooperatorName(long id) {
Cooperator cooperator = repository.findById(id).orElse(null);
if (cooperator != null) {
String name = cooperator.getFirstName() + " " + StringUtils.defaultIfBlank(cooperator.getLastName(), "");
return name.strip();
}
return null;
}
}
......@@ -236,7 +236,7 @@ public class InventoryControllerTest extends AbstractApiV1Test {
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$.[0]").isMap())
.andExpect(jsonPath("$.[0].createdBy", is(USER_CURATOR1_ID)))
.andExpect(jsonPath("$.[0].createdBy").isString())
.andExpect(jsonPath("$.[0].logDate").exists())
.andExpect(jsonPath("$.[0].classPk").isMap())
.andExpect(jsonPath("$.[0].classPk.classname", is(Inventory.class.getCanonicalName())))
......
Markdown is supported
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