Skip to content

Commit

Permalink
#4550 - Clean up code
Browse files Browse the repository at this point in the history
- Clean up code in the document export module
  • Loading branch information
reckart committed Feb 24, 2024
1 parent 8833985 commit 4291e36
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ public void setUp() throws Exception
new CasStorageCachePropertiesImpl(), null, schemaService));

var xmiFormatSupport = new XmiFormatSupport(new XmiFormatProperties());
importExportSerivce = new DocumentImportExportServiceImpl(repositoryProperties,
asList(xmiFormatSupport), casStorageService, schemaService, properties,
checksRegistry, repairsRegistry, xmiFormatSupport);
importExportSerivce = new DocumentImportExportServiceImpl(asList(xmiFormatSupport),
casStorageService, schemaService, properties, checksRegistry, repairsRegistry,
xmiFormatSupport);

// Dynamically generate a SourceDocument with an incrementing ID when asked for one
when(documentService.getSourceDocument(any(), any())).then(invocation -> {
Expand Down
9 changes: 5 additions & 4 deletions inception/inception-export/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@
<groupId>de.tudarmstadt.ukp.inception.app</groupId>
<artifactId>inception-export-api</artifactId>
</dependency>
<dependency>
<groupId>de.tudarmstadt.ukp.inception.app</groupId>
<artifactId>inception-documents-api</artifactId>
</dependency>
<dependency>
<groupId>de.tudarmstadt.ukp.inception.app</groupId>
<artifactId>inception-annotation-storage-api</artifactId>
Expand Down Expand Up @@ -146,6 +142,11 @@


<!-- TEST DEPENDENCIES -->
<dependency>
<groupId>de.tudarmstadt.ukp.inception.app</groupId>
<artifactId>inception-documents-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.tudarmstadt.ukp.inception.app</groupId>
<artifactId>inception-schema</artifactId>
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;

import de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedTag;
import de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedTagSet;
import de.tudarmstadt.ukp.clarin.webanno.model.Project;
import de.tudarmstadt.ukp.clarin.webanno.model.Tag;
Expand Down Expand Up @@ -52,25 +50,23 @@ public static TagSet importTagSetFromJsonWithOverwrite(Project aProject,
InputStream aInputStream, AnnotationSchemaService aAnnotationService)
throws IOException
{
ExportedTagSet importedTagSet = JSONUtil.fromJsonStream(ExportedTagSet.class, aInputStream);
var importedTagSet = JSONUtil.fromJsonStream(ExportedTagSet.class, aInputStream);

if (aAnnotationService.existsTagSet(importedTagSet.getName(), aProject)) {
// A tagset exists so we'll have to replace it
return replaceTagSet(aProject, importedTagSet, aAnnotationService);
}
else {
// Proceed normally
return createTagSet(aProject, importedTagSet, aAnnotationService);
}

// Proceed normally
return createTagSet(aProject, importedTagSet, aAnnotationService);
}

private static TagSet replaceTagSet(Project project, ExportedTagSet importedTagSet,
AnnotationSchemaService aAnnotationService)
throws IOException
{
String importedTagSetName = importedTagSet.getName();
de.tudarmstadt.ukp.clarin.webanno.model.TagSet tagsetInUse = aAnnotationService
.getTagSet(importedTagSetName, project);
var importedTagSetName = importedTagSet.getName();
var tagsetInUse = aAnnotationService.getTagSet(importedTagSetName, project);
// Remove all tags associated with Tagset
aAnnotationService.removeAllTags(tagsetInUse);
// Copy and update TagSet Information from imported tagset
Expand All @@ -82,8 +78,8 @@ private static TagSet replaceTagSet(Project project, ExportedTagSet importedTagS

// Add all tags from imported tagset
int rank = 0;
for (ExportedTag tag : importedTagSet.getTags()) {
Tag newTag = new Tag();
for (var tag : importedTagSet.getTags()) {
var newTag = new Tag();
newTag.setDescription(tag.getDescription());
newTag.setName(tag.getName());
newTag.setRank(rank);
Expand All @@ -99,33 +95,32 @@ public static TagSet importTagSetFromJson(Project project, InputStream tagInputS
AnnotationSchemaService aAnnotationService)
throws IOException
{
String text = IOUtils.toString(tagInputStream, "UTF-8");
var text = IOUtils.toString(tagInputStream, "UTF-8");

ExportedTagSet importedTagSet = JSONUtil.getObjectMapper().readValue(text,
ExportedTagSet.class);
var importedTagSet = JSONUtil.getObjectMapper().readValue(text, ExportedTagSet.class);
return createTagSet(project, importedTagSet, aAnnotationService);
}

public static TagSet createTagSet(Project project, ExportedTagSet aExTagSet,
AnnotationSchemaService aAnnotationService)
throws IOException
{
String exTagSetName = aExTagSet.getName();
var exTagSetName = aExTagSet.getName();
if (aAnnotationService.existsTagSet(exTagSetName, project)) {
exTagSetName = copyTagSetName(aAnnotationService, exTagSetName, project);
}

TagSet newTagSet = new TagSet();
var newTagSet = new TagSet();
newTagSet.setDescription(aExTagSet.getDescription());
newTagSet.setName(exTagSetName);
newTagSet.setLanguage(aExTagSet.getLanguage());
newTagSet.setProject(project);
newTagSet.setCreateTag(aExTagSet.isCreateTag());
aAnnotationService.createTagSet(newTagSet);

List<Tag> tags = new ArrayList<>();
for (ExportedTag exTag : aExTagSet.getTags()) {
Tag tag = new Tag();
var tags = new ArrayList<Tag>();
for (var exTag : aExTagSet.getTags()) {
var tag = new Tag();
tag.setDescription(exTag.getDescription());
tag.setTagSet(newTagSet);
tag.setName(exTag.getName());
Expand All @@ -152,16 +147,15 @@ public static TagSet createTagSet(Project project, ExportedTagSet aExTagSet,
public static String copyTagSetName(AnnotationSchemaService aAnnotationService, String aName,
Project aProject)
{
String betterTagSetName = "copy_of_" + aName;
var betterTagSetName = "copy_of_" + aName;
int i = 1;
while (true) {
if (aAnnotationService.existsTagSet(betterTagSetName, aProject)) {
betterTagSetName = "copy_of_" + aName + "(" + i + ")";
i++;
}
else {
if (!aAnnotationService.existsTagSet(betterTagSetName, aProject)) {
return betterTagSetName;
}

betterTagSetName = "copy_of_" + aName + "(" + i + ")";
i++;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,12 @@ public static String exportLayerToJson(AnnotationSchemaService aSchemaService,
*/
public static Map<String, String> getTagSetFromFile(String aLineSeparatedTags)
{
Map<String, String> tags = new LinkedHashMap<>();
StringTokenizer st = new StringTokenizer(aLineSeparatedTags, "\n");
var tags = new LinkedHashMap<String, String>();
var st = new StringTokenizer(aLineSeparatedTags, "\n");
while (st.hasMoreTokens()) {
StringTokenizer stTag = new StringTokenizer(st.nextToken(), "\t");
String tag = stTag.nextToken();
String description;
if (stTag.hasMoreTokens()) {
description = stTag.nextToken();
}
else {
description = tag;
}
var stTag = new StringTokenizer(st.nextToken(), "\t");
var tag = stTag.nextToken();
var description = stTag.hasMoreTokens() ? stTag.nextToken() : tag;
tags.put(tag.trim(), description);
}
return tags;
Expand All @@ -118,7 +112,7 @@ private static void createTagSet(TagSet aTagSet, ExportedTagSet aExTagSet, Proje
aTagSet.setProject(aProject);
aAnnotationService.createTagSet(aTagSet);

for (ExportedTag exTag : aExTagSet.getTags()) {
for (var exTag : aExTagSet.getTags()) {
// do not duplicate tag
if (aAnnotationService.existsTag(exTag.getName(), aTagSet)) {
continue;
Expand All @@ -141,20 +135,23 @@ private static void setLayer(AnnotationSchemaService aAnnotationService, Annotat
aLayer.setCrossSentence(aExLayer.isCrossSentence());
aLayer.setDescription(aExLayer.getDescription());
aLayer.setEnabled(aExLayer.isEnabled());

if (aExLayer.getAnchoringMode() == null) {
// This allows importing old projects which did not have the anchoring mode yet
aLayer.setAnchoringMode(aExLayer.isLockToTokenOffset(), aExLayer.isMultipleTokens());
}
else {
aLayer.setAnchoringMode(aExLayer.getAnchoringMode());
}

if (aExLayer.getOverlapMode() == null) {
// This allows importing old projects which did not have the overlap mode yet
aLayer.setOverlapMode(aExLayer.isAllowStacking() ? ANY_OVERLAP : OVERLAP_ONLY);
}
else {
aLayer.setOverlapMode(aExLayer.getOverlapMode());
}

aLayer.setValidationMode(aExLayer.getValidationMode() != null ? aExLayer.getValidationMode()
: ValidationMode.NEVER);
aLayer.setLinkedListBehavior(aExLayer.isLinkedListBehavior());
Expand All @@ -163,20 +160,21 @@ private static void setLayer(AnnotationSchemaService aAnnotationService, Annotat
aLayer.setProject(aProject);
aLayer.setType(aExLayer.getType());
aLayer.setTraits(aExLayer.getTraits());

aAnnotationService.createOrUpdateLayer(aLayer);
}

public static AnnotationLayer importLayerFile(AnnotationSchemaService annotationService,
User user, Project project, InputStream aIS)
throws IOException
{
String text = IOUtils.toString(aIS, "UTF-8");
var text = IOUtils.toString(aIS, "UTF-8");

var exLayers = JSONUtil.getObjectMapper().readValue(text, ExportedAnnotationLayer[].class);

// First import the layers but without setting the attach-layers/features
Map<String, ExportedAnnotationLayer> exLayersMap = new HashMap<>();
Map<String, AnnotationLayer> layersMap = new HashMap<>();
var exLayersMap = new HashMap<String, ExportedAnnotationLayer>();
var layersMap = new HashMap<String, AnnotationLayer>();
for (var exLayer : exLayers) {
var layer = createLayer(annotationService, project, exLayer);
layersMap.put(layer.getName(), layer);
Expand Down Expand Up @@ -254,7 +252,8 @@ private static void setFeature(AnnotationSchemaService aAnnotationService,
aFeature.setUiName(aExFeature.getUiName());
aFeature.setProject(aProject);
aFeature.setName(aExFeature.getName());
boolean isItChainedLayer = CHAIN_TYPE.equals(aFeature.getLayer().getType());

var isItChainedLayer = CHAIN_TYPE.equals(aFeature.getLayer().getType());
if (isItChainedLayer && (COREFERENCE_TYPE_FEATURE.equals(aExFeature.getName())
|| COREFERENCE_RELATION_FEATURE.equals(aExFeature.getName()))) {
aFeature.setType(CAS.TYPE_NAME_STRING);
Expand All @@ -268,6 +267,7 @@ else if (Token._TypeName.equals(aFeature.getLayer().getName())
else {
aFeature.setType(aExFeature.getType());
}

aFeature.setRemember(aExFeature.isRemember());
aFeature.setRequired(aExFeature.isRequired());
aFeature.setHideUnconstraintFeature(aExFeature.isHideUnconstraintFeature());
Expand All @@ -279,6 +279,7 @@ else if (Token._TypeName.equals(aFeature.getLayer().getName())
aFeature.setTraits(aExFeature.getTraits());
aFeature.setCuratable(aExFeature.isCuratable());
aFeature.setRank(aExFeature.getRank());

aAnnotationService.createFeature(aFeature);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Set;

import org.apache.commons.io.IOUtils;

Expand All @@ -37,14 +35,14 @@ public static TagSet importTagsetFromTabSeparated(AnnotationSchemaService annota
Project project, InputStream aInputStream, boolean aOverwrite)
throws IOException
{
String text = IOUtils.toString(aInputStream, UTF_8);
Map<String, String> tabbedTagsetFromFile = LayerImportExportUtils.getTagSetFromFile(text);
var text = IOUtils.toString(aInputStream, UTF_8);
var tabbedTagsetFromFile = LayerImportExportUtils.getTagSetFromFile(text);

Set<String> listOfTagsFromFile = tabbedTagsetFromFile.keySet();
int i = 0;
String tagSetName = "";
String tagSetDescription = "";
String tagsetLanguage = "";
var listOfTagsFromFile = tabbedTagsetFromFile.keySet();
var i = 0;
var tagSetName = "";
var tagSetDescription = "";
var tagsetLanguage = "";
de.tudarmstadt.ukp.clarin.webanno.model.TagSet tagSet = null;
for (String key : listOfTagsFromFile) {
// the first key is the tagset name and its
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import de.tudarmstadt.ukp.clarin.webanno.api.format.FormatSupport;
import de.tudarmstadt.ukp.clarin.webanno.diag.ChecksRegistry;
import de.tudarmstadt.ukp.clarin.webanno.diag.RepairsRegistry;
import de.tudarmstadt.ukp.inception.documents.api.RepositoryProperties;
import de.tudarmstadt.ukp.inception.export.DocumentImportExportServiceImpl;
import de.tudarmstadt.ukp.inception.export.exporters.ProjectLogExporter;
import de.tudarmstadt.ukp.inception.export.exporters.ProjectMetaInfExporter;
Expand All @@ -44,16 +43,14 @@ public class DocumentImportExportServiceAutoConfiguration
{
@Bean
public DocumentImportExportService documentImportExportService(
RepositoryProperties aRepositoryProperties,
@Lazy @Autowired(required = false) List<FormatSupport> aFormats,
CasStorageService aCasStorageService, AnnotationSchemaService aAnnotationService,
DocumentImportExportServiceProperties aServiceProperties,
ChecksRegistry aChecksRegistry, RepairsRegistry aRepairsRegistry,
XmiFormatSupport fallbackFormat)
{
return new DocumentImportExportServiceImpl(aRepositoryProperties, aFormats,
aCasStorageService, aAnnotationService, aServiceProperties, aChecksRegistry,
aRepairsRegistry, fallbackFormat);
return new DocumentImportExportServiceImpl(aFormats, aCasStorageService, aAnnotationService,
aServiceProperties, aChecksRegistry, aRepairsRegistry, fallbackFormat);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -63,8 +61,8 @@ public void exportData(FullProjectExportRequest aRequest, ProjectExportTaskMonit
ExportedProject aExProject, File aStage)
throws IOException
{
Project project = aRequest.getProject();
File logDir = new File(aStage + LOG_FOLDER);
var project = aRequest.getProject();
var logDir = new File(aStage + LOG_FOLDER);
FileUtils.forceMkdir(logDir);
if (projectService.getProjectLogFile(project).exists()) {
FileUtils.copyFileToDirectory(projectService.getProjectLogFile(project), logDir);
Expand All @@ -86,12 +84,11 @@ public void importData(ProjectImportRequest aRequest, Project aProject,
ExportedProject aExProject, ZipFile aZip)
throws IOException
{
for (Enumeration<? extends ZipEntry> zipEnumerate = aZip.entries(); zipEnumerate
.hasMoreElements();) {
ZipEntry entry = zipEnumerate.nextElement();
for (var zipEnumerate = aZip.entries(); zipEnumerate.hasMoreElements();) {
var entry = zipEnumerate.nextElement();

// Strip leading "/" that we had in ZIP files prior to 2.0.8 (bug #985)
String entryName = ZipUtils.normalizeEntryName(entry);
var entryName = ZipUtils.normalizeEntryName(entry);

if (entryName.startsWith(LOG + "/")) {
FileUtils.copyInputStreamToFile(aZip.getInputStream(entry),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -64,9 +62,9 @@ public void exportData(FullProjectExportRequest aRequest, ProjectExportTaskMonit
ExportedProject aExProject, File aStage)
throws IOException
{
File metaInfDir = new File(aStage + META_INF);
var metaInfDir = new File(aStage + META_INF);
FileUtils.forceMkdir(metaInfDir);
File metaInf = projectService.getMetaInfFolder(aRequest.getProject());
var metaInf = projectService.getMetaInfFolder(aRequest.getProject());
if (metaInf.exists()) {
FileUtils.copyDirectory(metaInf, metaInfDir);
}
Expand All @@ -87,15 +85,14 @@ public void importData(ProjectImportRequest aRequest, Project aProject,
ExportedProject aExProject, ZipFile aZip)
throws Exception
{
for (Enumeration<? extends ZipEntry> zipEnumerate = aZip.entries(); zipEnumerate
.hasMoreElements();) {
ZipEntry entry = zipEnumerate.nextElement();
for (var zipEnumerate = aZip.entries(); zipEnumerate.hasMoreElements();) {
var entry = zipEnumerate.nextElement();

// Strip leading "/" that we had in ZIP files prior to 2.0.8 (bug #985)
String entryName = ZipUtils.normalizeEntryName(entry);
var entryName = ZipUtils.normalizeEntryName(entry);

if (entryName.startsWith(META_INF_FOLDER + "/")) {
File metaInfDir = new File(projectService.getMetaInfFolder(aProject),
var metaInfDir = new File(projectService.getMetaInfFolder(aProject),
FilenameUtils.getPath(entry.getName().replace(META_INF_FOLDER + "/", "")));
// where the file reside in the META-INF/... directory
FileUtils.forceMkdir(metaInfDir);
Expand Down
Loading

0 comments on commit 4291e36

Please sign in to comment.