Search in sources :

Example 1 with ApplicationCriteria

use of io.gravitee.repository.management.api.search.ApplicationCriteria in project gravitee-management-rest-api by gravitee-io.

the class ApplicationServiceImpl method findByNameAndStatus.

@Override
public Set<ApplicationListItem> findByNameAndStatus(String userName, String name, String status) {
    LOGGER.debug("Find applications by name {} and status {}", name, status);
    try {
        if (name == null || name.trim().isEmpty()) {
            return emptySet();
        }
        // find applications where the user is a member
        Set<String> appIds = membershipService.getMembershipsByMemberAndReference(MembershipMemberType.USER, userName, MembershipReferenceType.APPLICATION).stream().map(MembershipEntity::getReferenceId).collect(toSet());
        // find user groups
        List<String> groupIds = membershipService.getMembershipsByMemberAndReference(MembershipMemberType.USER, userName, MembershipReferenceType.GROUP).stream().filter(m -> m.getRoleId() != null && roleService.findById(m.getRoleId()).getScope().equals(RoleScope.APPLICATION)).map(MembershipEntity::getReferenceId).collect(toList());
        appIds.addAll(this.findByGroups(groupIds).stream().map(ApplicationListItem::getId).collect(toSet()));
        ApplicationCriteria criteria = new ApplicationCriteria.Builder().status(ApplicationStatus.valueOf(status)).name(name.trim()).environmentIds(singletonList(GraviteeContext.getCurrentEnvironment())).ids(appIds.toArray(new String[0])).build();
        Page<Application> applications = applicationRepository.search(criteria, null);
        return ApplicationStatus.ACTIVE.equals(status) ? convertToList(new HashSet<>(applications.getContent())) : convertToSimpleList(new HashSet<>(applications.getContent()));
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to find applications for name {}", name, ex);
        throw new TechnicalManagementException("An error occurs while trying to find applications for name " + name, ex);
    }
}
Also used : ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) ApplicationCriteria(io.gravitee.repository.management.api.search.ApplicationCriteria) UuidString(io.gravitee.rest.api.service.common.UuidString)

Example 2 with ApplicationCriteria

use of io.gravitee.repository.management.api.search.ApplicationCriteria in project gravitee-management-rest-api by gravitee-io.

the class ApplicationServiceImpl method findByOrganization.

@Override
public Set<ApplicationListItem> findByOrganization(String organizationId) {
    LOGGER.debug("Find applications by organization {} ", organizationId);
    try {
        if (organizationId == null || organizationId.trim().isEmpty()) {
            return emptySet();
        }
        final List<String> environmentIds = environmentService.findByOrganization(organizationId).stream().map(EnvironmentEntity::getId).collect(toList());
        ApplicationCriteria criteria = new ApplicationCriteria.Builder().environmentIds(environmentIds).build();
        Page<Application> applications = applicationRepository.search(criteria, null);
        return convertToSimpleList(new HashSet<>(applications.getContent()));
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to find applications for organization {}", organizationId, ex);
        throw new TechnicalManagementException("An error occurs while trying to find applications for organization {}" + organizationId, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) ApplicationCriteria(io.gravitee.repository.management.api.search.ApplicationCriteria) UuidString(io.gravitee.rest.api.service.common.UuidString)

Example 3 with ApplicationCriteria

use of io.gravitee.repository.management.api.search.ApplicationCriteria in project gravitee-management-rest-api by gravitee-io.

the class ApplicationService_FindByNameTest method shouldNotFindByName.

@Test
public void shouldNotFindByName() throws Exception {
    when(membershipService.getMembershipsByMemberAndReference(any(), any(), any())).thenReturn(Collections.emptySet());
    when(applicationRepository.search(any(), any())).thenReturn(new Page<>(Collections.emptyList(), 0, 0, 0));
    Set<ApplicationListItem> set = applicationService.findByNameAndStatus(null, "a", "ACTIVE");
    assertNotNull(set);
    assertEquals("result is empty", 0, set.size());
    ArgumentCaptor<ApplicationCriteria> queryCaptor = ArgumentCaptor.forClass(ApplicationCriteria.class);
    Mockito.verify(applicationRepository).search(queryCaptor.capture(), any());
    final ApplicationCriteria query = queryCaptor.getValue();
    assertEquals("a", query.getName());
}
Also used : ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApplicationCriteria(io.gravitee.repository.management.api.search.ApplicationCriteria) Test(org.junit.Test)

Aggregations

ApplicationCriteria (io.gravitee.repository.management.api.search.ApplicationCriteria)3 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 ApplicationListItem (io.gravitee.rest.api.model.application.ApplicationListItem)2 UuidString (io.gravitee.rest.api.service.common.UuidString)2 Test (org.junit.Test)1