/**
 * Converter for otela Enterprise Systems
 *
 * The MIT License (MIT)
 *
 * @copyright Copyright(C) 2016 otela Enterprise Systems
 * @author    Markus Bröker<markus.broeker@otela.net>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package de.otela.soap.client;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Converter {

	/**
	 * Erzeugt aus einem Remote-Objekt eine Map, die einfach zu durchlaufen ist
	 *
	 * @param object
	 * @return
	 */
	public static Map<String, String> remoteObject2Map(Object object) {

		Map<String, String> map = new HashMap<>();
		for (Field field : object.getClass().getDeclaredFields()) {
			field.setAccessible(true);
			String name = field.getName();

			try {
				Object value = field.get(object);

				if (value instanceof String) {
					map.put(name, String.valueOf(value));
				}

			} catch (Exception e) {
				System.err.println("Konvertierungsfehler");
			}
		}

		return map;
	}

	/**
	 * Simple Dump-Methode für die Antwort des Webservice
	 *
	 * @param object
	 */
	public static void printObject(Object object) {
		Map<String, String> map = remoteObject2Map(object);

		for (Entry<String, String> entry : map.entrySet()) {
			if (!entry.getValue().equals("")) {
				System.out.printf("%40s => %s\n", entry.getKey(), entry.getValue());
			}
		}
	}

	/**
	 * Mappt die otela Webservice Felder auf benutzerdefinierte Felder
	 *
	 * @param object
	 * @param otelaKeys
	 * @param myKeys
	 *
	 * @return List<String[]>
	 */
	public static List<String[]> transFormKeysToListArray(Object object, String[] otelaKeys, String[] myKeys) {

		if (otelaKeys.length != myKeys.length) {
			throw new IllegalArgumentException("Key-Sizes differ");
		}

		Map<String, String> original = remoteObject2Map(object);

		List<String[]> list = new ArrayList<>();
		for (int i = 0; i < otelaKeys.length; i++) {
			if (original.containsKey(otelaKeys[i])) {
				String value = original.get(otelaKeys[i]);

				list.add(new String[] { myKeys[i], value });
			}
		}

		return list;
	}

}
