use of de.westnordost.osmapi.map.data.Element in project StreetComplete by westnordost.
the class TagFilterValue method matches.
public boolean matches(Object obj) {
if (!(obj instanceof Element))
return false;
Element element = (Element) obj;
if (value == null) {
return element.getTags() != null && element.getTags().containsKey(key);
}
String tagValue = element.getTags() != null ? element.getTags().get(key) : null;
if (tagValue == null)
return op.startsWith("!");
if (isValueRegex()) {
if (op.equals("!~"))
return !tagValue.matches(value);
if (op.equals("~"))
return tagValue.matches(value);
} else {
if (op.equals("!="))
return !tagValue.equals(value);
if (op.equals("="))
return tagValue.equals(value);
}
return false;
}
use of de.westnordost.osmapi.map.data.Element in project StreetComplete by westnordost.
the class AOsmQuestChangesUpload method upload.
public synchronized void upload(AtomicBoolean cancelState) {
int commits = 0, obsolete = 0;
changesetIdsCache = new HashMap<>();
unlockedQuests = new ArrayList<>();
for (OsmQuest quest : questDB.getAll(null, QuestStatus.ANSWERED)) {
// break so that the unreferenced stuff is deleted still
if (cancelState.get())
break;
Element element = elementDB.get(quest.getElementType(), quest.getElementId());
long changesetId = getChangesetIdOrCreate(quest.getOsmElementQuestType(), quest.getChangesSource());
if (uploadQuestChange(changesetId, quest, element, false, false)) {
commits++;
} else {
obsolete++;
}
}
cleanUp();
String logMsg = "Committed " + commits + " changes";
if (obsolete > 0) {
logMsg += " but dropped " + obsolete + " changes because there were conflicts";
}
Log.i(TAG, logMsg);
if (!unlockedQuests.isEmpty()) {
int unlockedQuestsCount = unlockedQuests.size();
if (visibleQuestListener != null) {
visibleQuestListener.onQuestsCreated(unlockedQuests, QuestGroup.OSM);
}
Log.i(TAG, "Unlocked " + unlockedQuestsCount + " new quests");
}
closeOpenChangesets();
if (commits > 0) {
// changesets are closed delayed after X minutes of inactivity
ChangesetAutoCloserJob.scheduleJob();
}
}
use of de.westnordost.osmapi.map.data.Element in project StreetComplete by westnordost.
the class AOsmQuestChangesUpload method handleElementConflict.
private boolean handleElementConflict(long changesetId, OsmQuest quest, boolean alreadyHandlingChangesetConflict) {
Element element = updateElementFromServer(quest.getElementType(), quest.getElementId());
// element anymore, drop it (#720)
if (element != null) {
if (!questIsApplicableToElement(quest, element)) {
Log.d(TAG, "Dropping quest " + getQuestStringForLog(quest) + " because the quest is no longer applicable to the element");
deleteConflictingQuest(quest);
return false;
}
}
return uploadQuestChange(changesetId, quest, element, true, alreadyHandlingChangesetConflict);
}
use of de.westnordost.osmapi.map.data.Element in project StreetComplete by westnordost.
the class AOsmQuestChangesUpload method uploadQuestChange.
boolean uploadQuestChange(long changesetId, OsmQuest quest, Element element, boolean alreadyHandlingElementConflict, boolean alreadyHandlingChangesetConflict) {
Element elementWithChangesApplied = changesApplied(element, quest);
if (elementWithChangesApplied == null) {
deleteConflictingQuest(quest);
return false;
}
int[] newVersion = { element.getVersion() };
try {
osmDao.uploadChanges(changesetId, Collections.singleton(elementWithChangesApplied), diffElement -> {
if (diffElement.clientId == elementWithChangesApplied.getId()) {
newVersion[0] = diffElement.serverVersion;
/* It is not necessary (yet) to handle updating the element's id because
StreetComplete does not add or delete elements */
}
});
} catch (OsmConflictException e) {
return handleConflict(changesetId, quest, element, alreadyHandlingElementConflict, alreadyHandlingChangesetConflict, e);
}
Element updatedElement = copyElement(elementWithChangesApplied, newVersion[0]);
closeQuest(quest);
// save with new version when persisting to DB
elementDB.put(updatedElement);
statisticsDB.addOne(quest.getType().getClass().getSimpleName());
unlockedQuests.addAll(questUnlocker.unlockNewQuests(updatedElement));
return true;
}
use of de.westnordost.osmapi.map.data.Element in project StreetComplete by westnordost.
the class AOsmQuestChangesUpload method changesApplied.
private Element changesApplied(Element element, OsmQuest quest) {
// The element can be null if it has been deleted in the meantime (outside this app usually)
if (element == null) {
Log.d(TAG, "Dropping quest " + getQuestStringForLog(quest) + " because the associated element has already been deleted");
return null;
}
Element copy = copyElement(element, element.getVersion());
StringMapChanges changes = quest.getChanges();
if (changes.hasConflictsTo(copy.getTags())) {
Log.d(TAG, "Dropping quest " + getQuestStringForLog(quest) + " because there has been a conflict while applying the changes");
return null;
}
changes.applyTo(copy.getTags());
return copy;
}
Aggregations