Skip to content

Commit

Permalink
Specify Converter for fields of type Translatable
Browse files Browse the repository at this point in the history
  • Loading branch information
mmwinther committed Dec 15, 2023
1 parent 8b5fcfc commit a9e12c5
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
Expand All @@ -30,11 +31,13 @@
import no.ssb.klass.core.util.KlassResourceNotFoundException;
import no.ssb.klass.core.util.TimeUtil;
import no.ssb.klass.core.util.Translatable;
import no.ssb.klass.core.util.TranslatablePersistenceConverter;
import no.ssb.klass.core.util.AlphaNumericalComparator;

@Entity
@Table(indexes = {
// These must be case insensitive (JPA does not support specifying function based indexes)
// These must be case insensitive (JPA does not support specifying function
// based indexes)
@Index(columnList = "name_no", name = "cs_name_no_idx", unique = true),
@Index(columnList = "name_nn", name = "cs_name_nn_idx", unique = true),
@Index(columnList = "name_en", name = "cs_name_en_idx", unique = true)
Expand All @@ -49,7 +52,8 @@ public class ClassificationSeries extends BaseEntity implements ClassificationEn
private static final String PREFIX_CODELIST_NN = "Kodeliste for ";
private static final String PREFIX_CODELIST_EN = "Codelist for ";

// name is not stored as a Translatable since name is used in db queries for sorting and filtering
// name is not stored as a Translatable since name is used in db queries for
// sorting and filtering
@Column(name = "name_no")
private String nameNo;
@Column(name = "name_nn")
Expand All @@ -58,6 +62,7 @@ public class ClassificationSeries extends BaseEntity implements ClassificationEn
private String nameEn;
@Lob
@Column(columnDefinition = "text", nullable = false)
@Convert(converter = TranslatablePersistenceConverter.class)
private Translatable description;
@Column(nullable = false)
private Language primaryLanguage;
Expand Down Expand Up @@ -184,9 +189,9 @@ public ClassificationVersion getClassificationVersionDraft() {

/**
* drafts are excluded as they do not have a real date range.
*
*
* @param dateRange
* that versions should overlap
* that versions should overlap
* @return list of version that overlap provided DateRange
*/
public List<ClassificationVersion> getClassificationVersionsInRange(DateRange dateRange) {
Expand All @@ -207,10 +212,11 @@ public List<ClassificationVersion> getClassificationVersionsInRange(DateRange da
* drafts are excluded as they do not have a real date range.
*
* @param dateRange
* that versions should overlap
* that versions should overlap
* @param includeFuture
* if future version is included
* @return list of version that overlap provided DateRange and futured versions if value is true
* if future version is included
* @return list of version that overlap provided DateRange and futured versions
* if value is true
*/
public List<ClassificationVersion> getClassificationVersionsInRange(DateRange dateRange, Boolean includeFuture) {
checkNotNull(dateRange);
Expand All @@ -219,7 +225,8 @@ public List<ClassificationVersion> getClassificationVersionsInRange(DateRange da
if (classificationVersion.isDraft()) {
continue;
}
if (classificationVersion.getDateRange().overlaps(dateRange) && classificationVersion.showVersion(includeFuture)) {
if (classificationVersion.getDateRange().overlaps(dateRange)
&& classificationVersion.showVersion(includeFuture)) {
results.add(classificationVersion);
}
}
Expand All @@ -245,9 +252,10 @@ public ClassificationVersion getVersionByRange(DateRange dateRange) {
}

/**
* Find correspondenceTables that describes changes between versions of same classification, referred to as
* Find correspondenceTables that describes changes between versions of same
* classification, referred to as
* changeTables. A changeTable list changes between two classification versions.
*
*
* @param dateRange
* @return
*/
Expand All @@ -256,7 +264,8 @@ public List<CorrespondenceTable> getChangeTables(DateRange dateRange, Boolean in
List<CorrespondenceTable> changeTables = new ArrayList<>();
List<ClassificationVersion> versions = getClassificationVersionsInRange(dateRange, includeFuture);
if (versions.size() < 2) {
// Must have at least 2 versions to have a changeTable describing changes between versions
// Must have at least 2 versions to have a changeTable describing changes
// between versions
return changeTables;
}
sortVersionsByFromDate(versions);
Expand Down Expand Up @@ -307,7 +316,8 @@ public List<ClassificationVersion> getClassificationVersionsSortedByFromDateReve
return versions;
}

// dont want to add yet another sort argument for something that is only used for 1 administration view
// dont want to add yet another sort argument for something that is only used
// for 1 administration view
// the alphanumerical sort algorithm is all ready a bit slow
private void moveDraftToTop(List<ClassificationVersion> versions) {
Optional<ClassificationVersion> find = versions.stream().filter(StatisticalClassification::isDraft).findAny();
Expand Down Expand Up @@ -384,17 +394,17 @@ public List<StatisticalUnit> getStatisticalUnits() {

private void setName(String name, Language language) {
switch (language) {
case NB:
nameNo = name;
break;
case NN:
nameNn = name;
break;
case EN:
nameEn = name;
break;
default:
throw new IllegalArgumentException("Unknown language: " + language);
case NB:
nameNo = name;
break;
case NN:
nameNn = name;
break;
case EN:
nameEn = name;
break;
default:
throw new IllegalArgumentException("Unknown language: " + language);
}
}

Expand Down Expand Up @@ -465,27 +475,27 @@ public static String getNamePrefix(Language language, ClassificationType classif

public static String getKlassifikasjonPrefix(Language language) {
switch (language) {
case NB:
return PREFIX_CLASSIFICATION_NB;
case NN:
return PREFIX_CLASSIFICATION_NN;
case EN:
return PREFIX_CLASSIFICATION_EN;
default:
return "";
case NB:
return PREFIX_CLASSIFICATION_NB;
case NN:
return PREFIX_CLASSIFICATION_NN;
case EN:
return PREFIX_CLASSIFICATION_EN;
default:
return "";
}
}

public static String getKodelistePrefix(Language language) {
switch (language) {
case NB:
return PREFIX_CODELIST_NB;
case NN:
return PREFIX_CODELIST_NN;
case EN:
return PREFIX_CODELIST_EN;
default:
return "";
case NB:
return PREFIX_CODELIST_NB;
case NN:
return PREFIX_CODELIST_NN;
case EN:
return PREFIX_CODELIST_EN;
default:
return "";
}
}

Expand All @@ -495,9 +505,12 @@ public boolean isDeleted() {

@Override
public void setDeleted() {
// Soft delete (where classification is only marked as deleted and not actually deleted from database) causes
// problems with unique constraint on classification name. To ensure that a deleted classification will not
// block further use of a classification name, a timestamp is added to the name of the deleted classification
// Soft delete (where classification is only marked as deleted and not actually
// deleted from database) causes
// problems with unique constraint on classification name. To ensure that a
// deleted classification will not
// block further use of a classification name, a timestamp is added to the name
// of the deleted classification
Date now = TimeUtil.now();
for (Language language : Language.getAllSupportedLanguages()) {
String name = getName(language);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.DiscriminatorValue;
import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne;
Expand All @@ -17,24 +18,28 @@

import no.ssb.klass.core.util.DateRange;
import no.ssb.klass.core.util.Translatable;
import no.ssb.klass.core.util.TranslatablePersistenceConverter;

@Entity
@DiscriminatorValue("version")
public class ClassificationVersion extends StatisticalClassification {
@ManyToOne
private ClassificationSeries classification;


@OneToMany(cascade = CascadeType.ALL, mappedBy = "classificationVersion")
private List<ClassificationVariant> classificationVariants;

@Column(length = 4000)
@Convert(converter = TranslatablePersistenceConverter.class)
private Translatable legalBase;
@Column(length = 4000)
@Convert(converter = TranslatablePersistenceConverter.class)
private Translatable publications;
@Column(length = 4000)
@Convert(converter = TranslatablePersistenceConverter.class)
private Translatable derivedFrom;
@Column(length = 4000)
@Convert(converter = TranslatablePersistenceConverter.class)
private Translatable alias;

// For Hibernate
Expand Down Expand Up @@ -95,8 +100,6 @@ public ClassificationVersion copyClassificationVersion(DateRange dateRange) {
return newClassificationVersion;
}



public ClassificationSeries getClassification() {
return classification;
}
Expand All @@ -115,7 +118,6 @@ public ClassificationVariant findVariantByFullName(String variantName, Language
.findFirst().orElse(null);
}


@Override
public Language getPrimaryLanguage() {
return classification.getPrimaryLanguage();
Expand All @@ -135,10 +137,12 @@ public boolean isPublishedInAnyLanguage() {
}

/**
* Use this method for displaying version (formatted output) When more than one given version exists for a given
* year, the year is prefixed with the month (Exs: Bydeler 03-2014), note that if given version contains an alias
* Use this method for displaying version (formatted output) When more than one
* given version exists for a given
* year, the year is prefixed with the month (Exs: Bydeler 03-2014), note that
* if given version contains an alias
* value then this will be returned instead.
*
*
* @return Formatted name
*/
public String getName(Language language) {
Expand All @@ -155,8 +159,6 @@ private String getGeneratedName(Language language) {
return StringUtils.capitalize(classification.getNameWithoutPrefix(language)) + ' ' + datePostfix;
}



@Override
public String getNameInPrimaryLanguage() {
return getName(classification.getPrimaryLanguage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@
import java.time.LocalDate;

import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;

import com.google.common.base.Strings;

import no.ssb.klass.core.util.Translatable;
import no.ssb.klass.core.util.TranslatablePersistenceConverter;

@Entity
public class ConcreteClassificationItem extends ClassificationItem {
private String code;
@Column(length = 2048)
@Convert(converter = TranslatablePersistenceConverter.class)
private Translatable officialName;
@Column(length = 1024)
@Convert(converter = TranslatablePersistenceConverter.class)
private Translatable shortName;
@Column(length = 6000)
@Convert(converter = TranslatablePersistenceConverter.class)
private Translatable notes;

@Column
Expand Down Expand Up @@ -65,7 +70,8 @@ public void setCode(String code) {
}

/**
* @return official name for specified language, if none empty string is returned, never null
* @return official name for specified language, if none empty string is
* returned, never null
*/
@Override
public String getOfficialName(Language language) {
Expand All @@ -78,7 +84,8 @@ public void setOfficialName(String officialName, Language language) {
}

/**
* @return short name for specified language, if none empty string is returned, never null
* @return short name for specified language, if none empty string is returned,
* never null
*/
@Override
public String getShortName(Language language) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Index;
Expand All @@ -25,13 +26,15 @@
import no.ssb.klass.core.util.DateRange;
import no.ssb.klass.core.util.TimeUtil;
import no.ssb.klass.core.util.Translatable;
import no.ssb.klass.core.util.TranslatablePersistenceConverter;

@Entity
@Table(indexes = { @Index(columnList = "source_id", name = "ct_source_idx"),
@Index(columnList = "target_id", name = "ct_target_idx") })
public class CorrespondenceTable extends BaseEntity implements ClassificationEntityOperations, Publishable, Draftable {
@Lob
@Column(columnDefinition = "text", nullable = false)
@Convert(converter = TranslatablePersistenceConverter.class)
private Translatable description;
private Published published;
@ManyToOne(optional = false, fetch = FetchType.LAZY)
Expand Down
9 changes: 7 additions & 2 deletions klass-shared/src/main/java/no/ssb/klass/core/model/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;

import no.ssb.klass.core.util.Translatable;
import no.ssb.klass.core.util.TranslatablePersistenceConverter;

@Entity
public class Level extends BaseEntity {
@Column(nullable = false, length = 1000)
@Convert(converter = TranslatablePersistenceConverter.class)
private Translatable name;
@Column(nullable = false)
private final int levelNumber;
Expand All @@ -43,14 +46,16 @@ public Level(int levelNumber, Translatable name) {
}

/**
* @return name for specified language, if no name an empty string is returned, never null
* @return name for specified language, if no name an empty string is returned,
* never null
*/
public String getName(Language language) {
return name.getString(language);
}

/**
* Not public since clients should add classificationItems through owning version/variant. This so that
* Not public since clients should add classificationItems through owning
* version/variant. This so that
* version/variant can enforce no duplicated codes.
*/
void addClassificationItem(ClassificationItem classificationItem) {
Expand Down
Loading

0 comments on commit a9e12c5

Please sign in to comment.