use of com.cpjd.roblu.models.metrics.RGallery in project Roblu by wdavies973.
the class EventMergeTask method run.
@Override
public void run() {
try {
// Load teams for each event
RTeam[] localTeams = io.loadTeams(localEventID);
RTeam[] targetTeams = io.loadTeams(targetEventID);
RForm localForm = io.loadForm(localEventID);
RForm targetForm = io.loadForm(targetEventID);
for (RTeam team : localTeams) team.verify(localForm);
for (RTeam team : targetTeams) team.verify(targetForm);
// Start merging
for (RTeam local : localTeams) {
// Find the team
for (RTeam target : targetTeams) {
if (local.getNumber() == target.getNumber()) {
for (RTab localTab : local.getTabs()) {
// Find tab
for (RTab targetTab : target.getTabs()) {
if (targetTab.getTitle().equalsIgnoreCase(localTab.getTitle())) {
// Match found, start merging metrics
for (RMetric localMetric : localTab.getMetrics()) {
// Find metric
for (RMetric targetMetric : targetTab.getMetrics()) {
/*
* Okay, the forms might be different, so we need to run some checks before we override the localMetric:
* -Same instanceof type (obviously)
* -Same title (within reason, do a little trimming, and ignore caps)
* -ID - ID should not be considered, they might not be equal
*
* If we find a good candidate for the metric, override the local. Conditions
* -Target is not modified: do nothing
* -Target is modified: overwrite if local not modified
* -Both modified: Compare team last edit time stamp
*/
if ((localMetric.getClass().equals(targetMetric.getClass())) && localMetric.getTitle().toLowerCase().replaceAll(" ", "").equals(targetMetric.getTitle().toLowerCase().replaceAll(" ", ""))) {
if (localMetric instanceof RGallery) {
// Just add the images
if (((RGallery) localMetric).getPictureIDs() == null)
((RGallery) localMetric).setPictureIDs(new ArrayList<Integer>());
if (((RGallery) localMetric).getImages() != null) {
// Add images to the current gallery
for (int i = 0; i < ((RGallery) targetMetric).getImages().size(); i++) {
((RGallery) localMetric).getPictureIDs().add(io.savePicture(localEventID, ((RGallery) targetMetric).getImages().get(i)));
}
}
// Don't forget to clear the pictures from memory after they've been merged
((RGallery) targetMetric).setImages(null);
local.setLastEdit(target.getLastEdit());
} else // Alright, looks like we can do the checks now
if (targetMetric.isModified() && !localMetric.isModified()) {
int tabIndex = local.getTabs().indexOf(localTab);
int metricIndex = local.getTabs().get(tabIndex).getMetrics().indexOf(localMetric);
local.getTabs().get(tabIndex).getMetrics().set(metricIndex, targetMetric);
local.setLastEdit(target.getLastEdit());
} else if (targetMetric.isModified() && localMetric.isModified() && target.getLastEdit() > local.getLastEdit()) {
int tabIndex = local.getTabs().indexOf(localTab);
int metricIndex = local.getTabs().get(tabIndex).getMetrics().indexOf(localMetric);
local.getTabs().get(tabIndex).getMetrics().set(metricIndex, targetMetric);
local.setLastEdit(target.getLastEdit());
}
break;
}
}
}
break;
}
}
}
break;
}
}
io.saveTeam(localEventID, local);
}
listener.success();
} catch (Exception e) {
Log.d("RBS", "Error occurred in EventMergeTask: " + e.getMessage());
listener.error();
}
}
use of com.cpjd.roblu.models.metrics.RGallery 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());
}
Aggregations