Commit 617b84be authored by Matija Obreza's avatar Matija Obreza
Browse files

Team entities

parent d854e338
/**
* Copyright 2013 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.model;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;
@MappedSuperclass
public abstract class VersionedAuditedModel extends AuditedModel {
private static final long serialVersionUID = 8415401512360230669L;
@Version
private long version=0;
public long getVersion() {
return version;
}
public void setVersion(long version) {
this.version = version;
}
}
/**
* Copyright 2013 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.model.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import org.genesys2.server.model.VersionedAuditedModel;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Store;
/**
* A team is a group of users, allowed to manage data in the system.
*
* @author matijaobreza
*/
@Entity
@Table(uniqueConstraints = { @UniqueConstraint(name = "team_UC_name", columnNames = { "name" }) })
public class Team extends VersionedAuditedModel {
private static final long serialVersionUID = -6992621329254944604L;
@Column(nullable = false, unique = true)
@Field(name = "name", store = Store.NO)
private String name;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "teammember", joinColumns = @JoinColumn(name = "teamId"), inverseJoinColumns=@JoinColumn(name="userId"))
private Set<User> members;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "teaminstitute", joinColumns = @JoinColumn(name = "teamId"), inverseJoinColumns = @JoinColumn(name = "instituteId"))
@OrderBy("code")
private List<FaoInstitute> institutes = new ArrayList<FaoInstitute>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* Get {@link User}s that are members of this team.
*
* @return
*/
public Set<User> getMembers() {
return members;
}
public void setMembers(Set<User> members) {
this.members = members;
}
/**
* Get {@link FaoInstitute}s that this {@link Team} is allowed to manage.
*
* @return
*/
public List<FaoInstitute> getInstitutes() {
return institutes;
}
public void setInstitutes(List<FaoInstitute> institutes) {
this.institutes = institutes;
}
}
package org.genesys2.server.model.impl;
public class TeamMember {
}
/**
* Copyright 2013 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.persistence.domain;
import java.util.List;
import org.genesys2.server.model.impl.Team;
import org.genesys2.server.model.impl.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
public interface TeamRepository extends JpaRepository<Team, Long> {
@Query("select t from Team t where ?1 member of t.members")
List<Team> listForUser(User user);
}
/**
* Copyright 2013 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.service;
import java.util.List;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.Team;
import org.genesys2.server.model.impl.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
public interface TeamService {
/**
* Add team to the system
*
* @param name
* a unique {@link Team} name
* @return a Team
*/
Team addTeam(String name);
/**
* Add a User to the team
*
* @param team
* @param user
* @return
*/
@PreAuthorize("hasRole('ADMINISTRATOR'")
Team addTeamMember(Team team, User user);
/**
* Remove User from the Team
*
* @param team
* @param user
*/
@PreAuthorize("hasRole('ADMINISTRATOR'")
Team removeTeamMember(Team team, User user);
/**
* Remove current user from team
*
* @param team
*/
void removeMe(Team team);
@PreAuthorize("hasRole('ADMINISTRATOR'")
Team addTeamInstitute(Team team, FaoInstitute institute);
@PreAuthorize("hasRole('ADMINISTRATOR'")
Team removeTeamInsitute(Team team, FaoInstitute institute);
/**
* List user's teams
*
* @return
*/
List<Team> listMyTeams();
/**
* List all teams
*
* @return
*/
@PreAuthorize("hasRole('ADMINISTRATOR'")
Page<Team> listTeams(Pageable pageable);
}
/**
* Copyright 2013 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.service.impl;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.Team;
import org.genesys2.server.model.impl.User;
import org.genesys2.server.persistence.domain.TeamRepository;
import org.genesys2.server.security.AuthUserDetails;
import org.genesys2.server.service.TeamService;
import org.genesys2.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(readOnly = true)
public class TeamServiceImpl implements TeamService {
private static final Log LOG = LogFactory.getLog(TeamServiceImpl.class);
@Autowired
private TeamRepository teamRepository;
@Autowired
private UserService userService;
@Override
@Transactional(readOnly = false)
public Team addTeam(String name) {
User user = getCurrentUser();
Team team = new Team();
team.setName(name);
Set<User> member1 = new HashSet<User>();
member1.add(user);
team.setMembers(member1);
teamRepository.save(team);
LOG.info("New team created " + team);
return team;
}
@Override
@Transactional(readOnly = false)
@PreAuthorize("hasRole('ADMINISTRATOR'")
public Team addTeamMember(Team team, User user) {
if (team.getMembers().contains(user)) {
LOG.info("User already member of this team");
return team;
}
team.getMembers().add(user);
teamRepository.save(team);
LOG.info("Added user to " + team);
return team;
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR'")
@Transactional(readOnly = false)
public Team removeTeamMember(Team team, User user) {
if (team.getMembers().remove(user)) {
teamRepository.save(team);
LOG.info("Removed " + user + " from " + team);
}
LOG.warn("Could not remove user from team.");
return team;
}
@Override
@Transactional(readOnly = false)
public void removeMe(Team team) {
User user = getCurrentUser();
boolean removed = team.getMembers().remove(user);
if (removed) {
teamRepository.save(team);
LOG.info("Removed " + user + " from " + team);
}
LOG.warn("Could not remove user from team.");
}
private User getCurrentUser() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
LOG.warn("No authentication in security context.");
throw new RuntimeException("No authentication object in security context");
}
if (authentication.getPrincipal() instanceof AuthUserDetails) {
return ((AuthUserDetails) authentication.getPrincipal()).getUser();
}
throw new RuntimeException("No User object in security context");
}
@Override
@Transactional(readOnly = false)
@PreAuthorize("hasRole('ADMINISTRATOR'")
public Team addTeamInstitute(Team team, FaoInstitute institute) {
if (team.getInstitutes().contains(institute)) {
LOG.info("Institute already assigned to this team");
} else {
team.getInstitutes().add(institute);
teamRepository.save(team);
LOG.info("Added teams to " + team);
}
return team;
}
@Override
@Transactional(readOnly = false)
@PreAuthorize("hasRole('ADMINISTRATOR'")
public Team removeTeamInsitute(Team team, FaoInstitute institute) {
if (team.getInstitutes().contains(institute)) {
team.getInstitutes().remove(institute);
teamRepository.save(team);
LOG.info("Removed institute from " + team);
}
return team;
}
@Override
public List<Team> listMyTeams() {
User user = getCurrentUser();
return teamRepository.listForUser(user);
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR'")
public Page<Team> listTeams(Pageable pageable) {
return teamRepository.findAll(pageable);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment