blob: eb8e0a5c3bb2586edb3a52ec17bacdf23375f313 [file] [log] [blame]
/**
*
* Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*/
package org.eclipse.osbp.utils.entitymock;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import org.eclipse.osbp.dsl.common.datatypes.IDto;
import org.eclipse.osbp.dsl.dto.lib.impl.DtoServiceAccess;
import org.eclipse.osbp.dsl.dto.lib.services.IDTOService;
import org.eclipse.osbp.dsl.dto.lib.services.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class ABaseMockEntity<E> extends ABaseMockObject {
private final static Logger log = LoggerFactory.getLogger(ABaseMockEntity.class);
protected static boolean asBoolean(Object value) {
return (value instanceof Boolean) ? (Boolean)value : (value == null) ? false : Boolean.parseBoolean(value.toString());
}
protected static Date asDate(Object value) {
return (value instanceof Date) ? (Date)value : null;
}
protected static double asDouble(Object value) {
if (value instanceof Double) {
return (Double)value;
}
else if (value instanceof Float) {
return (Float)value;
}
else if (value == null) {
return 0.0;
}
return Double.parseDouble(value.toString());
}
protected static float asFloat(Object value) {
if (value instanceof Float) {
return (Float)value;
}
else if (value == null) {
return (float)0.0;
}
return Float.parseFloat(value.toString());
}
protected static long asLong(Object value) {
if (value instanceof Long) {
return (Long)value;
}
else if (value instanceof Integer) {
return (Integer)value;
}
else if (value instanceof Short) {
return (Short)value;
}
else if (value == null) {
return 0;
}
return Long.parseLong(value.toString());
}
protected static int asInt(Object value) {
if (value instanceof Integer) {
return (Integer)value;
}
else if (value instanceof Short) {
return (Short)value;
}
else if (value == null) {
return 0;
}
return Integer.parseInt(value.toString());
}
protected static short asShort(Object value) {
if (value instanceof Short) {
return (Short)value;
}
else if (value == null) {
return 0;
}
return Short.parseShort(value.toString());
}
// protected static Time asTime(Object value) {
// return (value instanceof Date) ? (Date)value : null;
// }
protected static String asString(Object value) {
return (value == null) ? null : value.toString();
}
protected final AEntityMockDataGenerator mockDataGenerator;
protected final String persistenceUnit;
protected E entity;
public ABaseMockEntity(AEntityMockDataGenerator mockDataGenerator, String persistenceUnit) {
this.mockDataGenerator = mockDataGenerator;
this.persistenceUnit = persistenceUnit;
}
public final String getPersistenceUnit() {
return persistenceUnit;
}
public abstract E generateEntity(Object iteratorItem);
protected final void generateAttribute(String key, Class<IDto> dtoClass, double optionalFor) {
String mockEntityName = "Existing"+dtoClass.getSimpleName();
if (mockDataGenerator.getMockObjects(mockEntityName) == null) {
List<Object> existingDtos = new ArrayList<>();
try {
IDTOService dtoService = (IDTOService) DtoServiceAccess.getService(dtoClass);
if (dtoService == null) {
log.error("NO DTO SERVICE FOUND for "
+ dtoClass.getCanonicalName()
+ " in persistence " + persistenceUnit);
}
else {
Query queryAll = new Query();
Collection<?> dtoObjects = dtoService.find(queryAll);
for (Object dtoObject : dtoObjects) {
existingDtos.add(dtoObject);
}
}
}
catch (Exception e) {
log.error("could not retrieve existing entities via dto service for "+dtoClass.getCanonicalName(), e);
}
mockDataGenerator.addMockObjects(mockEntityName, existingDtos);
}
generateAttribute(key, mockEntityName, optionalFor);
}
protected final void generateAttribute(String key, String mockEntityName, double optionalFor) {
Object[] mockObjects = mockDataGenerator.getMockObjects(mockEntityName).toArray();
// --- no objects are available ---
if (mockObjects.length < 1) {
mockData.put(key, null);
}
// --- if at least 100% have to be optional, EVERYTHING is optional ---
else if (optionalFor >= 1.0) {
mockData.put(key, null);
}
// --- if up to 0% have to be optional, EVERYTHING is mandatory ---
else if (optionalFor <= 0.0) {
mockData.put(key, mockObjects[fillerProvider.unsignedinteger(mockObjects.length-1)]);
}
// --- if a random is lower than the given optionalFor, this item is optional ---
else if (fillerProvider.unsigneddouble(0) < optionalFor*100.0) {
mockData.put(key, null);
}
// --- otherwise
else {
mockData.put(key, mockObjects[fillerProvider.unsignedinteger(mockObjects.length-1)]);
}
}
}