Skip to content

Commit

Permalink
Merge pull request #310 from Crown-Commercial-Service/NAS-4_WEB-2746_…
Browse files Browse the repository at this point in the history
…add_TPP_fields_to_AS_and_GM

added regulation and agreementType
  • Loading branch information
Chee-Ng authored Nov 11, 2024
2 parents 575c7d3 + fb58c39 commit 6a00e34
Show file tree
Hide file tree
Showing 12 changed files with 490 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class GlobalErrorHandler implements ErrorController {

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler({ValidationException.class, HttpMessageNotReadableException.class,
MethodArgumentTypeMismatchException.class, InvalidAgreementException.class, InvalidLotException.class, InvalidSchemeExpection.class, InvalidOrganisationException.class, InvalidContactDetailExpection.class})
MethodArgumentTypeMismatchException.class, InvalidAgreementException.class, InvalidLotException.class, InvalidSchemeExpection.class, InvalidOrganisationException.class, InvalidContactDetailExpection.class, InvalidRegulationException.class, InvalidAgreementTypeException.class})
public ApiErrors handleValidationException(final Exception exception) {

rollbar.warning(exception, "Request validation exception");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package uk.gov.crowncommercial.dts.scale.service.agreements.exception;

import uk.gov.crowncommercial.dts.scale.service.agreements.model.dto.Regulation;

public class InvalidAgreementTypeException extends RuntimeException {

private static final long serialVersionUID = 1L;
public static final String ERROR_MSG_TEMPLATE = "Invalid agreementType '%s' passed in. agreementType must be one of the following: [Dynamic Purchasing System, Dynamic Market, Open Framework, Closed Framework, PCR15 Framework, PCR06 Framework]";
public static final String ERROR_MSG_TEMPLATE_FOR_REGULATION = "agreementType must be one of the following: '%s' for '%s'";

public InvalidAgreementTypeException(final String agreementType) {
super(String.format(ERROR_MSG_TEMPLATE, agreementType));
}

public InvalidAgreementTypeException(final String allowList, final Regulation regulation) {
super(String.format(ERROR_MSG_TEMPLATE_FOR_REGULATION, allowList, regulation.getName()));
}
}



Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package uk.gov.crowncommercial.dts.scale.service.agreements.exception;

public class InvalidRegulationException extends RuntimeException {

private static final long serialVersionUID = 1L;
public static final String ERROR_MSG_TEMPLATE = "Invalid regulation '%s' passed in. Regulation must be one of the following: [PA2023, PCR2015, PCR2006]";

public InvalidRegulationException(final String regulation) {
super(String.format(ERROR_MSG_TEMPLATE, regulation));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public interface AgreementDetailMapper {
@Mapping(source = "benefits", target = "benefits", qualifiedByName = "commercialAgreementBenefitsToStrings")
@Mapping(source = "lots", target = "lots", qualifiedByName = "lotsToLotSummaries")
@Mapping(source = "organisationRoles", target = "owner", qualifiedByName = "orgRolesToOrganization")
@Mapping(source = "regulation", target = "regulation")
@Mapping(source = "agreementType", target = "agreementType", qualifiedByName = "agreementTypeEnumToString")
AgreementDetail commercialAgreementToAgreementDetail(CommercialAgreement dbModel);

@Mapping(source = "benefits", target = "benefits", qualifiedByName = "stringsToCommercialAgreementBenefits")
Expand Down Expand Up @@ -160,4 +162,13 @@ public static Organization orgRolesToOrganization(Set<CommercialAgreementOrgRole

return null;
}

@Named("agreementTypeEnumToString")
public static String agreementTypeEnumToString(AgreementType agreementType) {
if (agreementType != null) {
return agreementType.getName();
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import java.io.Serializable;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import lombok.Data;
import uk.gov.crowncommercial.dts.scale.service.agreements.exception.*;

/**
* Agreement Detail.
Expand Down Expand Up @@ -76,6 +80,16 @@ public class AgreementDetail implements Serializable {
*/
private Boolean preDefinedLotRequired;

/**
* TPP- The regulation that this agreement is under
*/
private Regulation regulation;

/**
* TPP- The agreement type of this agreement
*/
private AgreementType agreementType;

public AgreementDetail() {
}

Expand All @@ -88,4 +102,41 @@ public AgreementDetail(String name, String description, String ownerName, LocalD
this.detailUrl = url;
this.preDefinedLotRequired = preDefinedLotRequired;
}

public void setRegulation(String regulationInput) {
try {
if (regulationInput != null) {
this.regulation = Regulation.valueOf(regulationInput.toUpperCase());
}
} catch (IllegalArgumentException e) {
throw new InvalidRegulationException(regulationInput);
}
}

public void setAgreementType(String agreementTypeInput) {
if (agreementTypeInput == null) {return;}

AgreementType userInputedEnum = AgreementType.getAgreementTypeFromName(agreementTypeInput);
if (userInputedEnum == null) {throw new InvalidAgreementTypeException(agreementTypeInput);};

if (this.regulation != null) {
List<AgreementType> allowedList = getAllowedAgreementTypes(this.regulation);
if (allowedList == null || !allowedList.contains(userInputedEnum)) {throw new InvalidAgreementTypeException(AgreementType.getStringFormatForAgreementTypes(allowedList), regulation);}
this.agreementType = userInputedEnum;
}
}

private List<AgreementType> getAllowedAgreementTypes(Regulation regulation) {
switch (regulation) {
case PA2023:
return Arrays.asList(AgreementType.DYNAMIC_MARKET, AgreementType.OPEN_FRAMEWORK, AgreementType.CLOSED_FRAMEWORK);
case PCR2015:
return Arrays.asList(AgreementType.DYNAMIC_PURCHASING_SYSTEM, AgreementType.PCR15_FRAMEWORK);
case PCR2006:
return Arrays.asList(AgreementType.PCR06_FRAMEWORK);
default:
return null;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package uk.gov.crowncommercial.dts.scale.service.agreements.model.dto;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

public enum AgreementType {

@JsonProperty("Dynamic Purchasing System")
DYNAMIC_PURCHASING_SYSTEM("Dynamic Purchasing System"),

@JsonProperty("Dynamic Market")
DYNAMIC_MARKET("Dynamic Market"),

@JsonProperty("Open Framework")
OPEN_FRAMEWORK("Open Framework"),

@JsonProperty("Closed Framework")
CLOSED_FRAMEWORK("Closed Framework"),

@JsonProperty("PCR15 Framework")
PCR15_FRAMEWORK("PCR15 Framework"),

@JsonProperty("PCR06 Framework")
PCR06_FRAMEWORK("PCR06 Framework");

private String name;

private AgreementType(String name){
this.name = name;
}

public String getName(){
return this.name;
}

@JsonCreator
public static AgreementType getAgreementTypeFromName(String value) {

for (AgreementType a : AgreementType.values()) {
if (a.getName().equalsIgnoreCase(value)) {
return a;
}
}

return null;
}

public static String getStringFormatForAgreementTypes( List<AgreementType> agreementTypeList){

StringBuilder sb = new StringBuilder();
sb.append("[");

for (int i = 0; i < agreementTypeList.size(); i++) {
sb.append(agreementTypeList.get(i).getName());
if (i < agreementTypeList.size() - 1) {
sb.append(", ");
}
}

sb.append("]");

return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package uk.gov.crowncommercial.dts.scale.service.agreements.model.dto;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* Regulation
*/
public enum Regulation {

@JsonProperty("PA2023")
PA2023("PA2023"),

@JsonProperty("PCR2015")
PCR2015("PCR2015"),

@JsonProperty("PCR2006")
PCR2006("PCR2006");

private String name;

private Regulation(String name){
this.name = name;
}

public String getName(){
return this.name;
}

@JsonCreator
public static Regulation getRegulationFromName(String value) {

for (Regulation r : Regulation.values()) {
if (r.getName().equalsIgnoreCase(value)) {
return r;
}
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import lombok.Data;
import lombok.experimental.FieldDefaults;
import uk.gov.crowncommercial.dts.scale.service.agreements.exception.InvalidAgreementException;
import uk.gov.crowncommercial.dts.scale.service.agreements.model.dto.*;

/**
* Commercial Agreement.
Expand Down Expand Up @@ -49,6 +50,12 @@ public class CommercialAgreement {
@Column(name = "agreement_url")
String detailUrl;

@Column(name = "regulation")
String regulation;

@Column(name = "agreement_type")
String agreementType;

@OneToMany(mappedBy = "agreement")
Set<Lot> lots;

Expand Down Expand Up @@ -104,8 +111,7 @@ public void isValid(){
if (endDate == null) {throw new InvalidAgreementException("endDate");}
if (detailUrl == null || detailUrl.isEmpty()) {throw new InvalidAgreementException("detailUrl");}
if (preDefinedLotRequired == null) {throw new InvalidAgreementException("preDefinedLotRequired");}
}

}
public CommercialAgreementBenefit addBenefit(CommercialAgreementBenefit benefit){
this.benefits.add(benefit);
benefit.setAgreement(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public CommercialAgreement createOrUpdateAgreement(final CommercialAgreement mod
existingModel.setEndDate(model.getEndDate());
existingModel.setDetailUrl(model.getDetailUrl());
existingModel.setPreDefinedLotRequired(model.getPreDefinedLotRequired());
existingModel.setRegulation(model.getRegulation());
existingModel.setAgreementType(model.getAgreementType());

if (model.getBenefits() != null && !model.getBenefits().isEmpty() ){
commercialAgreementBenefitService.removeBenefits(existingModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import uk.gov.crowncommercial.dts.scale.service.agreements.model.dto.AgreementType;
import uk.gov.crowncommercial.dts.scale.service.agreements.model.dto.Regulation;
import uk.gov.crowncommercial.dts.scale.service.agreements.model.entity.CommercialAgreement;
import uk.gov.crowncommercial.dts.scale.service.agreements.model.entity.Lot;

Expand Down Expand Up @@ -85,11 +87,15 @@ public CommercialAgreement getExpandedCommercialAgreement(CommercialAgreement mo
// We have the data response, so now use it to populate our expansion data
String agreementDesc = validateWordpressData("summary", jsonData, agreementId);
String agreementEndDate = validateWordpressData("end_date", jsonData, agreementId);
String agreementRegulation = validateWordpressData("regulation", jsonData, agreementId);
String agreementType = validateWordpressData("regulation_type", jsonData, agreementId);

if(agreementDesc != null) model.setDescription(agreementDesc);

// Only override the end date if the service doesn't have one configured - our data is the master data here
// Only override if the service doesn't have one configured - our data is the master data here
if(agreementEndDate != null && model.getEndDate() == null) model.setEndDate(LocalDate.parse(agreementEndDate));
if(agreementRegulation != null && model.getRegulation() == null) model.setRegulation(Regulation.getRegulationFromName(agreementRegulation).toString());
if(agreementType != null && model.getAgreementType() == null) model.setAgreementType(AgreementType.getAgreementTypeFromName(agreementType).toString());
}

return model;
Expand Down
Loading

0 comments on commit 6a00e34

Please sign in to comment.