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);
}
}
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);
}
}
}
}
}
}
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());
}
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) {
}
}
}
}
}
}
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++;
}
}
}
Aggregations