|
| 1 | +(: |
| 2 | +This module converts an Excel (based on our template) to XMI form. |
| 3 | +We make the Excel just like UML. |
| 4 | +:) |
| 5 | + |
| 6 | +xquery version "1.0-ml"; |
| 7 | + |
| 8 | +module namespace xlsxm = "http://marklogic.com/xmi2es/xlsx/mapper"; |
| 9 | +import module namespace pt = "http://marklogic.com/xmi2es/problemTracker" at "/xmi2es/problemTracker.xqy"; |
| 10 | +import module namespace xlsx = "http://marklogic.com/xmi2es/xlsx" at "/xmi2es/excel2uml.xqy"; |
| 11 | + |
| 12 | +declare variable $FIRST-PROP-ROW := 13; |
| 13 | + |
| 14 | +(: |
| 15 | +Convert the excel to JSON. Put errors in $pt |
| 16 | +:) |
| 17 | +declare function xlsxm:convert($excel, $pt) as json:object { |
| 18 | + let $json := json:object() |
| 19 | + |
| 20 | + (: Get the stuff we need from the xlsx file :) |
| 21 | + let $stringTable := xdmp:zip-get($excel, "xl/sharedStrings.xml")/node() |
| 22 | + let $contents := xdmp:zip-get($excel, "[Content_Types].xml")/node() |
| 23 | + let $mappingSheet := |
| 24 | + if (exists($contents/*:Override[@PartName eq "/xl/worksheets/sheet2.xml"])) then xdmp:zip-get($excel, "xl/worksheets/sheet2.xml")/node() |
| 25 | + else pt:addProblem($pt, (), "excel", "No mapping sheet found", ()) |
| 26 | + let $entitySheets := |
| 27 | + for $sheet in $contents/*:Override[ |
| 28 | + @ContentType eq "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml" |
| 29 | + and @PartName ne "/xl/worksheets/sheet1.xml" |
| 30 | + and @PartName ne "/xl/worksheets/sheet2.xml"]/@PartName |
| 31 | + return xdmp:zip-get($excel, fn:substring($sheet, 2))/node() |
| 32 | + let $mapping := json:object() |
| 33 | + let $entities := json:object() |
| 34 | + let $_ := ( |
| 35 | + map:put($json, "mapping", $mapping), |
| 36 | + map:put($mapping, "source", xlsx:excelCell($mappingSheet, "mapping", $stringTable, "B1", $pt, ())), |
| 37 | + map:put($mapping, "notes", xlsx:excelCell($mappingSheet, "mapping", $stringTable, "B2", $pt, ())), |
| 38 | + map:put($json, "entities", $entities) |
| 39 | + ) |
| 40 | + let $_ := for $entitySheet at $pos in $entitySheets return |
| 41 | + let $entityName := xlsx:excelCell($entitySheet, concat("Sheet at ", ($pos + 2)), $stringTable, "B1", $xlsx:VAL-MANDATORY, ()) |
| 42 | + return |
| 43 | + if (string-length($entityName) eq 0) then () |
| 44 | + else if (map:contains($entities, $entityName)) then pt:addProblem($pt, (), $entityName, "Ignoring duplicate entity", ($pos + 2)) |
| 45 | + else |
| 46 | + let $thisEntity := json:object() |
| 47 | + let $thisAttributes := json:object() |
| 48 | + let $_ := map:put($entities, $entityName, $thisEntity) |
| 49 | + let $_ := map:put($thisEntity, "source", xlsx:excelCell($entitySheet, $entityName, $stringTable, "B2", $pt, ())) |
| 50 | + let $_ := map:put($thisEntity, "notes", xlsx:excelCell($entitySheet, $entityName, $stringTable, "B3", $pt, ())) |
| 51 | + let $_ := map:put($thisEntity, "discoveryCollections", xlsx:excelCell($entitySheet, $entityName, $stringTable, "B4", $pt, (), $xlsx:DELIM-COMMA-LINE)) |
| 52 | + let $_ := map:put($thisEntity, "discoveryURIPatterns", xlsx:excelCell($entitySheet, $entityName, $stringTable, "B5", $pt, ())) |
| 53 | + let $_ := map:put($thisEntity, "discoverySampleData", xlsx:excelCell($entitySheet, $entityName, $stringTable, "B6", $pt, ())) |
| 54 | + let $_ := map:put($thisEntity, "attributes", $thisAttributes) |
| 55 | + for $row in $FIRST-PROP-ROW to xlsx:excelLastRow($entitySheet, $entityName, $stringTable, $FIRST-PROP-ROW, $pt) return |
| 56 | + let $attribName := xlsx:excelCell($entitySheet, $entityName, $stringTable, "A"||$row, $pt, $xlsx:VAL-MANDATORY) |
| 57 | + return |
| 58 | + if (string-length($attribName) eq 0) then () |
| 59 | + else if (map:contains($thisAttributes, $attribName)) then pt:addProblem($pt, (), $attribName, "Ignoring duplicate attribute", $row) |
| 60 | + else |
| 61 | + let $thisAttrib := json:object() |
| 62 | + return ( |
| 63 | + map:put($thisAttributes, $attribName, $thisAttrib), |
| 64 | + map:put($thisAttrib, "mapping", xlsx:excelCell($entitySheet, $entityName, $stringTable, "B" || $row, $pt, ())), |
| 65 | + map:put($thisAttrib, "notes", xlsx:excelCell($entitySheet, $entityName, $stringTable, "C" || $row, $pt, ())), |
| 66 | + map:put($thisAttrib, "discoverySampleData", xlsx:excelCell($entitySheet, $entityName, $stringTable, "D" || $row, $pt, ())), |
| 67 | + map:put($thisAttrib, "discoveryAKA", xlsx:excelCell($entitySheet, $entityName, $stringTable, "E" || $row, $pt, (), $xlsx:DELIM-COMMA-LINE)) |
| 68 | + ) |
| 69 | + |
| 70 | + return $json |
| 71 | +}; |
| 72 | + |
| 73 | +(: |
| 74 | +We ingest the spreadsheet and convert it to a JSON. |
| 75 | +Expected |
| 76 | +:) |
| 77 | +declare function xlsxm:transform( |
| 78 | + $content as map:map, |
| 79 | + $context as map:map |
| 80 | +) as map:map* { |
| 81 | + let $excelURI := map:get($content, "uri") |
| 82 | + let $excelDoc := map:get($content, "value") |
| 83 | + let $docName := substring-before(substring-after($excelURI,"/xmi2es/excel-mapper/"), ".xlsx") |
| 84 | + let $jsonURI := concat("/xmi2es/excel-mapper/", $docName, ".json") |
| 85 | + |
| 86 | + (: convert Excel to XMI :) |
| 87 | + let $problems := pt:init() |
| 88 | + let $jsonDoc := xlsxm:convert($excelDoc, $problems) |
| 89 | + |
| 90 | + (: Return original content, transformed content, problems during conversion:) |
| 91 | + return ($content, |
| 92 | + map:new(( |
| 93 | + map:entry("uri", concat("/xmi2es/excel-mapper/findings/", $docName, ".xml")), |
| 94 | + map:entry("value", pt:dumpProblems($problems)))), |
| 95 | + map:new(( |
| 96 | + map:entry("uri", $jsonURI), |
| 97 | + map:entry("value",xdmp:to-json($jsonDoc))))) |
| 98 | +}; |
0 commit comments