diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 7b5710675a83a45b5f4d18b37306096f4fec6c17..f3d990a634760f49960dff75acdcf61a8f3df3de 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,6 +3,10 @@ <component name="ChangeListManager"> <list default="true" id="ad75bb9b-12db-4cad-af64-4c68cecdb87b" name="Default Changelist" comment=""> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/README.md" beforeDir="false" afterPath="$PROJECT_DIR$/README.md" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/commit-to-solr.js" beforeDir="false" afterPath="$PROJECT_DIR$/commit-to-solr.js" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/lib/CatalogSolr.js" beforeDir="false" afterPath="$PROJECT_DIR$/lib/CatalogSolr.js" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/test/catalog.lib.spec.js" beforeDir="false" afterPath="$PROJECT_DIR$/test/catalog.lib.spec.js" afterDir="false" /> </list> <ignored path="$PROJECT_DIR$/.tmp/" /> <ignored path="$PROJECT_DIR$/temp/" /> @@ -15,30 +19,31 @@ </component> <component name="FileEditorManager"> <leaf SIDE_TABS_SIZE_LIMIT_KEY="300"> - <file pinned="false" current-in-tab="true"> - <entry file="file://$PROJECT_DIR$/lib/CatalogSolr.js"> + <file pinned="false" current-in-tab="false"> + <entry file="file://$PROJECT_DIR$/test-data/CATALOG.json"> <provider selected="true" editor-type-id="text-editor"> - <state relative-caret-position="150"> - <caret line="22" column="28" selection-start-line="22" selection-start-column="28" selection-end-line="22" selection-end-column="28" /> + <state relative-caret-position="435"> + <caret line="29" column="5" selection-start-line="29" selection-start-column="5" selection-end-line="29" selection-end-column="5" /> </state> </provider> </entry> </file> <file pinned="false" current-in-tab="false"> - <entry file="file://$PROJECT_DIR$/package.json"> + <entry file="file://$PROJECT_DIR$/commit-to-solr.js"> <provider selected="true" editor-type-id="text-editor"> - <state relative-caret-position="120"> - <caret line="8" column="28" selection-start-line="8" selection-end-line="9" /> + <state relative-caret-position="107"> + <caret line="69" column="22" selection-start-line="69" selection-start-column="22" selection-end-line="69" selection-end-column="22" /> + <caret line="70" column="18" lean-forward="true" selection-start-line="70" selection-start-column="18" selection-end-line="70" selection-end-column="18" /> </state> </provider> </entry> </file> - <file pinned="false" current-in-tab="false"> + <file pinned="false" current-in-tab="true"> <entry file="file://$PROJECT_DIR$/README.md"> <provider selected="true" editor-type-id="split-provider[text-editor;markdown-preview-editor]"> <state split_layout="SPLIT"> - <first_editor relative-caret-position="255"> - <caret line="24" lean-forward="true" selection-start-line="24" selection-end-line="24" /> + <first_editor relative-caret-position="632"> + <caret line="54" selection-start-line="54" selection-end-line="54" /> </first_editor> <second_editor /> </state> @@ -46,19 +51,19 @@ </entry> </file> <file pinned="false" current-in-tab="false"> - <entry file="file://$PROJECT_DIR$/generate-datacrates.js"> + <entry file="file://$PROJECT_DIR$/lib/CatalogSolr.js"> <provider selected="true" editor-type-id="text-editor"> - <state relative-caret-position="350"> - <caret line="25" column="13" selection-start-line="25" selection-start-column="13" selection-end-line="25" selection-end-column="13" /> + <state relative-caret-position="405"> + <caret line="154" column="42" lean-forward="true" selection-start-line="154" selection-start-column="42" selection-end-line="154" selection-end-column="42" /> </state> </provider> </entry> </file> <file pinned="false" current-in-tab="false"> - <entry file="file://$PROJECT_DIR$/commit-to-solr.js"> + <entry file="file://$PROJECT_DIR$/test/catalog.lib.spec.js"> <provider selected="true" editor-type-id="text-editor"> - <state relative-caret-position="1695"> - <caret line="113" column="11" lean-forward="true" selection-start-line="113" selection-start-column="11" selection-end-line="113" selection-end-column="11" /> + <state relative-caret-position="405"> + <caret line="27" selection-start-line="27" selection-end-line="29" selection-end-column="62" /> </state> </provider> </entry> @@ -82,17 +87,18 @@ <option value="$PROJECT_DIR$/lib/Catalog.js" /> <option value="$PROJECT_DIR$/test-data/recordTypes.json" /> <option value="$PROJECT_DIR$/test-data/CATALOG.json" /> - <option value="$PROJECT_DIR$/test-data/FARMTOFREEWAYS_CATALOG.json" /> <option value="$APPLICATION_CONFIG_DIR$/scratches/scratch_3.js" /> <option value="$PROJECT_DIR$/test-data/CatalogSolr.json" /> <option value="$PROJECT_DIR$/fields.json" /> <option value="$PROJECT_DIR$/test/catalog.spec.js" /> <option value="$PROJECT_DIR$/test-data/fields.json" /> - <option value="$PROJECT_DIR$/lib/CatalogSolr.js" /> <option value="$PROJECT_DIR$/.gitignore" /> <option value="$PROJECT_DIR$/generate-datacrates.js" /> <option value="$PROJECT_DIR$/config.json" /> <option value="$PROJECT_DIR$/package.json" /> + <option value="$PROJECT_DIR$/test-data/FARMTOFREEWAYS_CATALOG.json" /> + <option value="$PROJECT_DIR$/lib/CatalogSolr.js" /> + <option value="$PROJECT_DIR$/test/catalog.lib.spec.js" /> <option value="$PROJECT_DIR$/commit-to-solr.js" /> <option value="$PROJECT_DIR$/README.md" /> </list> @@ -110,6 +116,7 @@ <foldersAlwaysOnTop value="true" /> </navigator> <panes> + <pane id="Scope" /> <pane id="ProjectPane"> <subPane> <expand> @@ -140,7 +147,6 @@ <select /> </subPane> </pane> - <pane id="Scope" /> </panes> </component> <component name="PropertiesComponent"> @@ -172,14 +178,26 @@ </list> </option> </component> - <component name="RunManager" selected="Node.js.commit-to-solr"> + <component name="RunManager" selected="Mocha.catalog.lib.spec.js"> <configuration name="commit-to-solr" type="NodeJSConfigurationType" path-to-js-file="commit-to-solr.js" working-dir="$PROJECT_DIR$"> <method v="2" /> </configuration> <configuration name="generate-datacrates" type="NodeJSConfigurationType" application-parameters="--d $PROJECT_DIR$/../../peppermint/publication/public --n 100000" path-to-js-file="generate-datacrates.js" working-dir="$PROJECT_DIR$"> <method v="2" /> </configuration> - <configuration name="catalog.spec.js" type="mocha-javascript-test-runner"> + <configuration name="catalog.create.spec.js" type="mocha-javascript-test-runner"> + <node-interpreter>project</node-interpreter> + <node-options /> + <mocha-package>$PROJECT_DIR$/node_modules/mocha</mocha-package> + <working-directory>$PROJECT_DIR$</working-directory> + <pass-parent-env>true</pass-parent-env> + <ui>bdd</ui> + <extra-mocha-options /> + <test-kind>TEST_FILE</test-kind> + <test-file>$PROJECT_DIR$/test/catalog.create.spec.js</test-file> + <method v="2" /> + </configuration> + <configuration name="catalog.lib.spec.js" type="mocha-javascript-test-runner"> <node-interpreter>project</node-interpreter> <node-options /> <mocha-package>$PROJECT_DIR$/node_modules/mocha</mocha-package> @@ -188,11 +206,12 @@ <ui>bdd</ui> <extra-mocha-options /> <test-kind>TEST_FILE</test-kind> - <test-file>$PROJECT_DIR$/test/catalog.spec.js</test-file> + <test-file>$PROJECT_DIR$/test/catalog.lib.spec.js</test-file> <method v="2" /> </configuration> <list> - <item itemvalue="Mocha.catalog.spec.js" /> + <item itemvalue="Mocha.catalog.create.spec.js" /> + <item itemvalue="Mocha.catalog.lib.spec.js" /> <item itemvalue="Node.js.commit-to-solr" /> <item itemvalue="Node.js.generate-datacrates" /> </list> @@ -208,61 +227,63 @@ <option name="presentableId" value="Default" /> <updated>1558421147646</updated> <workItem from="1558421149207" duration="30478000" /> - <workItem from="1558654598476" duration="85668000" /> + <workItem from="1558654598476" duration="88833000" /> + <workItem from="1559519708231" duration="1338000" /> </task> <servers /> </component> <component name="TestHistory"> - <history-entry file="catalog_spec_js - 2019.05.29 at 14h 50m 34s.xml"> - <configuration name="catalog.spec.js" configurationId="mocha-javascript-test-runner" /> + <history-entry file="catalog_lib_spec_js - 2019.05.31 at 16h 41m 31s.xml"> + <configuration name="catalog.lib.spec.js" configurationId="mocha-javascript-test-runner" /> </history-entry> - <history-entry file="catalog_spec_js - 2019.05.29 at 15h 29m 39s.xml"> - <configuration name="catalog.spec.js" configurationId="mocha-javascript-test-runner" /> + <history-entry file="catalog_lib_spec_js - 2019.05.31 at 16h 42m 34s.xml"> + <configuration name="catalog.lib.spec.js" configurationId="mocha-javascript-test-runner" /> </history-entry> - <history-entry file="catalog_spec_js - 2019.05.29 at 15h 29m 51s.xml"> - <configuration name="catalog.spec.js" configurationId="mocha-javascript-test-runner" /> + <history-entry file="catalog_lib_spec_js - 2019.05.31 at 16h 43m 10s.xml"> + <configuration name="catalog.lib.spec.js" configurationId="mocha-javascript-test-runner" /> </history-entry> - <history-entry file="catalog_spec_js - 2019.05.29 at 15h 31m 49s.xml"> - <configuration name="catalog.spec.js" configurationId="mocha-javascript-test-runner" /> + <history-entry file="catalog_lib_spec_js - 2019.05.31 at 16h 43m 23s.xml"> + <configuration name="catalog.lib.spec.js" configurationId="mocha-javascript-test-runner" /> </history-entry> - <history-entry file="catalog_spec_js - 2019.05.29 at 15h 31m 51s.xml"> - <configuration name="catalog.spec.js" configurationId="mocha-javascript-test-runner" /> + <history-entry file="catalog_lib_spec_js - 2019.05.31 at 16h 44m 11s.xml"> + <configuration name="catalog.lib.spec.js" configurationId="mocha-javascript-test-runner" /> </history-entry> - <history-entry file="catalog_spec_js - 2019.05.29 at 15h 33m 16s.xml"> - <configuration name="catalog.spec.js" configurationId="mocha-javascript-test-runner" /> + <history-entry file="catalog_lib_spec_js - 2019.05.31 at 16h 44m 21s.xml"> + <configuration name="catalog.lib.spec.js" configurationId="mocha-javascript-test-runner" /> </history-entry> - <history-entry file="catalog_spec_js - 2019.05.29 at 15h 34m 30s.xml"> - <configuration name="catalog.spec.js" configurationId="mocha-javascript-test-runner" /> + <history-entry file="catalog_create_spec_js - 2019.05.31 at 16h 44m 51s.xml"> + <configuration name="catalog.create.spec.js" configurationId="mocha-javascript-test-runner" /> </history-entry> - <history-entry file="catalog_spec_js - 2019.05.29 at 15h 35m 20s.xml"> - <configuration name="catalog.spec.js" configurationId="mocha-javascript-test-runner" /> + <history-entry file="catalog_lib_spec_js - 2019.06.03 at 10h 15m 35s.xml"> + <configuration name="catalog.lib.spec.js" configurationId="mocha-javascript-test-runner" /> </history-entry> - <history-entry file="catalog_spec_js - 2019.05.29 at 15h 35m 32s.xml"> - <configuration name="catalog.spec.js" configurationId="mocha-javascript-test-runner" /> + <history-entry file="catalog_lib_spec_js - 2019.06.03 at 10h 15m 42s.xml"> + <configuration name="catalog.lib.spec.js" configurationId="mocha-javascript-test-runner" /> </history-entry> - <history-entry file="catalog_spec_js - 2019.05.29 at 15h 35m 38s.xml"> - <configuration name="catalog.spec.js" configurationId="mocha-javascript-test-runner" /> + <history-entry file="catalog_lib_spec_js - 2019.06.03 at 10h 15m 56s.xml"> + <configuration name="catalog.lib.spec.js" configurationId="mocha-javascript-test-runner" /> </history-entry> </component> <component name="TimeTrackingManager"> - <option name="totallyTimeSpent" value="116146000" /> + <option name="totallyTimeSpent" value="120649000" /> </component> <component name="ToolWindowManager"> - <frame x="1441" y="-277" width="1920" height="1177" extended-state="6" /> + <frame x="1440" y="-277" width="1920" height="1177" extended-state="6" /> + <editor active="true" /> <layout> - <window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.15654951" /> + <window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.1563981" /> <window_info id="Structure" order="1" side_tool="true" weight="0.25" /> <window_info id="Favorites" order="2" side_tool="true" /> <window_info anchor="bottom" id="Message" order="0" /> <window_info anchor="bottom" id="Find" order="1" /> <window_info anchor="bottom" id="Run" order="2" weight="0.3281106" /> - <window_info anchor="bottom" id="Debug" order="3" weight="0.28847927" /> + <window_info anchor="bottom" id="Debug" order="3" visible="true" weight="0.28847927" /> <window_info anchor="bottom" id="Cvs" order="4" weight="0.25" /> <window_info anchor="bottom" id="Inspection" order="5" weight="0.4" /> <window_info anchor="bottom" id="TODO" order="6" /> <window_info anchor="bottom" id="Docker" order="7" show_stripe_button="false" /> <window_info anchor="bottom" id="Version Control" order="8" /> - <window_info active="true" anchor="bottom" id="Terminal" order="9" visible="true" weight="0.23778802" /> + <window_info anchor="bottom" id="Terminal" order="9" weight="0.23778802" /> <window_info anchor="bottom" id="Event Log" order="10" side_tool="true" /> <window_info anchor="right" id="Commander" order="0" weight="0.4" /> <window_info anchor="right" id="Ant Build" order="1" weight="0.25" /> @@ -285,51 +306,37 @@ </state> </provider> </entry> - <entry file="file://$PROJECT_DIR$/test/catalog.spec.js"> - <provider selected="true" editor-type-id="text-editor"> - <state relative-caret-position="-1761"> - <caret column="20" selection-end-line="1" /> - </state> - </provider> - </entry> - <entry file="file://$PROJECT_DIR$/test-data/fields.json"> - <provider selected="true" editor-type-id="text-editor"> - <state relative-caret-position="75"> - <caret line="5" column="24" lean-forward="true" selection-start-line="5" selection-start-column="24" selection-end-line="5" selection-end-column="24" /> - </state> - </provider> - </entry> - <entry file="file://$PROJECT_DIR$/test-data/CATALOG.json"> + <entry file="file://$PROJECT_DIR$/test-data/CatalogSolr.json"> <provider selected="true" editor-type-id="text-editor"> - <state relative-caret-position="329"> - <caret line="45" column="9" selection-start-line="45" selection-start-column="9" selection-end-line="45" selection-end-column="9" /> + <state relative-caret-position="-17"> + <caret line="27" column="31" selection-start-line="27" selection-start-column="5" selection-end-line="27" selection-end-column="31" /> </state> </provider> </entry> - <entry file="file://$PROJECT_DIR$/test-data/CatalogSolr.json"> + <entry file="file://$PROJECT_DIR$/node_modules/lodash/lodash.js"> <provider selected="true" editor-type-id="text-editor"> - <state relative-caret-position="-17"> - <caret line="27" column="31" selection-start-line="27" selection-start-column="5" selection-end-line="27" selection-end-column="31" /> + <state relative-caret-position="489"> + <caret line="521" lean-forward="true" selection-start-line="521" selection-end-line="521" /> </state> </provider> </entry> - <entry file="file://$PROJECT_DIR$/test-data/FARMTOFREEWAYS_CATALOG.json"> + <entry file="file://$PROJECT_DIR$/.gitignore"> <provider selected="true" editor-type-id="text-editor"> - <state relative-caret-position="915"> - <caret line="61" column="20" selection-start-line="61" selection-start-column="20" selection-end-line="61" selection-end-column="20" /> + <state relative-caret-position="45"> + <caret line="3" column="20" lean-forward="true" selection-start-line="3" selection-start-column="20" selection-end-line="3" selection-end-column="20" /> </state> </provider> </entry> - <entry file="file://$PROJECT_DIR$/node_modules/lodash/lodash.js"> + <entry file="file://$PROJECT_DIR$/config.json"> <provider selected="true" editor-type-id="text-editor"> - <state relative-caret-position="489"> - <caret line="521" lean-forward="true" selection-start-line="521" selection-end-line="521" /> + <state relative-caret-position="135"> + <caret line="9" selection-start-line="9" selection-end-line="9" /> </state> </provider> </entry> <entry file="file://$PROJECT_DIR$/generate-datacrates.js"> <provider selected="true" editor-type-id="text-editor"> - <state relative-caret-position="350"> + <state relative-caret-position="375"> <caret line="25" column="13" selection-start-line="25" selection-start-column="13" selection-end-line="25" selection-end-column="13" /> </state> </provider> @@ -348,41 +355,63 @@ </state> </provider> </entry> - <entry file="file://$PROJECT_DIR$/commit-to-solr.js"> + <entry file="file://$PROJECT_DIR$/test-data/fields.json"> <provider selected="true" editor-type-id="text-editor"> - <state relative-caret-position="1695"> - <caret line="113" column="11" lean-forward="true" selection-start-line="113" selection-start-column="11" selection-end-line="113" selection-end-column="11" /> + <state relative-caret-position="75"> + <caret line="5" column="24" lean-forward="true" selection-start-line="5" selection-start-column="24" selection-end-line="5" selection-end-column="24" /> </state> </provider> </entry> - <entry file="file://$PROJECT_DIR$/.gitignore"> + <entry file="file://$PROJECT_DIR$/test-data/FARMTOFREEWAYS_CATALOG.json"> <provider selected="true" editor-type-id="text-editor"> - <state relative-caret-position="45"> - <caret line="3" column="20" lean-forward="true" selection-start-line="3" selection-start-column="20" selection-end-line="3" selection-end-column="20" /> + <state relative-caret-position="272"> + <caret line="19" column="8" lean-forward="true" selection-start-line="19" selection-start-column="8" selection-end-line="19" selection-end-column="8" /> </state> </provider> </entry> - <entry file="file://$PROJECT_DIR$/README.md"> - <provider selected="true" editor-type-id="split-provider[text-editor;markdown-preview-editor]"> - <state split_layout="SPLIT"> - <first_editor relative-caret-position="255"> - <caret line="24" lean-forward="true" selection-start-line="24" selection-end-line="24" /> - </first_editor> - <second_editor /> + <entry file="file://$PROJECT_DIR$/test/catalog.create.spec.js"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="273"> + <caret line="126" column="5" lean-forward="true" selection-start-line="126" selection-start-column="5" selection-end-line="126" selection-end-column="5" /> </state> </provider> </entry> - <entry file="file://$PROJECT_DIR$/config.json"> + <entry file="file://$PROJECT_DIR$/test-data/CATALOG.json"> <provider selected="true" editor-type-id="text-editor"> - <state relative-caret-position="135"> - <caret line="9" selection-start-line="9" selection-end-line="9" /> + <state relative-caret-position="435"> + <caret line="29" column="5" selection-start-line="29" selection-start-column="5" selection-end-line="29" selection-end-column="5" /> </state> </provider> </entry> <entry file="file://$PROJECT_DIR$/lib/CatalogSolr.js"> <provider selected="true" editor-type-id="text-editor"> - <state relative-caret-position="150"> - <caret line="22" column="28" selection-start-line="22" selection-start-column="28" selection-end-line="22" selection-end-column="28" /> + <state relative-caret-position="405"> + <caret line="154" column="42" lean-forward="true" selection-start-line="154" selection-start-column="42" selection-end-line="154" selection-end-column="42" /> + </state> + </provider> + </entry> + <entry file="file://$PROJECT_DIR$/test/catalog.lib.spec.js"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="405"> + <caret line="27" selection-start-line="27" selection-end-line="29" selection-end-column="62" /> + </state> + </provider> + </entry> + <entry file="file://$PROJECT_DIR$/commit-to-solr.js"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="107"> + <caret line="69" column="22" selection-start-line="69" selection-start-column="22" selection-end-line="69" selection-end-column="22" /> + <caret line="70" column="18" lean-forward="true" selection-start-line="70" selection-start-column="18" selection-end-line="70" selection-end-column="18" /> + </state> + </provider> + </entry> + <entry file="file://$PROJECT_DIR$/README.md"> + <provider selected="true" editor-type-id="split-provider[text-editor;markdown-preview-editor]"> + <state split_layout="SPLIT"> + <first_editor relative-caret-position="632"> + <caret line="54" selection-start-line="54" selection-end-line="54" /> + </first_editor> + <second_editor /> </state> </provider> </entry> diff --git a/README.md b/README.md index bcb42a23d69e9f08022900d4ab7fa00b95e9574b..8ec21bdcafed9d8b5d97bb87b75dbf9f3a157aa9 100644 --- a/README.md +++ b/README.md @@ -47,24 +47,6 @@ const entryPath = path.join('test-data','CATALOG.json'); let entryJson = fs.readFileSync(entryPath).toString(); entryJson = JSON.parse(entryJson); -const graph = _.each(ca['@graph'], (g) => { -return catalog.ensureObjArray(g); -}); - -const solrObject = {}; -_.each(fieldConfig, (field, name) => { -let graphElement = _.filter(graph, (g) => { - return _.find(g['@type'], (gg) => gg === name) ? g : undefined; -}); -if (graphElement) { - _.each(graphElement, (ge) => { - if (Array.isArray(solrObject[name])) { - solrObject[name].push(catalog.getGraphElement(fieldConfig[name], graph, ge)); - } else { - solrObject[name] = [catalog.getGraphElement(fieldConfig[name], graph, ge)]; - } - }); -} -}); +const solrObject = catalog.createSolrObject(entryJson, '@graph'); ``` diff --git a/commit-to-solr.js b/commit-to-solr.js index a42d0fa53405541606e06a30e970c1620dcfb1f1..87ad0a7733008661ee173349973a5b3f8ba9d2d1 100644 --- a/commit-to-solr.js +++ b/commit-to-solr.js @@ -62,38 +62,12 @@ function jsonRecords(basePath, dirs) { return records; } -function createCatalogSolr(catalog, ca) { - - //Peter's idea is to convert everything into an array then it is safer to work to convert - const graph = _.each(ca['@graph'], (g) => { - return catalog.ensureObjArray(g); - }); - - const catalogSolr = {}; - _.each(fieldConfig, (field, name) => { - let graphElement = _.filter(graph, (g) => { - return _.find(g['@type'], (gg) => gg === name) ? g : undefined; - }); - if (graphElement) { - _.each(graphElement, (ge) => { - if (Array.isArray(catalogSolr[name])) { - catalogSolr[name].push(catalog.getGraphElement(fieldConfig[name], graph, ge)); - } else { - catalogSolr[name] = [catalog.getGraphElement(fieldConfig[name], graph, ge)]; - } - }); - } - }); - - return catalogSolr; -} - function solrObjects(recs) { let catalog = new CatalogSolr(); catalog.setConfig(fieldConfig); const catalogs = []; - recs.forEach((rec) => { - const solrObj = createCatalogSolr(catalog, rec); + recs.forEach((record) => { + const solrObj = catalog.createSolrObject(record, '@graph'); if (solrObj) { if (solrObj.Dataset) { solrObj.Dataset.forEach((c) => { diff --git a/lib/CatalogSolr.js b/lib/CatalogSolr.js index 4ee3eec2680045160c006468ed7b32505d14cb65..80f92b0c5c034538f00ab5bc85a5fb5adb35f865 100644 --- a/lib/CatalogSolr.js +++ b/lib/CatalogSolr.js @@ -143,6 +143,32 @@ class CatalogSolr { return base; } + + createSolrObject(jsonld, rootConfig) { + + //Peter's idea is to convert everything into an array then it is safer to work to convert + const graph = _.each(jsonld[rootConfig], (g) => { + return this.ensureObjArray(g); + }); + + const solrObject = {}; + _.each(this.config, (field, type) => { + let graphElement = _.filter(graph, (g) => { + return _.find(g['@type'], (gg) => gg === type) ? g : undefined; + }); + if (graphElement) { + _.each(graphElement, (ge) => { + if (Array.isArray(solrObject[type])) { + solrObject[type].push(this.getGraphElement(this.config[type], graph, ge)); + } else { + solrObject[type] = [this.getGraphElement(this.config[type], graph, ge)]; + } + }); + } + }); + + return solrObject; + } } module.exports = CatalogSolr; \ No newline at end of file diff --git a/test/catalog.spec.js b/test/catalog.create.spec.js similarity index 100% rename from test/catalog.spec.js rename to test/catalog.create.spec.js diff --git a/test/catalog.lib.spec.js b/test/catalog.lib.spec.js new file mode 100644 index 0000000000000000000000000000000000000000..25fc0f260bf0f13a527628ae27b0a4856c853a6b --- /dev/null +++ b/test/catalog.lib.spec.js @@ -0,0 +1,34 @@ +const assert = require('assert'); +const _ = require('lodash'); +const path = require('path'); +const fs = require('fs-extra'); +const CatalogSolr = require('../lib/CatalogSolr'); + + +function jsonRecord(basePath, fileName) { + try { + const entryPath = path.join(basePath, fileName); + if (fs.existsSync(entryPath)) { + let entryJson = fs.readFileSync(entryPath).toString(); + return JSON.parse(entryJson); + } + } catch (e) { + console.error(e.message); + process.exit(-1); + } +} + +describe('create solr object', function () { + const fieldsPath = path.join(process.cwd(), 'test-data', 'fields.json'); + let catalog = new CatalogSolr(); + const fields = require(fieldsPath); + catalog.setConfig(fields); + + it('should use library to create a solr object', function () { + const ca = jsonRecord(process.cwd(), path.join('test-data', 'FARMTOFREEWAYS_CATALOG.json')); + + const solrObject = catalog.createSolrObject(ca, '@graph'); + + assert.strictEqual(solrObject['Dataset'][0]['record_format_s'], 'Dataset','Dataset not loaded'); + }); +}); \ No newline at end of file