Search in sources :

Example 1 with RelatedProjectJoin

use of ca.corefacility.bioinformatics.irida.model.joins.impl.RelatedProjectJoin in project irida by phac-nml.

the class ProjectSettingsAssociatedProjectsController method getPotentialAssociatedProjects.

/**
 * Get {@link Project}s that could be associated with this project
 *
 * @param projectId
 * 		The current project ID
 * @param principal
 * 		The logged in user
 * @param page
 * 		The page to request
 * @param count
 * 		The number of elements in the page
 * @param sortedBy
 * 		The property to sort by
 * @param sortDir
 * 		The direction to sort in
 * @param projectName
 * 		The project name to search for
 *
 * @return A {@code Map<String,Object>} of elements for a datatable
 */
@RequestMapping("/ajax/available")
@PreAuthorize("hasRole('ROLE_ADMIN') or hasPermission(#projectId, 'isProjectOwner')")
@ResponseBody
public Map<String, Object> getPotentialAssociatedProjects(@PathVariable Long projectId, final Principal principal, @RequestParam Integer page, @RequestParam Integer count, @RequestParam String sortedBy, @RequestParam String sortDir, @RequestParam(value = "name", required = false, defaultValue = "") String projectName) {
    Project project = projectService.read(projectId);
    Sort.Direction sortDirection = sortDir.equals("asc") ? Sort.Direction.ASC : Sort.Direction.DESC;
    List<RelatedProjectJoin> relatedProjectJoins = projectService.getRelatedProjects(project);
    List<Project> projects;
    long totalElements;
    int totalPages;
    final Page<Project> search = projectService.getUnassociatedProjects(project, projectName, page, count, sortDirection, sortedBy);
    totalElements = search.getTotalElements();
    totalPages = search.getTotalPages();
    projects = search.getContent();
    Map<String, Object> map = getProjectsDataMap(projects, relatedProjectJoins);
    map.put("totalAssociated", totalElements);
    map.put("totalPages", totalPages);
    return map;
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) Sort(org.springframework.data.domain.Sort) RelatedProjectJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.RelatedProjectJoin) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with RelatedProjectJoin

use of ca.corefacility.bioinformatics.irida.model.joins.impl.RelatedProjectJoin in project irida by phac-nml.

the class ProjectSettingsAssociatedProjectsController method getAssociatedProjectsForProject.

/**
 * Find all projects that have been associated with a project.
 *
 * @param currentProject
 * 		The project to find the associated projects of.
 * @param currentUser
 * 		The currently logged in user.
 *
 * @return List of Maps containing information about the associated projects.
 */
private List<Map<String, String>> getAssociatedProjectsForProject(Project currentProject, User currentUser, boolean isAdmin) {
    List<RelatedProjectJoin> relatedProjectJoins = projectService.getRelatedProjects(currentProject);
    List<Map<String, String>> projects = new ArrayList<>();
    for (RelatedProjectJoin rpj : relatedProjectJoins) {
        Project project = rpj.getObject();
        Map<String, String> map = new HashMap<>();
        map.put("name", project.getLabel());
        map.put("id", project.getId().toString());
        map.put("auth", "authorized");
        projects.add(map);
    }
    return projects;
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RelatedProjectJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.RelatedProjectJoin) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with RelatedProjectJoin

use of ca.corefacility.bioinformatics.irida.model.joins.impl.RelatedProjectJoin in project irida by phac-nml.

the class ProjectServiceImplTest method testRemoveRelatedProject.

@Test
public void testRemoveRelatedProject() {
    RelatedProjectJoin join = new RelatedProjectJoin();
    projectService.removeRelatedProject(join);
    verify(relatedProjectRepository).delete(join);
}
Also used : RelatedProjectJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.RelatedProjectJoin) Test(org.junit.Test)

Example 4 with RelatedProjectJoin

use of ca.corefacility.bioinformatics.irida.model.joins.impl.RelatedProjectJoin in project irida by phac-nml.

the class ProjectSettingsAssociatedProjectsControllerTest method testGetAssociatedProjectsPage.

@Test
public void testGetAssociatedProjectsPage() {
    ExtendedModelMap model = new ExtendedModelMap();
    Principal principal = () -> USER_NAME;
    Long projectId = 1L;
    User u = new User();
    u.setSystemRole(Role.ROLE_ADMIN);
    Project p = new Project("my project");
    p.setId(projectId);
    Project o = new Project("other project");
    o.setId(2L);
    List<RelatedProjectJoin> relatedProjects = Lists.newArrayList(new RelatedProjectJoin(p, o));
    when(projectService.read(projectId)).thenReturn(p);
    when(userService.getUserByUsername(USER_NAME)).thenReturn(u);
    when(projectService.getRelatedProjects(p)).thenReturn(relatedProjects);
    controller.getAssociatedProjectsPage(projectId, model, principal);
    assertTrue(model.containsAttribute("isAdmin"));
    assertTrue(model.containsAttribute("associatedProjects"));
    verify(projectService).read(projectId);
    verify(userService, times(2)).getUserByUsername(USER_NAME);
    verify(projectService).getRelatedProjects(p);
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) User(ca.corefacility.bioinformatics.irida.model.user.User) RelatedProjectJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.RelatedProjectJoin) Principal(java.security.Principal) Test(org.junit.Test)

Example 5 with RelatedProjectJoin

use of ca.corefacility.bioinformatics.irida.model.joins.impl.RelatedProjectJoin in project irida by phac-nml.

the class ProjectSettingsAssociatedProjectsControllerTest method testGetPotentialAssociatedProjectsAsAdmin.

@SuppressWarnings("unchecked")
@Test
public void testGetPotentialAssociatedProjectsAsAdmin() {
    Long projectId = 1L;
    Principal principal = () -> USER_NAME;
    int page = 1;
    int count = 10;
    String sortedBy = "id";
    String sortDir = "asc";
    String projectName = "";
    Project p1 = new Project("p1");
    when(projectService.read(projectId)).thenReturn(p1);
    User user = new User();
    user.setSystemRole(Role.ROLE_ADMIN);
    when(userService.getUserByUsername(USER_NAME)).thenReturn(user);
    // (specification, page, count, sortDirection, sortedBy);
    Project p2 = new Project("p2");
    p2.setId(2L);
    Project p3 = new Project("p3");
    p3.setId(3L);
    List<RelatedProjectJoin> relatedJoins = Lists.newArrayList(new RelatedProjectJoin(p1, p2));
    when(projectService.getRelatedProjects(p1)).thenReturn(relatedJoins);
    Page<Project> projectPage = new PageImpl<>(Lists.newArrayList(p2, p3));
    when(projectService.getUnassociatedProjects(p1, projectName, page, count, Direction.ASC, sortedBy)).thenReturn(projectPage);
    Map<String, Object> potentialAssociatedProjects = controller.getPotentialAssociatedProjects(projectId, principal, page, count, sortedBy, sortDir, projectName);
    assertTrue(potentialAssociatedProjects.containsKey("associated"));
    List<Map<String, String>> associated = (List<Map<String, String>>) potentialAssociatedProjects.get("associated");
    assertEquals(2, associated.size());
    for (Map<String, String> pmap : associated) {
        if (pmap.get("id").equals("2")) {
            assertTrue(pmap.containsKey("associated"));
        }
    }
    verify(projectService).read(projectId);
    verify(projectService).getRelatedProjects(p1);
    verify(projectService).getUnassociatedProjects(p1, projectName, page, count, Direction.ASC, sortedBy);
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) User(ca.corefacility.bioinformatics.irida.model.user.User) Project(ca.corefacility.bioinformatics.irida.model.project.Project) RelatedProjectJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.RelatedProjectJoin) List(java.util.List) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) Principal(java.security.Principal) Test(org.junit.Test)

Aggregations

RelatedProjectJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.RelatedProjectJoin)16 Project (ca.corefacility.bioinformatics.irida.model.project.Project)14 Test (org.junit.Test)10 User (ca.corefacility.bioinformatics.irida.model.user.User)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Principal (java.security.Principal)3 Map (java.util.Map)3 WithMockUser (org.springframework.security.test.context.support.WithMockUser)3 ExtendedModelMap (org.springframework.ui.ExtendedModelMap)3 List (java.util.List)2 PageImpl (org.springframework.data.domain.PageImpl)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Sort (org.springframework.data.domain.Sort)1 Transactional (org.springframework.transaction.annotation.Transactional)1