Skip to content

Commit

Permalink
Use auto incrementable int as primary key, add created and modified d…
Browse files Browse the repository at this point in the history
…ate to entity, renamed isOrdered method
  • Loading branch information
maallen committed Aug 14, 2023
1 parent 6378b8b commit 24d3587
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.box.l10n.mojito.entity;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Index;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;

/** Entity that stores the checksum of a translated file downloaded via a third party sync. */
@Entity
Expand All @@ -17,46 +19,64 @@
columnList = "repository_id, locale_id, file_name",
unique = true),
})
public class ThirdPartyFileChecksum implements Serializable {
public class ThirdPartyFileChecksum extends SettableAuditableEntity {

@EmbeddedId private ThirdPartyFileChecksumId thirdPartyFileChecksumId;
@Embedded private ThirdPartyFileChecksumCompositeId thirdPartyFileChecksumCompositeId;

@Column(name = "checksum")
private String checksum;
@Column(name = "md5")
private String md5;

@Column(name = "last_modified_date")
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime lastModifiedDate;

public ThirdPartyFileChecksum() {}

public ThirdPartyFileChecksum(
ThirdPartyFileChecksumId thirdPartyFileChecksumId, String checksum) {
this.thirdPartyFileChecksumId = thirdPartyFileChecksumId;
this.checksum = checksum;
ThirdPartyFileChecksumCompositeId thirdPartyFileChecksumCompositeId, String md5) {
this.thirdPartyFileChecksumCompositeId = thirdPartyFileChecksumCompositeId;
this.md5 = md5;
}

@PreUpdate
public void preUpdate() {
lastModifiedDate = new DateTime();
}

public ThirdPartyFileChecksumId getThirdPartyFileChecksumId() {
return thirdPartyFileChecksumId;
public ThirdPartyFileChecksumCompositeId getThirdPartyFileChecksumCompositeId() {
return thirdPartyFileChecksumCompositeId;
}

public void setThirdPartyFileChecksumId(ThirdPartyFileChecksumId thirdPartyFileChecksumId) {
this.thirdPartyFileChecksumId = thirdPartyFileChecksumId;
public void setThirdPartyFileChecksumCompositeId(
ThirdPartyFileChecksumCompositeId thirdPartyFileChecksumCompositeId) {
this.thirdPartyFileChecksumCompositeId = thirdPartyFileChecksumCompositeId;
}

public String getChecksum() {
return checksum;
public String getMd5() {
return md5;
}

public void setChecksum(String checksum) {
this.checksum = checksum;
public void setMd5(String checksum) {
this.md5 = checksum;
}

public Locale getLocale() {
return thirdPartyFileChecksumId.getLocale();
return thirdPartyFileChecksumCompositeId.getLocale();
}

public Repository getRepository() {
return thirdPartyFileChecksumId.getRepository();
return thirdPartyFileChecksumCompositeId.getRepository();
}

public String getFileName() {
return thirdPartyFileChecksumId.getFileName();
return thirdPartyFileChecksumCompositeId.getFileName();
}

public DateTime getLastModifiedDate() {
return lastModifiedDate;
}

public void setLastModifiedDate(DateTime lastModifiedDate) {
this.lastModifiedDate = lastModifiedDate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import javax.persistence.ManyToOne;

@Embeddable
public class ThirdPartyFileChecksumId implements Serializable {
public class ThirdPartyFileChecksumCompositeId implements Serializable {

@ManyToOne(optional = false)
@JoinColumn(
Expand All @@ -25,9 +25,9 @@ public class ThirdPartyFileChecksumId implements Serializable {
foreignKey = @ForeignKey(name = "FK__THIRD_PARTY_CHECKSUM__LOCALE__ID"))
private Locale locale;

public ThirdPartyFileChecksumId() {}
public ThirdPartyFileChecksumCompositeId() {}

public ThirdPartyFileChecksumId(Repository repository, Locale locale, String fileName) {
public ThirdPartyFileChecksumCompositeId(Repository repository, Locale locale, String fileName) {
this.repository = repository;
this.locale = locale;
this.fileName = fileName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.box.l10n.mojito.service.thirdparty;

import com.box.l10n.mojito.entity.ThirdPartyFileChecksum;
import com.box.l10n.mojito.entity.ThirdPartyFileChecksumId;
import com.box.l10n.mojito.entity.ThirdPartyFileChecksumCompositeId;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResource(exported = false)
public interface ThirdPartyFileChecksumRepository
extends JpaRepository<ThirdPartyFileChecksum, ThirdPartyFileChecksumId> {
extends JpaRepository<ThirdPartyFileChecksum, ThirdPartyFileChecksumCompositeId> {

Optional<ThirdPartyFileChecksum> findByThirdPartyFileChecksumId(
ThirdPartyFileChecksumId thirdPartyFileChecksumId);
Optional<ThirdPartyFileChecksum> findById(Long thirdPartyFileChecksumId);

Optional<ThirdPartyFileChecksum> findByThirdPartyFileChecksumCompositeId(
ThirdPartyFileChecksumCompositeId thirdPartyFileChecksumCompositeId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ private Stream<List<TextUnitDTO>> partitionSingulars(
true,
true,
null);
parameters.setOrdered(true);
parameters.setOrderByTextUnitID(true);
return partitionedStream(parameters, textUnitSearcher::search);
}

Expand All @@ -946,7 +946,7 @@ private Stream<List<TextUnitDTO>> partitionSingulars(
true,
null,
includeTextUnitWithPattern);
parameters.setOrdered(true);
parameters.setOrderByTextUnitID(true);
return partitionedStream(parameters, textUnitSearcher::search);
}

Expand All @@ -964,7 +964,7 @@ private Stream<List<TextUnitDTO>> partitionPlurals(
false,
false,
"%");
parameters.setOrdered(true);
parameters.setOrderByTextUnitID(true);
return partitionedStream(parameters, textUnitSearcher::search);
}

Expand Down Expand Up @@ -999,7 +999,7 @@ private Stream<List<TextUnitDTO>> partitionPlurals(
"%",
includeTextUnitsWithPattern);

parameters.setOrdered(true);
parameters.setOrderByTextUnitID(true);

return partitionedStream(parameters, searchFunction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ PageFetcherOffsetAndLimitSplitIterator<TextUnitDTO> getSourceTextUnitIterator(
parameters.setOffset(offset);
parameters.setLimit(limit);
parameters.setPluralFormsFiltered(true);
parameters.setOrdered(true);
parameters.setOrderByTextUnitID(true);
List<TextUnitDTO> search = textUnitSearcher.search(parameters);
return search;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.box.l10n.mojito.entity.Locale;
import com.box.l10n.mojito.entity.Repository;
import com.box.l10n.mojito.entity.ThirdPartyFileChecksum;
import com.box.l10n.mojito.entity.ThirdPartyFileChecksumId;
import com.box.l10n.mojito.entity.ThirdPartyFileChecksumCompositeId;
import java.util.Optional;
import org.apache.commons.codec.digest.DigestUtils;

Expand All @@ -17,17 +17,22 @@ public static boolean isFileEqualToPreviousRun(
String fileContent) {

String currentChecksum = DigestUtils.md5Hex(fileContent);
ThirdPartyFileChecksumId thirdPartyFileChecksumId =
new ThirdPartyFileChecksumId(repository, locale, fileName);
ThirdPartyFileChecksumCompositeId thirdPartyFileChecksumCompositeId =
new ThirdPartyFileChecksumCompositeId(repository, locale, fileName);

Optional<ThirdPartyFileChecksum> thirdPartyFileChecksum =
thirdPartyFileChecksumRepository.findByThirdPartyFileChecksumId(thirdPartyFileChecksumId);
if (thirdPartyFileChecksum.isPresent()
&& thirdPartyFileChecksum.get().getChecksum().equals(currentChecksum)) {
Optional<ThirdPartyFileChecksum> thirdPartyFileChecksumOpt =
thirdPartyFileChecksumRepository.findByThirdPartyFileChecksumCompositeId(
thirdPartyFileChecksumCompositeId);
if (thirdPartyFileChecksumOpt.isPresent()
&& thirdPartyFileChecksumOpt.get().getMd5().equals(currentChecksum)) {
return true;
} else if (thirdPartyFileChecksumOpt.isPresent()) {
ThirdPartyFileChecksum thirdPartyFileChecksum = thirdPartyFileChecksumOpt.get();
thirdPartyFileChecksum.setMd5(currentChecksum);
thirdPartyFileChecksumRepository.save(thirdPartyFileChecksum);
} else {
thirdPartyFileChecksumRepository.save(
new ThirdPartyFileChecksum(thirdPartyFileChecksumId, currentChecksum));
new ThirdPartyFileChecksum(thirdPartyFileChecksumCompositeId, currentChecksum));
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,15 +441,15 @@ NativeCriteria getCriteriaForSearch(TextUnitSearcherParameters searchParameters)
c.setOffset(searchParameters.getOffset());
}

if (searchParameters.isOrdered()) {
if (searchParameters.isOrderedByTextUnitID()) {
c.setOrder(NativeExps.order().add("tu.id", NativeOrderExp.OrderType.ASC));
}

if (searchParameters instanceof TextUnitSearcherParametersForTesting) {
TextUnitSearcherParametersForTesting textUnitSearcherParametersForTesting =
(TextUnitSearcherParametersForTesting) searchParameters;

if (textUnitSearcherParametersForTesting.isOrdered()) {
if (textUnitSearcherParametersForTesting.isOrderedByTextUnitID()) {
c.setOrder(
NativeExps.order()
.add("tu.id", NativeOrderExp.OrderType.ASC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class TextUnitSearcherParameters {
boolean pluralFormsFiltered = true;
boolean pluralFormsExcluded = false;

boolean isOrdered = false;
boolean isOrderedByTextUnitID = false;
Long pluralFormId;
Boolean doNotTranslateFilter;
DateTime tmTextUnitCreatedBefore;
Expand Down Expand Up @@ -311,11 +311,11 @@ public void setSkipAssetPathWithPattern(String skipAssetPathWithPattern) {
this.skipAssetPathWithPattern = skipAssetPathWithPattern;
}

public boolean isOrdered() {
return isOrdered;
public boolean isOrderedByTextUnitID() {
return isOrderedByTextUnitID;
}

public void setOrdered(boolean ordered) {
isOrdered = ordered;
public void setOrderByTextUnitID(boolean ordered) {
isOrderedByTextUnitID = ordered;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public class TextUnitSearcherParametersForTesting extends TextUnitSearcherParame

boolean ordered = true;

public boolean isOrdered() {
public boolean isOrderedByTextUnitID() {
return ordered;
}

public void setOrdered(boolean ordered) {
public void setOrderByTextUnitID(boolean ordered) {
this.ordered = ordered;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
create table third_party_sync_file_checksum (repository_id bigint not null, locale_id bigint not null, checksum char(32) not null, file_name varchar(255) not null);
create table third_party_sync_file_checksum (
id bigint(20) NOT NULL AUTO_INCREMENT,
repository_id bigint not null,
locale_id bigint not null,
md5 char(32) not null,
file_name varchar(255) not null,
created_date datetime not null,
last_modified_date datetime,
primary key (id)
);
alter table third_party_sync_file_checksum add constraint FK__THIRD_PARTY_CHECKSUM__REPO__ID foreign key (repository_id) references repository (id);
alter table third_party_sync_file_checksum add constraint FK__THIRD_PARTY_CHECKSUM__LOCALE__ID foreign key (locale_id) references locale (id);
create index I__THIRD_PARTY__CHECKSUM__REPOSITORY_ID__FILE_NAME__LOCALE_ID on third_party_sync_file_checksum(repository_id, locale_id, file_name);
create unique index I__THIRD_PARTY__CHECKSUM__REPOSITORY_ID__FILE_NAME__LOCALE_ID on third_party_sync_file_checksum(repository_id, locale_id, file_name);
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,8 @@ public void testPullWithUntranslatedUnits() throws Exception {
.thenReturn(smartlingJsonResponseWithOriginalString);

Mockito.when(
thirdPartyFileChecksumRepositoryMock.findByThirdPartyFileChecksumId(
isA(ThirdPartyFileChecksumId.class)))
thirdPartyFileChecksumRepositoryMock.findByThirdPartyFileChecksumCompositeId(
isA(ThirdPartyFileChecksumCompositeId.class)))
.thenReturn(Optional.empty());

// For the first pass, mock a fully translated response
Expand Down

0 comments on commit 24d3587

Please sign in to comment.