Commit 7d09390c authored by Andrey Bezugliy's avatar Andrey Bezugliy
Browse files

Added:

- `loadParticipantsCsv` to allow loading data for sample apps, which use participation mixin
  (see @unitybase/dom package)

Modified:
- Exposed `getModelName` function for helping developing application-level data loading helpers
- Modules `dataLoader` and `csv1` - required via `@unitybase/base`
parent dbf1eb87
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptLibraryMappings">
<file url="file://$PROJECT_DIR$" libraries="{UB_modules}" />
<file url="PROJECT" libraries="{UB_modules}" />
<file url="file://$PROJECT_DIR$" libraries="{ECMAScript 6, Node.js Core, UB_modules}" />
<includedPredefinedLibrary name="ECMAScript 6" />
<includedPredefinedLibrary name="Node.js Core" />
</component>
</project>
\ No newline at end of file
......@@ -2,9 +2,6 @@
<library name="UB_modules" type="javaScript">
<properties>
<option name="frameworkName" value="node_modules" />
<sourceFilesUrls>
<item url="file://$UB_HOME$/node_modules" />
</sourceFilesUrls>
</properties>
<CLASSES>
<root url="file://$UB_HOME$/node_modules" />
......
# 1.2.0
Added:
- `loadParticipantsCsv` to allow loading data for sample apps, which use participation mixin
(see @unitybase/dom package)
Modified:
- Exposed `getModelName` function for helping developing application-level data loading helpers
- Modules `dataLoader` and `csv1` - required via `@unitybase/base`
# 1.1.1
- Patched `loadAdm` to recognized users and groups
......@@ -4,7 +4,7 @@ This package helps initialize UnityBase models with less code written.
# Entity data initialization
There `loadCsv` function loads data from CSV file using minial possible parameters passed, for example:
The `loadCsv` function loads data from CSV file using minial possible parameters passed, for example:
```javascript
const csvLoaderUtils = require('@unitybase/csv-loader-utils');
......@@ -27,6 +27,27 @@ The method assumes that CSV file names with the data follows the pattern: `<enti
for example `ubm_desktop_adm-bpm.csv`
## Participants mixin
Participants cannot be loaded using standard methods, because they are managed via cusotm methods `addParticipant`
and `removeParticipant` of the corresponding entities.
In order to help that, there is a function `loadParticipantsCsv`.
It may be used as the following:
```javascript
dataLoader.loadParticipantsCsv(conn, __dirname, 'doc_Document');
```
In such a case, the `_initialData` folder supposed to have a file named `doc_Document_ppt-doc.csv` with content
like the following:
```csv
documentID;subjectID;role
document1;user1;owner
document1;user1;owner
```
# Localization
......
const
fs = require('fs'),
path = require('path'),
_ = require('lodash'),
dataLoader = require('dataLoader'),
COMMON_UB_MODEL_PREFIX = 'ub_model_';
dataLoader = require('@unitybase/base/dataLoader'),
csv = require('@unitybase/base/csv1');
const COMMON_UB_MODEL_PREFIX = 'ub_model_';
/**
* @param {number} entityNameColIndex Index of column with entity name.
......@@ -60,7 +64,7 @@ function _doBuildDelegateCommand(functionNameColIndex, row) {
* @param dirName
* @returns {*}
*/
function _getModelName(dirName) {
function getModelName(dirName) {
let modelName = path.basename(dirName);
if (modelName === 'locale') {
dirName = path.dirname(dirName);
......@@ -90,7 +94,7 @@ function _getModelName(dirName) {
function loadCsv(conn, dirName, entityName, attrs, mapping, tranLen) {
console.log(`\t\tCreating "${entityName}" instances...`);
const fileName = `${dirName}/${entityName}-${_getModelName(dirName)}.csv`;
const fileName = `${dirName}/${entityName}-${getModelName(dirName)}.csv`;
dataLoader.loadSimpleCSVData(conn, fileName, entityName, attrs, mapping, 1, null, tranLen);
}
......@@ -108,7 +112,7 @@ function _mapAttributeValue(mapping, row, index) {
} else if (_.isFunction(curMapObj)) {
return curMapObj(row);
} else {
throw new Error('Invalid mapping definition in element #' + a);
throw new Error('Invalid mapping definition in element #' + index);
}
}
......@@ -140,7 +144,7 @@ function loadLocaleFromCsv(session, dirName, locale, entityName, attrs, keyAttrs
const
mapping = attrs.map((a, i) => i),
lang = path.basename(locale).split('^')[0],
fileName = `${dirName}/${lang}^${entityName}-${_getModelName(dirName)}.csv`,
fileName = `${dirName}/${lang}^${entityName}-${getModelName(dirName)}.csv`,
csvData = /** @type Array<Array> */ loadCsvAsArray(fileName);
const
......@@ -168,8 +172,6 @@ function loadLocaleFromCsv(session, dirName, locale, entityName, attrs, keyAttrs
dataLoader.localizeEntity(session, localizationConfig, locale);
}
// TODO: remove as soon as Pasha add this to dataLoader module
const csv = require('csv1'), fs = require('fs');
/**
* Load data from CSV with delimiter (";" by default)
* @param {string} fileName Full path to file
......@@ -228,24 +230,25 @@ function loadAdm(conn, dirName, entityName) {
*
* @example
* Example of usage:
* dataLoader.loadArrayData(
* conn, [
*
* dataLoader.loadArrayData(
* conn, [
* [
* 'bpm_desktop', false, 'bpm_Definitions', 'bpm_ProcessDefinition', 'Process Definitions', null, 10,
* ['name', 'engineType', 'externalID']
* ],
* ],
* 'ubm_navshortcut',
* ['desktopID', 'isFolder', 'parentID', 'code', 'caption', 'iconCls', 'displayOrder', 'cmdCode'],
* [
* 'bpm_desktop', false, 'bpm_Definitions', 'bpm_ProcessDefinition', 'Process Definitions', null, 10,
* ['name', 'engineType', 'externalID']
* lookup(conn, 'ubm_desktop', 'code', 0),
* 1,
* lookup(conn, 'ubm_navshortcut', 'code', 2),
* 3, 4, 5, 6,
* buildShowListCommand(3, 7, 8)
* ],
* ],
* 'ubm_navshortcut',
* ['desktopID', 'isFolder', 'parentID', 'code', 'caption', 'iconCls', 'displayOrder', 'cmdCode'],
* [
* lookup(conn, 'ubm_desktop', 'code', 0),
* 1,
* lookup(conn, 'ubm_navshortcut', 'code', 2),
* 3, 4, 5, 6,
* buildShowListCommand(3, 7, 8)
* ],
* 1000
* );
* 1000
* );
*
* This will generate JSON for show-list command with specified list of columns. Entity name is taken from 3 column.
*/
......@@ -261,18 +264,19 @@ function buildShowListCommand(entityNameColIndex, fieldListColIndex, orderByColI
*
* @example
* Example of usage:
* dataLoader.loadArrayData(
* conn,
* [['bpm_desktop', false, 'camunda', 'camunda_Database-sync', 'Synchronization', null, 10]],
* 'ubm_navshortcut',
* ['desktopID', 'isFolder', 'parentID', 'code', 'caption', 'iconCls', 'displayOrder'],
* [
* dataLoader.lookup(conn, 'ubm_desktop', 'code', 0), 1,
* dataLoader.lookup(conn, 'ubm_navshortcut', 'code', 2), 3, 4, 5, 6,
* loaderUtils.buildDelegateCommand(7)
* ],
* 1000
* );
*
* dataLoader.loadArrayData(
* conn,
* [['bpm_desktop', false, 'camunda', 'camunda_Database-sync', 'Synchronization', null, 10]],
* 'ubm_navshortcut',
* ['desktopID', 'isFolder', 'parentID', 'code', 'caption', 'iconCls', 'displayOrder'],
* [
* dataLoader.lookup(conn, 'ubm_desktop', 'code', 0), 1,
* dataLoader.lookup(conn, 'ubm_navshortcut', 'code', 2), 3, 4, 5, 6,
* loaderUtils.buildDelegateCommand(7)
* ],
* 1000
* );
*
* This will generate JSON for show-form command with specified entity and form code.
*/
......@@ -287,18 +291,19 @@ function buildDelegateCommand(functionNameColIndex) {
*
* @example
* Example of usage:
* dataLoader.loadArrayData(
* conn,
* [['bpm_desktop', false, 'camunda', 'camunda_Database-sync', 'Synchronization', null, 10]],
* 'ubm_navshortcut',
* ['desktopID', 'isFolder', 'parentID', 'code', 'caption', 'iconCls', 'displayOrder'],
* [
* dataLoader.lookup(conn, 'ubm_desktop', 'code', 0), 1,
* dataLoader.lookup(conn, 'ubm_navshortcut', 'code', 2), 3, 4, 5, 6,
* loaderUtils.buildShowFormCommand(3)
* ],
* 1000
* );
*
* dataLoader.loadArrayData(
* conn,
* [['bpm_desktop', false, 'camunda', 'camunda_Database-sync', 'Synchronization', null, 10]],
* 'ubm_navshortcut',
* ['desktopID', 'isFolder', 'parentID', 'code', 'caption', 'iconCls', 'displayOrder'],
* [
* dataLoader.lookup(conn, 'ubm_desktop', 'code', 0), 1,
* dataLoader.lookup(conn, 'ubm_navshortcut', 'code', 2), 3, 4, 5, 6,
* loaderUtils.buildShowFormCommand(3)
* ],
* 1000
* );
*
* This will generate JSON for show-form command with specified entity and form code.
*/
......@@ -340,4 +345,73 @@ function buildCommand(cmdTypeColIndex, entityNameColIndex, fieldListColIndex, or
formCodeColIndex);
}
module.exports = {loadCsv, localize, loadAdm, buildCommand, buildShowListCommand, buildShowFormCommand, buildDelegateCommand};
/**
* This is a function, which loads participants (see @unitybase/dom package, participation entity mixin).
* @param {UBConnection} conn
* @param {string} dirName Pass "__dirname" of module. Is used to automatically determine model name.
* It prevents from copy-paste mistakes.
* @param {string} entityName
*
* The CSV file MUST have exactly 3 columns in the exact following order:
* - location (code)
* - subject (user name or role name)
* - role (participation role - this is an enum code, see more info in the participation mixin documencation.
*
* @example
* Example of usage:
* dataLoader.loadParticipantsCsv(conn, __dirname, 'hh_Location');
*
* This will load file hh_Location_ppt-<model-dir>.csv and add participations to the hh_Location entity.
*/
function loadParticipantsCsv(conn, dirName, entityName) {
console.log(`\t\tLoading participants for "${entityName}" instances...`);
const fileName = `${dirName}/${entityName}_ppt-${getModelName(dirName)}.csv`;
let fContent = fs.readFileSync(fileName);
if (!fContent) {
throw new Error('File ' + fileName + ' is empty or not exist')
}
fContent = fContent.trim();
const csvData = csv.parse(fContent, ';');
if (!Array.isArray(csvData)) {
throw new Error('Invalid CSV format or file ' + fileName + ' not found')
}
const startRow = 1;
if (csvData.length < startRow) {
throw new Error('Length of CSVData (' + csvData.length + ') is smaller then startRow' + startRow)
}
if (startRow > 0) {
csvData.splice(0, startRow);
}
const locationLookup = dataLoader.lookup(conn, entityName, 'code', 0);
const subjectLookup = dataLoader.lookup(conn, 'uba_subject', 'code', 1);
csvData.forEach(r => {
const locationID = locationLookup(r);
if (typeof locationID !== 'number') throw new Error(`Location "${r[0]}" does not exist`);
const subjectID = subjectLookup(r);
if (typeof subjectID !== 'number') throw new Error(`Subject "${r[1]}" does not exist`);
conn.query({
entity: entityName,
method: 'addParticipant',
execParams: {ID: locationID, subjectID, role: r[2]}
});
});
}
module.exports = {
loadCsv,
localize,
loadAdm,
loadParticipantsCsv,
buildCommand,
buildShowListCommand,
buildShowFormCommand,
buildDelegateCommand,
getModelName
};
......@@ -3,9 +3,14 @@
"version": "1.1.1",
"description": "Extension of '@unitybase/dataLoader' package, which contains helper methods to initialize UnityBase models from csv files.",
"dependencies": {
"@unitybase/base": "^4.2.11",
"lodash": "^4"
},
"devDependencies": {},
"devDependencies": {
"@unitybase/base": "^4.2.11",
"@unitybase/ub": "^4.0.31",
"@unitybase/stubs": "^4.1.5"
},
"engines": {
"UnityBase": ">=4.0"
},
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment