| /******************************************************************************** |
| * Copyright (c) 2015-2020 Contributors to the Eclipse Foundation |
| * |
| * See the NOTICE file(s) distributed with this work for additional |
| * information regarding copyright ownership. |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| ********************************************************************************/ |
| package org.eclipse.mdm.businessobjects.utils; |
| |
| import java.time.LocalDateTime; |
| import java.time.ZoneOffset; |
| import java.time.format.DateTimeFormatter; |
| import java.util.ArrayList; |
| import java.util.List; |
| import java.util.Map; |
| import java.util.Objects; |
| import java.util.stream.Collectors; |
| import java.util.stream.Stream; |
| |
| import org.eclipse.mdm.api.base.model.FileLink; |
| import org.eclipse.mdm.api.base.model.MimeType; |
| import org.eclipse.mdm.api.base.model.Value; |
| import org.eclipse.mdm.api.base.model.ValueType; |
| import org.eclipse.mdm.businessobjects.control.MDMEntityAccessException; |
| import org.eclipse.mdm.businessobjects.entity.MDMFileLink; |
| |
| import com.google.common.base.Strings; |
| |
| /** |
| * Serializer for values. |
| */ |
| public final class Serializer { |
| |
| private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"); |
| |
| private Serializer() { |
| } |
| |
| public static Object serializeValue(Value value) { |
| if (value.getValueType().isDate()) { |
| return formatter.format(value.extract()); |
| } else if (value.getValueType().isFileLink()) { |
| return serializeFileLink(value.extract(ValueType.FILE_LINK)); |
| } else if (value.getValueType().isFileLinkSequence()) { |
| return Stream.of(value.extract(ValueType.FILE_LINK_SEQUENCE)).map(Serializer::serializeFileLink) |
| .collect(Collectors.toList()); |
| } else { |
| return value.extract().toString(); |
| } |
| } |
| |
| public static MDMFileLink serializeFileLink(FileLink fileLink) { |
| return new MDMFileLink(fileLink.getRemotePath(), fileLink.getMimeType().toString(), fileLink.getDescription()); |
| } |
| |
| public static LocalDateTime parseDate(String value) { |
| return LocalDateTime.from(formatter.parse(value)); |
| } |
| |
| public static void applyValue(org.eclipse.mdm.api.base.model.Value value, Object newValue) { |
| value.set(deserializeValue(value.getValueType(), newValue)); |
| } |
| |
| public static Object deserializeValue(ValueType<?> type, Object newValue) { |
| |
| if (type.isBoolean()) { |
| if (newValue instanceof Number) { |
| return ((Number) newValue).intValue() == 1; |
| } else if (newValue instanceof String) { |
| return Integer.parseInt((String) newValue) == 1; |
| } |
| } else if (type.isByte()) { |
| if (newValue instanceof Number) { |
| return (byte) ((Number) newValue).intValue(); |
| } else if (newValue instanceof String) { |
| return Byte.parseByte((String) newValue); |
| } |
| } else if (type.isShort()) { |
| if (newValue instanceof Number) { |
| return (short) ((Number) newValue).intValue(); |
| } else if (newValue instanceof String) { |
| return Short.parseShort((String) newValue); |
| } |
| } else if (type.isInteger()) { |
| if (newValue instanceof Number) { |
| return ((Number) newValue).intValue(); |
| } else if (newValue instanceof String) { |
| return Integer.parseInt((String) newValue); |
| } |
| } else if (type.isLong()) { |
| if (newValue instanceof Number) { |
| return ((Number) newValue).longValue(); |
| } else if (newValue instanceof String) { |
| return Long.parseLong((String) newValue); |
| } |
| } else if (type.isFloat()) { |
| if (newValue instanceof Number) { |
| return ((Number) newValue).floatValue(); |
| } else if (newValue instanceof String) { |
| return Float.parseFloat((String) newValue); |
| } |
| } else if (type.isDouble()) { |
| if (newValue instanceof Number) { |
| return ((Number) newValue).doubleValue(); |
| } else if (newValue instanceof String) { |
| return Double.parseDouble((String) newValue); |
| } |
| } else if (type.isDate()) { |
| if (newValue instanceof Number) { |
| return LocalDateTime.ofEpochSecond((long) newValue, 0, ZoneOffset.UTC); |
| } else if (newValue instanceof String) { |
| if (Strings.isNullOrEmpty((String) newValue)) { |
| return null; |
| } else { |
| return Serializer.parseDate((String) newValue); |
| } |
| } |
| } else if (type.isFileLink()) { |
| if (newValue instanceof FileLink) { |
| return newValue; |
| } else { |
| return deserializeFileLink(newValue); |
| } |
| } else if (type.isFileLinkSequence()) { |
| if (newValue instanceof FileLink[]) { |
| return newValue; |
| } else if (newValue instanceof List) { |
| if (((List<?>) newValue).isEmpty()) { |
| return new FileLink[0]; |
| } else { |
| List<FileLink> fileLinks = new ArrayList<>(); |
| for (Object o : (List<?>) newValue) { |
| fileLinks.add(deserializeFileLink(o)); |
| } |
| return fileLinks.toArray(new FileLink[0]); |
| } |
| } |
| } |
| // TODO mkoller on 2018-12-06: Missing ValueTypes: ByteStream, Blob, |
| // FloatComplex, DoubleComplex, Enumeration and all sequence |
| // ValueTypes |
| return newValue; |
| } |
| |
| private static FileLink deserializeFileLink(Object newValue) { |
| |
| if (newValue == null || newValue instanceof String && ((String) newValue).trim().isEmpty()) { |
| return null; |
| } else if (newValue instanceof Map<?, ?>) { |
| Map<?, ?> map = (Map<?, ?>) newValue; |
| String remotePath = Objects.toString(map.get("remotePath")); |
| MimeType mimeType = new MimeType(Objects.toString(map.get("mimeType"))); |
| String description = Objects.toString(map.get("description")); |
| return FileLink.newRemote(remotePath, mimeType, description); |
| } |
| |
| throw new MDMEntityAccessException("Cannot deserialize FILE_LINK: " + newValue); |
| } |
| } |