All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.cloud.sdk.service.prov.v4.util.EntityDataToOData Maven / Gradle / Ivy

There is a newer version: 1.40.11
Show newest version
/*******************************************************************************
 * (c) 201X SAP SE or an SAP affiliate company. All rights reserved.
 ******************************************************************************/
package com.sap.cloud.sdk.service.prov.v4.util;

import java.util.List;
import java.util.Map;

import org.apache.olingo.commons.api.data.Entity;
import org.apache.olingo.commons.api.data.EntityCollection;
import org.apache.olingo.commons.api.edm.EdmEntityType;

import com.sap.cloud.sdk.service.prov.api.EntityData;
import com.sap.cloud.sdk.service.prov.v4.rt.core.EntityDataUtil;

/**
 * Utility class to convert POJO's and EntityData/Lists to Olingo Entities
 *
 */
public class EntityDataToOData {
	
	
	private EntityDataToOData() {
		throw new IllegalStateException("EntityDataToOData class");
	}
	
	/**
	 * Converts object(entityData or Pojo) to entity
	 */
	public static Entity convertToEntity(Object data, EdmEntityType entityType) {
		if(data instanceof EntityData)
			return convertEntityDataToEntity((EntityData) data);
		else
			return convertPojoDataToEntity(data, entityType);
	}
	
	/**
	 * Converts list(of entityData or of Pojo) to entityCollection
	 * @param data
	 * @return
	 */
	public static EntityCollection convertToEntityCollection(List dataList, EdmEntityType entityType) {
		
		EntityCollection ec = new EntityCollection();
		if(dataList.isEmpty() || dataList.get(0) instanceof EntityData)
			return convertEntityDataListToEntityCollection(dataList, ec);
		else
		{
			List entities = ec.getEntities();
			for(Object data : dataList) {
				entities.add(convertPojoDataToEntity(data, entityType));
			}
		}
		
		return ec;
	}
	
	/**
	 * Converts EntityData to Olingo Entity
	 * @return Entity
	 * @param data EntityData
	 */
	public static Entity convertEntityDataToEntity(EntityData data) {
		Entity retEntity = null;
		if (data != null) {
			retEntity = EntityDataUtil.getEntityFromEntityData(data);
		}
		return retEntity;
	}
	
	/**
	 * Converts EntityDataList to Olingo EntityCollection
	 * @return EntityCollection
	 * @param dataList List of EntityData
	 * @param entityCollection EntityCollection
	 */
	 public static EntityCollection convertEntityDataListToEntityCollection(List dataList, EntityCollection entityCollection) {
		 EntityCollection localEntityCollection = entityCollection;
		 if (entityCollection == null) {
			 localEntityCollection = new EntityCollection();
		 }
		 for(EntityData data : dataList){
			 Entity entity = convertEntityDataToEntity(data);
			 if (entity != null) {
				 localEntityCollection.getEntities().add(entity);
			 }
		 }
			return localEntityCollection;
		}

	 /**
		 * Converts POJO to Olingo Entity
		 * @return Entity
		 * @param pojoData Object
		 * @param EdmEntityType Entity
		 */
	public static Entity convertPojoDataToEntity(Object pojoData, EdmEntityType entity) {
		Map map = ODataServiceUtility.getHashMapFromSinglePojo(pojoData);
		return MapToOData.convertMapToEntity(map,entity);		
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy