com.liferay.batch.engine.internal.reader.BatchEngineImportTaskItemReaderUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com.liferay.batch.engine.service
Show all versions of com.liferay.batch.engine.service
Liferay Batch Engine Service
The newest version!
/**
* SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/
package com.liferay.batch.engine.internal.reader;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.liferay.batch.engine.action.ItemReaderPostAction;
import com.liferay.batch.engine.model.BatchEngineImportTask;
import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.feature.flag.FeatureFlagManagerUtil;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.ArrayUtil;
import com.liferay.portal.kernel.util.MapUtil;
import com.liferay.portal.kernel.util.SetUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Ivica Cardic
*/
public class BatchEngineImportTaskItemReaderUtil {
public static T convertValue(
BatchEngineImportTask batchEngineImportTask, Class itemClass,
Map fieldNameValueMap,
List itemReaderPostActions)
throws ReflectiveOperationException {
Map extendedProperties = new HashMap<>();
T item = itemClass.newInstance();
boolean keepCreatorInfo = false;
if (FeatureFlagManagerUtil.isEnabled("LPD-11036") &&
StringUtil.equals(
batchEngineImportTask.getParameterValue(
"importCreatorStrategy"),
"KEEP_CREATOR")) {
keepCreatorInfo = true;
}
Set restrictedFieldNames = _getRestrictedFieldNames(
batchEngineImportTask);
for (Map.Entry entry : fieldNameValueMap.entrySet()) {
String name = entry.getKey();
if (restrictedFieldNames.contains(name)) {
continue;
}
Field field = null;
for (Field declaredField : itemClass.getDeclaredFields()) {
if (name.equals(declaredField.getName()) ||
Objects.equals(
StringPool.UNDERLINE + name, declaredField.getName())) {
field = declaredField;
break;
}
}
if (field != null) {
field.setAccessible(true);
ObjectMapper objectMapper = _getObjectMapper(
field, keepCreatorInfo);
field.set(
item,
objectMapper.convertValue(
entry.getValue(), field.getType()));
continue;
}
for (Field declaredField : itemClass.getDeclaredFields()) {
JsonAnySetter[] jsonAnySetters =
declaredField.getAnnotationsByType(JsonAnySetter.class);
if (jsonAnySetters.length > 0) {
field = declaredField;
break;
}
}
if (field == null) {
extendedProperties.put(
entry.getKey(), (Serializable)entry.getValue());
}
else {
field.setAccessible(true);
Map map = (Map)field.get(item);
map.put(entry.getKey(), entry.getValue());
}
}
for (ItemReaderPostAction itemReaderPostAction :
itemReaderPostActions) {
itemReaderPostAction.run(
batchEngineImportTask, extendedProperties, item);
}
return item;
}
public static Map mapFieldNames(
Map fieldNameMappingMap,
Map fieldNameValueMap) {
if ((fieldNameMappingMap == null) || fieldNameMappingMap.isEmpty()) {
return fieldNameValueMap;
}
Map targetFieldNameValueMap = new HashMap<>();
for (Map.Entry entry : fieldNameValueMap.entrySet()) {
String targetFieldName = (String)fieldNameMappingMap.get(
entry.getKey());
if (Validator.isNotNull(targetFieldName)) {
Object object = targetFieldNameValueMap.get(targetFieldName);
if ((object != null) && (object instanceof Map)) {
Map, ?> map = (Map)object;
map.putAll((Map)entry.getValue());
}
else {
targetFieldNameValueMap.put(
targetFieldName, entry.getValue());
}
continue;
}
String[] fieldNameParts = StringUtil.split(
entry.getKey(), StringPool.PERIOD);
targetFieldName = (String)fieldNameMappingMap.get(
fieldNameParts[0]);
if (Validator.isNull(targetFieldName)) {
continue;
}
Matcher multiselectPicklistMatcher =
_multiselectPicklistPattern.matcher(fieldNameParts[1]);
if (multiselectPicklistMatcher.matches()) {
if (fieldNameParts[1].startsWith("name_")) {
continue;
}
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy