final Storage storage = Storage.getSessionStorageIfSupported();
if (storage != null) {
final String v = storage.getItem(APPLICATION.VIEW_ID);
if (v != null && !v.isEmpty()) viewID = Long.parseLong(v);
}
final PTInstruction requestData = new PTInstruction();
final JSONArray cookies = new JSONArray();
// load all cookies at startup
final Collection<String> cookieNames = Cookies.getCookieNames();
if (cookieNames != null) {
int i = 0;
for (final String cookie : cookieNames) {
final JSONObject jsoObject = new JSONObject();
jsoObject.put(PROPERTY.KEY, new JSONString(cookie));
jsoObject.put(PROPERTY.VALUE, new JSONString(Cookies.getCookie(cookie)));
cookies.set(i++, jsoObject);
}
}
requestData.put(APPLICATION.KEY, APPLICATION.KEY_.START);
requestData.put(APPLICATION.SEQ_NUM, 0);
requestData.put(HISTORY.TOKEN, History.getToken());
requestData.put(PROPERTY.COOKIES, cookies);
if (viewID != null) requestData.put(APPLICATION.VIEW_ID, viewID);
final RequestCallback requestCallback = new RequestCallback() {
@Override
public void onDataReceived(final JSONObject data) {
try {
if (data.containsKey(APPLICATION.VIEW_ID)) {
applicationViewID = (long) data.get(APPLICATION.VIEW_ID).isNumber().doubleValue();
if (storage != null) storage.setItem(APPLICATION.VIEW_ID, Long.toString(applicationViewID));
uiBuilder.init(applicationViewID, requestBuilder);
}
uiBuilder.update(data);
} catch (final RuntimeException exception) {
log.log(Level.SEVERE, "Failed to process data with error #" + exception.getMessage() + ", data: " + data, exception);
Window.alert("Failed to process data with error #" + exception.getMessage() + ", data: " + data);
}
}
@Override
public void onError(final Throwable exception) {
uiBuilder.onCommunicationError(exception);
}
};
requestBuilder = newRequestBuilder(requestCallback);
requestBuilder.send(requestData.toString());
} catch (final Exception e) {
log.log(Level.SEVERE, "Loading application has failed #" + e.getMessage(), e);
Window.alert("Loading application has failed #" + e);
}