• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kit-data-manager / ro-crate-java / #373

28 Apr 2025 09:57AM UTC coverage: 86.891% (+0.9%) from 85.949%
#373

Pull #247

github

web-flow
Merge a4e0ad782 into 0ab3f3ca9
Pull Request #247: Generalize reading and writing crates

249 of 278 new or added lines in 16 files covered. (89.57%)

2 existing lines in 2 files now uncovered.

1889 of 2174 relevant lines covered (86.89%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.71
/src/main/java/edu/kit/datamanager/ro_crate/writer/FolderStrategy.java
1
package edu.kit.datamanager.ro_crate.writer;
2

3
import com.fasterxml.jackson.databind.JsonNode;
4
import com.fasterxml.jackson.databind.ObjectMapper;
5
import edu.kit.datamanager.ro_crate.Crate;
6
import edu.kit.datamanager.ro_crate.entities.data.DataEntity;
7
import edu.kit.datamanager.ro_crate.objectmapper.MyObjectMapper;
8
import org.apache.commons.io.FileUtils;
9
import org.slf4j.Logger;
10
import org.slf4j.LoggerFactory;
11

12
import java.io.ByteArrayInputStream;
13
import java.io.File;
14
import java.io.IOException;
15
import java.io.InputStream;
16
import java.nio.charset.StandardCharsets;
17

18
/**
19
 * A class for writing a crate to a folder.
20
 *
21
 * @author Nikola Tzotchev on 9.2.2022 г.
22
 * @version 1
23
 */
24
public class FolderStrategy implements GenericWriterStrategy<String> {
1✔
25

26
    private static final Logger logger = LoggerFactory.getLogger(FolderStrategy.class);
1✔
27

28
    @Override
29
    public void save(Crate crate, String destination) {
30
        File file = new File(destination);
1✔
31
        try {
32
            FileUtils.forceMkdir(file);
1✔
33
            ObjectMapper objectMapper = MyObjectMapper.getMapper();
1✔
34
            JsonNode node = objectMapper.readTree(crate.getJsonMetadata());
1✔
35
            String str = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(node);
1✔
36
            InputStream inputStream = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
1✔
37

38
            File json = new File(destination, "ro-crate-metadata.json");
1✔
39
            FileUtils.copyInputStreamToFile(inputStream, json);
1✔
40
            inputStream.close();            
1✔
41
            // save also the preview files to the crate destination
42
            if (crate.getPreview() != null) {
1✔
43
                crate.getPreview().saveAllToFolder(file);
1✔
44
            }
45
            for (var e : crate.getUntrackedFiles()) {
1✔
46
                if (e.isDirectory()) {
1✔
47
                    FileUtils.copyDirectoryToDirectory(e, file);
1✔
48
                } else {
49
                    FileUtils.copyFileToDirectory(e, file);
1✔
50
                }
51
            }
1✔
NEW
52
        } catch (IOException e) {
×
NEW
53
            logger.error("Error creating destination directory!", e);
×
54
        }
1✔
55
        for (DataEntity dataEntity : crate.getAllDataEntities()) {
1✔
56
            try {
57
                dataEntity.savetoFile(file);
1✔
NEW
58
            } catch (IOException e) {
×
NEW
59
                logger.error("Cannot save " + dataEntity.getId() + " to destination folder!", e);
×
60
            }
1✔
61
        }
1✔
62
    }
1✔
63
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc