Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EPMRPP-89420 search and filter organizaions #985

Merged
merged 5 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import static com.epam.ta.reportportal.commons.querygen.constant.GeneralCriteriaConstant.CRITERIA_OWNER;
import static com.epam.ta.reportportal.commons.querygen.constant.GeneralCriteriaConstant.CRITERIA_PROJECT;
import static com.epam.ta.reportportal.commons.querygen.constant.GeneralCriteriaConstant.CRITERIA_PROJECT_ID;
import static com.epam.ta.reportportal.commons.querygen.constant.GeneralCriteriaConstant.CRITERIA_SLUG;
import static com.epam.ta.reportportal.commons.querygen.constant.GeneralCriteriaConstant.CRITERIA_START_TIME;
import static com.epam.ta.reportportal.commons.querygen.constant.GeneralCriteriaConstant.CRITERIA_USER_ID;
import static com.epam.ta.reportportal.commons.querygen.constant.IntegrationCriteriaConstant.CRITERIA_INTEGRATION_TYPE;
Expand Down Expand Up @@ -65,12 +66,15 @@
import static com.epam.ta.reportportal.commons.querygen.constant.LogCriteriaConstant.CRITERIA_LOG_PROJECT_ID;
import static com.epam.ta.reportportal.commons.querygen.constant.LogCriteriaConstant.CRITERIA_LOG_TIME;
import static com.epam.ta.reportportal.commons.querygen.constant.LogCriteriaConstant.CRITERIA_TEST_ITEM_ID;
import static com.epam.ta.reportportal.commons.querygen.constant.OrganizationCriteriaConstant.CRITERIA_ORG_CREATED_DATE;
import static com.epam.ta.reportportal.commons.querygen.constant.OrganizationCriteriaConstant.CRITERIA_ORG_TYPE;
import static com.epam.ta.reportportal.commons.querygen.constant.ProjectCriteriaConstant.CRITERIA_ALLOCATED_STORAGE;
import static com.epam.ta.reportportal.commons.querygen.constant.ProjectCriteriaConstant.CRITERIA_PROJECT_ATTRIBUTE_NAME;
import static com.epam.ta.reportportal.commons.querygen.constant.ProjectCriteriaConstant.CRITERIA_PROJECT_CREATION_DATE;
import static com.epam.ta.reportportal.commons.querygen.constant.ProjectCriteriaConstant.CRITERIA_PROJECT_KEY;
import static com.epam.ta.reportportal.commons.querygen.constant.ProjectCriteriaConstant.CRITERIA_PROJECT_NAME;
import static com.epam.ta.reportportal.commons.querygen.constant.ProjectCriteriaConstant.CRITERIA_PROJECT_ORGANIZATION;
import static com.epam.ta.reportportal.commons.querygen.constant.ProjectCriteriaConstant.CRITERIA_PROJECT_ORGANIZATION_ID;
import static com.epam.ta.reportportal.commons.querygen.constant.ProjectCriteriaConstant.CRITERIA_PROJECT_TYPE;
import static com.epam.ta.reportportal.commons.querygen.constant.TestItemCriteriaConstant.CRITERIA_CLUSTER_ID;
import static com.epam.ta.reportportal.commons.querygen.constant.TestItemCriteriaConstant.CRITERIA_DURATION;
Expand Down Expand Up @@ -145,14 +149,14 @@
import com.epam.ta.reportportal.commons.querygen.query.JoinEntity;
import com.epam.ta.reportportal.commons.querygen.query.QuerySupplier;
import com.epam.ta.reportportal.entity.activity.Activity;
import com.epam.ta.reportportal.entity.activity.EventSubject;
import com.epam.ta.reportportal.entity.dashboard.Dashboard;
import com.epam.ta.reportportal.entity.enums.LogLevel;
import com.epam.ta.reportportal.entity.filter.UserFilter;
import com.epam.ta.reportportal.entity.integration.Integration;
import com.epam.ta.reportportal.entity.item.TestItem;
import com.epam.ta.reportportal.entity.launch.Launch;
import com.epam.ta.reportportal.entity.log.Log;
import com.epam.ta.reportportal.entity.organization.Organization;
import com.epam.ta.reportportal.entity.project.Project;
import com.epam.ta.reportportal.entity.project.ProjectInfo;
import com.epam.ta.reportportal.entity.user.User;
Expand Down Expand Up @@ -189,6 +193,9 @@ public enum FilterTarget {
.get(),
new CriteriaHolderBuilder().newBuilder(CRITERIA_PROJECT_ORGANIZATION,
PROJECT.ORGANIZATION, String.class).get(),
new CriteriaHolderBuilder().newBuilder(CRITERIA_SLUG, PROJECT.SLUG, String.class).get(),
new CriteriaHolderBuilder().newBuilder(CRITERIA_PROJECT_ORGANIZATION_ID,
PROJECT.ORGANIZATION_ID, Long.class).get(),
new CriteriaHolderBuilder().newBuilder(CRITERIA_PROJECT_TYPE, PROJECT.PROJECT_TYPE,
String.class).get(),
new CriteriaHolderBuilder().newBuilder(CRITERIA_PROJECT_ATTRIBUTE_NAME,
Expand Down Expand Up @@ -1401,6 +1408,42 @@ protected void joinTables(QuerySupplier query) {
protected Field<Long> idField() {
return FILTER.ID;
}
},

ORGANIZATION_TARGET(Organization.class, Arrays.asList(
new CriteriaHolderBuilder().newBuilder(CRITERIA_ID, ORGANIZATION.ID, Long.class).get(),
new CriteriaHolderBuilder().newBuilder(CRITERIA_NAME, ORGANIZATION.NAME, String.class).get(),
new CriteriaHolderBuilder().newBuilder(CRITERIA_SLUG, ORGANIZATION.SLUG, String.class).get(),
new CriteriaHolderBuilder().newBuilder(CRITERIA_ORG_CREATED_DATE, ORGANIZATION.CREATION_DATE,
Timestamp.class).get(),
new CriteriaHolderBuilder().newBuilder(CRITERIA_ORG_TYPE, ORGANIZATION.ORGANIZATION_TYPE,
String.class).get()
)) {
@Override
protected Collection<? extends SelectField> selectFields() {
return Lists.newArrayList(ORGANIZATION.ID,
ORGANIZATION.ID,
ORGANIZATION.NAME,
ORGANIZATION.SLUG,
ORGANIZATION.CREATION_DATE,
ORGANIZATION.ORGANIZATION_TYPE
);
}

@Override
protected void addFrom(SelectQuery<? extends Record> query) {
query.addFrom(ORGANIZATION);
}

@Override
protected void joinTables(QuerySupplier query) {

}

@Override
protected Field<Long> idField() {
return ORGANIZATION.ID.cast(Long.class);
}
};

public static final String FILTERED_QUERY = "filtered";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public final class GeneralCriteriaConstant {
public static final String CRITERIA_LAUNCH_ID = "launchId";
public static final String CRITERIA_START_TIME = "startTime";
public static final String CRITERIA_END_TIME = "endTime";
public static final String CRITERIA_SLUG = "slug";


private GeneralCriteriaConstant() {
//static only
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2024 EPAM Systems
*
* 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 com.epam.ta.reportportal.commons.querygen.constant;

/**
* Search criteria fields for Organization.
*
* @author Siarhei Hrabko
*/
public final class OrganizationCriteriaConstant {

public static final String CRITERIA_ORG_TYPE = "organization_type";
public static final String CRITERIA_ORG_CREATED_DATE = "creation_date";


private OrganizationCriteriaConstant() {
//static only
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public final class ProjectCriteriaConstant {
public static final String CRITERIA_PROJECT_SLUG = "slug";
public static final String CRITERIA_ALLOCATED_STORAGE = "allocatedStorage";
public static final String CRITERIA_PROJECT_ORGANIZATION = "organization";
public static final String CRITERIA_PROJECT_ORGANIZATION_ID = "organization_id";
public static final String CRITERIA_PROJECT_CREATION_DATE = "creationDate";
public static final String CRITERIA_PROJECT_ATTRIBUTE_NAME = "attributeName";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,24 @@

package com.epam.ta.reportportal.dao.organization;

import com.epam.ta.reportportal.dao.ReportPortalRepository;
import com.epam.ta.reportportal.dao.FilterableRepository;
import com.epam.ta.reportportal.entity.organization.Organization;

import java.util.List;
import java.util.Optional;

/**
* Repository interface for searching and filtering organization records.
*
* @author Siarhei Hrabko
*/
public interface OrganizationRepository extends ReportPortalRepository<Organization, Long> {

/**
* @param name name of organization
* @return {@link Optional} of {@link Organization}
*/
Optional<Organization> findByName(String name);

/**
* @param slug slug name of organization
* @return {@link Optional} of {@link Organization}
*/
Optional<Organization> findBySlug(String slug);
public interface OrganizationRepositoryCustom extends FilterableRepository<Organization> {

List<Organization> findAllByUserLogin(String login);

Optional<Organization> findById(Long orgId);

Optional<Organization> findOrganizationByName(String name);

Optional<Organization> findOrganizationBySlug(String slug);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright 2024 EPAM Systems
*
* 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 com.epam.ta.reportportal.dao.organization;

import static com.epam.ta.reportportal.dao.util.QueryUtils.collectJoinFields;
import static com.epam.ta.reportportal.dao.util.ResultFetchers.ORGANIZATION_FETCHER;
import static com.epam.ta.reportportal.jooq.Tables.ORGANIZATION;
import static com.epam.ta.reportportal.jooq.Tables.ORGANIZATION_USER;
import static com.epam.ta.reportportal.jooq.Tables.USERS;

import com.epam.ta.reportportal.commons.querygen.QueryBuilder;
import com.epam.ta.reportportal.commons.querygen.Queryable;
import com.epam.ta.reportportal.entity.organization.Organization;
import java.util.List;
import java.util.Optional;
import org.jooq.DSLContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Repository;

/**
* Repository implementation class for searching and filtering organization records.
*
* @author Siarhei Hrabko
*/
@Repository
public class OrganizationRepositoryCustomImpl implements OrganizationRepositoryCustom {

@Autowired
private DSLContext dsl;

@Override
public List<Organization> findByFilter(Queryable filter) {
return ORGANIZATION_FETCHER.apply(
dsl.fetch(QueryBuilder.newBuilder(filter, collectJoinFields(filter))
.wrap()
.build()));
}

@Override
public Page<Organization> findByFilter(Queryable filter, Pageable pageable) {
return PageableExecutionUtils.getPage(
ORGANIZATION_FETCHER.apply(
dsl.fetch(QueryBuilder.newBuilder(filter, collectJoinFields(filter, pageable.getSort()))
.with(pageable)
.wrap()
.withWrapperSort(pageable.getSort())
.build())),
pageable,
() -> dsl.fetchCount(QueryBuilder.newBuilder(filter).build())
);
}


@Override
public List<Organization> findAllByUserLogin(String login) {
return ORGANIZATION_FETCHER.apply(dsl.select(ORGANIZATION.fields())
.from(ORGANIZATION)
.join(ORGANIZATION_USER)
.on(ORGANIZATION.ID.eq(ORGANIZATION_USER.ORGANIZATION_ID))
.join(USERS)
.on(ORGANIZATION_USER.USER_ID.eq(USERS.ID))
.where(USERS.LOGIN.eq(login))
.fetch());
}

@Override
public Optional<Organization> findById(Long orgId) {
return dsl.select()
.from(ORGANIZATION)
.where(ORGANIZATION.ID.eq(orgId))
.fetchOptionalInto(Organization.class);
}

@Override
public Optional<Organization> findOrganizationByName(String name) {
return dsl.select()
.from(ORGANIZATION)
.where(ORGANIZATION.NAME.eq(name))
.fetchOptionalInto(Organization.class);
}

@Override
public Optional<Organization> findOrganizationBySlug(String slug) {
return dsl.select()
.from(ORGANIZATION)
.where(ORGANIZATION.SLUG.eq(slug))
.fetchOptionalInto(Organization.class);
}

}
14 changes: 10 additions & 4 deletions src/main/java/com/epam/ta/reportportal/dao/util/RecordMappers.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,15 @@ public class RecordMappers {
return project;
};

/**
* Maps record into {@link Organization} object
*/
public static final RecordMapper<? super Record, Organization> ORGANIZATION_MAPPER = row -> {
Organization project = row.into(Organization.class);

return project;
};

/**
* Maps record into {@link TestItemResults} object
*/
Expand Down Expand Up @@ -391,16 +400,13 @@ public class RecordMappers {
ProjectUser projectUser = new ProjectUser();
projectUser.setProjectRole(r.into(PROJECT_USER.PROJECT_ROLE).into(ProjectRole.class));

Organization organization = new Organization();
organization.setId(r.get(ORGANIZATION.ID));

Project project = new Project();
project.setId(r.get(PROJECT_USER.PROJECT_ID));
project.setName(r.get(PROJECT.NAME));
project.setKey(r.get(PROJECT.KEY));
project.setSlug(r.get(PROJECT.SLUG));
project.setProjectType(ProjectType.valueOf(r.get(PROJECT.PROJECT_TYPE)));
project.setOrganization(organization);
project.setOrganizationId(r.get(ORGANIZATION.ID));

User user = new User();
user.setLogin(r.get(USERS.LOGIN));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import com.epam.ta.reportportal.entity.item.TestItem;
import com.epam.ta.reportportal.entity.launch.Launch;
import com.epam.ta.reportportal.entity.log.Log;
import com.epam.ta.reportportal.entity.organization.Organization;
import com.epam.ta.reportportal.entity.pattern.PatternTemplateTestItem;
import com.epam.ta.reportportal.entity.project.Project;
import com.epam.ta.reportportal.entity.project.ProjectAttribute;
Expand Down Expand Up @@ -121,6 +122,25 @@ private ResultFetchers() {
return new ArrayList<>(projects.values());
};

/**
* Fetches records from db results into list of {@link Organization} objects.
*/
public static final Function<Result<? extends Record>, List<Organization>> ORGANIZATION_FETCHER = records -> {
Map<Long, Organization> orgs = Maps.newLinkedHashMap();
records.forEach(record -> {
Long id = record.get(PROJECT.ID);
Organization organization;
if (!orgs.containsKey(id)) {
organization = RecordMappers.ORGANIZATION_MAPPER.map(record);
} else {
organization = orgs.get(id);
}
orgs.put(id, organization);
});

return new ArrayList<>(orgs.values());
};

/**
* Fetches records from db results into list of {@link Launch} objects.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,8 @@ public class Project implements Serializable {
@Column(name = "organization")
private String org;

@ManyToOne(fetch = FetchType.LAZY)
@MapsId("organization_id")
private Organization organization;
@Column(name = "organization_id", nullable = false)
private Long organizationId;

@Column(name = "key")
private String key;
Expand Down
Loading
Loading