Skip to content

Commit

Permalink
feat: new checks for maxLength and duplicate columns (#447)
Browse files Browse the repository at this point in the history
  • Loading branch information
marianfoo authored Dec 22, 2023
1 parent cadbd9d commit 7d47e28
Show file tree
Hide file tree
Showing 17 changed files with 108 additions and 11 deletions.
4 changes: 3 additions & 1 deletion docs/pages/Checks.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ The following types of errors are handled by the UI5 Spreadsheet Upload Control:

- **Data Type Mismatch**: The control checks that the data types in the uploaded file match the expected data types.
- **Custom Errors**: The control allows you to add custom errors to the error dialog. You can add errors to the `messages` property of the `SpreadsheetUpload` control. After the event the upload is canceled and the errors are displayed in the Error Dialog (see [Events](./Events.md) for more information).
- **Backend Errors**: If the backend service returns an error, it is displayed. In case of checks during saving (e.g. RAP or CAP), no error is displayed in the draft scenario in Fiori Element Apps, as Fiori Element catches these errors.
- **Backend Errors**: If the backend service returns an error, it is displayed. In case of checks during saving (e.g. RAP or CAP), no error is displayed in the draft scenario in Fiori Element Apps, as Fiori Element catches these errors.
- **Duplicate Columns**: The control checks that the uploaded file does not contain duplicate columns. If there is a duplicate column, an error message is displayed.
- **Max Length**: The control checks that the length of the data in the uploaded file does not exceed the maximum length of the corresponding field. If the length exceeds the maximum length, an error message is displayed.
7 changes: 7 additions & 0 deletions docs/pages/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,13 @@ This option determines whether or not the columns in the Spreadsheet File should
For instance, if there is a column named `Test column` in the Spreadsheet File, and this particular column name is absent from the metadata, an error will occur by default.
However, if there are columns that are not being uploaded to the backend, you can configure this option to `true` in order to bypass the verification process.

### `skipMaxLengthCheck`

**default:** `false`

This option determines whether or not the max length of the columns in the Spreadsheet File should be verified.
If there is a `maxLength` defined in the metadata, the value in the Spreadsheet File will be checked.

### `showBackendErrorMessages`

**default:** `false`
Expand Down
5 changes: 5 additions & 0 deletions packages/ui5-cc-spreadsheetimporter/src/Component.gen.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ declare module "./Component" {
previewColumns?: string[] | PropertyBindingInfo | `{${string}}`;
skipMandatoryFieldCheck?: boolean | PropertyBindingInfo | `{${string}}`;
skipColumnsCheck?: boolean | PropertyBindingInfo | `{${string}}`;
skipMaxLengthCheck?: boolean | PropertyBindingInfo | `{${string}}`;
showBackendErrorMessages?: boolean | PropertyBindingInfo | `{${string}}`;
showOptions?: boolean | PropertyBindingInfo | `{${string}}`;
availableOptions?: string[] | PropertyBindingInfo | `{${string}}`;
Expand Down Expand Up @@ -109,6 +110,10 @@ declare module "./Component" {
getSkipColumnsCheck(): boolean;
setSkipColumnsCheck(skipColumnsCheck: boolean): this;

// property: skipMaxLengthCheck
getSkipMaxLengthCheck(): boolean;
setSkipMaxLengthCheck(skipMaxLengthCheck: boolean): this;

// property: showBackendErrorMessages
getShowBackendErrorMessages(): boolean;
setShowBackendErrorMessages(showBackendErrorMessages: boolean): this;
Expand Down
2 changes: 2 additions & 0 deletions packages/ui5-cc-spreadsheetimporter/src/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default class Component extends UIComponent {
previewColumns: { type: "string[]", defaultValue: [] },
skipMandatoryFieldCheck: { type: "boolean", defaultValue: false },
skipColumnsCheck: { type: "boolean", defaultValue: false },
skipMaxLengthCheck: { type: "boolean", defaultValue: false },
showBackendErrorMessages: { type: "boolean", defaultValue: false },
showOptions: { type: "boolean", defaultValue: false },
// @ts-ignore
Expand Down Expand Up @@ -130,6 +131,7 @@ export default class Component extends UIComponent {
this.setPreviewColumns(oCompData?.previewColumns);
this.setSkipMandatoryFieldCheck(oCompData?.skipMandatoryFieldCheck);
this.setSkipColumnsCheck(oCompData?.skipColumnsCheck);
this.setSkipMaxLengthCheck(oCompData?.skipMaxLengthCheck);
this.setShowBackendErrorMessages(oCompData?.showBackendErrorMessages);
this.setShowOptions(oCompData?.showOptions);
this.setDebug(oCompData?.debug);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ export default class MessageHandler extends ManagedObject {
return this.messages;
}

checkDuplicateColumns(columnNames: string[]) {
const duplicateColumns = columnNames.filter((columnName, index) => columnNames.indexOf(columnName) !== index);
if (duplicateColumns.length > 0) {
const errorMessage = {
title: this.spreadsheetUploadController.util.geti18nText("duplicateColumns", [duplicateColumns.join(", ")]),
type: CustomMessageTypes.DuplicateColumns,
ui5type: MessageType.Error,
group: false
} as Messages;
this.addMessageToMessages(errorMessage);
}
}

checkMandatoryColumns(data: PayloadArray, columnNames: string[], mandatoryFieldsUser: string[], mandatoryFieldsMetadata: string[], typeLabelList: ListObject) {
// concat mandatory fields arrays and remove duplicates
const mandatoryFields = [...new Set([...mandatoryFieldsUser, ...mandatoryFieldsMetadata])];
Expand Down Expand Up @@ -98,6 +111,32 @@ export default class MessageHandler extends ManagedObject {
}
}

checkMaxLength(data: ArrayData, typeLabelList: ListObject, fieldMatchType: string) {
for (const [index, row] of data.entries()) {
for (const [key, valueTypeLabelList] of typeLabelList) {
if (valueTypeLabelList.maxLength) {
const value = Util.getValueFromRow(row, valueTypeLabelList.label, key, this.spreadsheetUploadController.component.getFieldMatchType() as FieldMatchType);
// check if value is longer then max length
if (value && value.rawValue && value.rawValue.length > valueTypeLabelList.maxLength) {
// the length was exceeded by x characters
const exceededLength = value.rawValue.length - valueTypeLabelList.maxLength;
const errorMessage = {
title: this.spreadsheetUploadController.util.geti18nText("maxLengthExceeded", [valueTypeLabelList.maxLength, valueTypeLabelList.label]),
type: CustomMessageTypes.MaxLengthExceeded,
row: index + 2,
counter: 1,
ui5type: MessageType.Error,
rawValue: value.rawValue,
maxLength: valueTypeLabelList.maxLength,
excededLength: exceededLength
} as Messages;
this.addMessageToMessages(errorMessage);
}
}
}
}
}

checkColumnNames(columnNames: string[], fieldMatchType: string, typeLabelList: ListObject) {
for (let index = 0; index < columnNames.length; index++) {
const columnName = columnNames[index];
Expand Down Expand Up @@ -165,13 +204,11 @@ export default class MessageHandler extends ManagedObject {
* Display messages.
*/
async displayMessages() {
if (!this.messageDialog) {
this.messageDialog = (await Fragment.load({
name: "cc.spreadsheetimporter.XXXnamespaceXXX.fragment.MessagesDialog",
type: "XML",
controller: this
})) as Dialog;
}
this.messageDialog = (await Fragment.load({
name: "cc.spreadsheetimporter.XXXnamespaceXXX.fragment.MessagesDialog",
type: "XML",
controller: this
})) as Dialog;
this.messageDialog.setModel(this.spreadsheetUploadController.componentI18n, "i18n");
this.messageDialog.setModel(new JSONModel(), "messages");
const messagesGrouped = this.groupMessages(this.messages);
Expand All @@ -198,7 +235,9 @@ export default class MessageHandler extends ManagedObject {
if (!groups[message.title]) {
groups[message.title] = [];
}
if (message.rawValue && message.formattedValue) {
if (message.maxLength && message.excededLength) {
messageText = this.spreadsheetUploadController.util.geti18nText("maxLengthExceededWithLength", [message.maxLength, message.excededLength, message.rawValue]);
} else if (message.rawValue && message.formattedValue) {
messageText = this.spreadsheetUploadController.util.geti18nText("errorInRowWithValueFormatted", [message.row, message.formattedValue, message.rawValue]);
} else if (message.rawValue) {
messageText = this.spreadsheetUploadController.util.geti18nText("errorInRowWithValue", [message.row, message.rawValue]);
Expand Down Expand Up @@ -228,6 +267,7 @@ export default class MessageHandler extends ManagedObject {

private onCloseMessageDialog() {
this.messageDialog.close();
this.messageDialog.destroy();
// rest file uploader content
this.spreadsheetUploadController.resetContent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export default class SpreadsheetUpload extends ManagedObject {
function (versionInfo: any) {
const version = versionInfo.version;
const text = "UI5 Version Info: " + versionInfo.name + " - " + versionInfo.version;
console.log("UI5 Version Info", versionInfo);
Log.debug("constructor", undefined, "SpreadsheetUpload: SpreadsheetUpload", () => this.component.logger.returnObject({ ui5version: version, isOpenUI5: this.isOpenUI5 }));
}.bind(this)
);
Expand Down Expand Up @@ -225,6 +224,9 @@ export default class SpreadsheetUpload extends ManagedObject {
if (options.hasOwnProperty("skipColumnsCheck")) {
this.component.setSkipColumnsCheck(options.skipColumnsCheck);
}
if (options.hasOwnProperty("skipColumnsCheck")) {
this.component.setSkipColumnsCheck(options.useTableSelector);
}
if (options.hasOwnProperty("showBackendErrorMessages")) {
this.component.setShowBackendErrorMessages(options.showBackendErrorMessages);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ export default class SpreadsheetUploadDialog extends ManagedObject {
this.component.getMandatoryFields(),
this.spreadsheetUploadController.typeLabelList
);
if (this.component.getSkipColumnsCheck() === false) {
this.messageHandler.checkDuplicateColumns(columnNames);
if (!this.component.getSkipMaxLengthCheck()) {
this.messageHandler.checkMaxLength(spreadsheetSheetsData, this.spreadsheetUploadController.typeLabelList, this.component.getFieldMatchType());
}
if (!this.component.getSkipColumnsCheck()) {
this.messageHandler.checkColumnNames(columnNames, this.component.getFieldMatchType(), this.spreadsheetUploadController.typeLabelList);
}
}
Expand Down
8 changes: 8 additions & 0 deletions packages/ui5-cc-spreadsheetimporter/src/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,13 @@ export const CustomMessageTypes: { [key: string]: CustomMessageType } = {
Formatting: {
title: "Formatting",
group: true
},
DuplicateColumns: {
title: "DuplicateColumns",
group: false
},
MaxLengthExceeded: {
title: "MaxLengthExceeded",
group: true
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ messageDialogButtonContinue=Fortfahren
mandatoryFieldNotFilled=Pflichtfeld {0} ist nicht ausgefüllt
columnNotFound=Spalte {0} stimmt mit keinem Datenfeld überein
keyColumnNotFound=Pflichtfeld {0} ist nicht in der Datei vorhanden
duplicateColumns=Folgende Spalten sind doppelt: {0}
maxLengthExceeded=Maximale Länge von {0} Zeichen in Spalte {1} überschritten
maxLengthExceededWithLength=Maximale Länge von {0} Zeichen um {1} Zeichen für Wert "{2}" überschritten

## Error Messages Download
messageTitle=Fehlertyp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ messageDialogButtonContinue=Continue
mandatoryFieldNotFilled=Required field {0} is not filled
columnNotFound=Column {0} does not match any data field
keyColumnNotFound=Required field {0} is not present in the file
duplicateColumns=Following columns are duplicated: {0}
maxLengthExceeded=Maximum length of {0} characters exceeded in column {1}
maxLengthExceededWithLength=Maximum length of {0} characters exceeded by {1} characters for value "{2}"

## Error Messages Download
messageTitle=Error type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ messageDialogButtonContinue=Continuar
mandatoryFieldNotFilled=Campo obligatorio {0} no está lleno
columnNotFound=Columna {0} no coincide con ningún campo de datos
keyColumnNotFound=Campo obligatorio {0} no está presente en el archivo
duplicateColumns=Las siguientes columnas están duplicadas: {0}
maxLengthExceeded=Longitud máxima de {0} caracteres excedida en columna {1}
maxLengthExceededWithLength=Longitud máxima de {0} caracteres excedida por {1} caracteres para el valor "{2}"

## Error Messages Download
messageTitle=Tipo de error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ messageDialogButtonContinue=Continuer
mandatoryFieldNotFilled=Le champ obligatoire {0} n`est pas rempli
columnNotFound=La colonne {0} ne correspond à aucun champ de données
keyColumnNotFound=Le champ obligatoire {0} n`est pas présent dans le fichier
duplicateColumns=Les colonnes suivantes sont dupliquées : {0}
maxLengthExceeded=Longueur maximale de {0} caractères dépassée dans la colonne {1}
maxLengthExceededWithLength=Longueur maximale de {0} caractères dépassée de {1} caractères pour la valeur "{2}"

## Error Messages Download
messageTitle=Type d´erreur
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ messageDialogButtonContinue=जारी रखें
mandatoryFieldNotFilled=अनिवार्य फ़ील्ड {0} भरा नहीं है
columnNotFound=कॉलम {0} किसी डेटा फ़ील्ड से मेल नहीं खा रहा है
keyColumnNotFound=अनिवार्य फ़ील्ड {0} फ़ाइल में मौजूद नहीं है
duplicateColumns=निम्नलिखित कॉलम डुप्लिकेट हैं: {0}
maxLengthExceeded=कॉलम {1} में {0} वर्णों की अधिकतम लंबाई पार हो गई है
maxLengthExceededWithLength=मान "{2}" के लिए {0} वर्णों की अधिकतम लंबाई {1} वर्णों से पार हो गई है

## Error Messages Download
messageTitle=त्रुटि प्रकार
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ messageDialogButtonContinue=Continua
mandatoryFieldNotFilled=Il campo obbligatorio {0} non è stato compilato
columnNotFound=La colonna {0} non corrisponde a nessun campo dati
keyColumnNotFound=Il campo obbligatorio {0} non è presente nel file
duplicateColumns=Le seguenti colonne sono duplicate: {0}
maxLengthExceeded=Superata la lunghezza massima di {0} caratteri nella colonna {1}
maxLengthExceededWithLength=Superata la lunghezza massima di {0} caratteri di {1} caratteri per il valore "{2}"

## Error Messages Download
messageTitle=Tipo di errore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ messageDialogButtonContinue=続行
mandatoryFieldNotFilled=必須フィールド {0} が未入力です
columnNotFound=列 {0} はデータフィールドと一致しません
keyColumnNotFound=必須フィールド {0} がファイルに存在しません
duplicateColumns=次の列が重複しています: {0}
maxLengthExceeded=列 {1} での最大長さ {0} 文字を超えました
maxLengthExceededWithLength=値 "{2}" で最大長さ {0} 文字を {1} 文字超えました

## Error Messages Download
messageTitle=エラータイプ
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ messageDialogButtonContinue=继续
mandatoryFieldNotFilled=必填字段 {0} 未填写
columnNotFound=列 {0} 与任何数据字段不匹配
keyColumnNotFound=必填字段 {0} 不在文件中
duplicateColumns=以下列重复: {0}
maxLengthExceeded=列 {1} 中超出了 {0} 个字符的最大长度
maxLengthExceededWithLength=对于值 "{2}" 超出了 {0} 个字符的最大长度 {1} 个字符

## Error Messages Download
messageTitle=错误类型
Expand Down
3 changes: 3 additions & 0 deletions packages/ui5-cc-spreadsheetimporter/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export interface Messages {
ui5type: MessageType;
description?: string;
details?: MessagesDetails[];
maxLength?: number;
excededLength?: number;
}

export interface MessagesDetails {
Expand Down Expand Up @@ -96,6 +98,7 @@ export interface ComponentData {
previewColumns?: string[];
skipMandatoryFieldCheck?: boolean;
skipColumnsCheck?: boolean;
skipMaxLengthCheck?: boolean;
showBackendErrorMessages?: boolean;
showOptions?: boolean;
availableOptions?: AvailableOptionsType[];
Expand Down

0 comments on commit 7d47e28

Please sign in to comment.