From dc01a7cc43df9601e7db0f306e746a0e9512dcf0 Mon Sep 17 00:00:00 2001
From: Moises Sacal <moisbo@gmail.com>
Date: Mon, 3 Jun 2019 10:24:55 +1000
Subject: [PATCH] used library createSolrObject in commit-to-solr and redme

---
 .idea/workspace.xml      | 152 +++++++++++++++++++--------------------
 README.md                |  20 +-----
 commit-to-solr.js        |  30 +-------
 lib/CatalogSolr.js       |  12 ++--
 test/catalog.lib.spec.js |   2 +-
 5 files changed, 85 insertions(+), 131 deletions(-)

diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index f588113..f3d990a 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -2,10 +2,11 @@
 <project version="4">
   <component name="ChangeListManager">
     <list default="true" id="ad75bb9b-12db-4cad-af64-4c68cecdb87b" name="Default Changelist" comment="">
-      <change afterPath="$PROJECT_DIR$/test/catalog.lib.spec.js" afterDir="false" />
       <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.spec.js" beforeDir="false" afterPath="$PROJECT_DIR$/test/catalog.create.spec.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/" />
@@ -21,26 +22,30 @@
       <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="236">
-              <caret line="31" column="12" selection-start-line="31" selection-start-column="8" selection-end-line="31" selection-end-column="12" />
+            <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$/test/catalog.create.spec.js">
+        <entry file="file://$PROJECT_DIR$/commit-to-solr.js">
           <provider selected="true" editor-type-id="text-editor">
-            <state relative-caret-position="431">
-              <caret line="148" column="46" selection-start-line="148" selection-start-column="42" selection-end-line="148" selection-end-column="46" />
+            <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">
-        <entry file="file://$PROJECT_DIR$/test/catalog.lib.spec.js">
-          <provider selected="true" editor-type-id="text-editor">
-            <state relative-caret-position="495">
-              <caret line="33" column="3" lean-forward="true" selection-start-line="33" selection-start-column="3" selection-end-line="33" selection-end-column="3" />
+      <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="632">
+                <caret line="54" selection-start-line="54" selection-end-line="54" />
+              </first_editor>
+              <second_editor />
             </state>
           </provider>
         </entry>
@@ -48,26 +53,17 @@
       <file pinned="false" current-in-tab="false">
         <entry file="file://$PROJECT_DIR$/lib/CatalogSolr.js">
           <provider selected="true" editor-type-id="text-editor">
-            <state relative-caret-position="530">
-              <caret line="166" column="7" lean-forward="true" selection-start-line="166" selection-start-column="7" selection-end-line="166" selection-end-column="7" />
-            </state>
-          </provider>
-        </entry>
-      </file>
-      <file pinned="false" current-in-tab="true">
-        <entry file="file://$PROJECT_DIR$/test-data/FARMTOFREEWAYS_CATALOG.json">
-          <provider selected="true" editor-type-id="text-editor">
-            <state relative-caret-position="227">
-              <caret line="16" column="15" lean-forward="true" selection-start-line="16" selection-start-column="15" selection-end-line="16" selection-end-column="15" />
+            <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="355">
-              <caret line="76" column="20" selection-start-line="76" selection-start-column="8" selection-end-line="76" selection-end-column="20" />
+            <state relative-caret-position="405">
+              <caret line="27" selection-start-line="27" selection-end-line="29" selection-end-column="62" />
             </state>
           </provider>
         </entry>
@@ -100,11 +96,11 @@
         <option value="$PROJECT_DIR$/generate-datacrates.js" />
         <option value="$PROJECT_DIR$/config.json" />
         <option value="$PROJECT_DIR$/package.json" />
-        <option value="$PROJECT_DIR$/README.md" />
-        <option value="$PROJECT_DIR$/commit-to-solr.js" />
-        <option value="$PROJECT_DIR$/test/catalog.lib.spec.js" />
-        <option value="$PROJECT_DIR$/lib/CatalogSolr.js" />
         <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>
     </option>
   </component>
@@ -120,6 +116,7 @@
       <foldersAlwaysOnTop value="true" />
     </navigator>
     <panes>
+      <pane id="Scope" />
       <pane id="ProjectPane">
         <subPane>
           <expand>
@@ -150,7 +147,6 @@
           <select />
         </subPane>
       </pane>
-      <pane id="Scope" />
     </panes>
   </component>
   <component name="PropertiesComponent">
@@ -182,7 +178,7 @@
       </list>
     </option>
   </component>
-  <component name="RunManager" selected="Mocha.catalog.create.spec.js">
+  <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>
@@ -231,56 +227,57 @@
       <option name="presentableId" value="Default" />
       <updated>1558421147646</updated>
       <workItem from="1558421149207" duration="30478000" />
-      <workItem from="1558654598476" duration="88527000" />
+      <workItem from="1558654598476" duration="88833000" />
+      <workItem from="1559519708231" duration="1338000" />
     </task>
     <servers />
   </component>
   <component name="TestHistory">
-    <history-entry file="catalog_lib_spec_js - 2019.05.31 at 16h 34m 16s.xml">
+    <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_lib_spec_js - 2019.05.31 at 16h 35m 51s.xml">
+    <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_lib_spec_js - 2019.05.31 at 16h 40m 01s.xml">
+    <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_lib_spec_js - 2019.05.31 at 16h 40m 19s.xml">
+    <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_lib_spec_js - 2019.05.31 at 16h 41m 31s.xml">
+    <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_lib_spec_js - 2019.05.31 at 16h 42m 34s.xml">
+    <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_lib_spec_js - 2019.05.31 at 16h 43m 10s.xml">
-      <configuration name="catalog.lib.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_lib_spec_js - 2019.05.31 at 16h 43m 23s.xml">
+    <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_lib_spec_js - 2019.05.31 at 16h 44m 11s.xml">
+    <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_lib_spec_js - 2019.05.31 at 16h 44m 21s.xml">
+    <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="119005000" />
+    <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 active="true" anchor="bottom" id="Debug" order="3" visible="true" 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" />
@@ -330,16 +327,6 @@
         </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 />
-        </state>
-      </provider>
-    </entry>
     <entry file="file://$PROJECT_DIR$/config.json">
       <provider selected="true" editor-type-id="text-editor">
         <state relative-caret-position="135">
@@ -347,13 +334,6 @@
         </state>
       </provider>
     </entry>
-    <entry file="file://$PROJECT_DIR$/test-data/CATALOG.json">
-      <provider selected="true" editor-type-id="text-editor">
-        <state relative-caret-position="236">
-          <caret line="31" column="12" selection-start-line="31" selection-start-column="8" selection-end-line="31" selection-end-column="12" />
-        </state>
-      </provider>
-    </entry>
     <entry file="file://$PROJECT_DIR$/generate-datacrates.js">
       <provider selected="true" editor-type-id="text-editor">
         <state relative-caret-position="375">
@@ -382,38 +362,56 @@
         </state>
       </provider>
     </entry>
-    <entry file="file://$PROJECT_DIR$/commit-to-solr.js">
+    <entry file="file://$PROJECT_DIR$/test-data/FARMTOFREEWAYS_CATALOG.json">
       <provider selected="true" editor-type-id="text-editor">
-        <state relative-caret-position="355">
-          <caret line="76" column="20" selection-start-line="76" selection-start-column="8" selection-end-line="76" 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$/test/catalog.create.spec.js">
       <provider selected="true" editor-type-id="text-editor">
-        <state relative-caret-position="431">
-          <caret line="148" column="46" selection-start-line="148" selection-start-column="42" selection-end-line="148" selection-end-column="46" />
+        <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$/test-data/CATALOG.json">
+      <provider selected="true" editor-type-id="text-editor">
+        <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="530">
-          <caret line="166" column="7" lean-forward="true" selection-start-line="166" selection-start-column="7" selection-end-line="166" selection-end-column="7" />
+        <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="495">
-          <caret line="33" column="3" lean-forward="true" selection-start-line="33" selection-start-column="3" selection-end-line="33" selection-end-column="3" />
+        <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$/test-data/FARMTOFREEWAYS_CATALOG.json">
+    <entry file="file://$PROJECT_DIR$/commit-to-solr.js">
       <provider selected="true" editor-type-id="text-editor">
-        <state relative-caret-position="227">
-          <caret line="16" column="15" lean-forward="true" selection-start-line="16" selection-start-column="15" selection-end-line="16" selection-end-column="15" />
+        <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 bcb42a2..8ec21bd 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 a42d0fa..87ad0a7 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 3984a54..80f92b0 100644
--- a/lib/CatalogSolr.js
+++ b/lib/CatalogSolr.js
@@ -144,24 +144,24 @@ class CatalogSolr {
 
   }
 
-  createSolrObject(catalog, jsonld, root, config) {
+  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[root], (g) => {
-      return catalog.ensureObjArray(g);
+    const graph = _.each(jsonld[rootConfig], (g) => {
+      return this.ensureObjArray(g);
     });
 
     const solrObject = {};
-    _.each(config, (field, type) => {
+    _.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(catalog.getGraphElement(config[type], graph, ge));
+            solrObject[type].push(this.getGraphElement(this.config[type], graph, ge));
           } else {
-            solrObject[type] = [catalog.getGraphElement(config[type], graph, ge)];
+            solrObject[type] = [this.getGraphElement(this.config[type], graph, ge)];
           }
         });
       }
diff --git a/test/catalog.lib.spec.js b/test/catalog.lib.spec.js
index 51939d4..25fc0f2 100644
--- a/test/catalog.lib.spec.js
+++ b/test/catalog.lib.spec.js
@@ -27,7 +27,7 @@ describe('create solr object', function () {
   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(catalog, ca, '@graph', fields);
+    const solrObject = catalog.createSolrObject(ca, '@graph');
 
     assert.strictEqual(solrObject['Dataset'][0]['record_format_s'], 'Dataset','Dataset not loaded');
   });
-- 
GitLab