indexation.component.ts 11.1 KB
Newer Older
1
import { Component, OnInit, ViewChild, ViewContainerRef, TemplateRef, OnDestroy } from '@angular/core';
2
import { HttpClient } from '@angular/common/http';
3
import { TranslateService } from '@ngx-translate/core';
4
import { NotificationService } from '@service/notification/notification.service';
5
import { MatDialog, MatDialogRef } from '@angular/material/dialog';
6

7
import { ActivatedRoute, Router } from '@angular/router';
8
9
import { HeaderService } from '@service/header.service';
import { FiltersListService } from '@service/filtersList.service';
10
11

import { Overlay } from '@angular/cdk/overlay';
12
import { AppService } from '@service/app.service';
13
import { IndexingFormComponent } from './indexing-form/indexing-form.component';
14
import { tap, finalize, catchError, map, filter, take } from 'rxjs/operators';
Alex ORLUC's avatar
Alex ORLUC committed
15
import { DocumentViewerComponent } from '../viewer/document-viewer.component';
16
import { ConfirmComponent } from '@plugins/modal/confirm.component';
17
import { ActionsService } from '../actions/actions.service';
18
import { SortPipe } from '@plugins/sorting.pipe';
19
20
import { FunctionsService } from '@service/functions.service';
import { of, Subscription } from 'rxjs';
21
import { SelectIndexingModelComponent } from './select-indexing-model/select-indexing-model.component';
22
import { IndexationAttachmentsListComponent } from '@appRoot/attachments/indexation/indexation-attachments-list.component';
23
24

@Component({
25
    templateUrl: 'indexation.component.html',
26
27
28
29
    styleUrls: [
        'indexation.component.scss',
        'indexing-form/indexing-form.component.scss'
    ],
30
    providers: [ActionsService, SortPipe],
31
})
32
export class IndexationComponent implements OnInit, OnDestroy {
33

34
    @ViewChild('adminMenuTemplate', { static: true }) adminMenuTemplate: TemplateRef<any>;
35

36
    @ViewChild('appSelectIndexingModel', { static: false }) appSelectIndexingModel: SelectIndexingModelComponent;
37
    @ViewChild('indexingForm', { static: false }) indexingForm: IndexingFormComponent;
38
    @ViewChild('appIndexationAttachmentsList', { static: false }) appIndexationAttachmentsList: IndexationAttachmentsListComponent;
Alex ORLUC's avatar
Alex ORLUC committed
39
    @ViewChild('appDocumentViewer', { static: false }) appDocumentViewer: DocumentViewerComponent;
40

41
42
43
    loading: boolean = false;


44
45
    indexingModels: any[] = [];
    currentIndexingModel: any = {};
46
    currentGroupId: number;
47

48
    actionsList: any[] = [];
49
    actionsListLoaded: boolean = false;
50
51
52
53
54
55
56
    selectedAction: any = {
        id: 0,
        label: '',
        component: '',
        default: false,
        categoryUse: []
    };
57
    tmpFilename: string = '';
58

59
60
    dialogRef: MatDialogRef<any>;

61
62
    subscription: Subscription;

63
64
    isMailing: boolean = false;

65
    constructor(
66
        public translate: TranslateService,
67
        private route: ActivatedRoute,
68
        private _activatedRoute: ActivatedRoute,
69
70
71
72
73
74
75
        public http: HttpClient,
        public dialog: MatDialog,
        private headerService: HeaderService,
        public filtersListService: FiltersListService,
        private notify: NotificationService,
        public overlay: Overlay,
        public viewContainerRef: ViewContainerRef,
76
        public appService: AppService,
77
        public actionService: ActionsService,
78
        private router: Router,
79
80
        private sortPipe: SortPipe,
        public functions: FunctionsService
81
    ) {
82

83
84
85
        _activatedRoute.queryParams.subscribe(
            params => this.tmpFilename = params.tmpfilename
        );
86

87
        // Event after process action
88
        this.subscription = this.actionService.catchAction().subscribe(resIds => {
89
90
91

            this.appIndexationAttachmentsList.saveAttachments(resIds[0]);

92
            if (['closeAndIndexAction', 'saveAndIndexRegisteredMailAction'].indexOf(this.selectedAction.component) > -1) {
93
94
95
96
97
98
99
100
                this.appDocumentViewer.templateListForm.reset();
                this.appDocumentViewer.file = {
                    name: '',
                    type: '',
                    content: null,
                    src: null
                };
                this.appDocumentViewer.triggerEvent.emit('cleanFile');
101
102
103
104
105
106
                this.appSelectIndexingModel.resetIndexingModel();
            } else {
                const param = this.isMailing ? {
                    isMailing: true
                } : null;
                this.router.navigate([`/resources/${resIds[0]}`], { queryParams: param });
107
108
            }
        });
109
110
111
112
    }

    ngOnInit(): void {
        this.headerService.injectInSideBarLeft(this.adminMenuTemplate, this.viewContainerRef, 'adminMenu', 'form');
113
        this.headerService.sideBarButton = { icon: 'fa fa-home', label: this.translate.instant('lang.backHome'), route: '/home' };
114

115
116
117
118
119
        this.fetchData();
    }

    fetchData() {
        this.loading = false;
120
        this.headerService.setHeader(this.translate.instant('lang.recordingMail'));
121
122

        this.route.params.subscribe(params => {
123
            this.currentGroupId = params['groupId'];
124

125
            this.http.get('../rest/indexing/groups/' + this.currentGroupId + '/actions').pipe(
126
                map((data: any) => {
127
128
129
130
131
132
133
134
                    data.actions = data.actions.map((action: any, index: number) => ({
                        id: action.id,
                        label: action.label,
                        component: action.component,
                        enabled: action.enabled,
                        default: index === 0 ? true : false,
                        categoryUse: action.categories
                    }));
135
136
                    return data;
                }),
137
                tap((data: any) => {
138
139
                    this.selectedAction = data.actions[0];
                    this.actionsList = data.actions;
140
                    this.actionsListLoaded = true;
141
142
143
144
145
146
147
                }),
                finalize(() => this.loading = false),
                catchError((err: any) => {
                    this.notify.handleErrors(err);
                    return of(false);
                })
            ).subscribe();
148
        },
149
150
151
        (err: any) => {
            this.notify.handleErrors(err);
        });
152
    }
153

154
155
    isEmptyIndexingModels() {
        return this.appSelectIndexingModel !== undefined && this.appSelectIndexingModel.getIndexingModels().length === 0;
156
157
    }

158
    onSubmit() {
159
        if (this.indexingForm.isValidForm() && this.appIndexationAttachmentsList.isValid()) {
160
            this.actionService.loading = true;
161
            const formatdatas = this.indexingForm.formatDatas(this.indexingForm.getDatas());
162

163
            formatdatas['modelId'] = this.currentIndexingModel.master !== null ? this.currentIndexingModel.master : this.currentIndexingModel.id;
164
            formatdatas['chrono'] = true;
165

166
            this.appDocumentViewer.getFile().pipe(
167
                take(1),
168
169
170
171
                tap((data: any) => {
                    formatdatas['encodedFile'] = data.content;
                    formatdatas['format'] = data.format;

172
173
                    this.isMailing = !this.functions.empty(formatdatas.recipients) && formatdatas.recipients.length > 0 && this.currentIndexingModel.category === 'outgoing' && formatdatas['encodedFile'] === null;

174
                    if (formatdatas['encodedFile'] === null && this.currentIndexingModel.mandatoryFile) {
175
                        this.notify.error(this.translate.instant('lang.mandatoryFile'));
176
177
                        this.actionService.loading = false;
                    } else if (formatdatas['encodedFile'] === null && this.currentIndexingModel.category !== 'registeredMail') {
178
179
                        this.dialogRef = this.dialog.open(
                            ConfirmComponent, {
180
181
182
183
184
185
186
                                panelClass: 'maarch-modal',
                                autoFocus: false,
                                disableClose: true,
                                data: {
                                    title: this.translate.instant('lang.noFile'),
                                    msg: this.translate.instant('lang.noFileMsg')
                                }
187
188
                            }
                        );
189
190

                        this.dialogRef.afterClosed().pipe(
191
192
                            tap((result: string) => {
                                if (result !== 'ok') {
193
194
195
                                    this.actionService.loading = false;
                                }
                            }),
196
                            filter((result: string) => result === 'ok'),
197
                            tap(() => {
198
199
200
201
202
                                this.actionService.launchIndexingAction(
                                    this.selectedAction,
                                    this.headerService.user.id,
                                    this.currentGroupId, formatdatas
                                );
203
204
205
206
207
208
209
                            }),
                            catchError((err: any) => {
                                this.notify.handleErrors(err);
                                return of(false);
                            })
                        ).subscribe();
                    } else {
210
211
212
213
214
                        this.actionService.launchIndexingAction(
                            this.selectedAction,
                            this.headerService.user.id,
                            this.currentGroupId, formatdatas
                        );
215
216
217
                    }
                })
            ).subscribe();
218
        } else {
219
            this.notify.error(this.translate.instant('lang.mustFixErrors'));
220
        }
221
222
223
    }

    formatDatas(datas: any) {
224
        const formatData: any = {};
225
226
227
228
        const regex = /indexingCustomField_[.]*/g;

        formatData['customFields'] = {};

229
        datas.forEach((element: any) => {
230
231
232
233
234
235
236

            if (element.identifier.match(regex) !== null) {

                formatData['customFields'][element.identifier.split('_')[1]] = element.default_value;

            } else {
                formatData[element.identifier] = element.default_value;
237
            }
238
        });
239
        return formatData;
240
    }
241
242
243
244

    loadIndexingModel(indexingModel: any) {
        this.currentIndexingModel = indexingModel;
    }
245
246
247
248

    selectAction(action: any) {
        this.selectedAction = action;
    }
249
250


251
252
253
254
255
    ngOnDestroy() {
        // unsubscribe to ensure no memory leaks
        this.subscription.unsubscribe();
    }

256
257
258
    showActionInCurrentCategory(action: any) {

        if (this.selectedAction.categoryUse.indexOf(this.indexingForm.getCategory()) === -1) {
259
            const newAction = this.actionsList.filter(actionList => actionList.categoryUse.indexOf(this.indexingForm.getCategory()) > -1)[0];
260
            if (newAction !== undefined) {
261
                this.selectedAction = this.actionsList.filter(actionList => actionList.categoryUse.indexOf(this.indexingForm.getCategory()) > -1)[0];
262
263
264
265
266
267
268
269
270
271
            } else {
                this.selectedAction = {
                    id: 0,
                    label: '',
                    component: '',
                    default: false,
                    categoryUse: []
                };
            }
        }
272
        if (action.categoryUse.indexOf(this.indexingForm.getCategory()) > -1) {
273
274
275
276
277
278
            return true;
        } else {
            return false;
        }
    }

279
    refreshDatas() {
280
        this.appDocumentViewer.setDatas(this.indexingForm.formatDatas(this.indexingForm.getDatas()));
281
282
    }

283
}