mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-10-15 16:51:07 +00:00
bug4371 Added abstraction for data import response overlay types
This commit is contained in:
1 parent
38a2953978
commit
27bf70518c
3 files changed
+84
-46
No files matched your search
+75
@@ -0,0 +1,75 @@
|
||||
package com.sap.sailing.gwt.ui.adminconsole.resulthandling;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.google.gwt.core.client.JavaScriptObject;
|
||||
import com.google.gwt.core.client.JsArray;
|
||||
import com.google.gwt.json.client.JSONParser;
|
||||
|
||||
/**
|
||||
* Abstract super-class for data import response overlay type providing convenience methods.
|
||||
*/
|
||||
class AbstractDataImportResponse extends JavaScriptObject {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(AbstractDataImportResponse.class.getName());
|
||||
|
||||
/**
|
||||
* {@link JSONParser#parseStrict(String) Strictly parses} the provided {@link String} representation into JSON and
|
||||
* {@link JavaScriptObject#cast() casts} it to the provided {@link Class type class}.
|
||||
*
|
||||
* @param json
|
||||
* {@link String} representation to parse into JSON
|
||||
* @param clazz
|
||||
* {@link Class type class} to cast parse result to
|
||||
* @return the parsed and casted object or <code>null</code> if any {@link Exception exception} occurs
|
||||
*/
|
||||
protected static final <T extends AbstractDataImportResponse> T parse(String json, Class<T> clazz) {
|
||||
try {
|
||||
return JSONParser.parseStrict(json).isObject().getJavaScriptObject().cast();
|
||||
} catch (Exception e) {
|
||||
logger.severe(() -> "Failed to parse import " + clazz.getSimpleName());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected AbstractDataImportResponse() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps an array containing element of any type into a {@link List list} of the same type by keeping their order.
|
||||
*
|
||||
* @param array
|
||||
* the array which should be mapped
|
||||
* @return the {@link List list} containing the array's elements or an empty list if the array is <code>null</code>
|
||||
* or empty
|
||||
*/
|
||||
protected final <T> List<T> asList(T[] array) {
|
||||
return (array == null || array.length == 0) ? Collections.emptyList() : Arrays.asList(array);
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps a {@link JsArray JavaScript array} containing any type of {@link JavaScriptObject JavaScript objects} into a
|
||||
* {@link List list} of the same type by keeping their order.
|
||||
*
|
||||
* @param array
|
||||
* the {@link JsArray JavaScript array} which should be mapped
|
||||
* @return the {@link List list} containing the array's elements or an empty list if the array is <code>null</code>
|
||||
* or empty
|
||||
*/
|
||||
protected final <T extends JavaScriptObject> List<T> asList(JsArray<T> array) {
|
||||
if (array == null || array.length() == 0) {
|
||||
return Collections.emptyList();
|
||||
} else {
|
||||
final List<T> list = new ArrayList<T>(array.length());
|
||||
for (int index = 0; index < array.length(); index++) {
|
||||
list.add(array.get(index));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+2
-13
@@ -1,22 +1,11 @@
|
||||
package com.sap.sailing.gwt.ui.adminconsole.resulthandling;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.google.gwt.core.client.JavaScriptObject;
|
||||
import com.google.gwt.json.client.JSONParser;
|
||||
|
||||
public class ExpeditionDataImportResponse extends JavaScriptObject {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(ExpeditionDataImportResponse.class.getName());
|
||||
public class ExpeditionDataImportResponse extends AbstractDataImportResponse {
|
||||
|
||||
public static final ExpeditionDataImportResponse parse(String json) {
|
||||
try {
|
||||
return (ExpeditionDataImportResponse) JSONParser.parseStrict(json).isObject().getJavaScriptObject();
|
||||
} catch (Exception e) {
|
||||
logger.severe("Failed to parse ExpeditionDataImportResponse object!");
|
||||
return null;
|
||||
}
|
||||
return AbstractDataImportResponse.parse(json, ExpeditionDataImportResponse.class);
|
||||
}
|
||||
|
||||
protected ExpeditionDataImportResponse() {
|
||||
|
||||
+7
-33
@@ -1,62 +1,36 @@
|
||||
package com.sap.sailing.gwt.ui.adminconsole.resulthandling;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.google.gwt.core.client.JavaScriptObject;
|
||||
import com.google.gwt.core.client.JsArray;
|
||||
import com.google.gwt.json.client.JSONParser;
|
||||
|
||||
public class SensorDataImportResponse extends JavaScriptObject {
|
||||
private static final Logger logger = Logger.getLogger(SensorDataImportResponse.class.getName());
|
||||
public class SensorDataImportResponse extends AbstractDataImportResponse {
|
||||
|
||||
public static final SensorDataImportResponse parse(String json) {
|
||||
try {
|
||||
return (SensorDataImportResponse) JSONParser.parseStrict(json).isObject().getJavaScriptObject();
|
||||
} catch (Exception e) {
|
||||
logger.severe("failed to parse result");
|
||||
return null;
|
||||
}
|
||||
return AbstractDataImportResponse.parse(json, SensorDataImportResponse.class);
|
||||
}
|
||||
|
||||
protected SensorDataImportResponse() {
|
||||
}
|
||||
|
||||
public final List<String> getUploads() {
|
||||
final String[] uploads = uploads();
|
||||
if (uploads == null || uploads.length == 0) {
|
||||
return Collections.emptyList();
|
||||
} else {
|
||||
return Arrays.asList(uploads);
|
||||
}
|
||||
return asList(this.uploads());
|
||||
}
|
||||
|
||||
public final List<ErrorMessage> getErrors() {
|
||||
final JsArray<ErrorMessage> errors = errors();
|
||||
if (errors == null || errors.length() == 0) {
|
||||
return Collections.emptyList();
|
||||
} else {
|
||||
final ArrayList<ErrorMessage> errorsAsList = new ArrayList<>(errors.length());
|
||||
for (int i = 0; i < errors.length(); i++) {
|
||||
errorsAsList.add(errors.get(i));
|
||||
}
|
||||
return errorsAsList;
|
||||
}
|
||||
return asList(this.errors());
|
||||
}
|
||||
|
||||
private final native String[] uploads() /*-{
|
||||
return this.uploads;
|
||||
return this.uploads;
|
||||
}-*/;
|
||||
|
||||
private final native JsArray<ErrorMessage> errors() /*-{
|
||||
return this.errors;
|
||||
return this.errors;
|
||||
}-*/;
|
||||
|
||||
public final boolean didSucceedImportingAnyFile() {
|
||||
return getUploads() != null && getUploads().size() > 0;
|
||||
return getUploads() != null && !getUploads().isEmpty();
|
||||
}
|
||||
|
||||
public final boolean hasErrors() {
|
||||
|
||||
Reference in new issue
Block a user