Skip to content

Commit

Permalink
Merge pull request #28 from knaw-huc/TT-822_import_task_response_fixes
Browse files Browse the repository at this point in the history
Import task response fixes
  • Loading branch information
BasLee authored Mar 30, 2021
2 parents 3b626b2 + 7f30dff commit ffffa8e
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ To index, we first create three documents, each with one file and version, using

Then:

- Response statuses should be: [200, 200, 200](- "?=#importResult.status");
- Response statuses should be: [201, 201, 201](- "?=#importResult.status");
- Full response of first imported document:

[ ](- "ext:embed=#importResult.body")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ To index, we first create three documents, each with one file and version, using

Then:

- Response statuses should be: [200, 200, 200](- "?=#importResult.status");
- Response statuses should be: [201, 201, 201](- "?=#importResult.status");
- Full response of first imported document:

[ ](- "ext:embed=#importResult.body")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package nl.knaw.huc.api;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.MoreObjects;
import nl.knaw.huc.core.Document;
import nl.knaw.huc.core.Version;

import java.util.UUID;

public class ResultImportDocument {
private final Document document;
private final Version version;
private final boolean isNewVersion;

public ResultImportDocument(Document document, Version version, boolean isNewVersion) {
this.document = document;
this.version = version;
this.isNewVersion = isNewVersion;
}

@JsonProperty
public UUID getDocumentId() {
return document.getId();
}

@JsonProperty
public UUID getFileId() {
return version.getFileId();
}

@JsonProperty
public UUID getVersionId() {
return version.getId();
}

@JsonProperty
public String getContentsSha() {
return version.getContentsSha();
}

@JsonProperty
public boolean isNewVersion() {
return isNewVersion;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("document", document)
.add("version", version)
.add("isNewVersion", isNewVersion)
.toString();
}
}
2 changes: 1 addition & 1 deletion textrepo-app/src/main/java/nl/knaw/huc/db/VersionsDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface VersionsDao {
Optional<Version> findLatestByFileId(@Bind UUID fileId);

@SqlQuery("select id, file_id, created_at, contents_sha " +
"from versions where file_id = ? order by created_at asc")
"from versions where file_id = ? order by created_at desc")
@RegisterConstructorMapper(value = Version.class)
List<Version> findByFileId(UUID fileId);

Expand Down
11 changes: 7 additions & 4 deletions textrepo-app/src/main/java/nl/knaw/huc/resources/HeaderLink.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package nl.knaw.huc.resources;

import nl.knaw.huc.resources.rest.ContentsResource;
import nl.knaw.huc.resources.rest.DocumentMetadataResource;
import nl.knaw.huc.resources.rest.DocumentsResource;
import nl.knaw.huc.resources.rest.FileMetadataResource;
import nl.knaw.huc.resources.rest.FileVersionsResource;
import nl.knaw.huc.resources.rest.FilesResource;
import nl.knaw.huc.resources.rest.TypesResource;
import nl.knaw.huc.resources.rest.VersionsResource;

import javax.ws.rs.core.Link;
import javax.ws.rs.core.UriBuilder;

import java.net.URI;

import static javax.ws.rs.core.UriBuilder.fromResource;
Expand All @@ -23,12 +24,14 @@ public static class Rel {
}

public enum Uri {
DOCUMENT(fromResource(DocumentsResource.class).path("/{id}")),
CONTENTS(fromResource(ContentsResource.class).path("{sha}")),
DOCUMENT(fromResource(DocumentsResource.class).path("{id}")),
DOCUMENT_METADATA(fromResource(DocumentMetadataResource.class)),
FILE(fromResource(FilesResource.class).path("/{id}")),
FILE(fromResource(FilesResource.class).path("{id}")),
FILE_METADATA(fromResource(FileMetadataResource.class)),
FILE_VERSIONS(fromResource(FileVersionsResource.class)),
TYPE(fromResource(TypesResource.class).path("/{id}"));
TYPE(fromResource(TypesResource.class).path("{id}")),
VERSION(fromResource(VersionsResource.class).path("{id}"));

private final UriBuilder uriBuilder;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import java.io.InputStream;

import static java.util.Objects.requireNonNull;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.MediaType.MULTIPART_FORM_DATA;
import static nl.knaw.huc.resources.HeaderLink.Uri.CONTENTS;
import static nl.knaw.huc.resources.HeaderLink.Uri.DOCUMENT;
import static nl.knaw.huc.resources.HeaderLink.Uri.FILE;
import static nl.knaw.huc.resources.HeaderLink.Uri.VERSION;

@Api(tags = {"task", "import"})
@Path("task/import")
Expand All @@ -48,27 +53,44 @@ public Response importDocumentContentsForFileWithType(
@NotBlank @PathParam("externalId") String externalId,
@NotBlank @PathParam("typeName") String typeName,
@QueryParam("allowNewDocument") @DefaultValue("false") boolean allowNewDocument,
@QueryParam("asLatestVersion") @DefaultValue("false") boolean asLatestVersion,
@NotNull @FormDataParam("contents") InputStream uploadedInputStream,
@NotNull @FormDataParam("contents") FormDataContentDisposition fileDetail
) {
log.debug(
"Import document contents for file with type: " +
"Importing document contents for file with type: " +
"externalId={}, " +
"typeName={}, " +
"allowNewDocument={}",
externalId, typeName, allowNewDocument
"allowNewDocument={}, " +
"asLatestVersion={}",
externalId, typeName, allowNewDocument, asLatestVersion
);

final var builder = factory.getDocumentImportBuilder();
final var importTask = builder.allowNewDocument(allowNewDocument)
.forExternalId(externalId)
.withTypeName(typeName)
.forFilename(fileDetail.getFileName())
.withContents(uploadedInputStream)
.build();
final var importTask =
factory.getDocumentImportBuilder()
.allowNewDocument(allowNewDocument)
.asLatestVersion(asLatestVersion)
.forExternalId(externalId)
.withTypeName(typeName)
.forFilename(fileDetail.getFileName())
.withContents(uploadedInputStream)
.build();

importTask.run();
log.debug("Imported document contents");
return Response.ok().build();
final var result = importTask.run();
log.debug("Imported document contents: {}", result);

final ResponseBuilder builder;
if (result.isNewVersion()) {
builder = Response.created(VERSION.build(result.getVersionId()));
} else {
builder = Response.ok();
}

return builder.entity(result)
.link(CONTENTS.build(result.getContentsSha()), "contents")
.link(DOCUMENT.build(result.getDocumentId()), "document")
.link(FILE.build(result.getFileId()), "file")
.link(VERSION.build(result.getVersionId()), "version")
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,49 @@
import static java.time.LocalDateTime.now;
import static java.util.Objects.requireNonNull;

public class SetCurrentFileContents implements InTransactionProvider<Version> {
public class SetFileContents implements InTransactionProvider<Version> {

private final Supplier<UUID> idGenerator;

private final Supplier<UUID> versionIdGenerator;

private final TextRepoFile file;
private final Contents contents;
private final boolean asLatestVersion;

private Handle transaction;

public SetCurrentFileContents(
Supplier<UUID> versionIdGenerator,
TextRepoFile file,
Contents contents
) {
this.versionIdGenerator = requireNonNull(versionIdGenerator);
public SetFileContents(Supplier<UUID> idGenerator, TextRepoFile file, Contents contents, boolean asLatestVersion) {
this.idGenerator = requireNonNull(idGenerator);
this.file = requireNonNull(file);
this.contents = requireNonNull(contents);
this.asLatestVersion = asLatestVersion;
}

@Override
public Version executeIn(Handle transaction) {
this.transaction = requireNonNull(transaction);
return latestVersionIfIdentical().orElseGet(this::createNewVersionWithContents);
if (asLatestVersion) {
return latestVersionIfIdentical().orElseGet(this::createNewVersionWithContents);
}
return anyVersionIfIdentical().orElseGet(this::createNewVersionWithContents);
}

private Optional<Version> latestVersionIfIdentical() {
return versions().findLatestByFileId(file.getId())
.filter(this::hasIdenticalContents);
}

private Optional<Version> anyVersionIfIdentical() {
return versions().findByFileId(file.getId()).stream()
.filter(this::hasIdenticalContents)
.findFirst();
}

private boolean hasIdenticalContents(Version candidate) {
return candidate.getContentsSha().equals(contents.getSha224());
}

private Version createNewVersionWithContents() {
final var id = versionIdGenerator.get();
final var id = idGenerator.get();
final var version = new Version(id, file.getId(), contents.getSha224(), now());
contents().insert(contents);
versions().insert(version);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package nl.knaw.huc.service.task.importer;

import nl.knaw.huc.core.Version;
import nl.knaw.huc.api.ResultImportDocument;
import nl.knaw.huc.service.task.Task;

import java.io.InputStream;

public interface ImportFileTaskBuilder {
ImportFileTaskBuilder allowNewDocument(boolean allowNewDocument);

ImportFileTaskBuilder asLatestVersion(boolean asLatestVersion);

ImportFileTaskBuilder forExternalId(String externalId);

ImportFileTaskBuilder withTypeName(String type);
Expand All @@ -16,5 +18,5 @@ public interface ImportFileTaskBuilder {

ImportFileTaskBuilder withContents(InputStream inputStream);

Task<Version> build();
Task<ResultImportDocument> build();
}
Loading

0 comments on commit ffffa8e

Please sign in to comment.