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

Changes related to "Modify Create Program endpoint to require Data Center id" #430

Merged
merged 9 commits into from
Nov 15, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public ResponseEntity<List<DataCenterDTO>> listDataCenters(
}

@GetMapping(value = "/{datacenter_short_name}/programs")

public ResponseEntity<ProgramsResponseDTO> listDataCenterPrograms(
@Parameter(hidden = true) @RequestHeader(value = "Authorization", required = true)
final String authorization,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public ResponseEntity<CreateProgramResponseDTO> createProgram(
grpc2JsonConverter.fromJson(
grpc2JsonConverter.getJsonFromObject(createProgramRequestDTO),
CreateProgramRequest.class);
CreateProgramResponse response = serviceFacade.createProgram(request);
CreateProgramResponse response =
serviceFacade.createProgram(request, createProgramRequestDTO.getDataCenterId());
return new ResponseEntity(
grpc2JsonConverter.prepareCreateProgramResponse(response), HttpStatus.CREATED);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.icgc.argo.program_service.model.exceptions.BadRequestException;
import org.icgc.argo.program_service.model.exceptions.ForbiddenException;
import org.icgc.argo.program_service.model.exceptions.NotFoundException;
import org.icgc.argo.program_service.model.exceptions.UnauthorizedException;
import org.icgc.argo.program_service.model.exceptions.*;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
Expand Down Expand Up @@ -79,4 +76,19 @@ public ResponseEntity<Object> handleBadRequestException(
new HttpHeaders(),
BAD_REQUEST);
}

@ExceptionHandler(RecordNotFoundException.class)
public ResponseEntity<Object> handleRecordNotFoundException(
HttpServletRequest req, RecordNotFoundException ex) {
val message = ex.getMessage();
log.error(message);
return new ResponseEntity<Object>(
Map.of(
"message", ex.getMessage(),
"timestamp", new Date(),
"path", req.getServletPath(),
"error", BAD_REQUEST.getReasonPhrase()),
new HttpHeaders(),
BAD_REQUEST);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.icgc.argo.program_service.model.dto;

import java.util.List;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -13,5 +14,6 @@
public class CreateProgramRequestDTO {

private Program program;
private UUID dataCenterId;
List<UserDTO> admins;
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
/*
* Copyright (c) 2023. The Ontario Institute for Cancer Research. All rights reserved.
* Copyright (c) 2023 The Ontario Institute for Cancer Research. All rights reserved
*
* 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
* This program and the accompanying materials are made available under the terms of the GNU Affero General Public License v3.0.
* 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/>.
*
* http://www.apache.org/licenses/LICENSE-2.0
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* 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.icgc.argo.program_service.model.exceptions;

import static org.springframework.http.HttpStatus.BAD_REQUEST;

import lombok.NonNull;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(BAD_REQUEST)
public class BadRequestException extends RuntimeException {

public BadRequestException(@NonNull String message) {
super(message);
}


public static void checkNotFound(
boolean expression, @NonNull String message, @NonNull Object... args) {
if (!expression) {
throw new BadRequestException(String.format(message, args));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
/*
* Copyright (c) 2019. The Ontario Institute for Cancer Research. All rights reserved.
* Copyright (c) 2023 The Ontario Institute for Cancer Research. All rights reserved
*
* 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
* This program and the accompanying materials are made available under the terms of the GNU Affero General Public License v3.0.
* 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/>.
*
* http://www.apache.org/licenses/LICENSE-2.0
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* 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.
*
*/

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2023 The Ontario Institute for Cancer Research. All rights reserved
*
* This program and the accompanying materials are made available under the terms of the GNU Affero General Public License v3.0.
* 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/>.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
*/

package org.icgc.argo.program_service.model.exceptions;

import lombok.NonNull;

public class ProgramRuntimeException extends RuntimeException {

public ProgramRuntimeException(@NonNull String message) {
super(message);
}


public static void checkNotFound(
boolean expression, @NonNull String message, @NonNull Object... args) {
if (!expression) {
throw new ProgramRuntimeException(String.format(message, args));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2020 The Ontario Institute for Cancer Research. All rights reserved
*
* This program and the accompanying materials are made available under the terms of the GNU Affero General Public License v3.0.
* 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/>.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
*/

package org.icgc.argo.program_service.model.exceptions;

import lombok.NonNull;

public class RecordNotFoundException extends RuntimeException {

public RecordNotFoundException(@NonNull String message) {
super(message);
}

public static void checkNotFound(
boolean expression, @NonNull String message, @NonNull Object... args) {
if (!expression) {
throw new RecordNotFoundException(String.format(message, args));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
/*
* Copyright (c) 2019. The Ontario Institute for Cancer Research. All rights reserved.
* Copyright (c) 2023 The Ontario Institute for Cancer Research. All rights reserved
*
* 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
* This program and the accompanying materials are made available under the terms of the GNU Affero General Public License v3.0.
* 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/>.
*
* http://www.apache.org/licenses/LICENSE-2.0
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* 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.
*
*/

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
/*
* Copyright (c) 2017. The Ontario Institute for Cancer Research. All rights reserved.
* Copyright (c) 2023 The Ontario Institute for Cancer Research. All rights reserved
*
* 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
* This program and the accompanying materials are made available under the terms of the GNU Affero General Public License v3.0.
* 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/>.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* 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.icgc.argo.program_service.security;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,23 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.Predicate;
import javax.validation.ValidatorFactory;

import io.netty.util.internal.StringUtil;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.lang.StringUtils;
import org.icgc.argo.program_service.converter.DataCenterConverter;
import org.icgc.argo.program_service.converter.ProgramConverter;
import org.icgc.argo.program_service.model.dto.DataCenterRequestDTO;
import org.icgc.argo.program_service.model.entity.*;
import org.icgc.argo.program_service.model.exceptions.BadRequestException;
import org.icgc.argo.program_service.model.exceptions.NotFoundException;
import org.icgc.argo.program_service.model.exceptions.RecordNotFoundException;
import org.icgc.argo.program_service.model.join.*;
import org.icgc.argo.program_service.proto.Program;
import org.icgc.argo.program_service.repositories.*;
Expand Down Expand Up @@ -159,16 +165,63 @@ public ProgramEntity getProgram(@NonNull String name, boolean allowInactive) {
}

public ProgramEntity createWithSideEffect(
@NonNull Program program, Consumer<ProgramEntity> consumer) {
@NonNull Program program, Consumer<ProgramEntity> consumer) {
val programEntity = createProgram(program);
consumer.accept(programEntity);
return programEntity;
}

public ProgramEntity createWithSideEffect(
@NonNull Program program, Consumer<ProgramEntity> consumer, UUID dataCenterId) {
val programEntity = createProgram(program, dataCenterId);
consumer.accept(programEntity);
return programEntity;
}
public ProgramEntity createProgram(@NonNull Program program)
throws DataIntegrityViolationException {
val programEntity = programConverter.programToProgramEntity(program);

val now = LocalDateTime.now(ZoneId.of("UTC"));
programEntity.setCreatedAt(now);
programEntity.setUpdatedAt(now);

val p = programRepository.save(programEntity);
val cancers = cancerRepository.findAllByNameIn(program.getCancerTypesList());
val primarySites = primarySiteRepository.findAllByNameIn(program.getPrimarySitesList());
val countries = countryRepository.findAllByNameIn(program.getCountriesList());
List<InstitutionEntity> institutions =
institutionRepository.findAllByNameIn(program.getInstitutionsList());
if (institutions.size() != program.getInstitutionsList().size()) {
institutions = filterAndAddInstitutions(program.getInstitutionsList());
}

List<ProgramCancer> programCancers = mapToList(cancers, x -> createProgramCancer(p, x).get());
List<ProgramPrimarySite> programPrimarySites =
mapToList(primarySites, x -> createProgramPrimarySite(p, x).get());
List<ProgramInstitution> programInstitutions =
mapToList(institutions, x -> createProgramInstitution(p, x).get());
List<ProgramCountry> programCountries =
mapToList(countries, x -> createProgramCountry(p, x).get());

programCancerRepository.saveAll(programCancers);
programPrimarySiteRepository.saveAll(programPrimarySites);
programInstitutionRepository.saveAll(programInstitutions);
programCountryRepository.saveAll(programCountries);

return programEntity;
}
public ProgramEntity createProgram(@NonNull Program program, UUID dataCenterId)
throws DataIntegrityViolationException {
val programEntity = programConverter.programToProgramEntity(program);

if (dataCenterId == null) {
throw new BadRequestException("DataCenterId cannot be null or empty");
}
val dataCenterEntity = dataCenterRepository.findById(dataCenterId);
if (dataCenterEntity.isEmpty()) {
throw new RecordNotFoundException("DataCenterId '" + dataCenterId + "' not found");
}
programEntity.setDataCenterId(dataCenterId);
val now = LocalDateTime.now(ZoneId.of("UTC"));
programEntity.setCreatedAt(now);
programEntity.setUpdatedAt(now);
Expand Down
Loading