Example usage for com.google.gwt.jsonp.client JsonpRequestBuilder setTimeout

List of usage examples for com.google.gwt.jsonp.client JsonpRequestBuilder setTimeout

Introduction

In this page you can find the example usage for com.google.gwt.jsonp.client JsonpRequestBuilder setTimeout.

Prototype

public void setTimeout(int timeout) 

Source Link

Usage

From source file:gov.wa.wsdot.apps.analytics.client.activities.twitter.view.tweet.TweetView.java

License:Open Source License

private void updateSentiment(final String sentimentType) {

    String url = JSON_URL + this.id + "/" + sentimentType;

    JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
    // Set timeout for 30 seconds (30000 milliseconds)
    jsonp.setTimeout(30000);
    jsonp.requestObject(url, new AsyncCallback<Mention>() {

        @Override//from w w  w. j  av a2 s  .  com
        public void onFailure(Throwable caught) {
            Window.alert("Failure: " + caught.getMessage());
        }

        @Override
        public void onSuccess(Mention result) {
            if (sentimentType.equalsIgnoreCase("positive")) {
                sentiment.setIconType(IconType.SENTIMENT_SATISFIED);
                sentiment.setIconColor("teal accent-4");
            } else if (sentimentType.equalsIgnoreCase("negative")) {
                sentiment.setIconType(IconType.SENTIMENT_DISSATISFIED);
                sentiment.setIconColor("deep-orange");
            } else {
                sentiment.setIconType(IconType.SENTIMENT_NEUTRAL);
                sentiment.setIconColor("black");
            }
        }
    });
}

From source file:gov.wa.wsdot.apps.analytics.client.activities.twitter.view.tweets.TweetsView.java

License:Open Source License

@EventHandler
void onSentimentDisplay(SentimentDisplayEvent event) {
    pageNum = 1;/*from   w w w  .j  a  va2s. c  om*/
    tweetsList.clear();
    moreTweetsBtn.setVisible(false);

    String url = Consts.HOST_URL + "/mentions/" + event.getSentiment() + "/" + currentAccount + startDate
            + endDate + "/";
    currentUrl = url;

    tweetsLoader.setVisible(true);

    JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
    // Set timeout for 30 seconds (30000 milliseconds)
    jsonp.setTimeout(30000);
    jsonp.requestObject(url + pageNum, new AsyncCallback<Mention>() {

        @Override
        public void onFailure(Throwable caught) {
            Window.alert("Failure: " + caught.getMessage());
            tweetsLoader.setVisible(false);
        }

        @Override
        public void onSuccess(Mention mention) {
            if (mention.getMentions() != null) {
                updateTweetsList(mention.getMentions());
                tweetsLoader.setVisible(false);
            }
        }
    });
}

From source file:gov.wa.wsdot.apps.analytics.client.activities.twitter.view.tweets.TweetsView.java

License:Open Source License

/**
 * Loads in the next set of 10 tweets using the values of currentAccount, currentDate and pageNum.
 * @param e//from  ww w  .j  a v a2s  . c om
 */
@UiHandler("moreTweetsBtn")
public void onMore(ClickEvent e) {

    int nextPage = pageNum + 1;

    tweetsLoader.setVisible(true);

    JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
    // Set timeout for 30 seconds (30000 milliseconds)
    jsonp.setTimeout(30000);
    jsonp.requestObject(currentUrl + nextPage, new AsyncCallback<Mention>() {

        @Override
        public void onFailure(Throwable caught) {
            Window.alert("Failure: " + caught.getMessage());
            tweetsLoader.setVisible(false);
        }

        @Override
        public void onSuccess(Mention mention) {
            if (mention.getMentions() != null) {
                pageNum++;
                updateTweetsList(mention.getMentions());
                tweetsLoader.setVisible(false);
            }
        }
    });
}

From source file:gov.wa.wsdot.apps.analytics.client.activities.twitter.view.tweets.TweetsView.java

License:Open Source License

/**
 * Updates the list of tweets through a jsonp request.
 *
 * @param startDate/*  w w w .j  av a  2  s . c o  m*/
 * @param endDate
 * @param account : The account, can be "All" for all accounts
 */
public static void updateTweets(String startDate, String endDate, String account) {

    tweetsList.clear();
    moreTweetsBtn.setVisible(false);

    DateTimeFormat fmt = DateTimeFormat.getFormat("/yyyy/M/d");
    String screenName = account;

    String url = Consts.HOST_URL + "/summary/mentions/" + screenName + startDate + endDate + "/";
    currentUrl = url;

    tweetsLoader.setVisible(true);

    JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
    // Set timeout for 30 seconds (30000 milliseconds)
    jsonp.setTimeout(30000);
    jsonp.requestObject(url + pageNum, new AsyncCallback<Mention>() {

        @Override
        public void onFailure(Throwable caught) {
            Window.alert("Failure: " + caught.getMessage());
            tweetsLoader.setVisible(false);
        }

        @Override
        public void onSuccess(Mention mention) {
            if (mention.getMentions() != null) {
                updateTweetsList(mention.getMentions());
                tweetsLoader.setVisible(false);
            }
        }
    });
}

From source file:gov.wa.wsdot.mobile.client.activities.amtrakcascades.schedules.details.AmtrakCascadesSchedulesDetailsActivity.java

License:Open Source License

/**
 * Get train schedules for those with a departing and arriving station.
 * //  w ww  .ja v a  2s. c  om
 * @param url URL of the Web services API
 * @param toLocation arriving station name
 * @param fromLocation departing station name
 */
private void getDepartingArrivingTrains(String url, final String fromLocation, final String toLocation) {
    JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
    jsonp.setTimeout(30000); // Set timeout for 30 seconds
    jsonp.requestObject(url, new AsyncCallback<AmtrakCascadesScheduleFeed>() {

        @Override
        public void onFailure(Throwable caught) {
            view.hideProgressIndicator();
        }

        @Override
        public void onSuccess(AmtrakCascadesScheduleFeed result) {
            if (result.getSchedule() != null) {
                serviceItems.clear();
                AmtrakCascadesScheduleItem scheduleItem;
                int numItems = result.getSchedule().length();

                int i = 0;
                int startingTripNumber = 0;
                int currentTripNumber = 0;
                while (i < numItems) { // Loop through all trains
                    Date scheduledDepartureTime = null;
                    locationItems = new ArrayList<Map<String, AmtrakCascadesScheduleItem>>();
                    stationItems = new HashMap<String, AmtrakCascadesScheduleItem>();

                    startingTripNumber = result.getSchedule().get(i).getTripNumber();
                    currentTripNumber = startingTripNumber;
                    List<String> trainNameList = new ArrayList<String>();
                    int tripCounter = 0;
                    while (currentTripNumber == startingTripNumber && i < numItems) { // Trains are grouped by two or more 
                        scheduleItem = new AmtrakCascadesScheduleItem();

                        if (result.getSchedule().get(i).getArrivalComment() != null) {
                            scheduleItem.setArrivalComment(result.getSchedule().get(i).getArrivalComment());
                        }

                        if (result.getSchedule().get(i).getArrivalScheduleType() != null) {
                            scheduleItem.setArrivalScheduleType(
                                    result.getSchedule().get(i).getArrivalScheduleType());
                        }

                        if (result.getSchedule().get(i).getArrivalTime() != null) {
                            scheduleItem.setArrivalTime(
                                    result.getSchedule().get(i).getArrivalTime().toString().substring(6, 19));
                        }

                        if (result.getSchedule().get(i).getDepartureComment() != null) {
                            scheduleItem.setDepartureComment(result.getSchedule().get(i).getDepartureComment());
                        }

                        if (result.getSchedule().get(i).getDepartureScheduleType() != null) {
                            scheduleItem.setDepartureScheduleType(
                                    result.getSchedule().get(i).getDepartureScheduleType());
                        }

                        if (result.getSchedule().get(i).getDepartureTime() != null) {
                            scheduleItem.setDepartureTime(
                                    result.getSchedule().get(i).getDepartureTime().toString().substring(6, 19));
                        }

                        if (result.getSchedule().get(i).getScheduledArrivalTime() != null) {
                            scheduleItem.setScheduledArrivalTime(result.getSchedule().get(i)
                                    .getScheduledArrivalTime().toString().substring(6, 19));
                        }

                        scheduleItem.setStationName(result.getSchedule().get(i).getStationName());

                        if (result.getSchedule().get(i).getTrainMessage() != "") {
                            scheduleItem.setTrainMessage(result.getSchedule().get(i).getTrainMessage());
                        }

                        if (result.getSchedule().get(i).getScheduledDepartureTime() != null) {
                            scheduleItem.setScheduledDepartureTime(result.getSchedule().get(i)
                                    .getScheduledDepartureTime().toString().substring(6, 19));

                            // We sort by scheduled departure time of the From station.
                            if (fromLocation.equalsIgnoreCase(scheduleItem.getStationName())) {
                                scheduledDepartureTime = new Date(
                                        Long.parseLong((scheduleItem.getScheduledDepartureTime())));
                            }
                        }

                        int trainNumber = result.getSchedule().get(i).getTrainNumber();
                        scheduleItem.setTrainNumber(trainNumber);
                        String serviceName = trainNumberMap.get(trainNumber);

                        if (serviceName == null) {
                            serviceName = "Bus Service";
                        }

                        scheduleItem.setSortOrder(result.getSchedule().get(i).getSortOrder());
                        String trainName = trainNumber + " " + serviceName;

                        // Add the train name for ever other record. When there is one orgin and destination point
                        // the train name will be the same. If the tripNumber is the same over more than two records
                        // then we have multiple origin and destination points and likely different train names.
                        // e.g. 515 Amtrak Cascades Train, 8911 Bus Service
                        if (tripCounter % 2 == 0) {
                            trainNameList.add(trainName);
                        }
                        scheduleItem.setTrainName(trainName);
                        scheduleItem.setTripNumber(result.getSchedule().get(i).getTripNumber());
                        scheduleItem.setUpdateTime(
                                result.getSchedule().get(i).getUpdateTime().toString().substring(6, 19));

                        stationItems.put(scheduleItem.getStationName(), scheduleItem);

                        i++;
                        if (i < numItems) {
                            currentTripNumber = result.getSchedule().get(i).getTripNumber();
                        }

                        tripCounter++;
                    }

                    if (trainNameList.size() > 1) {
                        StringBuilder sb = new StringBuilder();
                        for (String s : trainNameList) {
                            if (sb.length() > 0)
                                sb.append(", ");
                            sb.append(s);
                        }
                        stationItems.get(fromLocation).setTrainName(sb.toString());
                    }

                    locationItems.add(stationItems);
                    serviceItems.add(new AmtrakCascadesServiceItem(scheduledDepartureTime, locationItems));

                }
            }

            Collections.sort(serviceItems, AmtrakCascadesServiceItem.scheduledDepartureTimeComparator);

            view.hideProgressIndicator();
            view.setFromToLocation(fromLocation, toLocation);
            view.setTitle("Departing: " + amtrakStations.get(fromLocation) + " and Arriving: "
                    + amtrakStations.get(toLocation));
            view.render(serviceItems);

            view.refresh();
            accessibility.postScreenChangeNotification();
        }
    });
}

From source file:gov.wa.wsdot.mobile.client.activities.amtrakcascades.schedules.details.AmtrakCascadesSchedulesDetailsActivity.java

License:Open Source License

/**
 * Get train schedules for those with just a departing station.
 * /*from w ww.j  av  a2s . c o  m*/
 * @param url URL of the Web services API
 * @param toLocation arriving station name
 * @param fromLocation departing station name
 */
private void getDepartingTrains(String url, final String fromLocation, final String toLocation) {
    JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
    jsonp.setTimeout(30000); // Set timeout for 30 seconds
    jsonp.requestObject(url, new AsyncCallback<AmtrakCascadesScheduleFeed>() {

        @Override
        public void onFailure(Throwable caught) {
            view.hideProgressIndicator();
        }

        @Override
        public void onSuccess(AmtrakCascadesScheduleFeed result) {
            if (result.getSchedule() != null) {
                serviceItems.clear();
                AmtrakCascadesScheduleItem scheduleItem;
                int numItems = result.getSchedule().length();

                for (int i = 0; i < numItems; i++) { // Loop through all trains
                    Date scheduledTime = null;
                    locationItems = new ArrayList<Map<String, AmtrakCascadesScheduleItem>>();
                    stationItems = new HashMap<String, AmtrakCascadesScheduleItem>();
                    scheduleItem = new AmtrakCascadesScheduleItem();

                    if (result.getSchedule().get(i).getArrivalComment() != null) {
                        scheduleItem.setArrivalComment(result.getSchedule().get(i).getArrivalComment());
                    }

                    if (result.getSchedule().get(i).getArrivalScheduleType() != null) {
                        scheduleItem
                                .setArrivalScheduleType(result.getSchedule().get(i).getArrivalScheduleType());
                    }

                    if (result.getSchedule().get(i).getArrivalTime() != null) {
                        scheduleItem.setArrivalTime(
                                result.getSchedule().get(i).getArrivalTime().toString().substring(6, 19));
                    }

                    if (result.getSchedule().get(i).getDepartureComment() != null) {
                        scheduleItem.setDepartureComment(result.getSchedule().get(i).getDepartureComment());
                    }

                    if (result.getSchedule().get(i).getDepartureScheduleType() != null) {
                        scheduleItem.setDepartureScheduleType(
                                result.getSchedule().get(i).getDepartureScheduleType());
                    }

                    if (result.getSchedule().get(i).getDepartureTime() != null) {
                        scheduleItem.setDepartureTime(
                                result.getSchedule().get(i).getDepartureTime().toString().substring(6, 19));
                    }

                    scheduleItem.setStationName(result.getSchedule().get(i).getStationName());

                    if (result.getSchedule().get(i).getTrainMessage() != "") {
                        scheduleItem.setTrainMessage(result.getSchedule().get(i).getTrainMessage());
                    }

                    if (result.getSchedule().get(i).getScheduledArrivalTime() != null) {
                        scheduleItem.setScheduledArrivalTime(result.getSchedule().get(i)
                                .getScheduledArrivalTime().toString().substring(6, 19));

                        if (fromLocation.equalsIgnoreCase(scheduleItem.getStationName())) {
                            scheduledTime = new Date(Long.parseLong((scheduleItem.getScheduledArrivalTime())));
                        }
                    }

                    if (result.getSchedule().get(i).getScheduledDepartureTime() != null) {
                        scheduleItem.setScheduledDepartureTime(result.getSchedule().get(i)
                                .getScheduledDepartureTime().toString().substring(6, 19));

                        // We sort by scheduled departure time of the From station.
                        if (fromLocation.equalsIgnoreCase(scheduleItem.getStationName())) {
                            scheduledTime = new Date(
                                    Long.parseLong((scheduleItem.getScheduledDepartureTime())));
                        }
                    }

                    int trainNumber = result.getSchedule().get(i).getTrainNumber();
                    scheduleItem.setTrainNumber(trainNumber);
                    String serviceName = trainNumberMap.get(trainNumber);
                    if (serviceName == null) {
                        serviceName = "Bus Service";
                    }
                    scheduleItem.setTrainName(trainNumber + " " + serviceName);
                    scheduleItem.setTripNumber(result.getSchedule().get(i).getTripNumber());
                    scheduleItem.setUpdateTime(
                            result.getSchedule().get(i).getUpdateTime().toString().substring(6, 19));

                    stationItems.put(scheduleItem.getStationName(), scheduleItem);

                    locationItems.add(stationItems);
                    serviceItems.add(new AmtrakCascadesServiceItem(scheduledTime, locationItems));
                }
            }

            Collections.sort(serviceItems, AmtrakCascadesServiceItem.scheduledDepartureTimeComparator);

            view.hideProgressIndicator();
            String location = fromLocation;
            view.setFromToLocation(fromLocation, location);
            view.setTitle(amtrakStations.get(fromLocation));
            view.render(serviceItems);

            view.refresh();
        }
    });
}

From source file:gov.wa.wsdot.mobile.client.activities.borderwait.BorderWaitActivity.java

License:Open Source License

private void createBorderWaitList(final BorderWaitView view) {

    /** // www  .  j a  v a 2s . c  o  m
     * Check the cache table for the last time data was downloaded. If we are within
     * the allowed time period, don't sync, otherwise get fresh data from the server.
     */
    dbService.getCacheLastUpdated(Tables.BORDER_WAIT, new ListCallback<GenericRow>() {

        @Override
        public void onFailure(DataServiceException error) {
        }

        @Override
        public void onSuccess(List<GenericRow> result) {
            boolean shouldUpdate = true;

            if (!result.isEmpty()) {
                double now = System.currentTimeMillis();
                double lastUpdated = result.get(0).getDouble(CachesColumns.CACHE_LAST_UPDATED);
                shouldUpdate = (Math.abs(now - lastUpdated) > (15 * 60000)); // Refresh every 15 minutes.
            }

            view.showProgressIndicator();

            if (shouldUpdate) {
                /**
                 * Check the travel border wait table for any starred entries. If we find some,
                 * save them to a list so we can re-star those after we flush the database.
                 */
                dbService.getStarredBorderWaits(new ListCallback<GenericRow>() {

                    @Override
                    public void onFailure(DataServiceException error) {
                    }

                    @Override
                    public void onSuccess(List<GenericRow> result) {
                        starred.clear();

                        if (!result.isEmpty()) {
                            for (GenericRow row : result) {
                                starred.add(row.getInt(BorderWaitColumns.BORDER_WAIT_ID));
                            }
                        }

                        JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
                        // Set timeout for 30 seconds (30000 milliseconds)
                        jsonp.setTimeout(30000);
                        jsonp.requestObject(BORDER_WAIT_URL, new AsyncCallback<BorderCrossings>() {

                            @Override
                            public void onFailure(Throwable caught) {
                                view.hideProgressIndicator();
                                phoneGap.getNotification().alert("Can't load data. Check your connection.",
                                        new AlertCallback() {
                                            @Override
                                            public void onOkButtonClicked() {
                                                // TODO Auto-generated method stub
                                            }
                                        }, "Connection Error");
                            }

                            @Override
                            public void onSuccess(BorderCrossings result) {

                                if (result.getWaitTimes() != null) {
                                    borderWaitItems.clear();
                                    BorderWaitItem item;

                                    int numItems = result.getWaitTimes().getItems().length();

                                    for (int i = 0; i < numItems; i++) {
                                        item = new BorderWaitItem();

                                        item.setId(result.getWaitTimes().getItems().get(i).getId());
                                        item.setTitle(result.getWaitTimes().getItems().get(i).getName());
                                        item.setUpdated(result.getWaitTimes().getItems().get(i).getUpdated());
                                        item.setLane(result.getWaitTimes().getItems().get(i).getLane());
                                        item.setRoute(result.getWaitTimes().getItems().get(i).getRoute());
                                        item.setDirection(
                                                result.getWaitTimes().getItems().get(i).getDirection());
                                        item.setWait(result.getWaitTimes().getItems().get(i).getWait());

                                        if (starred.contains(result.getWaitTimes().getItems().get(i).getId())) {
                                            item.setIsStarred(1);
                                        }

                                        borderWaitItems.add(item);

                                    }

                                    // Purge existing border wait times covered by incoming data
                                    dbService.deleteBorderWaits(new VoidCallback() {

                                        @Override
                                        public void onFailure(DataServiceException error) {
                                        }

                                        @Override
                                        public void onSuccess() {
                                            // Bulk insert all the new border wait times
                                            dbService.insertBorderWaits(borderWaitItems,
                                                    new RowIdListCallback() {

                                                        @Override
                                                        public void onFailure(DataServiceException error) {
                                                        }

                                                        @Override
                                                        public void onSuccess(List<Integer> rowIds) {
                                                            // Update the cache table with the time we did the update
                                                            ArrayList<CacheItem> cacheItems = new ArrayList<CacheItem>();
                                                            cacheItems.add(new CacheItem(Tables.BORDER_WAIT,
                                                                    System.currentTimeMillis()));
                                                            dbService.updateCachesTable(cacheItems,
                                                                    new VoidCallback() {

                                                                        @Override
                                                                        public void onFailure(
                                                                                DataServiceException error) {
                                                                        }

                                                                        @Override
                                                                        public void onSuccess() {
                                                                            dbService.getBorderWaits(
                                                                                    new ListCallback<GenericRow>() {

                                                                                        @Override
                                                                                        public void onFailure(
                                                                                                DataServiceException error) {
                                                                                        }

                                                                                        @Override
                                                                                        public void onSuccess(
                                                                                                List<GenericRow> result) {
                                                                                            getBorderWaits(view,
                                                                                                    result);
                                                                                        }
                                                                                    });
                                                                        }
                                                                    });
                                                        }
                                                    });
                                        }
                                    });
                                }
                            }
                        });
                    }
                });

            } else {
                dbService.getBorderWaits(new ListCallback<GenericRow>() {

                    @Override
                    public void onFailure(DataServiceException error) {
                    }

                    @Override
                    public void onSuccess(List<GenericRow> result) {
                        getBorderWaits(view, result);
                    }
                });
            }
        }
    });
}

From source file:gov.wa.wsdot.mobile.client.activities.ferries.schedules.departures.FerriesRouteDeparturesActivity.java

License:Open Source License

protected void createFerryTerminalSailingSpaces() {
    /** //from w  w  w . j av a 2 s  . c  o m
     * Check the cache table for the last time data was downloaded. If we are within
     * the allowed time period, don't sync, otherwise get fresh data from the server.
     */
    dbService.getCacheLastUpdated(Tables.FERRIES_TERMINAL_SAILING_SPACE, new ListCallback<GenericRow>() {

        @Override
        public void onFailure(DataServiceException error) {
        }

        @Override
        public void onSuccess(List<GenericRow> result) {
            boolean shouldUpdate = true;

            view.showProgressIndicator();

            if (!result.isEmpty()) {
                double now = System.currentTimeMillis();
                double lastUpdated = result.get(0).getDouble(CachesColumns.CACHE_LAST_UPDATED);
                shouldUpdate = (Math.abs(now - lastUpdated) > (15 * 1000)); // Refresh every 15 seconds.
            }

            if (shouldUpdate) {
                JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
                // Set timeout for 30 seconds (30000 milliseconds)
                jsonp.setTimeout(30000);
                jsonp.requestObject(TERMINAL_SAILING_SPACE_URL,
                        new AsyncCallback<FerriesTerminalSailingSpaceFeed>() {

                            @Override
                            public void onFailure(Throwable caught) {
                                view.hideProgressIndicator();
                            }

                            @Override
                            public void onSuccess(FerriesTerminalSailingSpaceFeed result) {
                                if (result.getTerminals() != null) {
                                    DateTimeFormat dateFormat = DateTimeFormat.getFormat("MMMM d, yyyy h:mm a");
                                    ferriesTerminalSailingSpaceItems.clear();
                                    FerriesTerminalSailingSpaceItem item;
                                    int numItems = result.getTerminals().length();

                                    for (int i = 0; i < numItems; i++) {
                                        item = new FerriesTerminalSailingSpaceItem();
                                        item.setTerminalId(result.getTerminals().get(i).getTerminalId());
                                        item.setTerminalName(result.getTerminals().get(i).getTerminalName());
                                        item.setTerminalAbbrev(
                                                result.getTerminals().get(i).getTerminalAbbrev());
                                        item.setTerminalDepartingSpaces(
                                                new JSONArray(result.getTerminals().get(i).getDepartingSpaces())
                                                        .toString());
                                        item.setLastUpdated(
                                                dateFormat.format(new Date(System.currentTimeMillis())));

                                        ferriesTerminalSailingSpaceItems.add(item);
                                    }

                                    // Purge existing terminal items covered by incoming data
                                    dbService.deleteFerriesTerminalSailingSpace(new VoidCallback() {

                                        @Override
                                        public void onFailure(DataServiceException error) {
                                        }

                                        @Override
                                        public void onSuccess() {
                                            // Bulk insert all the new ferries terminal sailing space items
                                            dbService.insertFerriesTerminalSailingSpace(
                                                    ferriesTerminalSailingSpaceItems, new RowIdListCallback() {

                                                        @Override
                                                        public void onFailure(DataServiceException error) {
                                                        }

                                                        @Override
                                                        public void onSuccess(List<Integer> rowIds) {
                                                            // Update the cache table with the time we did the update
                                                            List<CacheItem> cacheItems = new ArrayList<CacheItem>();
                                                            cacheItems.add(new CacheItem(
                                                                    Tables.FERRIES_TERMINAL_SAILING_SPACE,
                                                                    System.currentTimeMillis()));
                                                            dbService.updateCachesTable(cacheItems,
                                                                    new VoidCallback() {

                                                                        @Override
                                                                        public void onFailure(
                                                                                DataServiceException error) {
                                                                        }

                                                                        @Override
                                                                        public void onSuccess() {
                                                                            // Get available drive-up vehicle spaces.
                                                                            dbService
                                                                                    .getFerriesTerminalSailingSpace(
                                                                                            scheduleDateItems
                                                                                                    .get(view
                                                                                                            .getDayOfWeekSelected())
                                                                                                    .getFerriesTerminalItem()
                                                                                                    .get(sailingsIndex)
                                                                                                    .getDepartingTerminalID()
                                                                                                    .toString(),
                                                                                            new ListCallback<GenericRow>() {

                                                                                                @Override
                                                                                                public void onFailure(
                                                                                                        DataServiceException error) {
                                                                                                }

                                                                                                @Override
                                                                                                public void onSuccess(
                                                                                                        List<GenericRow> result) {
                                                                                                    getFerryTerminalSailingSpaces(
                                                                                                            result);
                                                                                                }
                                                                                            });
                                                                        }
                                                                    });
                                                        }
                                                    });
                                        }
                                    });

                                }
                            }
                        });

            } else {
                // Get available drive-up vehicle spaces.
                dbService.getFerriesTerminalSailingSpace(
                        scheduleDateItems.get(view.getDayOfWeekSelected()).getFerriesTerminalItem()
                                .get(sailingsIndex).getDepartingTerminalID().toString(),
                        new ListCallback<GenericRow>() {

                            @Override
                            public void onFailure(DataServiceException error) {
                            }

                            @Override
                            public void onSuccess(List<GenericRow> result) {
                                getFerryTerminalSailingSpaces(result);
                            }

                        });
            }
        }
    });
}

From source file:gov.wa.wsdot.mobile.client.activities.ferries.schedules.departures.FerriesRouteDeparturesActivity.java

License:Open Source License

private void createFerryTerminalCameras() {

    /** //from   w  ww  .  j  a  v  a2s .c  o m
     * Check the cache table for the last time data was downloaded. If we are within
     * the allowed time period, don't sync, otherwise get fresh data from the server.
     */
    dbService.getCacheLastUpdated(Tables.CAMERAS, new ListCallback<GenericRow>() {

        @Override
        public void onFailure(DataServiceException error) {
        }

        @Override
        public void onSuccess(List<GenericRow> result) {
            boolean shouldUpdate = true;

            if (!result.isEmpty()) {
                double now = System.currentTimeMillis();
                double lastUpdated = result.get(0).getDouble(CachesColumns.CACHE_LAST_UPDATED);
                shouldUpdate = (Math.abs(now - lastUpdated) > (7 * 86400000)); // Refresh every 7 days.
            }

            view.showProgressIndicator();

            if (shouldUpdate) {
                /**
                 * Check the cameras table for any starred entries. If we find some,
                 * save them to a list so we can re-star those after we flush the database.
                 */
                dbService.getStarredCameras(new ListCallback<GenericRow>() {

                    @Override
                    public void onFailure(DataServiceException error) {
                    }

                    @Override
                    public void onSuccess(List<GenericRow> result) {
                        starred.clear();

                        if (!result.isEmpty()) {
                            for (GenericRow row : result) {
                                starred.add(row.getInt(CamerasColumns.CAMERA_ID));
                            }
                        }

                        JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
                        // Set timeout for 30 seconds (30000 milliseconds)
                        jsonp.setTimeout(30000);
                        jsonp.requestObject(CAMERAS_URL, new AsyncCallback<CamerasFeed>() {

                            @Override
                            public void onFailure(Throwable caught) {
                                view.hideProgressIndicator();
                                phoneGap.getNotification().alert("Can't load data. Check your connection.",
                                        new AlertCallback() {
                                            @Override
                                            public void onOkButtonClicked() {
                                                // TODO Auto-generated method stub
                                            }
                                        }, "Connection Error");
                            }

                            @Override
                            public void onSuccess(CamerasFeed result) {

                                if (result.getCameras() != null) {
                                    cameraItems.clear();
                                    CameraItem item;

                                    int numCameras = result.getCameras().getItems().length();

                                    for (int i = 0; i < numCameras; i++) {
                                        item = new CameraItem();

                                        item.setCameraId(result.getCameras().getItems().get(i).getId());
                                        item.setTitle(result.getCameras().getItems().get(i).getTitle());
                                        item.setImageUrl(result.getCameras().getItems().get(i).getUrl());
                                        item.setLatitude(result.getCameras().getItems().get(i).getLat());
                                        item.setLongitude(result.getCameras().getItems().get(i).getLon());
                                        item.setHasVideo(result.getCameras().getItems().get(i).getHasVideo());
                                        item.setRoadName(result.getCameras().getItems().get(i).getRoadName());

                                        if (starred.contains(result.getCameras().getItems().get(i).getId())) {
                                            item.setIsStarred(1);
                                        }

                                        cameraItems.add(item);
                                    }

                                    // Purge existing cameras covered by incoming data
                                    dbService.deleteCameras(new VoidCallback() {

                                        @Override
                                        public void onFailure(DataServiceException error) {
                                        }

                                        @Override
                                        public void onSuccess() {
                                            // Bulk insert all the new cameras
                                            dbService.insertCameras(cameraItems, new RowIdListCallback() {

                                                @Override
                                                public void onFailure(DataServiceException error) {
                                                }

                                                @Override
                                                public void onSuccess(List<Integer> rowIds) {
                                                    // Update the cache table with the time we did the update
                                                    List<CacheItem> cacheItems = new ArrayList<CacheItem>();
                                                    cacheItems.add(new CacheItem(Tables.CAMERAS,
                                                            System.currentTimeMillis()));

                                                    dbService.updateCachesTable(cacheItems, new VoidCallback() {

                                                        @Override
                                                        public void onFailure(DataServiceException error) {
                                                        }

                                                        @Override
                                                        public void onSuccess() {
                                                            getFerryTerminalCameras();
                                                        }
                                                    });
                                                }
                                            });
                                        }
                                    });
                                }
                            }
                        });
                    }
                });

            } else {
                getFerryTerminalCameras();
            }
        }
    });
}

From source file:gov.wa.wsdot.mobile.client.activities.ferries.schedules.FerriesRouteSchedulesActivity.java

License:Open Source License

private void createTopicsList(final boolean forceUpdate) {

    /** /*from www .java  2  s. c om*/
     * Check the cache table for the last time data was downloaded. If we are within
     * the allowed time period, don't sync, otherwise get fresh data from the server.
     */
    dbService.getCacheLastUpdated(Tables.FERRIES_SCHEDULES, new ListCallback<GenericRow>() {

        @Override
        public void onFailure(DataServiceException error) {
        }

        @Override
        public void onSuccess(List<GenericRow> result) {
            boolean shouldUpdate = true;

            if (!result.isEmpty()) {
                double now = System.currentTimeMillis();
                double lastUpdated = result.get(0).getDouble(CachesColumns.CACHE_LAST_UPDATED);
                shouldUpdate = (Math.abs(now - lastUpdated) > (15 * 60000)); // Refresh every 15 minutes.
            }

            view.showProgressIndicator();

            if (shouldUpdate || forceUpdate) {
                /**
                 * Check the ferry schedules table for any starred entries. If we find some,
                 * save them to a list so we can re-star those after we flush the database.
                 */
                dbService.getStarredFerriesSchedules(new ListCallback<GenericRow>() {

                    @Override
                    public void onFailure(DataServiceException error) {
                    }

                    @Override
                    public void onSuccess(List<GenericRow> result) {
                        starred.clear();

                        if (!result.isEmpty()) {
                            for (GenericRow row : result) {
                                starred.add(row.getInt(FerriesSchedulesColumns.FERRIES_SCHEDULE_ID));
                            }
                        }

                        JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
                        // Set timeout for 30 seconds (30000 milliseconds)
                        jsonp.setTimeout(30000);
                        jsonp.requestObject(FERRIES_SCHEDULES_URL, new AsyncCallback<FerriesRouteFeed>() {

                            @Override
                            public void onFailure(Throwable caught) {
                                view.hideProgressIndicator();
                                if (!alertShowing) {
                                    alertShowing = true;
                                    phoneGap.getNotification().alert("Can't load data. Check your connection.",
                                            new AlertCallback() {
                                                @Override
                                                public void onOkButtonClicked() {
                                                    alertShowing = false;
                                                }
                                            }, "Connection Error");
                                }
                            }

                            @Override
                            public void onSuccess(FerriesRouteFeed result) {

                                if (result.getRoutes() != null) {
                                    ferriesRouteItems.clear();
                                    FerriesRouteItem item;
                                    int numRoutes = result.getRoutes().length();

                                    for (int i = 0; i < numRoutes; i++) {
                                        item = new FerriesRouteItem();

                                        item.setRouteID(result.getRoutes().get(i).getRouteID());
                                        item.setDescription(result.getRoutes().get(i).getDescription());
                                        item.setCrossingTime(result.getRoutes().get(i).getCrossingTime());
                                        item.setScheduleDate(
                                                new JSONArray(result.getRoutes().get(i).getDate()).toString());
                                        item.setRouteAlert(
                                                new JSONArray(result.getRoutes().get(i).getRouteAlert())
                                                        .toString());
                                        item.setCacheDate(dateFormat.format(new Date(Long.parseLong(
                                                result.getRoutes().get(i).getCacheDate().substring(6, 19)))));

                                        if (starred.contains(result.getRoutes().get(i).getRouteID())) {
                                            item.setIsStarred(1);
                                        }

                                        ferriesRouteItems.add(item);
                                    }

                                    // Purge existing border wait times covered by incoming data
                                    dbService.deleteFerriesSchedules(new VoidCallback() {

                                        @Override
                                        public void onFailure(DataServiceException error) {
                                        }

                                        @Override
                                        public void onSuccess() {
                                            // Bulk insert all the new ferries schedules
                                            dbService.insertFerriesSchedules(ferriesRouteItems,
                                                    new RowIdListCallback() {

                                                        @Override
                                                        public void onFailure(DataServiceException error) {
                                                        }

                                                        @Override
                                                        public void onSuccess(List<Integer> rowIds) {
                                                            // Update the cache table with the time we did the update
                                                            List<CacheItem> cacheItems = new ArrayList<CacheItem>();
                                                            cacheItems
                                                                    .add(new CacheItem(Tables.FERRIES_SCHEDULES,
                                                                            System.currentTimeMillis()));
                                                            dbService.updateCachesTable(cacheItems,
                                                                    new VoidCallback() {

                                                                        @Override
                                                                        public void onFailure(
                                                                                DataServiceException error) {
                                                                        }

                                                                        @Override
                                                                        public void onSuccess() {
                                                                            dbService.getFerriesSchedules(
                                                                                    new ListCallback<GenericRow>() {

                                                                                        @Override
                                                                                        public void onFailure(
                                                                                                DataServiceException error) {
                                                                                        }

                                                                                        @Override
                                                                                        public void onSuccess(
                                                                                                List<GenericRow> result) {
                                                                                            getFerriesSchedules(
                                                                                                    result);
                                                                                        }
                                                                                    });
                                                                        }
                                                                    });
                                                        }
                                                    });
                                        }
                                    });
                                }
                            }
                        });
                    }
                });

            } else {
                dbService.getFerriesSchedules(new ListCallback<GenericRow>() {

                    @Override
                    public void onFailure(DataServiceException error) {
                    }

                    @Override
                    public void onSuccess(List<GenericRow> result) {
                        getFerriesSchedules(result);
                    }
                });
            }

        }
    });

}