use of com.erudika.para.core.utils.Config in project scoold by Erudika.
the class Post method updateTags.
public void updateTags(List<String> oldTags, List<String> newTags) {
List<String> deleteUs = new LinkedList<>();
List<Tag> updateUs = new LinkedList<>();
Map<String, Tag> oldTagz = Optional.ofNullable(oldTags).orElse(Collections.emptyList()).stream().map(t -> new Tag(getTagString(t))).distinct().collect(Collectors.toMap(t -> t.getId(), t -> t));
Map<String, Tag> newTagz = Optional.ofNullable(newTags).orElse(Collections.emptyList()).stream().map(t -> new Tag(getTagString(t))).distinct().collect(Collectors.toMap(t -> t.getId(), t -> t));
Map<String, Tag> existingTagz = client().readAll(Stream.concat(oldTagz.keySet().stream(), newTagz.keySet().stream()).distinct().collect(Collectors.toList())).stream().collect(Collectors.toMap(t -> t.getId(), t -> (Tag) t));
// add newly created tags
client().createAll(newTagz.values().stream().filter(t -> {
t.setCount(1);
return !existingTagz.containsKey(t.getId());
}).collect(Collectors.toList()));
// increment or decrement the count of the rest
existingTagz.values().forEach(t -> {
if (!oldTagz.containsKey(t.getId()) && newTagz.containsKey(t.getId())) {
t.setCount(t.getCount() + 1);
updateUs.add(t);
} else if (oldTagz.containsKey(t.getId()) && (newTags == null || !newTagz.containsKey(t.getId()))) {
t.setCount(t.getCount() - 1);
if (t.getCount() <= 0) {
// check if actual count is different
int c = client().getCount(Utils.type(Question.class), Collections.singletonMap(Config._TAGS, t.getTag())).intValue();
if (c <= 1) {
deleteUs.add(t.getId());
} else {
t.setCount(c);
}
} else {
updateUs.add(t);
}
}
// else: count remains unchanged
});
client().updateAll(updateUs);
client().deleteAll(deleteUs);
int tagsLimit = Math.min(ScooldServer.MAX_TAGS_PER_POST, 100);
setTags(newTagz.values().stream().limit(tagsLimit).map(t -> t.getTag()).collect(Collectors.toList()));
}
Aggregations