* @return Returns <code>true</code> if tag renaming is successful. Returns <code>false</code>, otherwise.
*/
public boolean renameTag(User user, String oldTag, String[] newTags) {
boolean isOkay = false;
Tag oldTagObj = GnizrDaoUtil.getTag(tagDao,oldTag);
UserTag userTagObj = null;
try {
userTagObj = GnizrDaoUtil.getUserTag(tagDao, user, oldTagObj);
} catch (MissingIdException e) {
logger.debug("no such user tag: " + user.getUsername() + "/" + oldTag);
}
if (oldTagObj != null && userTagObj != null && userTagObj.getCount() > 0) {
DaoResult<Bookmark> result = null;
List<Bookmark> oldBookmarks = null;
List<Bookmark> newBookmarks = null;
result = bookmarkDao.pageBookmarks(user, oldTagObj, 0, oldTagObj
.getCount());
oldBookmarks = result.getResult();
List<String> safeNewTags = getSafeTagsList(Arrays.asList(newTags));
for (Bookmark bm : oldBookmarks) {
initTagEntries(bm, safeNewTags);
}
List<Tag> tagsObjs = getTags(safeNewTags);
newBookmarks = tagDao.expandTag(user, oldTagObj, tagsObjs.toArray(new Tag[0]));
if (safeNewTags.contains(oldTag) == false) {
newBookmarks = tagDao.reduceTag(user, new Tag[] { oldTagObj });
}
Map<Integer, Bookmark> newBookmarksMap = GnizrDaoUtil
.getBookmarksMap(newBookmarks);
for (Bookmark oldBookmark : oldBookmarks) {
Bookmark newBookmark = newBookmarksMap.get(oldBookmark.getId());
if (newBookmark != null) {
try {
listenerExecutor.execute(new RunBookmarkUpdatedNotify(oldBookmark, newBookmark));
} catch (Exception e) {
logger.error("Error notifying BookmarkListeners", e);
}
} else {
logger
.error("After renaming a tag, bookmark "
+ oldBookmark.getId()
+ " doesn't appear in the expected list of updated bookmarks");
}
}
if(oldBookmarks.size() == newBookmarks.size()){
isOkay = true;
}
}
try {
userTagObj = GnizrDaoUtil.getUserTag(tagDao, user, oldTagObj);
if(userTagObj != null && userTagObj.getCount() == 0){
isOkay = tagDao.deleteUserTag(userTagObj.getId());
}
} catch (MissingIdException e) {
logger.debug("no such user tag: " + user.getUsername() + "/" + oldTag);
}
return isOkay;