/**
 * Axelor Business Solutions
 *
 * Copyright (C) 2005-2017 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.team.db.repo;

import java.util.Map;

import com.axelor.auth.AuthUtils;
import com.axelor.db.JpaRepository;
import com.axelor.inject.Beans;
import com.axelor.mail.db.MailFollower;
import com.axelor.mail.db.repo.MailFollowerRepository;
import com.axelor.team.db.Team;

public class TeamRepository extends JpaRepository<Team> {

	public TeamRepository() {
		super(Team.class);
	}

	public Team findByName(String name) {
		return all().filter("self.name = :name")
				.bind("name", name)
				.fetchOne();
	}

	@Override
	public Team save(Team entity) {
		final Team team = super.save(entity);
		final MailFollowerRepository followers = Beans.get(MailFollowerRepository.class);
		
		if (team.getMembers() != null) {
			team.getMembers().forEach(user -> followers.follow(team, user));
		}

		return team;
	}
	
	@Override
	public Map<String, Object> populate(Map<String, Object> json, Map<String, Object> context) {
		if (json == null || json.get("id") == null) {
			return json;
		}

		final Team entity = find((Long) json.get("id"));
		if (entity == null) {
			return json;
		}

		final MailFollowerRepository followers = Beans.get(MailFollowerRepository.class);
		final MailFollower follower = followers.findOne(entity, AuthUtils.getUser());

		json.put("_following", follower != null && follower.getArchived() == Boolean.FALSE);
		json.put("_image", entity.getImage() != null);

		return json;
	}
}

@@ -766,33 +766,97 @@
 ort 
-com.axelor.auth.AuthUtils
+javax.persistence.TypedQuery;%0A%0Aimport com.axelor.auth.AuthUtils;%0Aimport com.axelor.db.JPA
 ;%0Aim
@@ -1568,23 +1568,810 @@
 %0A%09%09%7D
-%0A%0A%09%09return team
+ else %7B%0A%09%09%09followers.findAll(entity).forEach(followers::unfollow);%0A%09%09%7D%0A%09%09%0A%09%09final TypedQuery%3CMailFollower%3E query = JPA.em().createQuery(%22%22%0A%09%09%09%09+ %22SELECT f FROM MailFollower f %22%0A%09%09%09%09+ %22LEFT JOIN f.user u %22%0A%09%09%09%09+ %22WHERE f.relatedModel = :model %22%0A%09%09%09%09+ %22%09AND f.relatedId = :id %22%0A%09%09%09%09+ %22%09AND u.id NOT IN %22%0A%09%09%09%09+ %22%09%09(SELECT x.id FROM Team t LEFT JOIN t.members x WHERE t.id = :id)%22, MailFollower.class);%0A%0A%09%09query.setParameter(%22model%22, Team.class.getName());%0A%09%09query.setParameter(%22id%22, team.getId());%0A%09%09query.getResultList().forEach(f -%3E followers.unfollow(team, f.getUser()));%0A%0A%09%09return team;%0A%09%7D%0A%0A%09@Override%0A%09public void remove(Team entity) %7B%0A%09%09final MailFollowerRepository followers = Beans.get(MailFollowerRepository.class);%0A%09%09followers.findAll(entity).forEach(followers::unfollow);%0A%09%09super.remove(entity)
 ;%0A%09%7D

Fix team subscription menus not removed