-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(browse-classifications): implement browse config endpoint
Closes: MSEARCH-674 Signed-off-by: psmagin <[email protected]>
- Loading branch information
Showing
19 changed files
with
720 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project version="4"> | ||
<component name="JpaPluginProjectSettings"> | ||
<option name="lastSelectedLanguage" value="Java" /> | ||
</component> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/org/folio/search/converter/BrowseConfigMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.folio.search.converter; | ||
|
||
import java.util.List; | ||
import org.folio.search.domain.dto.BrowseConfig; | ||
import org.folio.search.domain.dto.BrowseConfigCollection; | ||
import org.folio.search.domain.dto.BrowseOptionType; | ||
import org.folio.search.domain.dto.BrowseType; | ||
import org.folio.search.domain.dto.ShelvingOrderAlgorithmType; | ||
import org.folio.search.model.config.BrowseConfigEntity; | ||
import org.folio.search.model.config.BrowseConfigId; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
|
||
@Mapper(componentModel = "spring", | ||
imports = {BrowseOptionType.class, BrowseConfigId.class, ShelvingOrderAlgorithmType.class}) | ||
public interface BrowseConfigMapper { | ||
|
||
@Mapping(target = "configId", expression = "java(new BrowseConfigId(type.getValue(), config.getId().getValue()))") | ||
@Mapping(target = "shelvingAlgorithm", expression = "java(config.getShelvingAlgorithm().getValue())") | ||
BrowseConfigEntity convert(BrowseType type, BrowseConfig config); | ||
|
||
@Mapping(target = "id", expression = "java(BrowseOptionType.fromValue(source.getConfigId().getBrowseOptionType()))") | ||
@Mapping(target = "shelvingAlgorithm", | ||
expression = "java(ShelvingOrderAlgorithmType.fromValue(source.getShelvingAlgorithm()))") | ||
BrowseConfig convert(BrowseConfigEntity source); | ||
|
||
default BrowseConfigCollection convert(List<BrowseConfigEntity> entities) { | ||
return map(new BrowseConfigWrapper(entities)); | ||
} | ||
|
||
@Mapping(target = "totalRecords", expression = "java(source.configs().size())") | ||
BrowseConfigCollection map(BrowseConfigWrapper source); | ||
|
||
record BrowseConfigWrapper(List<BrowseConfigEntity> configs) { } | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/org/folio/search/model/config/BrowseConfigEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.folio.search.model.config; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Convert; | ||
import jakarta.persistence.EmbeddedId; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Table; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
import org.hibernate.proxy.HibernateProxy; | ||
|
||
@Getter | ||
@Setter | ||
@ToString | ||
@RequiredArgsConstructor | ||
@Entity | ||
@Table(name = "browse_config") | ||
public class BrowseConfigEntity { | ||
|
||
@EmbeddedId | ||
private BrowseConfigId configId; | ||
|
||
@Column(name = "shelving_algorithm", nullable = false) | ||
private String shelvingAlgorithm; | ||
|
||
@Convert(converter = StringListConverter.class) | ||
@Column(name = "type_ids", nullable = false) | ||
private List<String> typeIds = new ArrayList<>(); | ||
|
||
@Override | ||
public final int hashCode() { | ||
return Objects.hash(configId); | ||
} | ||
|
||
@Override | ||
public final boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null) { | ||
return false; | ||
} | ||
Class<?> effectiveClass = o instanceof HibernateProxy | ||
? ((HibernateProxy) o).getHibernateLazyInitializer().getPersistentClass() | ||
: o.getClass(); | ||
Class<?> thisEffectiveClass = this instanceof HibernateProxy | ||
? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass() | ||
: this.getClass(); | ||
if (thisEffectiveClass != effectiveClass) { | ||
return false; | ||
} | ||
BrowseConfigEntity that = (BrowseConfigEntity) o; | ||
return getConfigId() != null && Objects.equals(getConfigId(), that.getConfigId()); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/org/folio/search/model/config/BrowseConfigId.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.folio.search.model.config; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Embeddable; | ||
import java.util.Objects; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.hibernate.proxy.HibernateProxy; | ||
|
||
@Getter | ||
@Embeddable | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class BrowseConfigId { | ||
|
||
@Column(name = "browse_type") | ||
private String browseType; | ||
@Column(name = "browse_option_type") | ||
private String browseOptionType; | ||
|
||
@Override | ||
public final int hashCode() { | ||
return Objects.hash(browseType, browseOptionType); | ||
} | ||
|
||
@Override | ||
public final boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null) { | ||
return false; | ||
} | ||
Class<?> effectiveClass = o instanceof HibernateProxy | ||
? ((HibernateProxy) o).getHibernateLazyInitializer().getPersistentClass() | ||
: o.getClass(); | ||
Class<?> thisEffectiveClass = this instanceof HibernateProxy | ||
? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass() | ||
: this.getClass(); | ||
if (thisEffectiveClass != effectiveClass) { | ||
return false; | ||
} | ||
BrowseConfigId that = (BrowseConfigId) o; | ||
return browseType != null && Objects.equals(browseType, that.browseType) | ||
&& browseOptionType != null && Objects.equals(browseOptionType, that.browseOptionType); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/org/folio/search/model/config/StringListConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.folio.search.model.config; | ||
|
||
import static java.util.Collections.emptyList; | ||
|
||
import jakarta.persistence.AttributeConverter; | ||
import jakarta.persistence.Converter; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@Converter | ||
public class StringListConverter implements AttributeConverter<List<String>, String> { | ||
private static final String SPLIT_CHAR = ";"; | ||
|
||
@Override | ||
public String convertToDatabaseColumn(List<String> stringList) { | ||
return stringList != null ? String.join(SPLIT_CHAR, stringList) : ""; | ||
} | ||
|
||
@Override | ||
public List<String> convertToEntityAttribute(String string) { | ||
return string != null ? Arrays.asList(string.split(SPLIT_CHAR)) : emptyList(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/org/folio/search/repository/BrowseConfigEntityRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.folio.search.repository; | ||
|
||
import java.util.List; | ||
import org.folio.search.model.config.BrowseConfigEntity; | ||
import org.folio.search.model.config.BrowseConfigId; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface BrowseConfigEntityRepository extends JpaRepository<BrowseConfigEntity, BrowseConfigId> { | ||
|
||
List<BrowseConfigEntity> findByConfigId_BrowseType(String browseType); | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/org/folio/search/service/config/BrowseConfigService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.folio.search.service.config; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.folio.search.converter.BrowseConfigMapper; | ||
import org.folio.search.domain.dto.BrowseConfig; | ||
import org.folio.search.domain.dto.BrowseConfigCollection; | ||
import org.folio.search.domain.dto.BrowseOptionType; | ||
import org.folio.search.domain.dto.BrowseType; | ||
import org.folio.search.exception.RequestValidationException; | ||
import org.folio.search.repository.BrowseConfigEntityRepository; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class BrowseConfigService { | ||
|
||
private static final String BODY_VALIDATION_MSG = "Body doesn't match path parameter: %s"; | ||
|
||
private final BrowseConfigEntityRepository repository; | ||
private final BrowseConfigMapper mapper; | ||
|
||
public BrowseConfigCollection getConfigs(BrowseType type) { | ||
return mapper.convert(repository.findByConfigId_BrowseType(type.getValue())); | ||
} | ||
|
||
public void upsertConfig(BrowseType type, BrowseOptionType configId, BrowseConfig config) { | ||
if (configId != null && configId != config.getId()) { | ||
throw new RequestValidationException( | ||
BODY_VALIDATION_MSG.formatted(configId.getValue()), "id", config.getId().toString()); | ||
} | ||
|
||
var configEntity = mapper.convert(type, config); | ||
repository.save(configEntity); | ||
} | ||
} |
Oops, something went wrong.