Skip to content

Commit 40ba599

Browse files
committed
close #617
1 parent 1d6b190 commit 40ba599

12 files changed

Lines changed: 120 additions & 17 deletions

File tree

src/main/webapp/app/dbsqlite.worker.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { map } from 'rxjs/operators';
21
/* eslint-disable no-console */
32
/* eslint-disable spaced-comment */
43
/* eslint-disable @typescript-eslint/explicit-function-return-type */
@@ -130,7 +129,6 @@ addEventListener('message', e => {
130129
break;
131130
}
132131
case 'cleanOutDatedCached': {
133-
console.error('cleanOutDatedCached', e.data);
134132
cleanOutdatedCache(e.data);
135133

136134
break;
@@ -402,6 +400,18 @@ addEventListener('message', e => {
402400
db1.addTemplate(_sqlite3, e.data);
403401
break;
404402
}
403+
404+
case 'getExamTimestamp': {
405+
let db1 = dbs.get(e.data.payload.examId);
406+
if (db1 === undefined) {
407+
db1 = new DB(e.data.payload.examId);
408+
db1.initemptyDb(_sqlite3);
409+
dbs.set(e.data.payload.examId, db1);
410+
}
411+
db1.getExamTimestamp(_sqlite3, e.data);
412+
break;
413+
}
414+
405415
case 'countNonAlignWithPageNumber': {
406416
let db1 = dbs.get(e.data.payload.examId);
407417
if (db1 === undefined) {
@@ -553,6 +563,12 @@ class DB {
553563
this.db.exec('CREATE TABLE IF NOT EXISTS template(page INTEGER NOT NULL PRIMARY KEY,imageData CLOB NOT NULL)');
554564
this.db.exec('CREATE TABLE IF NOT EXISTS align(page INTEGER NOT NULL PRIMARY KEY,imageData CLOB NOT NULL)');
555565
this.db.exec('CREATE TABLE IF NOT EXISTS nonalign(page INTEGER NOT NULL PRIMARY KEY,imageData CLOB NOT NULL)');
566+
this.db.exec('create table IF NOT EXISTS exam(id INTEGER NOT NULL PRIMARY KEY, DT datetime default current_timestamp)');
567+
this.db.exec({
568+
sql: 'INSERT OR IGNORE INTO exam(id) VALUES (?)',
569+
bind: [1],
570+
});
571+
556572
// this.db.exec('CREATE UNIQUE INDEX templatepage ON template(page)');
557573
// this.db.exec('CREATE UNIQUE INDEX alignpage ON align(page)');
558574
// this.db.exec('CREATE UNIQUE INDEX nonalignpage ON nonalign(page)');
@@ -1337,7 +1353,7 @@ class DB {
13371353
}
13381354
}
13391355

1340-
// addExam(examId:number): Promise<number>;
1356+
// addExam(examId:number): initemptyDbPromise<number>;
13411357

13421358
addExam(sqlite3: any, data: any) {
13431359
try {
@@ -1350,6 +1366,21 @@ class DB {
13501366
}
13511367
}
13521368

1369+
getExamTimestamp(sqlite3: any, data: any) {
1370+
// const payload = data.payload;
1371+
this.initDb(sqlite3);
1372+
try {
1373+
const timestamp = this.db.selectValue('select DT from exam where id=1');
1374+
postMessage({
1375+
msg: data.msg,
1376+
uid: data.uid,
1377+
payload: new Date(timestamp).getTime(),
1378+
});
1379+
} finally {
1380+
this.close();
1381+
}
1382+
}
1383+
13531384
// countNonAlignWithPageNumber(examId:number, pageInscan:number): Promise<number>;
13541385

13551386
countNonAlignWithPageNumber(sqlite3: any, data: any) {

src/main/webapp/app/entities/exam/service/exam.service.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ export class ExamService {
7575
});
7676
}
7777

78+
getExamTimestamp(id: number): Observable<HttpResponse<any>> {
79+
return this.http.get(this.applicationConfigService.getEndpointFor('api') + '/getCacheTimeStamp' + `/${id}`, {
80+
observe: 'response',
81+
});
82+
}
83+
7884
addExamToCollectionIfMissing(examCollection: IExam[], ...examsToCheck: (IExam | null | undefined)[]): IExam[] {
7985
const exams: IExam[] = examsToCheck.filter(isPresent);
8086
if (exams.length > 0) {

src/main/webapp/app/scanexam/alignscan/alignscan.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ export class AlignScanComponent implements OnInit, CacheUploadNotification {
329329
private async saveData(): Promise<any> {
330330
// Change if partial update
331331
if (!this.partialAlign) {
332-
await this.db.addExam(+this.examId);
332+
await this.db.addExam(+this.examId, new Date());
333333
}
334334

335335
this.translateService.get('scanexam.exportcacheencours').subscribe(res => (this.message = '' + res));

src/main/webapp/app/scanexam/creerexamnbgrader/creerexamnbgrader.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ export class CreerexamComponentNbGrader implements OnInit, AfterViewInit, CacheU
374374
templateId: template1.body?.id,
375375
};
376376
const exam1 = await firstValueFrom(this.examService.create(exam));
377-
await this.cacheServiceImpl.addExam(exam1.body!.id!);
377+
await this.cacheServiceImpl.addExam(exam1.body!.id!, new Date());
378378
// template.content = this.editForm.get(['content'])!.value;
379379
// const t = await firstValueFrom( this.templateService.create(template))
380380
const htmlfilesToProcess: string[] = [];

src/main/webapp/app/scanexam/db/CacheServiceImpl.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,14 @@ export class CacheServiceImpl implements CacheService {
109109
getAlignSortByPageNumber(examId: number): Promise<ImageDB[]> {
110110
return this.service.getAlignSortByPageNumber(examId);
111111
}
112-
addExam(examId: number): Promise<number> {
113-
return this.service.addExam(examId);
112+
addExam(examId: number, d: Date): Promise<number> {
113+
return this.service.addExam(examId, d);
114114
}
115+
116+
getExamTimestamp(examId: number): Promise<number> {
117+
return this.service.getExamTimestamp(examId);
118+
}
119+
115120
addTemplate(elt: AlignImage): Promise<number> {
116121
return this.service.addTemplate(elt);
117122
}

src/main/webapp/app/scanexam/db/db.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ExportOptions, ImportOptions } from 'dexie-export-import';
55

66
export interface Exam {
77
id?: number;
8+
creationDate?: number;
89
}
910

1011
export interface Template {
@@ -56,8 +57,8 @@ export class ExamIndexDB extends Dexie {
5657
private nonAlignImages!: Table<NonAlignImage, number>;
5758

5859
constructor(private examId: number) {
59-
// super('ngdexieliveQuery');
6060
super('correctExam' + examId);
61+
// console.error('create database correctExam' + examId);
6162
this.version(3).stores({
6263
exams: '++id',
6364
templates: '++id, examId, [examId+pageNumber]',
@@ -379,12 +380,22 @@ export class ExamIndexDB extends Dexie {
379380
async getAlignSortByPageNumber() {
380381
return await this.alignImages.where({ examId: this.examId }).sortBy('pageNumber');
381382
}
382-
async addExam() {
383+
async addExam(d: Date) {
383384
return await this.exams.add({
384385
id: this.examId,
386+
creationDate: d ? d.getTime() : new Date().getTime(),
385387
});
386388
}
387389

390+
async getExamTimestamp(): Promise<number> {
391+
const e = await this.exams
392+
.where({
393+
id: this.examId,
394+
})
395+
.first();
396+
return e?.creationDate ?? 0;
397+
}
398+
388399
async addTemplate(elt: AlignImage) {
389400
return await this.templates.add({
390401
examId: this.examId,
@@ -659,13 +670,22 @@ export class AppDB implements CacheService {
659670
}
660671
return db1.getAlignSortByPageNumber();
661672
}
662-
async addExam(examId: number): Promise<number> {
673+
async addExam(examId: number, d: Date): Promise<number> {
674+
let db1 = this.dbs.get(examId);
675+
if (db1 === undefined) {
676+
db1 = new ExamIndexDB(examId);
677+
this.dbs.set(examId, db1);
678+
}
679+
return db1.addExam(d);
680+
}
681+
682+
async getExamTimestamp(examId: number): Promise<number> {
663683
let db1 = this.dbs.get(examId);
664684
if (db1 === undefined) {
665685
db1 = new ExamIndexDB(examId);
666686
this.dbs.set(examId, db1);
667687
}
668-
return db1.addExam();
688+
return db1.getExamTimestamp();
669689
}
670690

671691
async addTemplate(elt: AlignImage): Promise<number> {
@@ -779,7 +799,7 @@ export interface CacheService {
779799

780800
getNonAlignSortByPageNumber(examId: number): Promise<ImageDB[]>;
781801
getAlignSortByPageNumber(examId: number): Promise<ImageDB[]>;
782-
addExam(examId: number): Promise<number>;
802+
addExam(examId: number, d: Date): Promise<number>;
783803
addTemplate(elt: AlignImage): Promise<number>;
784804
countNonAlignWithPageNumber(examId: number, pageInscan: number): Promise<number>;
785805
countAlignWithPageNumber(examId: number, pageInscan: number): Promise<number>;
@@ -790,6 +810,7 @@ export interface CacheService {
790810
removePageNonAlignForExamForPagesAndReorder(examId: number, pages: number[]): Promise<void>;
791811
moveTemplatePages(examId: number, pageNumber: number, lastPage: number): Promise<void>;
792812
removePageTemplateForExamForPage(examId: number, lastPage: number): Promise<void>;
813+
getExamTimestamp(examId: number): Promise<number>;
793814
}
794815

795816
export const db = new AppDB();

src/main/webapp/app/scanexam/db/dbsqlite.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ export class SqliteCacheService implements CacheService {
205205
const el1 = {
206206
examIds: examIds,
207207
};
208-
console.error('cleanOutDatedCached', el1);
209208
return this._dispatch<void>('cleanOutDatedCached', el1).toPromise();
210209
}
211210

@@ -342,11 +341,19 @@ export class SqliteCacheService implements CacheService {
342341
examId: examId,
343342
}).toPromise();
344343
}
345-
addExam(examId: number): Promise<any> {
344+
addExam(examId: number, d: Date): Promise<any> {
346345
return this._dispatch('addExam', {
347346
examId: examId,
347+
d: d,
348348
}).toPromise();
349349
}
350+
351+
getExamTimestamp(examId: number): Promise<any> {
352+
return this._dispatch('getExamTimestamp', {
353+
examId: examId,
354+
}).toPromise();
355+
}
356+
350357
addTemplate(elt: AlignImage): Promise<any> {
351358
const enc = new TextEncoder(); // always utf-8
352359

src/main/webapp/app/scanexam/exam-detail/cacheUpload.service.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,6 @@ export class CacheUploadService {
316316
let data: Blob = (await firstValueFrom(this.getCache(examId + 'indexdb.json'))) as Blob;
317317
if (data.size === 0) {
318318
data = await firstValueFrom(this.getCache(examId + '_exam_template_indexdb.json'));
319-
console.error('data', data, showFailMessage);
320319
if (data.size === 0) {
321320
if (showFailMessage) {
322321
messageService.add({

src/main/webapp/app/scanexam/exam-detail/exam-detail.component.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { faSquarePollVertical as faSquarePollVertical } from '@fortawesome/free-
1111
import { faPenToSquare as faPenToSquare } from '@fortawesome/free-solid-svg-icons';
1212
import { CourseService } from '../../entities/course/service/course.service';
1313
import { ICourse } from '../../entities/course/course.model';
14-
import { IExam } from '../../entities/exam/exam.model';
14+
import { IExam, getExamIdentifier } from '../../entities/exam/exam.model';
1515
import { ExamService } from '../../entities/exam/service/exam.service';
1616
import { ActivatedRoute, Router, RouterLink } from '@angular/router';
1717
import { ConfirmationService, PrimeTemplate } from 'primeng/api';
@@ -179,7 +179,12 @@ export class ExamDetailComponent implements OnInit, CacheUploadNotification, Cac
179179
this.checkIfAlreadyAlign().then(res => {
180180
if (res && this.exam?.scanfileId) {
181181
this.showAssociation = true;
182-
this.initTemplate();
182+
this.checkNewCacheAvailable().then(res1 => {
183+
this.initTemplate();
184+
if (res1) {
185+
this.confirmDownloadNewCache();
186+
}
187+
});
183188
} else {
184189
this.blocked = false;
185190
}
@@ -210,6 +215,14 @@ export class ExamDetailComponent implements OnInit, CacheUploadNotification, Cac
210215
});
211216
}
212217

218+
async checkNewCacheAvailable(): Promise<boolean> {
219+
const p = await this.db.getExamTimestamp(+this.examId);
220+
const localts = p !== undefined ? p : 0;
221+
const s = await firstValueFrom(this.examService.getExamTimestamp(+this.examId));
222+
const serverts = s.body !== undefined ? s.body : 0;
223+
return serverts > localts;
224+
}
225+
213226
async checkIfAlreadyAlign(): Promise<boolean> {
214227
const p = await this.db.countNonAlignImage(+this.examId);
215228
const p1 = await this.db.countAlignImage(+this.examId);
@@ -370,6 +383,21 @@ export class ExamDetailComponent implements OnInit, CacheUploadNotification, Cac
370383
this.progress = v;
371384
}
372385

386+
confirmDownloadNewCache(): any {
387+
this.translateService.get('scanexam.confirmdownloadnewcache').subscribe(data1 => {
388+
this.confirmationService.confirm({
389+
message: data1,
390+
accept: () => {
391+
this.blocked = true;
392+
this.cacheUploadService.importCache(+this.examId, this.translateService, this.messageService, this, true);
393+
},
394+
reject: () => {
395+
this.blocked = false;
396+
},
397+
});
398+
});
399+
}
400+
373401
confirmDownload(): any {
374402
this.translateService.get('scanexam.confirmdownloadcache').subscribe(data1 => {
375403
this.confirmationService.confirm({
@@ -378,6 +406,9 @@ export class ExamDetailComponent implements OnInit, CacheUploadNotification, Cac
378406
this.blocked = true;
379407
this.cacheUploadService.importCache(+this.examId, this.translateService, this.messageService, this, true);
380408
},
409+
reject: () => {
410+
this.blocked = false;
411+
},
381412
});
382413
});
383414
}

src/main/webapp/i18n/en/scanexammodule.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
"uploadcacheencoursdetail": "(This may take a while)",
113113

114114
"confirmdownloadcache": "Are sure you want to download the cache from the server to the browser. This will remove your local image database to that equipment previously aligned.",
115+
"confirmdownloadnewcache": "A more up-to-date cache is available on the server. Do you want to download the cache from the server to the browser? This will delete your local image database on this device that you have previously aligned or downloaded.",
115116
"downloadcacheok": "Download file from server",
116117
"downloadcacheokdetail": "Import from local database successful",
117118
"downloadcacheko": "Download file from server",

0 commit comments

Comments
 (0)