/** * Returns an EntityData instance which is based on a list of properties * * @param propertiesMap List of properties used to construct a new instance of EntityData * @param keys List of key names * @param entityName Name of the newly created entity * @return An instance of EntityData */ public static EntityData createFromMap(Map<String, Object> propertiesMap,List<String> keys,String entityName){ @SuppressWarnings("unchecked") Map<String, Object> cloned = (Map<String, Object>) cloneMap(propertiesMap); DefaultEntityDataBuilder entityDataBuilder = new DefaultEntityDataBuilder(); cloned.forEach((key,value)-> entityDataBuilder.addElement(key,value)); if(keys!=null){ for( String key : keys){ entityDataBuilder.addKeyElement(key,propertiesMap.get(key)); } } return entityDataBuilder.buildEntityData(entityName); }
/** * Returns an EntityDataBuilder instance which can be used to construct an entityData from scratch. * @return An instance of EntityDataBuilder. */ public static EntityDataBuilder getBuilder() { return new DefaultEntityDataBuilder(); }
private List<String> getFlattenedElementsFromHashMap(String name, HashMap<String, Object> value) { List<String> flattened = new ArrayList<>(); if(value == null || value.isEmpty()){ return flattened; } for(Entry<String, Object> entry : value.entrySet()) { String path = name + "." + entry.getKey(); if(entry.getValue() instanceof HashMap) { flattened.addAll(getFlattenedElementsFromHashMap(path, (HashMap<String, Object>) entry.getValue())); } else flattened.add(path); } return flattened; }
public DefaultEntityDataBuilder(EntityData ed) { EntityMetadata em = ((HasMetadata)ed).getEntityMetadata(); elements.addAll(em.getElementNames()); Map<String, Object> localAssociationProperties = ed.getAssociations(); if(localAssociationProperties != null && !localAssociationProperties.isEmpty()){ associationProperties.putAll(localAssociationProperties); } for(String element : elements) { if(ed.contains(element)) { properties.put(element, ed.getElementValue(element)); } } keys.addAll(em.getKeyNames()); entityName = em.getName(); namespace = em.getNamespace(); elementTypes = copyElementTypes(em); }
/** * Returns an EntityDataBuilder instance which is partially initialized based on the passed entityData. * This method should be used if you need to construct an entityData starting from an existing entityData. * * For example:- You have an entityData for an entity Customer. But you would like to add an extra element to this entityData for the name of the Customer's company. * The first step you have to do is to get a builder by passing the existing entitydata. * EntityDataBuilder builder = EntityData.getBuilder(customerEntityData); * * Once you have the builder you can add the extra element, by calling the addElement method. * * * @param entityData The entityData from which a new EntityData is to be constructed, for example by adding an extra element. * @return An instance of EntityDataBuilder which is initialized based on the passed EntityData. */ public static EntityDataBuilder getBuilder(EntityData entityData) { return new DefaultEntityDataBuilder(entityData); }
@Override public EntityData buildEntityData(String name) { List<String> flattenedElements = new ArrayList<>(); for(String element : elementTypes.keySet()) { ElementType type = elementTypes.get(element); if(type.equals(ElementType.SIMPLE_TYPE)) flattenedElements.add(element); else { HashMap<String, Object> value = (HashMap<String, Object>) properties.get(element); flattenedElements.addAll(getFlattenedElementsFromHashMap(element, value)); } } DefaultEntityMetadata em = new DefaultEntityMetadata() .setEntityName(name) .setElements(elements) .setKeys(keys) .setNamespace(namespace) .setElementTypes(elementTypes) .setFlattenedElements(flattenedElements); if(associationProperties.size() > 0){ return new DefaultEntityData(properties, associationProperties, em); }else{ return new DefaultEntityData(properties, em); } }
private static EntityData createFromDeepMap(String serviceName, Map<String, Object> propertiesMap, Map<String, List<String>> keys, String entityName, String associationName) { Map<String, Object> cloned = (Map<String, Object>) cloneMap(propertiesMap); DefaultEntityDataBuilder entityDataBuilder = new DefaultEntityDataBuilder(); cloned.forEach((key,value)-> { if(value instanceof Map) { String associatedEntity = CSNUtil.getEntityName(serviceName, entityName, key); if(associatedEntity != null && !"".equals(associatedEntity.trim())) { entityDataBuilder.addAssociationElement(key, createFromDeepMap(serviceName, (Map<String, Object>)value, keys, associatedEntity, key)); }else { throw new IllegalStateException("Association with the name "+key+" is not found."); edList.add(createFromDeepMap(serviceName, (Map<String, Object>)valueMap, keys, associatedEntity, key)); entityDataBuilder.addAssociationElement(key, edList); }else{ entityDataBuilder.addElement(key,value); entityDataBuilder.addKeyElement(key, propertiesMap.get(key)); return entityDataBuilder.buildEntityData(associationName);