-
Notifications
You must be signed in to change notification settings - Fork 6
[NAE 1843] Import/Export services #296
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
Draft
SamuelPalaj
wants to merge
10
commits into
release/7.0.0
Choose a base branch
from
NAE-1843
base: release/7.0.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f317ee7
[NAE-1843] Import/Export services
SamuelPalaj 3bbb443
Merge branch 'refs/heads/release/7.0.0' into NAE-1843
SamuelPalaj 0c106a2
[NAE-1843] Import/Export services
SamuelPalaj 799c8b8
[NAE-1843] Import/Export services
SamuelPalaj e560c5b
[NAE-1843] Import/Export services
SamuelPalaj 62d8c2b
[NAE-1843] Import/Export services
SamuelPalaj 6bd9663
[NAE-1843] Import/Export services
SamuelPalaj 4e9e16f
[NAE-1843] Import/Export services
SamuelPalaj c1c0515
[NAE-1843] Import/Export services
SamuelPalaj 4dbf102
[NAE-1843] Import/Export services
SamuelPalaj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
146 changes: 146 additions & 0 deletions
146
src/main/java/com/netgrif/application/engine/archive/ZipService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package com.netgrif.application.engine.archive; | ||
|
||
import com.netgrif.application.engine.archive.interfaces.IArchiveService; | ||
import com.netgrif.application.engine.files.IStorageResolverService; | ||
import com.netgrif.application.engine.files.interfaces.IStorageService; | ||
import com.netgrif.application.engine.petrinet.domain.dataset.StorageField; | ||
import com.netgrif.application.engine.workflow.domain.CaseExportFiles; | ||
import com.netgrif.application.engine.workflow.domain.StorageFieldWithFileNames; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.*; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.UUID; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipInputStream; | ||
import java.util.zip.ZipOutputStream; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class ZipService implements IArchiveService { | ||
|
||
private final IStorageResolverService storageResolverService; | ||
|
||
@Override | ||
public void pack(String archivePath, CaseExportFiles caseExportFiles, String... additionalFiles) throws IOException { | ||
FileOutputStream fos = new FileOutputStream(archivePath); | ||
this.pack(fos, caseExportFiles, additionalFiles); | ||
fos.close(); | ||
} | ||
|
||
@Override | ||
public void pack(OutputStream archiveStream, CaseExportFiles caseExportFiles, String... additionalFiles) throws IOException { | ||
ZipOutputStream zipStream = new ZipOutputStream(archiveStream); | ||
for (String caseId : caseExportFiles.getCaseIds()) { | ||
for (StorageFieldWithFileNames fieldWithFileNames : caseExportFiles.getFieldsOfCase(caseId)) { | ||
StorageField<?> field = fieldWithFileNames.getField(); | ||
IStorageService storageService = storageResolverService.resolve(field.getStorageType()); | ||
for (String fileName : fieldWithFileNames.getFileNames()) { | ||
String filePath = storageService.getPath(caseId, field.getStringId(), fileName); | ||
InputStream fis = storageService.get(field, filePath); | ||
String newFileName = Paths.get(caseId, field.getStringId(), fileName).getFileName().toString(); | ||
createAndWriteZipEntry(newFileName, zipStream, fis); | ||
fis.close(); | ||
} | ||
} | ||
} | ||
for (String filePath : additionalFiles) { | ||
InputStream fis = new FileInputStream(filePath); | ||
createAndWriteZipEntry(Paths.get(filePath).getFileName().toString(), zipStream, fis); | ||
fis.close(); | ||
} | ||
zipStream.close(); | ||
} | ||
|
||
@Override | ||
public OutputStream createArchive(CaseExportFiles caseExportFiles) throws IOException { | ||
return createArchive(Files.createTempFile(UUID.randomUUID().toString(), ".zip").toString(), caseExportFiles); | ||
} | ||
|
||
@Override | ||
public OutputStream createArchive(String archivePath, CaseExportFiles caseExportFiles) throws IOException { | ||
File zipFile = new File(archivePath); | ||
return zipFiles(zipFile, caseExportFiles); | ||
} | ||
|
||
private OutputStream zipFiles(File zipFile, CaseExportFiles caseExportFiles) throws IOException { | ||
FileOutputStream fos = new FileOutputStream(zipFile); | ||
this.pack(fos, caseExportFiles); | ||
return fos; | ||
} | ||
|
||
@Override | ||
public void append(OutputStream archiveStream, String... filePaths) throws IOException { | ||
// todo implement | ||
} | ||
|
||
private void createAndWriteZipEntry(String fileName, ZipOutputStream zipStream, InputStream fis) throws IOException { | ||
// source https://www.baeldung.com/java-compress-and-uncompress | ||
ZipEntry zipEntry = new ZipEntry(fileName); | ||
zipStream.putNextEntry(zipEntry); | ||
byte[] bytes = new byte[1024]; | ||
int length; | ||
while ((length = fis.read(bytes)) >= 0) { | ||
zipStream.write(bytes, 0, length); | ||
} | ||
} | ||
|
||
@Override | ||
public String unpack(String archivePath, String outputPath) throws IOException { | ||
return this.unpack(new FileInputStream(archivePath), outputPath); | ||
} | ||
|
||
@Override | ||
public String unpack(InputStream archiveStream, String outputPath) throws IOException { | ||
// source: https://www.baeldung.com/java-compress-and-uncompress | ||
File destDir = new File(outputPath); | ||
|
||
byte[] buffer = new byte[1024]; | ||
ZipInputStream zis = new ZipInputStream(archiveStream); | ||
ZipEntry zipEntry = zis.getNextEntry(); | ||
while (zipEntry != null) { | ||
File newFile = newFile(destDir, zipEntry); | ||
if (zipEntry.isDirectory()) { | ||
if (!newFile.isDirectory() && !newFile.mkdirs()) { | ||
throw new IOException("Failed to create directory " + newFile); | ||
} | ||
} else { | ||
// fix for Windows-created archives | ||
File parent = newFile.getParentFile(); | ||
if (!parent.isDirectory() && !parent.mkdirs()) { | ||
throw new IOException("Failed to create directory " + parent); | ||
} | ||
|
||
// write file content | ||
FileOutputStream fos = new FileOutputStream(newFile); | ||
int len; | ||
while ((len = zis.read(buffer)) > 0) { | ||
fos.write(buffer, 0, len); | ||
} | ||
fos.close(); | ||
} | ||
zipEntry = zis.getNextEntry(); | ||
} | ||
|
||
zis.closeEntry(); | ||
zis.close(); | ||
return destDir.getPath(); | ||
} | ||
|
||
private File newFile(File destinationDir, ZipEntry zipEntry) throws IOException { | ||
File destFile = new File(destinationDir, zipEntry.getName()); | ||
|
||
String destDirPath = destinationDir.getCanonicalPath(); | ||
String destFilePath = destFile.getCanonicalPath(); | ||
|
||
if (!destFilePath.startsWith(destDirPath + File.separator)) { | ||
throw new IOException("Entry is outside of the target dir: " + zipEntry.getName()); | ||
} | ||
|
||
return destFile; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/netgrif/application/engine/archive/interfaces/IArchiveService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.netgrif.application.engine.archive.interfaces; | ||
|
||
import com.netgrif.application.engine.workflow.domain.CaseExportFiles; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
public interface IArchiveService { | ||
|
||
void pack(String archivePath, CaseExportFiles caseExportFiles, String... additionalFiles) throws IOException; | ||
|
||
void pack(OutputStream archiveStream, CaseExportFiles caseExportFiles, String... additionalFiles) throws IOException; | ||
|
||
OutputStream createArchive(CaseExportFiles caseExportFiles) throws IOException; | ||
|
||
OutputStream createArchive(String archivePath, CaseExportFiles caseExportFiles) throws IOException; | ||
|
||
void append(OutputStream archiveStream, String... filePaths) throws IOException; | ||
|
||
String unpack(String archivePath, String outputPath) throws IOException; | ||
|
||
String unpack(InputStream archiveStream, String outputPath) throws IOException; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...in/java/com/netgrif/application/engine/configuration/properties/CaseExportProperties.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.netgrif.application.engine.configuration.properties; | ||
|
||
import lombok.Data; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Data | ||
@Component | ||
@ConfigurationProperties(prefix = "nae.case.export") | ||
public class CaseExportProperties { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this a separate class? This class is used only in the properties bean. |
||
|
||
private String fileName; | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/netgrif/application/engine/configuration/properties/SchemaProperties.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.netgrif.application.engine.configuration.properties; | ||
|
||
|
||
import lombok.Data; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Data | ||
@Component | ||
@ConfigurationProperties(prefix = "nae.schema") | ||
public class SchemaProperties { | ||
|
||
private String location; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/com/netgrif/application/engine/utils/ImporterUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.netgrif.application.engine.utils; | ||
|
||
import com.netgrif.application.engine.importer.model.Tag; | ||
import com.netgrif.application.engine.petrinet.domain.I18nString; | ||
import com.netgrif.application.engine.petrinet.domain.dataset.logic.validation.DynamicValidation; | ||
import com.netgrif.application.engine.petrinet.domain.dataset.logic.validation.Validation; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class ImporterUtils { | ||
|
||
public static Map<String, String> buildTagsMap(List<Tag> tagsList) { | ||
Map<String, String> tags = new HashMap<>(); | ||
if (tagsList != null) { | ||
tagsList.forEach(tag -> { | ||
tags.put(tag.getKey(), tag.getValue()); | ||
}); | ||
} | ||
return tags; | ||
} | ||
|
||
public static Validation makeValidation(String rule, I18nString message, boolean dynamic) { | ||
return dynamic ? new DynamicValidation(rule, message) : new Validation(rule, message); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/com/netgrif/application/engine/workflow/domain/CaseExportFiles.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.netgrif.application.engine.workflow.domain; | ||
|
||
import java.util.*; | ||
|
||
public class CaseExportFiles { | ||
|
||
private final Map<String, List<StorageFieldWithFileNames>> caseFileMapping = new HashMap<>(); | ||
|
||
public void addFieldFilenames(String caseId, StorageFieldWithFileNames storageField) { | ||
List<StorageFieldWithFileNames> emptyFieldMapping = new ArrayList<>(); | ||
List<StorageFieldWithFileNames> fieldMapping = caseFileMapping.putIfAbsent(caseId, emptyFieldMapping); | ||
if (fieldMapping == null) { | ||
fieldMapping = emptyFieldMapping; | ||
} | ||
fieldMapping.add(storageField); | ||
} | ||
|
||
public Set<String> getCaseIds() { | ||
return caseFileMapping.keySet(); | ||
} | ||
|
||
public List<StorageFieldWithFileNames> getFieldsOfCase(String caseId) { | ||
return caseFileMapping.get(caseId); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/netgrif/application/engine/workflow/domain/StorageFieldWithFileNames.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.netgrif.application.engine.workflow.domain; | ||
|
||
import com.netgrif.application.engine.petrinet.domain.dataset.StorageField; | ||
import lombok.Data; | ||
|
||
import java.util.Set; | ||
|
||
@Data | ||
public class StorageFieldWithFileNames { | ||
|
||
private StorageField<?> field; | ||
private Set<String> fileNames; | ||
|
||
public StorageFieldWithFileNames(StorageField<?> field, Set<String> fileNames) { | ||
this.field = field; | ||
this.fileNames = fileNames; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.