OpenSlides/client/src/app/site/motions/services/motion-import.service.ts

366 lines
14 KiB
TypeScript
Raw Normal View History

2019-01-11 18:55:09 +01:00
import { Injectable } from '@angular/core';
2018-12-04 19:31:24 +01:00
import { MatSnackBar } from '@angular/material';
2019-01-31 08:20:15 +01:00
2019-01-11 18:55:09 +01:00
import { Papa } from 'ngx-papaparse';
2018-12-04 19:31:24 +01:00
import { TranslateService } from '@ngx-translate/core';
import { Category } from 'app/shared/models/motions/category';
import { CategoryRepositoryService } from 'app/core/repositories/motions/category-repository.service';
2018-12-04 19:31:24 +01:00
import { CreateMotion } from '../models/create-motion';
import { MotionBlock } from 'app/shared/models/motions/motion-block';
import { MotionBlockRepositoryService } from 'app/core/repositories/motions/motion-block-repository.service';
import { MotionRepositoryService } from 'app/core/repositories/motions/motion-repository.service';
import { UserRepositoryService } from 'app/core/repositories/users/user-repository.service';
2018-12-04 19:31:24 +01:00
import { ViewCsvCreateMotion, CsvMapping } from '../models/view-csv-create-motion';
import { BaseImportService, NewEntry } from 'app/core/ui-services/base-import.service';
2019-01-11 18:55:09 +01:00
import { ViewMotion } from '../models/view-motion';
2018-12-04 19:31:24 +01:00
/**
* Service for motion imports
*/
@Injectable({
providedIn: 'root'
})
2019-01-11 18:55:09 +01:00
export class MotionImportService extends BaseImportService<ViewMotion> {
2018-12-04 19:31:24 +01:00
/**
2019-01-11 18:55:09 +01:00
* List of possible errors and their verbose explanation
2018-12-04 19:31:24 +01:00
*/
2019-01-11 18:55:09 +01:00
public errorList = {
MotionBlock: 'Could not resolve the motion block',
Category: 'Could not resolve the category',
Submitters: 'Could not resolve the submitters',
Title: 'A title is required',
Text: "A content in the 'text' column is required",
Duplicates: 'A motion with this identifier already exists.'
};
2018-12-04 19:31:24 +01:00
/**
2019-01-11 18:55:09 +01:00
* The minimimal number of header entries needed to successfully create an entry
2018-12-04 19:31:24 +01:00
*/
2019-01-11 18:55:09 +01:00
public requiredHeaderLength = 3;
2018-12-04 19:31:24 +01:00
/**
* submitters that need to be created prior to importing
*/
public newSubmitters: CsvMapping[] = [];
/**
* Categories that need to be created prior to importing
*/
public newCategories: CsvMapping[] = [];
/**
* MotionBlocks that need to be created prior to importing
*/
public newMotionBlocks: CsvMapping[] = [];
/**
2019-01-11 18:55:09 +01:00
* Constructor. Defines the headers expected and calls the abstract class
* @param repo: The repository for motions.
2018-12-04 19:31:24 +01:00
* @param categoryRepo Repository to fetch pre-existing categories
* @param motionBlockRepo Repository to fetch pre-existing motionBlocks
* @param userRepo Repository to query/ create users
* @param translate Translation service
* @param papa External csv parser (ngx-papaparser)
* @param matSnackBar snackBar to display import errors
*/
public constructor(
private repo: MotionRepositoryService,
private categoryRepo: CategoryRepositoryService,
private motionBlockRepo: MotionBlockRepositoryService,
private userRepo: UserRepositoryService,
2019-01-11 18:55:09 +01:00
translate: TranslateService,
papa: Papa,
matSnackbar: MatSnackBar
2018-12-04 19:31:24 +01:00
) {
2019-01-11 18:55:09 +01:00
super(translate, papa, matSnackbar);
this.expectedHeader = [
'identifier',
'title',
'text',
'reason',
'submitters',
'category',
'origin',
'motion_block'
];
2018-12-04 19:31:24 +01:00
}
/**
2019-01-11 18:55:09 +01:00
* Clears all temporary data specific to this importer.
2018-12-04 19:31:24 +01:00
*/
2019-01-11 18:55:09 +01:00
public clearData(): void {
2018-12-04 19:31:24 +01:00
this.newSubmitters = [];
this.newCategories = [];
this.newMotionBlocks = [];
2019-01-11 18:55:09 +01:00
}
/**
* Parses a string representing an entry, extracting secondary data, appending
* the array of secondary imports as needed
*
* @param line
* @returns a new Entry representing a Motion
*/
public mapData(line: string): NewEntry<ViewMotion> {
const newEntry = new ViewCsvCreateMotion(new CreateMotion());
const headerLength = Math.min(this.expectedHeader.length, line.length);
for (let idx = 0; idx < headerLength; idx++) {
switch (this.expectedHeader[idx]) {
case 'submitters':
newEntry.csvSubmitters = this.getSubmitters(line[idx]);
break;
case 'category':
newEntry.csvCategory = this.getCategory(line[idx]);
break;
case 'motion_block':
newEntry.csvMotionblock = this.getMotionBlock(line[idx]);
break;
default:
newEntry.motion[this.expectedHeader[idx]] = line[idx];
2018-12-04 19:31:24 +01:00
}
2019-01-11 18:55:09 +01:00
}
const updateModels = this.repo.getMotionDuplicates(newEntry);
2019-02-27 11:24:04 +01:00
const entry: NewEntry<ViewMotion> = {
2019-01-11 18:55:09 +01:00
newEntry: newEntry,
duplicates: updateModels,
status: updateModels.length ? 'error' : 'new',
errors: updateModels.length ? ['Duplicates'] : []
};
2019-02-27 11:24:04 +01:00
if (!entry.newEntry.title) {
this.setError(entry, 'Title');
}
if (!entry.newEntry.text) {
this.setError(entry, 'Title');
}
return entry;
2018-12-04 19:31:24 +01:00
}
/**
2019-01-11 18:55:09 +01:00
* Executes the import. Creates all secondary data, maps the newly created
* secondary data to the new entries, then creates all entries without errors
* by submitting them to the server. The entries will receive the status
* 'done' on success.
2018-12-04 19:31:24 +01:00
*/
public async doImport(): Promise<void> {
this.newMotionBlocks = await this.createNewMotionBlocks();
this.newCategories = await this.createNewCategories();
this.newSubmitters = await this.createNewUsers();
2019-01-11 18:55:09 +01:00
for (const entry of this.entries) {
if (entry.status !== 'new') {
2018-12-04 19:31:24 +01:00
continue;
}
2019-01-11 18:55:09 +01:00
const openBlocks = (entry.newEntry as ViewCsvCreateMotion).solveMotionBlocks(this.newMotionBlocks);
2018-12-04 19:31:24 +01:00
if (openBlocks) {
2019-01-11 18:55:09 +01:00
this.setError(entry, 'MotionBlock');
2018-12-04 19:31:24 +01:00
this.updatePreview();
continue;
}
2019-01-11 18:55:09 +01:00
const openCategories = (entry.newEntry as ViewCsvCreateMotion).solveCategory(this.newCategories);
2018-12-04 19:31:24 +01:00
if (openCategories) {
2019-01-11 18:55:09 +01:00
this.setError(entry, 'Category');
2018-12-04 19:31:24 +01:00
this.updatePreview();
continue;
}
2019-01-11 18:55:09 +01:00
const openUsers = (entry.newEntry as ViewCsvCreateMotion).solveSubmitters(this.newSubmitters);
2018-12-04 19:31:24 +01:00
if (openUsers) {
2019-01-11 18:55:09 +01:00
this.setError(entry, 'Submitters');
2018-12-04 19:31:24 +01:00
this.updatePreview();
continue;
}
2019-01-11 18:55:09 +01:00
await this.repo.create((entry.newEntry as ViewCsvCreateMotion).motion);
entry.status = 'done';
2018-12-04 19:31:24 +01:00
}
this.updatePreview();
}
/**
* Checks the provided submitter(s) and returns an object with mapping of
* existing users and of users that need to be created
2019-01-11 18:55:09 +01:00
*
2018-12-04 19:31:24 +01:00
* @param submitterlist
2019-01-11 18:55:09 +01:00
* @returns a list of submitters mapped with (if already existing) their id
2018-12-04 19:31:24 +01:00
*/
public getSubmitters(submitterlist: string): CsvMapping[] {
const result: CsvMapping[] = [];
if (!submitterlist) {
return result;
}
const submitterArray = submitterlist.split(','); // TODO fails with 'full name'
for (const submitter of submitterArray) {
const existingSubmitters = this.userRepo.getUsersByName(submitter);
if (!existingSubmitters.length) {
if (!this.newSubmitters.find(listedSubmitter => listedSubmitter.name === submitter)) {
this.newSubmitters.push({ name: submitter });
}
result.push({ name: submitter });
}
if (existingSubmitters.length === 1) {
result.push({
name: existingSubmitters[0].short_name,
id: existingSubmitters[0].id
});
}
if (existingSubmitters.length > 1) {
result.push({
name: submitter,
multiId: existingSubmitters.map(ex => ex.id)
});
this.matSnackbar.open('TODO: multiple possible users found for this string', 'ok');
// TODO How to handle several submitters ? Is this possible?
// should have some kind of choice dialog there
}
}
return result;
}
/**
* Checks the provided category/ies and returns a mapping, expands
* newCategories if needed.
*
* The assumption is that there may or not be a prefix wit up to 5
* characters at the beginning, separated by ' - ' from the name.
* It will also accept a registered translation between the current user's
* language and english
2019-01-11 18:55:09 +01:00
*
2018-12-04 19:31:24 +01:00
* @param categoryString
2019-01-11 18:55:09 +01:00
* @returns categories mapped to existing categories
2018-12-04 19:31:24 +01:00
*/
2019-03-04 18:28:21 +01:00
public getCategory(categoryString: string): CsvMapping | null {
2018-12-04 19:31:24 +01:00
if (!categoryString) {
return null;
}
const category = this.splitCategoryString(categoryString);
const existingCategory = this.categoryRepo.getViewModelList().find(cat => {
if (category.prefix && cat.prefix !== category.prefix) {
return false;
}
if (cat.name === category.name) {
return true;
}
if (this.translate.instant(cat.name) === category.name) {
return true;
}
return false;
});
if (existingCategory) {
return {
name: existingCategory.prefixedName,
id: existingCategory.id
};
} else {
if (!this.newCategories.find(newCat => newCat.name === categoryString)) {
this.newCategories.push({ name: categoryString });
}
return { name: categoryString };
}
}
/**
* Checks the motionBlock provided in the string for existance, expands newMotionBlocks
* if needed. Note that it will also check for translation between the current
* user's language and english
2019-01-11 18:55:09 +01:00
*
2018-12-04 19:31:24 +01:00
* @param blockString
2019-01-11 18:55:09 +01:00
* @returns a CSVMap with the MotionBlock and an id (if the motionBlock is already in the dataStore)
2018-12-04 19:31:24 +01:00
*/
2019-03-04 18:28:21 +01:00
public getMotionBlock(blockString: string): CsvMapping | null {
2018-12-04 19:31:24 +01:00
if (!blockString) {
return null;
}
blockString = blockString.trim();
let existingBlock = this.motionBlockRepo.getMotionBlockByTitle(blockString);
if (!existingBlock) {
existingBlock = this.motionBlockRepo.getMotionBlockByTitle(this.translate.instant(blockString));
}
if (existingBlock) {
return { id: existingBlock.id, name: existingBlock.title };
} else {
if (!this.newMotionBlocks.find(newBlock => newBlock.name === blockString)) {
this.newMotionBlocks.push({ name: blockString });
}
return { name: blockString };
}
}
/**
* Creates all new Users needed for the import.
2019-01-11 18:55:09 +01:00
*
* @returns a promise with list of new Submitters, updated with newly created ids
2018-12-04 19:31:24 +01:00
*/
private async createNewUsers(): Promise<CsvMapping[]> {
const promises: Promise<CsvMapping>[] = [];
for (const user of this.newSubmitters) {
promises.push(this.userRepo.createFromString(user.name));
}
return await Promise.all(promises);
}
/**
* Creates all new Motion Blocks needed for the import.
2019-01-11 18:55:09 +01:00
*
* @returns a promise with list of new MotionBlocks, updated with newly created ids
2018-12-04 19:31:24 +01:00
*/
private async createNewMotionBlocks(): Promise<CsvMapping[]> {
const promises: Promise<CsvMapping>[] = [];
for (const block of this.newMotionBlocks) {
promises.push(
this.motionBlockRepo.create(new MotionBlock({ title: block.name })).then(identifiable => {
return { name: block.name, id: identifiable.id };
})
);
}
return await Promise.all(promises);
}
/**
* Creates all new Categories needed for the import.
2019-01-11 18:55:09 +01:00
*
* @returns a promise with list of new Categories, updated with newly created ids
2018-12-04 19:31:24 +01:00
*/
private async createNewCategories(): Promise<CsvMapping[]> {
const promises: Promise<CsvMapping>[] = [];
for (const category of this.newCategories) {
const cat = this.splitCategoryString(category.name);
promises.push(
this.categoryRepo
.create(
new Category({
name: cat.name,
prefix: cat.prefix ? cat.prefix : null
})
)
.then(identifiable => {
return { name: category.name, id: identifiable.id };
})
);
}
return await Promise.all(promises);
}
/**
* Helper to separate a category string from its' prefix. Assumes that a prefix is no longer
* than 5 chars and separated by a ' - '
2019-01-11 18:55:09 +01:00
*
2018-12-04 19:31:24 +01:00
* @param categoryString the string to parse
2019-01-11 18:55:09 +01:00
* @returns an object with .prefix and .name strings
2018-12-04 19:31:24 +01:00
*/
private splitCategoryString(categoryString: string): { prefix: string; name: string } {
let prefixSeparator = ' - ';
if (categoryString.startsWith(prefixSeparator)) {
prefixSeparator = prefixSeparator.substring(1);
}
categoryString = categoryString.trim();
let prefix = '';
const separatorIndex = categoryString.indexOf(prefixSeparator);
if (separatorIndex >= 0 && separatorIndex < 6) {
prefix = categoryString.substring(0, separatorIndex);
categoryString = categoryString.substring(separatorIndex + prefixSeparator.length);
}
return { prefix: prefix, name: categoryString };
}
}