SUPPORT-9422: add deserializer
This commit is contained in:
parent
0df57ed5a7
commit
48d47b6652
5 changed files with 76 additions and 7 deletions
|
|
@ -4,11 +4,15 @@ import java.sql.SQLException;
|
|||
import java.sql.Statement;
|
||||
import java.time.Duration;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import ervu_business_metrics.deserializer.DataWrapperDeserializer;
|
||||
import ervu_business_metrics.deserializer.ReferenceEntityDeserializer;
|
||||
import ervu_business_metrics.model.DataWrapper;
|
||||
import ervu_business_metrics.model.ReferenceEntity;
|
||||
import exception.AppInitializeException;
|
||||
import liquibase.integration.spring.SpringLiquibase;
|
||||
|
|
@ -131,7 +135,7 @@ public class AppConfig {
|
|||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
SimpleModule module = new SimpleModule();
|
||||
module.addDeserializer(ReferenceEntity.class, new ReferenceEntityDeserializer());
|
||||
customDeserializers().forEach(module::addDeserializer);
|
||||
|
||||
objectMapper.registerModule(module);
|
||||
return objectMapper;
|
||||
|
|
@ -142,4 +146,10 @@ public class AppConfig {
|
|||
return new RestTemplate();
|
||||
}
|
||||
|
||||
private Map<Class, JsonDeserializer> customDeserializers() {
|
||||
return Map.of(
|
||||
ReferenceEntity.class, new ReferenceEntityDeserializer(),
|
||||
DataWrapper.class, new DataWrapperDeserializer()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,39 @@
|
|||
package ervu_business_metrics.deserializer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.core.JacksonException;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import ervu_business_metrics.model.DataWrapper;
|
||||
|
||||
/**
|
||||
* @author Adel Kalimullin
|
||||
*/
|
||||
public class DataWrapperDeserializer extends JsonDeserializer<DataWrapper> {
|
||||
|
||||
@Override
|
||||
public DataWrapper deserialize(JsonParser jsonParser,
|
||||
DeserializationContext deserializationContext) throws IOException, JacksonException {
|
||||
JsonNode node = jsonParser.readValueAsTree();
|
||||
if (node.isTextual()){
|
||||
return new DataWrapper(List.of(node.asText()));
|
||||
}
|
||||
else if (node.isArray()){
|
||||
List<String> values = new ArrayList<>();
|
||||
for (JsonNode element : node) {
|
||||
if (element.isTextual()) {
|
||||
values.add(element.asText());
|
||||
}
|
||||
}
|
||||
return new DataWrapper(values);
|
||||
}
|
||||
|
||||
return new DataWrapper(Collections.emptyList());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
package ervu_business_metrics.kafka.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import ervu_business_metrics.model.DataWrapper;
|
||||
|
||||
/**
|
||||
* @author Adel Kalimullin
|
||||
|
|
@ -10,13 +10,13 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
|||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class ChangeActiveMessage {
|
||||
private boolean success;
|
||||
private List<String> data;
|
||||
private DataWrapper data;
|
||||
|
||||
public List<String> getData() {
|
||||
public DataWrapper getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(List<String> data) {
|
||||
public void setData(DataWrapper data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
package ervu_business_metrics.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Adel Kalimullin
|
||||
*/
|
||||
public class DataWrapper {
|
||||
private final List<String> ids;
|
||||
|
||||
public DataWrapper(List<String> values) {
|
||||
this.ids = values;
|
||||
}
|
||||
|
||||
public List<String> getIds() {
|
||||
return ids;
|
||||
}
|
||||
}
|
||||
|
|
@ -103,13 +103,15 @@ public class IdmDirectoriesService {
|
|||
ChangeActiveMessage.class
|
||||
);
|
||||
|
||||
if (changeActiveMessage.isSuccess() && !CollectionUtils.isEmpty(changeActiveMessage.getData())) {
|
||||
if (changeActiveMessage.isSuccess() &&
|
||||
changeActiveMessage.getData() != null &&
|
||||
!CollectionUtils.isEmpty(changeActiveMessage.getData().getIds())) {
|
||||
DataProcessor<T, ?> processor = (DataProcessor<T, ?>) dataProcessors.get(entityClass);
|
||||
if (processor == null) {
|
||||
throw new IllegalStateException("No processor found for " + entityClass.getSimpleName());
|
||||
}
|
||||
|
||||
processor.changeActiveStatus(changeActiveMessage.getData(), active);
|
||||
processor.changeActiveStatus(changeActiveMessage.getData().getIds(), active);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue