Commit f8738fbb authored by Безуглый Андрей's avatar Безуглый Андрей
Browse files

Small regression after migration to UB5 fs.readFileSync - required second param

Code formatting
parent 568d9e2e
......@@ -6,20 +6,25 @@
<option name="SMART_TABS" value="true" />
</value>
</option>
<JSCodeStyleSettings>
<option name="USE_SEMICOLON_AFTER_STATEMENT" value="false" />
<option name="FORCE_SEMICOLON_STYLE" value="true" />
</JSCodeStyleSettings>
<XML>
<option name="XML_LEGACY_SETTINGS_IMPORTED" value="true" />
</XML>
<codeStyleSettings language="JSON">
<option name="KEEP_BLANK_LINES_IN_CODE" value="1" />
<indentOptions>
<option name="INDENT_SIZE" value="4" />
<option name="USE_TAB_CHARACTER" value="true" />
<option name="CONTINUATION_INDENT_SIZE" value="4" />
<option name="TAB_SIZE" value="2" />
<option name="SMART_TABS" value="true" />
</indentOptions>
</codeStyleSettings>
<codeStyleSettings language="JavaScript">
<indentOptions>
<option name="USE_TAB_CHARACTER" value="true" />
<option name="INDENT_SIZE" value="2" />
<option name="TAB_SIZE" value="2" />
<option name="KEEP_INDENTS_ON_EMPTY_LINES" value="true" />
</indentOptions>
</codeStyleSettings>
......
# 3.0.2
FIXED:
* Small regression after migration to UB5 fs.readFileSync - required second param
# 3.0.1
CHANGED:
......
const fs = require('fs');
const path = require('path');
const _ = require('lodash');
const dataLoader = require('@unitybase/base/dataLoader');
const csv = require('@unitybase/base/csv1');
const fs = require('fs')
const path = require('path')
const _ = require('lodash')
const dataLoader = require('@unitybase/base/dataLoader')
const csv = require('@unitybase/base/csv1')
const COMMON_UB_MODEL_PREFIX = 'ub_model_';
const COMMON_UB_MODEL_PREFIX = 'ub_model_'
/**
......@@ -16,26 +16,26 @@ const COMMON_UB_MODEL_PREFIX = 'ub_model_';
* @private
*/
function _doBuildShowListCommand(entityNameColIndex, fieldListColIndex, orderByColIndex, row) {
function buildFieldList(l) {
return typeof l !== 'string' ? l : l ? JSON.parse(l) : null;
}
const cmd = {
entity: row[entityNameColIndex], method: 'select',
fieldList: buildFieldList(row[fieldListColIndex])
};
if (orderByColIndex) {
const orderByFieldList = buildFieldList(row[orderByColIndex]);
if (orderByFieldList) {
cmd.orderList = orderByFieldList.map(f => ({expression: f}));
}
}
return JSON.stringify({
cmdType: 'showList',
cmdData: {params: [cmd]}
});
function buildFieldList(l) {
return typeof l !== 'string' ? l : l ? JSON.parse(l) : null
}
const cmd = {
entity: row[entityNameColIndex], method: 'select',
fieldList: buildFieldList(row[fieldListColIndex])
}
if (orderByColIndex) {
const orderByFieldList = buildFieldList(row[orderByColIndex])
if (orderByFieldList) {
cmd.orderList = orderByFieldList.map(f => ({expression: f}))
}
}
return JSON.stringify({
cmdType: 'showList',
cmdData: {params: [cmd]}
})
}
......@@ -46,14 +46,14 @@ function _doBuildShowListCommand(entityNameColIndex, fieldListColIndex, orderByC
* @private
*/
function _doBuildShowFormCommand(formCodeColIndex, row) {
const
formCode = row[formCodeColIndex],
entityName = formCode.split('-')[0];
return JSON.stringify({cmdType: 'showForm', entity: entityName, formCode: formCode});
const
formCode = row[formCodeColIndex],
entityName = formCode.split('-')[0]
return JSON.stringify({cmdType: 'showForm', entity: entityName, formCode: formCode})
}
function _doBuildDelegateCommand(functionNameColIndex, row) {
return `${row[functionNameColIndex]}();`;
return `${row[functionNameColIndex]}();`
}
......@@ -63,19 +63,19 @@ function _doBuildDelegateCommand(functionNameColIndex, row) {
* @returns {*}
*/
function getModelName(dirName) {
let modelName = path.basename(dirName);
if (modelName === 'locale') {
dirName = path.dirname(dirName);
modelName = path.basename(dirName);
}
if (modelName[0] === '_') {
dirName = path.dirname(dirName);
modelName = path.basename(dirName);
}
if (modelName.startsWith(COMMON_UB_MODEL_PREFIX)) {
modelName = modelName.slice(COMMON_UB_MODEL_PREFIX.length);
}
return modelName;
let modelName = path.basename(dirName)
if (modelName === 'locale') {
dirName = path.dirname(dirName)
modelName = path.basename(dirName)
}
if (modelName[0] === '_') {
dirName = path.dirname(dirName)
modelName = path.basename(dirName)
}
if (modelName.startsWith(COMMON_UB_MODEL_PREFIX)) {
modelName = modelName.slice(COMMON_UB_MODEL_PREFIX.length)
}
return modelName
}
/**
......@@ -90,10 +90,10 @@ function getModelName(dirName) {
* @param {number} [tranLen]
*/
function loadCsv(conn, dirName, entityName, attrs, mapping, tranLen) {
console.log(`\t\tCreating "${entityName}" instances...`);
const fileName = `${dirName}/${entityName}-${getModelName(dirName)}.csv`;
dataLoader.loadSimpleCSVData(conn, fileName, entityName, attrs, mapping, 1, null, tranLen);
console.log(`\t\tCreating "${entityName}" instances...`)
const fileName = `${dirName}/${entityName}-${getModelName(dirName)}.csv`
dataLoader.loadSimpleCSVData(conn, fileName, entityName, attrs, mapping, 1, null, tranLen)
}
/**
......@@ -103,25 +103,25 @@ function loadCsv(conn, dirName, entityName, attrs, mapping, tranLen) {
* @return {*}
*/
function _mapAttributeValue(mapping, row, index) {
const curMapObj = mapping[index];
if (_.isNumber(curMapObj)) {
return row[curMapObj];
} else if (_.isFunction(curMapObj)) {
return curMapObj(row);
} else {
throw new Error('Invalid mapping definition in element #' + index);
}
const curMapObj = mapping[index]
if (_.isNumber(curMapObj)) {
return row[curMapObj]
} else if (_.isFunction(curMapObj)) {
return curMapObj(row)
} else {
throw new Error('Invalid mapping definition in element #' + index)
}
}
function localize(session, dirname, filename) {
const
localizationConfigStr = fs.readFileSync(path.join(dirname, '_localization.json')),
localizationConfig = JSON.parse(localizationConfigStr);
localizationConfig.forEach(
l => loadLocaleFromCsv(session, dirname, filename, l[0], l[1], l[2])
);
const
localizationConfigStr = fs.readFileSync(path.join(dirname, '_localization.json')),
localizationConfig = JSON.parse(localizationConfigStr)
localizationConfig.forEach(
l => loadLocaleFromCsv(session, dirname, filename, l[0], l[1], l[2])
)
}
......@@ -137,37 +137,37 @@ function localize(session, dirname, filename) {
* @param {number} [keyAttrsCount] = 1
*/
function loadLocaleFromCsv(session, dirName, locale, entityName, attrs, keyAttrsCount = 1) {
console.log(`\t\tLocalizing "${entityName}" instances...`);
const
mapping = attrs.map((a, i) => i),
lang = path.basename(locale).split('^')[0],
fileName = `${dirName}/${lang}^${entityName}-${getModelName(dirName)}.csv`,
csvData = /** @type Array<Array> */ loadCsvAsArray(fileName);
const
keyAttrs = [...new Array(attrs.length).keys()],
valueAttrs = keyAttrs.splice(keyAttrsCount);
function rowToLocalizationConfig(r) {
const
keyValue = keyAttrs.map(i => _mapAttributeValue(mapping, r, i)).join(';'),
execParams = {};
for (let i of valueAttrs) {
const attrValue = _mapAttributeValue(mapping, r, i);
if (attrValue) execParams[attrs[i]] = attrValue;
}
return {keyValue, execParams};
}
const localizationConfig = {
entity: entityName,
keyAttribute: keyAttrs.map(i => attrs[i]).join(';'),
localization: csvData.map(rowToLocalizationConfig)
};
dataLoader.localizeEntity(session, localizationConfig, locale);
console.log(`\t\tLocalizing "${entityName}" instances...`)
const
mapping = attrs.map((a, i) => i),
lang = path.basename(locale).split('^')[0],
fileName = `${dirName}/${lang}^${entityName}-${getModelName(dirName)}.csv`,
csvData = /** @type Array<Array> */ loadCsvAsArray(fileName)
const
keyAttrs = [...new Array(attrs.length).keys()],
valueAttrs = keyAttrs.splice(keyAttrsCount)
function rowToLocalizationConfig(r) {
const
keyValue = keyAttrs.map(i => _mapAttributeValue(mapping, r, i)).join(';'),
execParams = {}
for (let i of valueAttrs) {
const attrValue = _mapAttributeValue(mapping, r, i)
if (attrValue) execParams[attrs[i]] = attrValue
}
return {keyValue, execParams}
}
const localizationConfig = {
entity: entityName,
keyAttribute: keyAttrs.map(i => attrs[i]).join(';'),
localization: csvData.map(rowToLocalizationConfig)
}
dataLoader.localizeEntity(session, localizationConfig, locale)
}
/**
......@@ -177,20 +177,20 @@ function loadLocaleFromCsv(session, dirName, locale, entityName, attrs, keyAttrs
* @param {string} [delimiter=';'] CSV file delimiter
*/
function loadCsvAsArray(fileName, startRow = 1, delimiter = ';') {
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, delimiter);
if (!Array.isArray(csvData)) throw new Error(`Invalid CSV format or file ${fileName} not found`);
if (csvData.length < startRow) {
throw new Error(`Length of CSVData (${csvData.length}) is smaller then startRow (${startRow})`);
}
if (startRow > 0) csvData.splice(0, startRow);
return csvData;
let fContent = fs.readFileSync(fileName, 'utf8')
if (!fContent) throw new Error('File ' + fileName + ' is empty or not exist')
fContent = fContent.trim()
const csvData = csv.parse(fContent, delimiter)
if (!Array.isArray(csvData)) throw new Error(`Invalid CSV format or file ${fileName} not found`)
if (csvData.length < startRow) {
throw new Error(`Length of CSVData (${csvData.length}) is smaller then startRow (${startRow})`)
}
if (startRow > 0) csvData.splice(0, startRow)
return csvData
}
......@@ -204,18 +204,18 @@ function loadCsvAsArray(fileName, startRow = 1, delimiter = ';') {
* @param {string} entityName
*/
function loadAdm(conn, dirName, entityName) {
const
lookupUser = dataLoader.lookup(conn, 'uba_user', 'name', 1),
lookupRole = dataLoader.lookup(conn, 'uba_role', 'name', 1),
lookupGroup = dataLoader.lookup(conn, 'uba_group', 'code', 1);
loadCsv(conn, dirName, entityName + '_adm', ['instanceID', 'admSubjID'],
[
dataLoader.lookup(conn, entityName, 'code', 0),
row => lookupRole(row) || lookupGroup(row) || lookupUser(row)
]
);
const
lookupUser = dataLoader.lookup(conn, 'uba_user', 'name', 1),
lookupRole = dataLoader.lookup(conn, 'uba_role', 'name', 1),
lookupGroup = dataLoader.lookup(conn, 'uba_group', 'code', 1)
loadCsv(conn, dirName, entityName + '_adm', ['instanceID', 'admSubjID'],
[
dataLoader.lookup(conn, entityName, 'code', 0),
row => lookupRole(row) || lookupGroup(row) || lookupUser(row)
]
)
}
......@@ -251,7 +251,7 @@ function loadAdm(conn, dirName, entityName) {
* This will generate JSON for show-list command with specified list of columns. Entity name is taken from 3 column.
*/
function buildShowListCommand(entityNameColIndex, fieldListColIndex, orderByColIndex) {
return _doBuildShowListCommand.bind(null, entityNameColIndex, fieldListColIndex, orderByColIndex);
return _doBuildShowListCommand.bind(null, entityNameColIndex, fieldListColIndex, orderByColIndex)
}
......@@ -279,7 +279,7 @@ function buildShowListCommand(entityNameColIndex, fieldListColIndex, orderByColI
* This will generate JSON for show-form command with specified entity and form code.
*/
function buildDelegateCommand(functionNameColIndex) {
return _doBuildDelegateCommand.bind(null, functionNameColIndex);
return _doBuildDelegateCommand.bind(null, functionNameColIndex)
}
/**
......@@ -306,7 +306,7 @@ function buildDelegateCommand(functionNameColIndex) {
* This will generate JSON for show-form command with specified entity and form code.
*/
function buildShowFormCommand(formCodeColIndex) {
return _doBuildShowFormCommand.bind(null, formCodeColIndex);
return _doBuildShowFormCommand.bind(null, formCodeColIndex)
}
......@@ -321,26 +321,26 @@ function buildShowFormCommand(formCodeColIndex) {
* @returns {function(this:null)}
*/
function buildCommand(cmdTypeColIndex, entityNameColIndex, fieldListColIndex, orderByColIndex, formCodeColIndex) {
function doBuildCommand(cmdTypeColIndex, entityNameColIndex, fieldListColIndex, orderByColIndex, formCodeColIndex, row) {
const cmdType = row[cmdTypeColIndex];
if (!cmdType) return null;
switch (cmdType) {
case 'showList':
return _doBuildShowListCommand(entityNameColIndex, fieldListColIndex, orderByColIndex, row);
case 'showForm':
return _doBuildShowFormCommand(formCodeColIndex, row);
case 'delegate':
return _doBuildDelegateCommand(row);
default:
throw new Error(`Invalid command type: "${cmdType}"`);
}
}
return doBuildCommand.bind(null, cmdTypeColIndex,
entityNameColIndex, fieldListColIndex, orderByColIndex,
formCodeColIndex);
function doBuildCommand(cmdTypeColIndex, entityNameColIndex, fieldListColIndex, orderByColIndex, formCodeColIndex, row) {
const cmdType = row[cmdTypeColIndex]
if (!cmdType) return null
switch (cmdType) {
case 'showList':
return _doBuildShowListCommand(entityNameColIndex, fieldListColIndex, orderByColIndex, row)
case 'showForm':
return _doBuildShowFormCommand(formCodeColIndex, row)
case 'delegate':
return _doBuildDelegateCommand(row)
default:
throw new Error(`Invalid command type: "${cmdType}"`)
}
}
return doBuildCommand.bind(null, cmdTypeColIndex,
entityNameColIndex, fieldListColIndex, orderByColIndex,
formCodeColIndex)
}
/**
......@@ -362,54 +362,54 @@ function buildCommand(cmdTypeColIndex, entityNameColIndex, fieldListColIndex, or
* 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]}
});
});
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
};
loadCsv,
localize,
loadAdm,
loadParticipantsCsv,
buildCommand,
buildShowListCommand,
buildShowFormCommand,
buildDelegateCommand,
getModelName
}
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