Search in sources :

Example 6 with RFieldData

use of com.cpjd.roblu.models.metrics.RFieldData in project Roblu by wdavies973.

the class FormViewer method onActivityResult.

/**
 * Receive information from a child activity
 * @param requestCode the request code the child activity was launched with
 * @param resultCode the result code of the child activity
 * @param data any data returned with the child activity
 */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    /*
         * User created a new metric
         */
    if (requestCode == Constants.NEW_METRIC_REQUEST && resultCode == Constants.METRIC_CONFIRMED) {
        changesMade = true;
        Bundle b = data.getExtras();
        RMetric metric = (RMetric) b.getSerializable("metric");
        if (metric instanceof RFieldData && currentTab == 0) {
            Toast.makeText(getApplicationContext(), "You can't add the field data metric to the pit tab.", Toast.LENGTH_LONG).show();
            return;
        }
        metricsAdapter.addMetric(metric);
    } else /*
         * User edited a metric
         */
    if (requestCode == Constants.EDIT_METRIC_REQUEST && resultCode == Constants.METRIC_CONFIRMED) {
        changesMade = true;
        Bundle b = data.getExtras();
        RMetric metric = (RMetric) b.getSerializable("metric");
        metricsAdapter.reAdd(metric);
    } else /*
         * User discarded a metric edit
         */
    if (requestCode == Constants.EDIT_METRIC_REQUEST && resultCode == Constants.CANCELLED) {
        metricsAdapter.reAdd(metricEditHolder);
    }
}
Also used : Bundle(android.os.Bundle) RFieldData(com.cpjd.roblu.models.metrics.RFieldData) RMetric(com.cpjd.roblu.models.metrics.RMetric)

Example 7 with RFieldData

use of com.cpjd.roblu.models.metrics.RFieldData in project Roblu by wdavies973.

the class InitPacker method doInBackground.

@Override
protected Boolean doInBackground(Void... params) {
    /*
         * Make sure this thread has network permissions
         */
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
    StrictMode.setThreadPolicy(policy);
    Log.d("RBS", "Executing InitPacker task...");
    IO io = ioWeakReference.get();
    RSettings settings = io.loadSettings();
    RSyncSettings cloudSettings = io.loadCloudSettings();
    cloudSettings.setPurgeRequested(false);
    io.saveCloudSettings(cloudSettings);
    io.saveSettings(settings);
    Request r = new Request(settings.getServerIP());
    if (!r.ping()) {
        listener.statusUpdate("It appears as though the server is offline. Try again later.");
        return false;
    }
    /*
         * Load all teams from the event, also make sure that that the teams are verified
         */
    REvent event = io.loadEvent(eventID);
    event.setReadOnlyTeamNumber(-1);
    RForm form = io.loadForm(eventID);
    RTeam[] teams = io.loadTeams(eventID);
    if (event == null || form == null || teams == null || teams.length == 0) {
        Log.d("RBS", "Not enough data to warrant an event upload.");
        listener.statusUpdate("This event doesn't contain any teams or sufficient data to upload to the server. Create some teams!");
        return false;
    }
    // Generate the checkouts
    SyncHelper syncHelper = new SyncHelper(io, event, SyncHelper.MODES.NETWORK);
    ArrayList<RCheckout> checkouts = syncHelper.generateCheckoutsFromEvent(teams, -1);
    // Remove field data
    try {
        for (RCheckout checkout : checkouts) {
            for (RTab tab : checkout.getTeam().getTabs()) {
                for (RMetric metric : tab.getMetrics()) {
                    if (metric instanceof RFieldData) {
                        ((RFieldData) metric).setData(null);
                    }
                }
            }
        }
    } catch (Exception e) {
    // Doesn't matter
    }
    /*
         * Convert into JSON and upload
         */
    ObjectMapper mapper = new ObjectMapper();
    try {
        // serialization all the checkouts and pack them in an json array, this will be processed by the server
        String serializedCheckouts = syncHelper.packCheckouts(checkouts);
        String serializedForm = mapper.writeValueAsString(form);
        String serializedUI = mapper.writeValueAsString(settings.getRui());
        String eventName = event.getName();
        if (eventName == null)
            eventName = "";
        if (event.getKey() == null)
            event.setKey("");
        CloudCheckoutRequest ccr = new CloudCheckoutRequest(r, settings.getCode());
        Log.d("RBS", "Initializing init packer upload...");
        boolean success = ccr.init(settings.getTeamNumber(), eventName, serializedForm, serializedUI, serializedCheckouts, event.getKey());
        /*
             * Disable all other events with cloud syncing enabled
             */
        if (success) {
            REvent[] events = io.loadEvents();
            for (int i = 0; events != null && i < events.length; i++) {
                events[i].setCloudEnabled(events[i].getID() == eventID);
                io.saveEvent(events[i]);
            }
            cloudSettings.getCheckoutSyncIDs().clear();
            /*
                 * Add default sync ids
                 */
            for (RCheckout checkout : checkouts) {
                cloudSettings.getCheckoutSyncIDs().put(checkout.getID(), 0L);
            }
            io.saveCloudSettings(cloudSettings);
            io.saveSettings(settings);
        } else
            listener.statusUpdate("An error occurred. Event was not uploaded.");
        return success;
    } catch (Exception e) {
        Log.d("RBS", "An error occurred in InitPacker: " + e.getMessage());
        listener.statusUpdate("An error occurred. Event was not uploaded.");
        return false;
    } finally {
        /*
             * Set all images to null to return memory to normal
             */
        for (RCheckout checkout : checkouts) {
            for (RTab tab : checkout.getTeam().getTabs()) {
                for (RMetric metric : tab.getMetrics()) {
                    if (metric instanceof RGallery) {
                        ((RGallery) metric).setImages(null);
                    }
                }
            }
        }
    }
}
Also used : RTab(com.cpjd.roblu.models.RTab) RGallery(com.cpjd.roblu.models.metrics.RGallery) RFieldData(com.cpjd.roblu.models.metrics.RFieldData) RMetric(com.cpjd.roblu.models.metrics.RMetric) StrictMode(android.os.StrictMode) RForm(com.cpjd.roblu.models.RForm) REvent(com.cpjd.roblu.models.REvent) SyncHelper(com.cpjd.roblu.sync.SyncHelper) RCheckout(com.cpjd.roblu.models.RCheckout) RSyncSettings(com.cpjd.roblu.models.RSyncSettings) RSettings(com.cpjd.roblu.models.RSettings) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) CloudCheckoutRequest(com.cpjd.requests.CloudCheckoutRequest) RTeam(com.cpjd.roblu.models.RTeam) IO(com.cpjd.roblu.io.IO) Request(com.cpjd.http.Request) CloudCheckoutRequest(com.cpjd.requests.CloudCheckoutRequest)

Example 8 with RFieldData

use of com.cpjd.roblu.models.metrics.RFieldData in project Roblu by wdavies973.

the class SyncHelper method mergeCheckout.

/**
 * Merges the checkout with the active event.
 * Creates new team / match if they don't already exist
 * @param checkout the checkout to merge
 */
public void mergeCheckout(RCheckout checkout) {
    // Verify the target checkout, to make sure it's in sync with the local form
    checkout.getTeam().verify(form);
    // Check to see if there is a local team matching this checkout
    RTeam team = io.loadTeam(activeEvent.getID(), checkout.getTeam().getID());
    // The team was found, so do a merge
    if (team != null) {
        team.verify(form);
        boolean shouldOverrideLastEdited = false;
        for (RTab downloadedTab : checkout.getTeam().getTabs()) {
            boolean matchLocated = false;
            for (RTab localTab : team.getTabs()) {
                localTab.setWon(downloadedTab.isWon());
                // Found the match, start merging
                if (localTab.getTitle().equalsIgnoreCase(downloadedTab.getTitle())) {
                    /*
                         * Copy over the edit tabs
                         */
                    if (downloadedTab.getEdits() != null)
                        localTab.setEdits(downloadedTab.getEdits());
                    for (RMetric downloadedMetric : downloadedTab.getMetrics()) {
                        if (!(downloadedMetric instanceof RCalculation) && !(downloadedMetric instanceof RFieldData) && downloadedMetric.isModified())
                            shouldOverrideLastEdited = true;
                        for (RMetric localMetric : localTab.getMetrics()) {
                            // Found the metric, determine if a merge needs to occur
                            if (downloadedMetric.getID() == localMetric.getID()) {
                                // Ignore imports from this metric
                                if (downloadedMetric instanceof RFieldData)
                                    break;
                                /*
                                     * We have to deal with one special case scenario - the gallery.
                                     * The gallery should never be overrided, just added to
                                     */
                                if (downloadedMetric instanceof RGallery && localMetric instanceof RGallery) {
                                    if (((RGallery) localMetric).getPictureIDs() == null)
                                        ((RGallery) localMetric).setPictureIDs(new ArrayList<Integer>());
                                    if (((RGallery) downloadedMetric).getImages() != null) {
                                        // Add images to the current gallery
                                        for (int i = 0; i < ((RGallery) downloadedMetric).getImages().size(); i++) {
                                            ((RGallery) localMetric).getPictureIDs().add(io.savePicture(activeEvent.getID(), ((RGallery) downloadedMetric).getImages().get(i)));
                                        }
                                    }
                                    // Don't forget to clear the pictures from memory after they've been merged
                                    ((RGallery) downloadedMetric).setImages(null);
                                    break;
                                }
                                // If the local metric is already edited, keep whichever data is newest
                                if (localMetric.isModified()) {
                                    if (checkout.getTeam().getLastEdit() >= team.getLastEdit()) {
                                        int replaceIndex = localTab.getMetrics().indexOf(localMetric);
                                        localTab.getMetrics().set(replaceIndex, downloadedMetric);
                                    }
                                } else // Otherwise, just do a straight override
                                {
                                    int replaceIndex = localTab.getMetrics().indexOf(localMetric);
                                    localTab.getMetrics().set(replaceIndex, downloadedMetric);
                                }
                                break;
                            }
                        }
                    }
                    matchLocated = true;
                    break;
                }
            }
            if (!matchLocated) {
                // Add as a new match if a merge wasn't performed
                team.addTab(checkout.getTeam().getTabs().get(0));
                Collections.sort(team.getTabs());
            }
        }
        if (shouldOverrideLastEdited)
            team.setLastEdit(checkout.getTeam().getLastEdit());
    } else // The team was not found locally, create a new one
    {
        Log.d("RBS", "Team was not found, creating a new one.");
        team = new RTeam(checkout.getTeam().getName(), checkout.getTeam().getNumber(), checkout.getTeam().getID());
        team.setLastEdit(checkout.getTeam().getLastEdit());
        team.verify(form);
        if (checkout.getTeam().getTabs().size() > 1) {
            // this means the downloaded team was a PIT tab, so override the new team's tabs
            team.setTabs(checkout.getTeam().getTabs());
        } else {
            // otherwise just add them
            team.addTab(checkout.getTeam().getTabs().get(0));
        }
    }
    // Save the team
    io.saveTeam(activeEvent.getID(), team);
    // Request a UI refresh
    Utils.requestTeamViewerRefresh(context, team.getID());
    Log.d("RBS-Service", "Merged the team: " + checkout.getTeam().getName());
}
Also used : RTeam(com.cpjd.roblu.models.RTeam) RTab(com.cpjd.roblu.models.RTab) RGallery(com.cpjd.roblu.models.metrics.RGallery) RFieldData(com.cpjd.roblu.models.metrics.RFieldData) ArrayList(java.util.ArrayList) RMetric(com.cpjd.roblu.models.metrics.RMetric) RCalculation(com.cpjd.roblu.models.metrics.RCalculation)

Example 9 with RFieldData

use of com.cpjd.roblu.models.metrics.RFieldData in project Roblu by wdavies973.

the class FieldData method generateSheet.

@Override
public void generateSheet(XSSFSheet sheet, REvent event, RForm form, RTeam[] teams, ArrayList<RCheckout> checkouts) {
    Row one = createRow(sheet);
    createCell(one, 0, "Team#");
    createCell(one, 1, "Match#");
    // Find a random RFieldData reference
    RFieldData fieldData = null;
    try {
        mainLoop: for (RTab tab : teams[0].getTabs()) {
            if (tab.getTitle().equalsIgnoreCase("PIT") || tab.getTitle().equalsIgnoreCase("PREDICTIONS"))
                continue;
            for (RMetric metric2 : tab.getMetrics()) {
                if (metric2 instanceof RFieldData) {
                    fieldData = (RFieldData) metric2;
                    break mainLoop;
                }
            }
        }
    } catch (Exception e) {
    // }
    }
    // Copy the metrics over
    int index = 2;
    for (Object key : fieldData.getData().keySet()) {
        createCell(one, index, key.toString());
        index++;
    }
    // Start copying data
    for (RCheckout checkout : checkouts) {
        if (!checkout.getTeam().getTabs().get(0).getTitle().startsWith("Quals"))
            continue;
        Row row = createRow(sheet);
        createCell(row, 0, String.valueOf(checkout.getTeam().getNumber()));
        createCell(row, 1, checkout.getTeam().getTabs().get(0).getTitle());
        index = 0;
        mainLoop: for (RTab tab : checkout.getTeam().getTabs()) {
            for (RMetric metric2 : tab.getMetrics()) {
                if (metric2 instanceof RFieldData) {
                    try {
                        for (Object key : ((RFieldData) metric2).getData().keySet()) {
                            createCell(row, index + 2, ((RFieldData) metric2).getData().get(key).get(tab.isRedAlliance() ? 0 : 1).toString());
                            index++;
                        }
                        break mainLoop;
                    } catch (Exception e) {
                    }
                }
            }
        }
    }
}
Also used : RTab(com.cpjd.roblu.models.RTab) RFieldData(com.cpjd.roblu.models.metrics.RFieldData) RCheckout(com.cpjd.roblu.models.RCheckout) Row(org.apache.poi.ss.usermodel.Row) RMetric(com.cpjd.roblu.models.metrics.RMetric)

Example 10 with RFieldData

use of com.cpjd.roblu.models.metrics.RFieldData in project Roblu by wdavies973.

the class MatchData method generateSheet.

@Override
public void generateSheet(XSSFSheet sheet, REvent event, RForm form, RTeam[] teams, ArrayList<RCheckout> checkouts) {
    /*
         * Output the headers
         */
    Row row = createRow(sheet);
    createCell(row, 0, "Team Number");
    createCell(row, 1, "Match number");
    for (int i = 0; i < form.getMatch().size(); i++) {
        createCell(row, i + 2, form.getMatch().get(i).getTitle());
    }
    /*
         * Output the data
         */
    for (RCheckout checkout : checkouts) {
        if (checkout.getTeam().getTabs().get(0).getTitle().equalsIgnoreCase("PIT") || checkout.getTeam().getTabs().get(0).getTitle().equalsIgnoreCase("PREDICTIONS"))
            continue;
        Row data = createRow(sheet);
        Cell cs = data.createCell(0);
        cs.setCellValue(checkout.getTeam().getNumber());
        cs.setCellStyle(getStyle());
        createCell(data, 1, checkout.getTeam().getTabs().get(0).getTitle().replace("Quals ", ""));
        int index = 0;
        for (RMetric metric : checkout.getTeam().getTabs().get(0).getMetrics()) {
            if (shouldWriteMetric(checkout.getTeam(), metric)) {
                if (metric instanceof RStopwatch)
                    createCell(data, index + 2, ((RStopwatch) metric).getLapsString());
                else if (metric instanceof RCalculation)
                    createCell(data, index + 2, ((RCalculation) metric).getValue(checkout.getTeam().getTabs().get(0).getMetrics()));
                else if (metric instanceof RFieldData)
                    continue;
                else
                    createCell(data, index + 2, metric.toString());
            } else
                createCell(data, index + 2, "");
            index++;
        }
    }
}
Also used : RStopwatch(com.cpjd.roblu.models.metrics.RStopwatch) RFieldData(com.cpjd.roblu.models.metrics.RFieldData) RCheckout(com.cpjd.roblu.models.RCheckout) Row(org.apache.poi.ss.usermodel.Row) RMetric(com.cpjd.roblu.models.metrics.RMetric) Cell(org.apache.poi.ss.usermodel.Cell) RCalculation(com.cpjd.roblu.models.metrics.RCalculation)

Aggregations

RFieldData (com.cpjd.roblu.models.metrics.RFieldData)10 RMetric (com.cpjd.roblu.models.metrics.RMetric)8 RTab (com.cpjd.roblu.models.RTab)5 RCalculation (com.cpjd.roblu.models.metrics.RCalculation)5 RGallery (com.cpjd.roblu.models.metrics.RGallery)5 RTeam (com.cpjd.roblu.models.RTeam)4 RCounter (com.cpjd.roblu.models.metrics.RCounter)4 RStopwatch (com.cpjd.roblu.models.metrics.RStopwatch)4 RTextfield (com.cpjd.roblu.models.metrics.RTextfield)4 IO (com.cpjd.roblu.io.IO)3 RCheckout (com.cpjd.roblu.models.RCheckout)3 RBoolean (com.cpjd.roblu.models.metrics.RBoolean)3 RCheckbox (com.cpjd.roblu.models.metrics.RCheckbox)3 RChooser (com.cpjd.roblu.models.metrics.RChooser)3 RSlider (com.cpjd.roblu.models.metrics.RSlider)3 TextView (android.widget.TextView)2 RForm (com.cpjd.roblu.models.RForm)2 RDivider (com.cpjd.roblu.models.metrics.RDivider)2 RFieldDiagram (com.cpjd.roblu.models.metrics.RFieldDiagram)2 ArrayList (java.util.ArrayList)2