Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kbss cvut/termit UI#449 excel import #288

Merged
merged 5 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#

FROM maven:3-eclipse-temurin-17 as build
FROM maven:3-eclipse-temurin-17 AS build

WORKDIR /termit

Expand All @@ -31,7 +31,7 @@ COPY src src

RUN mvn package -B -P graphdb,standalone -DskipTests=true

FROM eclipse-temurin:17-jdk-alpine as runtime
FROM eclipse-temurin:17-jdk-alpine AS runtime
COPY --from=build /termit/target/termit.jar termit.jar

EXPOSE 8080
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import cz.cvut.kbss.termit.exception.PersistenceException;
import cz.cvut.kbss.termit.persistence.dao.util.Quad;
import cz.cvut.kbss.termit.service.export.ExportFormat;
import cz.cvut.kbss.termit.service.export.util.TypeAwareByteArrayResource;
import cz.cvut.kbss.termit.util.TypeAwareByteArrayResource;
import cz.cvut.kbss.termit.util.Configuration;
import cz.cvut.kbss.termit.util.Configuration.Persistence;
import cz.cvut.kbss.termit.util.TypeAwareResource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import cz.cvut.kbss.termit.dto.listing.VocabularyDto;
import cz.cvut.kbss.termit.event.VocabularyCreatedEvent;
import cz.cvut.kbss.termit.exception.NotFoundException;
import cz.cvut.kbss.termit.exception.TermItException;
import cz.cvut.kbss.termit.model.Vocabulary;
import cz.cvut.kbss.termit.model.acl.AccessControlList;
import cz.cvut.kbss.termit.model.acl.AccessControlRecord;
Expand All @@ -37,13 +36,14 @@
import cz.cvut.kbss.termit.persistence.snapshot.SnapshotCreator;
import cz.cvut.kbss.termit.service.business.async.AsyncTermService;
import cz.cvut.kbss.termit.service.changetracking.ChangeRecordProvider;
import cz.cvut.kbss.termit.service.document.util.TypeAwareFileSystemResource;
import cz.cvut.kbss.termit.util.TypeAwareFileSystemResource;
import cz.cvut.kbss.termit.service.export.ExportFormat;
import cz.cvut.kbss.termit.service.repository.ChangeRecordService;
import cz.cvut.kbss.termit.service.repository.VocabularyRepositoryService;
import cz.cvut.kbss.termit.service.security.authorization.VocabularyAuthorizationService;
import cz.cvut.kbss.termit.service.snapshot.SnapshotProvider;
import cz.cvut.kbss.termit.util.Configuration;
import cz.cvut.kbss.termit.util.TypeAwareClasspathResource;
import cz.cvut.kbss.termit.util.TypeAwareResource;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
Expand All @@ -61,7 +61,6 @@

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -259,15 +258,14 @@ public Vocabulary importVocabulary(URI vocabularyIri, MultipartFile file) {
*/
public TypeAwareResource getExcelTemplateFile() {
final Configuration config = context.getBean(Configuration.class);
final File templateFile = config.getTemplate().getExcelImport().map(File::new).orElseGet(() -> {
try {
assert getClass().getClassLoader().getResource("template/termit-import.xlsx") != null;
return new File(getClass().getClassLoader().getResource("template/termit-import.xlsx").toURI());
} catch (URISyntaxException e) {
throw new TermItException("Fatal error, unable to load Excel template file.", e);
}
});
return new TypeAwareFileSystemResource(templateFile, ExportFormat.EXCEL.getMediaType());
return config.getTemplate().getExcelImport().map(File::new)
.map(f -> (TypeAwareResource) new TypeAwareFileSystemResource(f,
ExportFormat.EXCEL.getMediaType()))
.orElseGet(() -> {
assert getClass().getClassLoader().getResource("template/termit-import.xlsx") != null;
return new TypeAwareClasspathResource("template/termit-import.xlsx",
ExportFormat.EXCEL.getMediaType());
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import cz.cvut.kbss.termit.model.resource.File;
import cz.cvut.kbss.termit.model.resource.Resource;
import cz.cvut.kbss.termit.service.IdentifierResolver;
import cz.cvut.kbss.termit.service.document.util.TypeAwareFileSystemResource;
import cz.cvut.kbss.termit.util.TypeAwareFileSystemResource;
import cz.cvut.kbss.termit.util.Configuration;
import cz.cvut.kbss.termit.util.TypeAwareResource;
import cz.cvut.kbss.termit.util.Utils;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cz.cvut.kbss.termit.service.document.html;

import cz.cvut.kbss.termit.exception.FileContentProcessingException;
import cz.cvut.kbss.termit.service.export.util.TypeAwareByteArrayResource;
import cz.cvut.kbss.termit.util.TypeAwareByteArrayResource;
import cz.cvut.kbss.termit.util.TypeAwareResource;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import cz.cvut.kbss.termit.exception.UnsupportedOperationException;
import cz.cvut.kbss.termit.model.Term;
import cz.cvut.kbss.termit.model.Vocabulary;
import cz.cvut.kbss.termit.service.export.util.TypeAwareByteArrayResource;
import cz.cvut.kbss.termit.util.TypeAwareByteArrayResource;
import cz.cvut.kbss.termit.service.repository.TermRepositoryService;
import cz.cvut.kbss.termit.service.repository.VocabularyRepositoryService;
import cz.cvut.kbss.termit.util.Constants;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
import cz.cvut.kbss.termit.exception.UnsupportedOperationException;
import cz.cvut.kbss.termit.model.Vocabulary;
import cz.cvut.kbss.termit.persistence.dao.skos.SKOSExporter;
import cz.cvut.kbss.termit.service.export.util.TypeAwareByteArrayResource;
import cz.cvut.kbss.termit.util.TypeAwareByteArrayResource;
import cz.cvut.kbss.termit.util.TypeAwareResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cz.cvut.kbss.jopa.vocabulary.SKOS;
import cz.cvut.kbss.jsonld.JsonLd;
import cz.cvut.kbss.termit.dto.RdfsResource;
import cz.cvut.kbss.termit.exception.importing.VocabularyImportException;
import cz.cvut.kbss.termit.model.Term;
import cz.cvut.kbss.termit.service.IdentifierResolver;
import cz.cvut.kbss.termit.service.export.util.TabularTermExportUtils;
Expand Down Expand Up @@ -63,6 +64,8 @@ class LocalizedSheetImporter {

private final Map<String, Term> labelToTerm = new LinkedHashMap<>();
private final Map<URI, Term> idToTerm = new HashMap<>();
// Identifiers discovered in this sheet
private final Set<URI> sheetIdentifiers = new HashSet<>();
private List<ExcelImporter.TermRelationship> rawDataToInsert;

LocalizedSheetImporter(Services services, PrefixMap prefixMap, List<Term> existingTerms,
Expand Down Expand Up @@ -139,11 +142,22 @@ private void findTerms(Sheet sheet) {
Term term = existingTerms.size() >= i ? existingTerms.get(i - 1) : new Term();
getAttributeValue(termRow, JsonLd.ID).ifPresent(id -> {
term.setUri(resolveTermUri(id));
if (sheetIdentifiers.contains(term.getUri())) {
throw new VocabularyImportException(
"Sheet " + sheet.getSheetName() + " contains multiple terms with the same identifier: " + id,
"error.vocabulary.import.excel.duplicateIdentifier");
}
idToTerm.put(term.getUri(), term);
sheetIdentifiers.add(term.getUri());
});
final Optional<String> label = getAttributeValue(termRow, SKOS.PREF_LABEL);
if (label.isPresent()) {
initSingularMultilingualString(term::getLabel, term::setLabel).set(langTag, label.get());
if (labelToTerm.containsKey(label.get())) {
throw new VocabularyImportException(
"Sheet " + sheet.getSheetName() + " contains multiple terms with the same label: " + label.get(),
"error.vocabulary.import.excel.duplicateLabel");
}
labelToTerm.put(label.get(), term);
} else {
if (i > existingTerms.size()) {
Expand Down Expand Up @@ -208,8 +222,8 @@ private void mapRowToTermAttributes(Term term, Row termRow) {
getAttributeValue(termRow, SKOS.EXACT_MATCH).ifPresent(
exm -> mapSkosMatchProperties(term, SKOS.EXACT_MATCH, splitIntoMultipleValues(exm)));
getAttributeValue(termRow, JsonLd.TYPE).flatMap(this::resolveTermType).ifPresent(t -> term.setTypes(Set.of(t)));
getAttributeValue(termRow, Vocabulary.s_p_ma_stav_pojmu).flatMap(this::resolveTermState)
.ifPresent(term::setState);
resolveTermState(getAttributeValue(termRow, Vocabulary.s_p_ma_stav_pojmu).orElse(null)).ifPresent(
term::setState);

}

Expand Down Expand Up @@ -290,10 +304,17 @@ private Optional<String> resolveTermType(String value) {
}

private Optional<URI> resolveTermState(String value) {
return languageService.getTermStates().stream()
.filter(t -> value.equals(t.getLabel().get(langTag)) || value.equals(
t.getUri().toString())).findFirst()
.map(RdfsResource::getUri);
if (value == null) {
return languageService.getInitialTermState().map(RdfsResource::getUri);
}
final Optional<URI> state = languageService.getTermStates().stream()
.filter(t -> value.equals(t.getLabel().get(langTag)) || value.equals(
t.getUri().toString())).findFirst()
.map(RdfsResource::getUri);
if (state.isPresent()) {
return state;
}
return languageService.getInitialTermState().map(RdfsResource::getUri);
}

List<ExcelImporter.TermRelationship> getRawDataToInsert() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.termit.service.export.util;
package cz.cvut.kbss.termit.util;

import cz.cvut.kbss.termit.util.TypeAwareResource;
import org.springframework.core.io.ByteArrayResource;

import java.util.Objects;
Expand Down Expand Up @@ -52,13 +51,12 @@ public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof TypeAwareByteArrayResource)) {
if (!(o instanceof TypeAwareByteArrayResource that)) {
return false;
}
if (!super.equals(o)) {
return false;
}
TypeAwareByteArrayResource that = (TypeAwareByteArrayResource) o;
return Objects.equals(mediaType, that.mediaType) &&
Objects.equals(fileExtension, that.fileExtension);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cz.cvut.kbss.termit.util;

import org.springframework.core.io.ClassPathResource;

import java.util.Optional;

/**
* Implementation of {@link TypeAwareResource} for files on classpath.
*/
public class TypeAwareClasspathResource extends ClassPathResource implements TypeAwareResource {

private final String mediaType;

public TypeAwareClasspathResource(String path, String mediaType) {
super(path);
this.mediaType = mediaType;
}

@Override
public Optional<String> getMediaType() {
return Optional.ofNullable(mediaType);
}

@Override
public Optional<String> getFileExtension() {
return getPath().contains(".") ? Optional.of(getPath().substring(getPath().lastIndexOf("."))) :
Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.termit.service.document.util;
package cz.cvut.kbss.termit.util;

import cz.cvut.kbss.termit.util.TypeAwareResource;
import org.springframework.core.io.FileSystemResource;

import java.io.File;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import cz.cvut.kbss.termit.service.IdentifierResolver;
import cz.cvut.kbss.termit.service.business.ResourceService;
import cz.cvut.kbss.termit.service.document.ResourceRetrievalSpecification;
import cz.cvut.kbss.termit.service.document.util.TypeAwareFileSystemResource;
import cz.cvut.kbss.termit.util.TypeAwareFileSystemResource;
import cz.cvut.kbss.termit.util.Configuration;
import cz.cvut.kbss.termit.util.Constants;
import cz.cvut.kbss.termit.util.Constants.QueryParams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import cz.cvut.kbss.termit.service.export.ExportConfig;
import cz.cvut.kbss.termit.service.export.ExportFormat;
import cz.cvut.kbss.termit.service.export.ExportType;
import cz.cvut.kbss.termit.service.export.util.TypeAwareByteArrayResource;
import cz.cvut.kbss.termit.util.TypeAwareByteArrayResource;
import cz.cvut.kbss.termit.util.Configuration;
import cz.cvut.kbss.termit.util.Constants;
import cz.cvut.kbss.termit.util.Constants.QueryParams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import cz.cvut.kbss.termit.rest.handler.ErrorInfo;
import cz.cvut.kbss.termit.service.IdentifierResolver;
import cz.cvut.kbss.termit.service.business.VocabularyService;
import cz.cvut.kbss.termit.service.document.util.TypeAwareFileSystemResource;
import cz.cvut.kbss.termit.util.TypeAwareFileSystemResource;
import cz.cvut.kbss.termit.util.Configuration;
import cz.cvut.kbss.termit.util.Constants;
import cz.cvut.kbss.termit.util.Constants.QueryParams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import cz.cvut.kbss.termit.service.document.DocumentManager;
import cz.cvut.kbss.termit.service.document.ResourceRetrievalSpecification;
import cz.cvut.kbss.termit.service.document.TextAnalysisService;
import cz.cvut.kbss.termit.service.export.util.TypeAwareByteArrayResource;
import cz.cvut.kbss.termit.util.TypeAwareByteArrayResource;
import cz.cvut.kbss.termit.service.repository.ChangeRecordService;
import cz.cvut.kbss.termit.service.repository.ResourceRepositoryService;
import cz.cvut.kbss.termit.util.TypeAwareResource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import cz.cvut.kbss.termit.service.export.ExportFormat;
import cz.cvut.kbss.termit.service.export.ExportType;
import cz.cvut.kbss.termit.service.export.VocabularyExporters;
import cz.cvut.kbss.termit.service.export.util.TypeAwareByteArrayResource;
import cz.cvut.kbss.termit.util.TypeAwareByteArrayResource;
import cz.cvut.kbss.termit.service.language.LanguageService;
import cz.cvut.kbss.termit.service.repository.ChangeRecordService;
import cz.cvut.kbss.termit.service.repository.TermRepositoryService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cz.cvut.kbss.termit.service.document.html;

import cz.cvut.kbss.termit.environment.Environment;
import cz.cvut.kbss.termit.service.export.util.TypeAwareByteArrayResource;
import cz.cvut.kbss.termit.util.TypeAwareByteArrayResource;
import cz.cvut.kbss.termit.util.TypeAwareResource;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
Expand Down
Loading