Project

General

Profile

1 29686 sandro.lab
package eu.dnetlib.functionality.index.model.util;
2
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.Collection;
6
import java.util.Date;
7
import java.util.HashSet;
8
import java.util.Iterator;
9
import java.util.LinkedHashMap;
10
import java.util.Map;
11
import java.util.Map.Entry;
12
import java.util.Properties;
13
import java.util.Set;
14
15
import eu.dnetlib.functionality.index.model.Any;
16
import eu.dnetlib.functionality.index.model.AnyMap;
17
import eu.dnetlib.functionality.index.model.AnySeq;
18
import eu.dnetlib.functionality.index.model.DataFactory;
19
import eu.dnetlib.functionality.index.model.Value;
20
import eu.dnetlib.functionality.index.model.impl.DefaultDataFactoryImpl;
21
22
/**
23
 * utility class for handling / conversion of Any objects.
24
 *
25
 * Hint: The Any-to-JSON conversion for Date(Time)s is not symmetric. If the Any object contains Date(Time) values, they will be serialized
26
 * to String values in simple timestamp format. But when parsing JSON to an Any object, Date(Time) strings will not be recognized anymore,
27
 * but just read as String values.
28
 */
29
public class AnyUtil {
30
31
	/** Immutable empty AnyMap instance. */
32
	public static final AnyMap EMPTY_MAP = DefaultDataFactoryImpl.IMMUTABLE_EMPTY_MAP;
33
34
	/**
35
	 * prevent instance creation.
36
	 */
37
	protected AnyUtil() {
38
		// prevent instance creation
39
	}
40
41
	/**
42
	 * Converts an Any object into a native java object.
43
	 *
44
	 * @param any
45
	 *            the Any object
46
	 * @return a Pojo
47
	 */
48
	public static Object anyToNative(final Any any) {
49
		if (any.isMap()) {
50
			final LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
51
			for (final Iterator<String> kIt = ((AnyMap) any).keySet().iterator(); kIt.hasNext();) {
52
				final String key = kIt.next();
53
				map.put(key, anyToNative(((AnyMap) any).get(key)));
54
			}
55
			return map;
56
		} else if (any.isSeq()) {
57
			final ArrayList<Object> list = new ArrayList<Object>();
58
			for (final Iterator<Any> aIt = any.iterator(); aIt.hasNext();) {
59
				final Any a = aIt.next();
60
				list.add(anyToNative(a));
61
			}
62
			return list;
63
		} else if (any.isString()) return ((Value) any).asString();
64
		else if (any.isLong()) return ((Value) any).asLong();
65
		else if (any.isDouble()) return ((Value) any).asDouble();
66
		else if (any.isBoolean()) return ((Value) any).asBoolean();
67
		else if (any.isDate()) return ((Value) any).asDate();
68
		else if (any.isDateTime()) return ((Value) any).asDateTime();
69
		else return ((Value) any).getObject();
70
	}
71
72
	/**
73
	 * Converts an object to an Any (recursively). The leaf object(s) must be convertable by {@link DataFactory#autoConvertValue(Object)}.
74
	 *
75
	 * @param object
76
	 *            The object to be converted. Supported (and tested in this order) are
77
	 *            <ul>
78
	 *            <li>{@code Map<String, Object>}
79
	 *            <li>{@code Collections<Object>}
80
	 *            <li>{@code Object[]}
81
	 *            <li>Any other object that can be {@link DataFactory#autoConvertValue(Object)}</li>
82
	 *            </ul>
83
	 *
84
	 * @return The converted Any
85
	 */
86
	@SuppressWarnings("unchecked")
87
	public static Any objectToAny(final Object object) {
88
		Any value = null;
89
		if (object instanceof Any) return (Any) object;
90
		else if (object instanceof Map<?, ?>) {
91
			value = mapToAny((Map<String, Object>) object);
92
		} else if (object instanceof Collection<?>) {
93
			value = collectionToAny((Collection<Object>) object);
94
		} else if (object.getClass().isArray()) {
95
			Object[] array = (Object[]) object;
96
			value = collectionToAny(Arrays.asList(array));
97
		} else {
98
			value = scalarObjectToAny(object);
99
		}
100
		return value;
101
	}
102
103
	/**
104
	 * Converts a collection to an AnySeq object.
105
	 *
106
	 * @param objects
107
	 *            The list of objects to convert.
108
	 * @return An AnySeq containing the objects as Any objects.
109
	 */
110
	private static AnySeq collectionToAny(final Collection<Object> objects) {
111
		AnySeq anySeq = null;
112
		if (objects != null) {
113
			anySeq = DataFactory.DEFAULT.createAnySeq();
114
			for (final Object obj : objects) {
115
				anySeq.add(objectToAny(obj));
116
			}
117
		}
118
		return anySeq;
119
	}
120
121
	/**
122
	 * Converts a scalar object to a Value object.
123
	 *
124
	 * @param obj
125
	 *            The object to convert.
126
	 * @return A Value representing the object.
127
	 */
128
	private static Any scalarObjectToAny(final Object obj) {
129
		return DataFactory.DEFAULT.autoConvertValue(obj);
130
	}
131
132
	/**
133
	 * Converts a map to an AnyMap object.
134
	 *
135
	 * @param map
136
	 *            The map (String to Object) to convert.
137
	 * @return An AnyMap representing the map with all Objects converted to Any.
138
	 */
139
	private static AnyMap mapToAny(final Map<String, Object> map) {
140
		AnyMap anyMap = null;
141
		if (map != null) {
142
			anyMap = DataFactory.DEFAULT.createAnyMap();
143
			for (final Entry<String, Object> entry : map.entrySet()) {
144
				anyMap.put(entry.getKey(), objectToAny(entry.getValue()));
145
			}
146
		}
147
		return anyMap;
148
	}
149
150
	/**
151
	 * get value for given path(list of keys) from AnyMap object. This methods throws no exception, if the path not exists an empty Any is
152
	 * the result.
153
	 *
154
	 * @param any
155
	 *            the Any object.
156
	 * @param path
157
	 *            path to the entry.
158
	 * @return value associated to the path.
159
	 */
160
	public static Any saveGet(final Any any, final String[] path) {
161
		if (path.length == 0) return DataFactory.DEFAULT.createAnyMap();
162
		try {
163
			Any current = any;
164
			for (final String key : path) {
165
				if (current.isMap()) {
166
					current = ((AnyMap) current).get(key);
167
				} else {
168
					current = null;
169
				}
170
			}
171
			if (current == null) return DataFactory.DEFAULT.createStringValue("undef");
172
			else return current;
173
		} catch (final Exception e) {
174
			return DataFactory.DEFAULT.createStringValue("undef");
175
		}
176
	}
177
178
	/**
179
	 * convert an exception to an any object.
180
	 *
181
	 * @param e
182
	 *            exception to convert
183
	 * @return any representation of exception
184
	 */
185
	public static AnyMap exceptionToAny(final Throwable e) {
186
		return exceptionToAny(e, new HashSet<String>());
187
	}
188
189
	/**
190
	 * convert an exception to an any object. stop in stacktrace printing when hitting known lines again.
191
	 *
192
	 * @param e
193
	 *            exception to convert
194
	 * @param visitedLines
195
	 *            lines that have been added to stacktraces before.
196
	 * @return any representation of exception
197
	 */
198
	private static AnyMap exceptionToAny(final Throwable e, final Collection<String> visitedLines) {
199
		final AnyMap any = DataFactory.DEFAULT.createAnyMap();
200
		any.put("type", e.getClass().getName());
201
		if (e.getMessage() != null) {
202
			any.put("message", e.getMessage());
203
		}
204
		final AnySeq st = DataFactory.DEFAULT.createAnySeq();
205
		for (final StackTraceElement stElement : e.getStackTrace()) {
206
			final String line = stElement.toString();
207
			st.add(line);
208
			if (!visitedLines.add(line)) {
209
				st.add("...");
210
				break;
211
			}
212
		}
213
		any.put("at", st);
214
		if ((e.getCause() != null) && (e.getCause() != e)) {
215
			any.put("causedBy", exceptionToAny(e.getCause(), visitedLines));
216
		}
217
		return any;
218
	}
219
220
	/**
221
	 * null save version.
222
	 */
223
	public static Double asDouble(final Any any) {
224
		return any == null ? null : any.asValue().asDouble();
225
	};
226
227
	/**
228
	 * null save version.
229
	 */
230
	public static Boolean asBoolean(final Any any) {
231
		return any == null ? null : any.asValue().asBoolean();
232
	};
233
234
	/**
235
	 * null save version.
236
	 */
237
	public static Date asDateTime(final Any any) {
238
		return any == null ? null : any.asValue().asDateTime();
239
	};
240
241
	/**
242
	 * null save version.
243
	 */
244
	public static Date asDate(final Any any) {
245
		return any == null ? null : any.asValue().asDate();
246
	};
247
248
	/**
249
	 * null save version.
250
	 */
251
	public static Long asLong(final Any any) {
252
		return any == null ? null : any.asValue().asLong();
253
	};
254
255
	/**
256
	 * null save version.
257
	 */
258
	public static String asString(final Any any) {
259
		return any == null ? null : any.asValue().asString();
260
	};
261
262
	/**
263
	 * null save version.
264
	 */
265
	public static AnyMap asMap(final Any any) {
266
		return any == null ? null : any.asMap();
267
	};
268
269
	/**
270
	 * null save version.
271
	 */
272
	public static AnySeq asSeq(final Any any) {
273
		return any == null ? null : any.asSeq();
274
	};
275
276
	/** convert AnyMap to java.util.Properties. */
277
	public static Properties anyToProperties(final AnyMap anyMap) {
278
		Properties props = new Properties();
279
		final Set<String> keySet = anyMap.keySet();
280
		for (final String key : keySet) {
281
			props.put(key, anyMap.get(key).toString());
282
		}
283
		return props;
284
	}
285
286
	/** convert java.util.Properties to AnyMap. */
287
	public static AnyMap propertiesToAny(final Properties props) {
288
		final AnyMap any = DataFactory.DEFAULT.createAnyMap();
289
		final Set<String> propNames = props.stringPropertyNames();
290
		for (final String prop : propNames) {
291
			any.put(prop, props.getProperty(prop));
292
		}
293
		return any;
294
	}
295
296
	/**
297
	 * returns the 1st map in the give SEQ that contains a value with the given name value, or null if not found.
298
	 *
299
	 * This is often useful for configs that are contained in a seq such as search filters.
300
	 *
301
	 * @since 1.1
302
	 */
303
	public static AnyMap findMapInSeq(final AnySeq seq, final String keyName, final String keyValue) {
304
		for (Any any : seq) {
305
			final String stringValue = any.asMap().getStringValue(keyName);
306
			if (keyValue.equals(stringValue)) return any.asMap();
307
		}
308
		return null;
309
	}
310
}