diff --git a/openmetadata-service/src/main/java/org/openmetadata/csv/EntityCsv.java b/openmetadata-service/src/main/java/org/openmetadata/csv/EntityCsv.java index bb48917be28..4d8f20ec48b 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/csv/EntityCsv.java +++ b/openmetadata-service/src/main/java/org/openmetadata/csv/EntityCsv.java @@ -30,15 +30,18 @@ import java.util.List; import java.util.Map; import java.util.UUID; import javax.ws.rs.core.Response; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVFormat.Builder; import org.apache.commons.csv.CSVPrinter; import org.apache.commons.csv.CSVRecord; +import org.openmetadata.common.utils.CommonUtil; import org.openmetadata.schema.EntityInterface; import org.openmetadata.schema.type.EntityReference; import org.openmetadata.schema.type.Include; import org.openmetadata.schema.type.TagLabel; import org.openmetadata.schema.type.TagLabel.TagSource; +import org.openmetadata.schema.type.csv.CsvDocumentation; import org.openmetadata.schema.type.csv.CsvErrorType; import org.openmetadata.schema.type.csv.CsvFile; import org.openmetadata.schema.type.csv.CsvHeader; @@ -46,6 +49,8 @@ import org.openmetadata.schema.type.csv.CsvImportResult; import org.openmetadata.schema.type.csv.CsvImportResult.Status; import org.openmetadata.service.Entity; import org.openmetadata.service.jdbi3.EntityRepository; +import org.openmetadata.service.util.EntityUtil; +import org.openmetadata.service.util.JsonUtils; import org.openmetadata.service.util.RestUtil.PutResponse; /** @@ -53,6 +58,7 @@ import org.openmetadata.service.util.RestUtil.PutResponse; * provide entity specific processing functionality to export an entity to a CSV record, and import an entity from a CSV * record. */ +@Slf4j public abstract class EntityCsv { public static final String IMPORT_STATUS_HEADER = "status"; public static final String IMPORT_STATUS_DETAILS = "details"; @@ -120,6 +126,19 @@ public abstract class EntityCsv { return CsvUtil.formatCsv(csvFile); } + public static CsvDocumentation getCsvDocumentation(String entityType) { + LOG.info("Initializing CSV documentation for entity {}", entityType); + String path = String.format(".*json/data/%s/%sCsvDocumentation.json$", entityType, entityType); + try { + List jsonDataFiles = EntityUtil.getJsonDataResources(path); + String json = CommonUtil.getResourceAsStream(EntityRepository.class.getClassLoader(), jsonDataFiles.get(0)); + return JsonUtils.readValue(json, CsvDocumentation.class); + } catch (IOException e) { + LOG.error("FATAL - Failed to load CSV documentation for entity {} from the path {}", entityType, path); + } + return null; + } + /** Implement this method to turn an entity into a list of fields */ protected abstract List toRecord(T entity); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/GlossaryRepository.java b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/GlossaryRepository.java index e1e6e0a4000..c97898e945b 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/GlossaryRepository.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/GlossaryRepository.java @@ -45,6 +45,7 @@ import org.openmetadata.schema.type.ProviderType; import org.openmetadata.schema.type.Relationship; import org.openmetadata.schema.type.TagLabel; import org.openmetadata.schema.type.TagLabel.TagSource; +import org.openmetadata.schema.type.csv.CsvDocumentation; import org.openmetadata.schema.type.csv.CsvHeader; import org.openmetadata.schema.type.csv.CsvImportResult; import org.openmetadata.service.Entity; @@ -142,22 +143,12 @@ public class GlossaryRepository extends EntityRepository { } public static class GlossaryCsv extends EntityCsv { - public static final List HEADERS = new ArrayList<>(); + public static final CsvDocumentation DOCUMENTATION = getCsvDocumentation(Entity.GLOSSARY); + public static final List HEADERS = DOCUMENTATION.getHeaders(); private final Glossary glossary; - static { - HEADERS.add(new CsvHeader().withName("parent").withRequired(false)); - HEADERS.add(new CsvHeader().withName("name").withRequired(true)); - HEADERS.add(new CsvHeader().withName("displayName").withRequired(false)); - HEADERS.add(new CsvHeader().withName("description").withRequired(true)); - HEADERS.add(new CsvHeader().withName("synonyms").withRequired(false)); - HEADERS.add(new CsvHeader().withName("relatedTerms").withRequired(false)); - HEADERS.add(new CsvHeader().withName("references").withRequired(false)); - HEADERS.add(new CsvHeader().withName("tags").withRequired(false)); - } - GlossaryCsv(Glossary glossary, String user) { - super(Entity.GLOSSARY_TERM, HEADERS, user); + super(Entity.GLOSSARY_TERM, DOCUMENTATION.getHeaders(), user); this.glossary = glossary; } @@ -184,7 +175,7 @@ public class GlossaryRepository extends EntityRepository { } // Field 7 - TermReferences - glossaryTerm.withReferences(getTermReferences(printer, record, 6)); + glossaryTerm.withReferences(getTermReferences(printer, record)); if (!processRecord) { return null; } @@ -197,16 +188,15 @@ public class GlossaryRepository extends EntityRepository { return glossaryTerm; } - private List getTermReferences(CSVPrinter printer, CSVRecord record, int fieldNumber) - throws IOException { - String termRefs = record.get(fieldNumber); + private List getTermReferences(CSVPrinter printer, CSVRecord record) throws IOException { + String termRefs = record.get(6); if (nullOrEmpty(termRefs)) { return null; } List termRefList = CsvUtil.fieldToStrings(termRefs); if (termRefList.size() % 2 != 0) { // List should have even numbered terms - termName and endPoint - importFailure(printer, invalidField(fieldNumber, "Term references should termName;endpoint"), record); + importFailure(printer, invalidField(6, "Term references should termName;endpoint"), record); processRecord = false; return null; } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/glossary/GlossaryResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/glossary/GlossaryResource.java index 0d86e409703..739a61906de 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/glossary/GlossaryResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/glossary/GlossaryResource.java @@ -54,11 +54,13 @@ import org.openmetadata.schema.type.csv.CsvImportResult; import org.openmetadata.service.Entity; import org.openmetadata.service.jdbi3.CollectionDAO; import org.openmetadata.service.jdbi3.GlossaryRepository; +import org.openmetadata.service.jdbi3.GlossaryRepository.GlossaryCsv; import org.openmetadata.service.jdbi3.ListFilter; import org.openmetadata.service.resources.Collection; import org.openmetadata.service.resources.EntityResource; import org.openmetadata.service.resources.glossary.GlossaryTermResource.GlossaryTermList; import org.openmetadata.service.security.Authorizer; +import org.openmetadata.service.util.JsonUtils; import org.openmetadata.service.util.RestUtil; import org.openmetadata.service.util.ResultList; @@ -391,6 +393,15 @@ public class GlossaryResource extends EntityResource