added generic method to list entities (#5570)

This commit is contained in:
Parth Panchal 2022-06-23 12:04:35 +05:30 committed by GitHub
parent a913cf46db
commit 88ffca764f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,31 @@
package org.openmetadata.client.listUtils;
import io.swagger.client.model.Paging;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class ListUtils {
public static ArrayList<Object> listResults(Object client, String methodName, Class<?> className) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException, InstantiationException {
Map<String, Object> data = new HashMap<>();
Object classInstance = className.getDeclaredConstructor().newInstance();
ArrayList<Object> arrayList = new ArrayList<>();
Paging paging;
String after;
Method method = client.getClass().getMethod(methodName, Map.class);
Method getData = classInstance.getClass().getMethod("getData");
Method getPaging = classInstance.getClass().getMethod("getPaging");
do {
classInstance = method.invoke(client, data);
arrayList.addAll((Collection<?>) getData.invoke(classInstance));
paging = (Paging) getPaging.invoke(classInstance);
after = paging.getAfter();
data.put("after", after);
} while (after != null);
return arrayList;
}
}