예제 #1
0
  void syncMetadata(
      OperationContext octxt,
      int itemId,
      MailItem.Type type,
      int folderId,
      int flags,
      String[] tags,
      Color color)
      throws ServiceException {
    boolean success = false;
    try {
      beginTransaction("syncMetadata", octxt);
      MailItem item = getItemById(itemId, type);
      int change_mask = getChangeMask(octxt, itemId, type);

      if ((change_mask & Change.FOLDER) != 0 || folderId == ID_AUTO_INCREMENT) {
        folderId = item.getFolderId();
      }

      if ((change_mask & Change.COLOR) != 0 || color.getValue() == ID_AUTO_INCREMENT) {
        color = item.getRgbColor();
      }
      Tag.NormalizedTags ntags;
      if ((change_mask & Change.TAGS) != 0 || tags == MailItem.TAG_UNCHANGED) {
        ntags = new Tag.NormalizedTags(item.getTags());
      } else {
        ntags = new Tag.NormalizedTags(this, tags);
      }
      if (flags == MailItem.FLAG_UNCHANGED) {
        flags = item.getFlagBitmask();
      } else {
        if ((change_mask & Change.UNREAD) != 0) {
          flags = (item.isUnread() ? Flag.BITMASK_UNREAD : 0) | (flags & ~Flag.BITMASK_UNREAD);
        }
        if ((change_mask & Change.FLAGS) != 0) {
          flags = item.getInternalFlagBitmask() | (flags & Flag.BITMASK_UNREAD);
        }
      }

      boolean unread = (flags & Flag.BITMASK_UNREAD) > 0;
      flags &= ~Flag.BITMASK_UNREAD;

      int prevIndexId = item.getIndexId();
      item.move(getFolderById(folderId));
      if (prevIndexId != item.getIndexId()) {
        index.add(item);
      }

      item.setColor(color);
      item.setTags(flags, ntags);
      if (getFlagById(Flag.ID_UNREAD).canTag(item)) {
        item.alterUnread(unread);
      }
      success = true;
    } finally {
      endTransaction(success);
    }
  }