| /** |
| * |
| * 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.Locale; |
| import java.util.Map; |
| import java.util.Set; |
| import java.util.TreeMap; |
| import java.util.regex.Matcher; |
| import java.util.regex.Pattern; |
| |
| import org.apache.commons.lang3.StringUtils; |
| import org.eclipse.osbp.dsl.common.datatypes.IDto; |
| |
| import org.eclipse.osbp.utils.fillertext.FillerTextProvider; |
| |
| public abstract class ABaseMockObject { |
| |
| protected final FillerTextProvider fillerProvider; |
| protected Map<String,Object> mockData; |
| |
| public ABaseMockObject() { |
| FillerTextProvider fillerProvider; |
| try { |
| fillerProvider = FillerTextProvider.get(Locale.getDefault()); |
| } |
| catch (Exception e) { |
| fillerProvider = null; |
| e.printStackTrace(); |
| } |
| this.fillerProvider = fillerProvider; |
| } |
| |
| public final Object getAttribute(String... keys) { |
| return mockData.get(StringUtils.join(keys, ".")); |
| } |
| |
| public final Object getAttribute(String key, AEntityMockDataGenerator mockDataGenerator, String peristenceUnit) { |
| Object mockValue = mockData.get(key); |
| if ((mockValue == null) && (mockDataGenerator instanceof AEntityMockDataGenerator)) { |
| String[] keys = key.split("\\.", 2); |
| if (keys.length == 2) { |
| mockValue = mockData.get(keys[0]); |
| if (mockValue instanceof IDto) { |
| mockValue = mockDataGenerator.getDtoAttribute((IDto)mockValue, keys[1]); |
| } |
| else { |
| mockValue = null; |
| } |
| } |
| } |
| return mockValue; |
| } |
| |
| public final Set<String> getAttributeNames() { |
| return mockData.keySet(); |
| } |
| |
| public final ABaseMockObject generateData() { |
| fillerProvider.reset(); |
| try { |
| generateDataRow(); |
| } |
| catch (Exception e) { |
| e.printStackTrace(); |
| } |
| replaceDigits(); |
| replaceVariables(); |
| return this; |
| } |
| |
| protected final void replaceDigits() { |
| for (String key : mockData.keySet()) { |
| Object value = mockData.get(key); |
| if (value instanceof String) { |
| if (((String)value).contains("#")) { |
| try { |
| mockData.put(key, fillerProvider.numerify((String)value)); |
| } |
| catch (Exception e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| } |
| } |
| |
| protected final void replaceVariables() { |
| Pattern possibleVariable = Pattern.compile("(\\[.*\\])"); |
| boolean again = false; |
| do { |
| again = false; |
| for (String key : mockData.keySet()) { |
| Object value = mockData.get(key); |
| if (value instanceof String) { |
| boolean replaced = false; |
| String text = (String) value; |
| try { |
| Matcher m = possibleVariable.matcher(text); |
| while (m.find()) { |
| String replace = m.group(); |
| String with = replace.substring(1, replace.length()-2); |
| if (mockData.containsKey(with)) { |
| with = mockData.get(with).toString(); |
| text = text.replaceAll(replace, with); |
| replaced = true; |
| } |
| } |
| } |
| catch (Exception e) { |
| e.printStackTrace(); |
| } |
| if (replaced) { |
| mockData.put(key, text); |
| again = true; |
| } |
| } |
| } |
| } while (again); |
| } |
| |
| protected abstract void generateDataRow(); |
| |
| protected final void reset() { |
| mockData = new TreeMap<String,Object>(); |
| } |
| |
| protected final void setFixAttribute(String key, Object value) { |
| mockData.put(key, value); |
| } |
| |
| protected final void generateAttribute(String key, String sourceKey) { |
| mockData.put(key, mockData.get(sourceKey)); |
| } |
| |
| protected final void generateAttribute(String key, Object[] values) { |
| mockData.put(key, values[fillerProvider.unsignedinteger(values.length-1)]); |
| } |
| |
| protected final void generateAttribute(String key, String whenKey, Map valuesMap) { |
| Object[] values = (Object[]) valuesMap.get(mockData.get(whenKey)); |
| if ((values == null) || values.length == 0) { |
| mockData.put(key, null); |
| } |
| else { |
| mockData.put(key, values[fillerProvider.unsignedinteger(values.length-1)]); |
| } |
| } |
| |
| protected final void generateAttribute(String key, double[] values) { |
| mockData.put(key, values[fillerProvider.unsignedinteger(values.length-1)]); |
| } |
| |
| protected final void generateSignedAttribute(String key, int decimals, double beginRange, double endRange, double rounded) { |
| mockData.put(key, fillerProvider.signeddouble(decimals, beginRange, endRange, rounded)); |
| } |
| |
| protected final void generateUnsignedAttribute(String key, int decimals, double beginRange, double endRange, double rounded) { |
| mockData.put(key, fillerProvider.signeddouble(decimals, beginRange, endRange, rounded)); |
| } |
| |
| protected final void generateAttribute(String key, int[] values) { |
| mockData.put(key, values[fillerProvider.unsignedinteger(values.length-1)]); |
| } |
| |
| protected final void generateSignedAttribute(String key, int beginRange, int endRange, double rounded) { |
| mockData.put(key, fillerProvider.signedinteger(beginRange, endRange, rounded)); |
| } |
| |
| protected final void generateUnsignedAttribute(String key, int beginRange, int endRange, double rounded) { |
| mockData.put(key, fillerProvider.signedinteger(beginRange, endRange, rounded)); |
| } |
| |
| protected final void generateAttribute(String key, ABaseMockObject subObject) { |
| subObject.reset(); |
| subObject.generateData(); |
| for (String subkey : subObject.mockData.keySet()) { |
| mockData.put(key+"."+subkey, subObject.mockData.get(subkey)); |
| } |
| } |
| |
| protected final void generateBooleanAttribute(String key) { |
| mockData.put(key, fillerProvider.trueOrFalse()); |
| } |
| |
| protected final void generateDateAttribute(String key, int pastYears, int futureYears) { |
| mockData.put(key, fillerProvider.date(pastYears, futureYears)); |
| } |
| |
| protected final void generateParagraphsAttribute(String key, int count) { |
| mockData.put(key, fillerProvider.paragraphs(count)); |
| } |
| |
| protected final void generateSentencesAttribute(String key, int count) { |
| mockData.put(key, fillerProvider.sentences(count)); |
| } |
| |
| protected final void generateWordsAttribute(String key, int count) { |
| mockData.put(key, fillerProvider.words(count)); |
| } |
| } |