/**
 * Axelor Business Solutions
 *
 * Copyright (C) 2005-2014 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.mail.db.repo;

import com.axelor.auth.db.User;
import com.axelor.db.JpaRepository;
import com.axelor.mail.db.MailFlags;
import com.axelor.mail.db.MailMessage;

public class MailFlagsRepository extends JpaRepository<MailFlags> {

	public MailFlagsRepository() {
		super(MailFlags.class);
	}

	public MailFlags findBy(MailMessage message, User user) {
		return all().filter("self.message = :message AND self.user = :user")
				.bind("message", message)
				.bind("user", user).fetchOne();
	}
}

@@ -732,16 +732,40 @@
 .repo;%0A%0A
+import java.util.List;%0A%0A
 import c
@@ -1230,11 +1230,824 @@
 ne();%0A%09%7D
+%0A%0A%09@Override%0A%09public MailFlags save(MailFlags entity) %7B%0A%09%09final MailFlags flags = super.save(entity);%0A%09%09final MailMessage message = flags.getMessage();%0A%09%09final MailMessage root = message.getRoot();%0A%0A%09%09if (flags.getIsStarred() == Boolean.FALSE) %7B%0A%09%09%09// message is root, so unflag children%0A%09%09%09if (root == null) %7B%0A%09%09%09%09List%3CMailFlags%3E childFlags = all().filter(%22self.message.root.id = ?%22, message.getId()).fetch();%0A%09%09%09%09for (MailFlags child : childFlags) %7B%0A%09%09%09%09%09child.setIsStarred(flags.getIsStarred());%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09return flags;%0A%09%09%7D%0A%0A%09%09MailFlags rootFlags = findBy(root, flags.getUser());%0A%09%09if (rootFlags == null) %7B%0A%09%09%09rootFlags = new MailFlags();%0A%09%09%09rootFlags.setMessage(root);%0A%09%09%09rootFlags.setUser(flags.getUser());%0A%09%09%7D%0A%0A%09%09rootFlags.setIsStarred(flags.getIsStarred());%0A%09%09super.save(rootFlags);%0A%0A%09%09return flags;%0A%09%7D
 %0A%7D%0A

Improve important message flag handling