Search in sources :

Example 1 with LICENCE

use of org.finos.waltz.schema.tables.Licence.LICENCE in project waltz by khartec.

the class SpdxLicenceImporter method importData.

private void importData(String path) throws IOException, URISyntaxException {
    List<SpdxLicence> spdxLicences = parseData(path);
    Timestamp now = DateTimeUtilities.nowUtcTimestamp();
    // fetching existing licences
    Set<String> existingExternalIds = licenceDao.findAll().stream().filter(l -> l.externalId() != null).map(l -> l.externalId().get().toLowerCase()).collect(Collectors.toSet());
    // add only new ones
    List<LicenceRecord> records = spdxLicences.stream().filter(l -> !existingExternalIds.contains(l.licenseId().toLowerCase())).map(l -> {
        LicenceRecord record = dsl.newRecord(LICENCE);
        record.setName(l.name());
        record.setExternalId(l.licenseId());
        record.setProvenance(PROVENANCE);
        record.setApprovalStatus(ApprovalStatus.PENDING_APPROVAL.name());
        record.setCreatedAt(now);
        record.setCreatedBy("admin");
        record.setLastUpdatedAt(now);
        record.setLastUpdatedBy("admin");
        return record;
    }).collect(toList());
    int[] execute = dsl.batchStore(records).execute();
    System.out.println("Licence records stored to database: " + execute.length);
    // now create bookmarks from seeAlso section
    deleteExistingBookmarks();
    Map<String, LicenceRecord> licencesByExternalId = dsl.selectFrom(LICENCE).where(LICENCE.EXTERNAL_ID.isNotNull()).fetch().stream().collect(Collectors.toMap(l -> l.getExternalId(), l -> l));
    List<BookmarkRecord> bookmarks = spdxLicences.stream().flatMap(l -> {
        Long licenceId = licencesByExternalId.get(l.licenseId()).getId();
        Stream<BookmarkRecord> stream = Stream.of(l.seeAlso()).map(url -> {
            BookmarkRecord bookmarkRecord = dsl.newRecord(BOOKMARK);
            bookmarkRecord.setTitle("See Also");
            bookmarkRecord.setKind("DOCUMENTATION");
            bookmarkRecord.setUrl(url);
            bookmarkRecord.setParentKind(EntityKind.LICENCE.name());
            bookmarkRecord.setParentId(licenceId);
            bookmarkRecord.setIsPrimary(false);
            bookmarkRecord.setProvenance(PROVENANCE);
            bookmarkRecord.setLastUpdatedBy("admin");
            bookmarkRecord.setCreatedAt(now);
            bookmarkRecord.setUpdatedAt(now);
            bookmarkRecord.setIsRequired(false);
            return bookmarkRecord;
        });
        BookmarkRecord spdxRecord = dsl.newRecord(BOOKMARK);
        spdxRecord.setTitle("SPDX");
        spdxRecord.setKind("DOCUMENTATION");
        spdxRecord.setUrl(String.format(SPDX_LICENCE_TEMPLATE_URL, l.licenseId()));
        spdxRecord.setParentKind(EntityKind.LICENCE.name());
        spdxRecord.setParentId(licenceId);
        spdxRecord.setIsPrimary(false);
        spdxRecord.setProvenance(PROVENANCE);
        spdxRecord.setLastUpdatedBy("admin");
        spdxRecord.setCreatedAt(now);
        spdxRecord.setUpdatedAt(now);
        spdxRecord.setIsRequired(false);
        return Stream.concat(stream, Stream.of(spdxRecord));
    }).collect(toList());
    int[] bookmarkStoreExecute = dsl.batchStore(bookmarks).execute();
    System.out.println("Bookmark records stored: " + bookmarkStoreExecute.length);
}
Also used : EntityKind(org.finos.waltz.model.EntityKind) URISyntaxException(java.net.URISyntaxException) DIConfiguration(org.finos.waltz.service.DIConfiguration) BookmarkRecord(org.finos.waltz.schema.tables.records.BookmarkRecord) BOOKMARK(org.finos.waltz.schema.tables.Bookmark.BOOKMARK) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) LICENCE(org.finos.waltz.schema.tables.Licence.LICENCE) LicenceDao(org.finos.waltz.data.licence.LicenceDao) Map(java.util.Map) DSLContext(org.jooq.DSLContext) URI(java.net.URI) Path(java.nio.file.Path) Files(java.nio.file.Files) Timestamp(java.sql.Timestamp) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) ApprovalStatus(org.finos.waltz.model.ApprovalStatus) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) DateTimeUtilities(org.finos.waltz.common.DateTimeUtilities) Paths(java.nio.file.Paths) Optional(java.util.Optional) LicenceRecord(org.finos.waltz.schema.tables.records.LicenceRecord) LicenceRecord(org.finos.waltz.schema.tables.records.LicenceRecord) Timestamp(java.sql.Timestamp) Stream(java.util.stream.Stream) BookmarkRecord(org.finos.waltz.schema.tables.records.BookmarkRecord)

Example 2 with LICENCE

use of org.finos.waltz.schema.tables.Licence.LICENCE in project waltz by khartec.

the class SpdxLicenceImporter method parseData.

private List<SpdxLicence> parseData(String directoryPath) throws IOException, URISyntaxException {
    URI directoryUrl = this.getClass().getClassLoader().getResource(directoryPath).toURI();
    try (Stream<Path> paths = Files.walk(Paths.get(directoryUrl))) {
        List<SpdxLicence> spdxLicences = paths.filter(Files::isRegularFile).map(this::parseSpdxLicence).filter(l -> l.isPresent()).map(l -> l.get()).collect(toList());
        System.out.printf("Parsed %s SPDX licence files \n", spdxLicences.size());
        return spdxLicences;
    }
}
Also used : Path(java.nio.file.Path) EntityKind(org.finos.waltz.model.EntityKind) URISyntaxException(java.net.URISyntaxException) DIConfiguration(org.finos.waltz.service.DIConfiguration) BookmarkRecord(org.finos.waltz.schema.tables.records.BookmarkRecord) BOOKMARK(org.finos.waltz.schema.tables.Bookmark.BOOKMARK) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) LICENCE(org.finos.waltz.schema.tables.Licence.LICENCE) LicenceDao(org.finos.waltz.data.licence.LicenceDao) Map(java.util.Map) DSLContext(org.jooq.DSLContext) URI(java.net.URI) Path(java.nio.file.Path) Files(java.nio.file.Files) Timestamp(java.sql.Timestamp) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) ApprovalStatus(org.finos.waltz.model.ApprovalStatus) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) DateTimeUtilities(org.finos.waltz.common.DateTimeUtilities) Paths(java.nio.file.Paths) Optional(java.util.Optional) LicenceRecord(org.finos.waltz.schema.tables.records.LicenceRecord) Files(java.nio.file.Files) URI(java.net.URI)

Aggregations

DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 IOException (java.io.IOException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 Paths (java.nio.file.Paths)2 Timestamp (java.sql.Timestamp)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Collectors.toList (java.util.stream.Collectors.toList)2 Stream (java.util.stream.Stream)2 DateTimeUtilities (org.finos.waltz.common.DateTimeUtilities)2 LicenceDao (org.finos.waltz.data.licence.LicenceDao)2 ApprovalStatus (org.finos.waltz.model.ApprovalStatus)2 EntityKind (org.finos.waltz.model.EntityKind)2