| /******************************************************************************** |
| * 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.entity; |
| |
| import java.util.ArrayList; |
| import java.util.Collections; |
| import java.util.Comparator; |
| import java.util.List; |
| import java.util.Map; |
| import java.util.stream.Collectors; |
| |
| import org.eclipse.mdm.api.base.model.BaseEntity; |
| import org.eclipse.mdm.api.base.model.ContextComponent; |
| import org.eclipse.mdm.api.base.model.Entity; |
| import org.eclipse.mdm.api.base.model.Sortable; |
| import org.eclipse.mdm.api.base.model.Value; |
| import org.eclipse.mdm.api.dflt.model.TemplateAttribute; |
| import org.eclipse.mdm.api.dflt.model.TemplateComponent; |
| |
| /** |
| * MDMEntity (Entity for a business object (contains a list of |
| * {@link MDMContextAttribute}s) |
| * |
| * @author Juergen Kleck, Peak Solution GmbH |
| */ |
| public class MDMContextEntity extends MDMEntity { |
| |
| /** |
| * sort index of the MDM business object |
| */ |
| private final Integer sortIndex; |
| /** |
| * list of attribute to transfer |
| */ |
| private List<MDMContextAttribute> attributes; |
| /** |
| * list of relations to transfer |
| */ |
| private List<MDMContextRelation> relations; |
| |
| /** |
| * Constructor. |
| * |
| * @param entity the business object |
| */ |
| public MDMContextEntity(Entity entity) { |
| super(entity); |
| // special handling for context component |
| if (entity instanceof ContextComponent) { |
| TemplateComponent tplCmp = TemplateComponent.of((ContextComponent) entity).get(); |
| this.sortIndex = tplCmp.getSortIndex(); |
| this.attributes = convertAttributeValues(entity.getValues(), tplCmp.getTemplateAttributes()); |
| this.relations = convertRelations(entity, tplCmp); |
| } else { |
| this.sortIndex = 0; |
| this.attributes = convertAttributeValues(entity.getValues(), null); |
| this.relations = convertRelations(entity, null); |
| } |
| } |
| |
| /** |
| * Constructor. |
| * |
| * @param name Name of the Entity |
| * @param id ID of the Entity |
| * @param type Type of the Entity |
| * @param sourceType Source type of the Entity |
| * @param sourceName Source name of the Entity |
| * @param attributes Attributes of the Entity |
| */ |
| public MDMContextEntity(String name, String id, String type, String sourceType, String sourceName, |
| List<MDMContextAttribute> attributes) { |
| super(name, id, type, sourceType, sourceName, Collections.emptyList()); |
| this.sortIndex = 0; |
| if (attributes != null) { |
| this.attributes = new ArrayList<>(attributes); |
| } else { |
| this.attributes = new ArrayList<>(); |
| } |
| this.relations = new ArrayList<>(); |
| } |
| |
| public Integer getSortIndex() { |
| return sortIndex; |
| } |
| |
| public List<MDMContextAttribute> getAttributes() { |
| return Collections.unmodifiableList(this.attributes); |
| } |
| |
| public List<MDMContextRelation> getRelations() { |
| return Collections.unmodifiableList(this.relations); |
| } |
| |
| /** |
| * converts the MDM business object values to string values |
| * |
| * @param values values of a MDM business object |
| * @param tplAttributes optional list of template attributes |
| * @return list with converted attribute values |
| */ |
| private List<MDMContextAttribute> convertAttributeValues(Map<String, Value> values, |
| List<TemplateAttribute> tplAttributes) { |
| |
| List<MDMContextAttribute> listAttrs = new ArrayList<>(); |
| for (MDMAttribute attr : super.convertAttributeValues(values)) { |
| TemplateAttribute tplAttr = null; |
| |
| if (tplAttributes != null && !tplAttributes.isEmpty()) { |
| tplAttr = tplAttributes.stream().filter(tpl -> tpl.getName().equals(attr.getName())).findFirst() |
| .orElse(null); |
| } |
| |
| listAttrs.add(toContextAttribute(attr, tplAttr)); |
| } |
| |
| // clear unneeded elements |
| listAttrs.removeIf(attr -> attr.getName().equals(BaseEntity.ATTR_NAME) |
| || attr.getName().equals(BaseEntity.ATTR_MIMETYPE) || attr.getName().equals(Sortable.ATTR_SORT_INDEX)); |
| |
| // pre-sort attributes by sort index |
| Collections.sort(listAttrs, Comparator.comparing(MDMContextAttribute::getSortIndex, |
| Comparator.nullsFirst(Comparator.naturalOrder()))); |
| |
| return listAttrs; |
| } |
| |
| private MDMContextAttribute toContextAttribute(MDMAttribute attr, TemplateAttribute tplAttr) { |
| Integer sortIndex = null; |
| Boolean optional = null; |
| Boolean readOnly = null; |
| String description = null; |
| if (tplAttr != null) { |
| sortIndex = tplAttr.getCatalogAttribute().getSortIndex(); |
| description = tplAttr.getCatalogAttribute().getDescription(); |
| optional = (Boolean) tplAttr.isOptional(); |
| readOnly = (Boolean) tplAttr.isValueReadOnly(); |
| } |
| return new MDMContextAttribute(attr, sortIndex, readOnly, optional, description); |
| } |
| |
| /** |
| * Converts all relations to MDMContextRelations. The potential ContextType of |
| * the children is assumed to be the parent's one. |
| * |
| * @param entity to get {@link MDMContextRelation}s for |
| * @return a list of {@link MDMContextRelation}s |
| */ |
| private List<MDMContextRelation> convertRelations(Entity entity, TemplateComponent tplCmp) { |
| |
| String parentId = tplCmp.getParentTemplateComponent().map(p -> p.getID()).orElse(null); |
| |
| return convertRelations(entity).stream().map(m -> new MDMContextRelation(m, parentId)) |
| .collect(Collectors.toList()); |
| } |
| } |