Skip to content

Commit 5ed81ce

Browse files
committed
1 parent 2f86156 commit 5ed81ce

2 files changed

Lines changed: 191 additions & 0 deletions

File tree

src/main/java/fr/istic/service/CacheUploadService.java

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.sql.SQLException;
2626
import java.sql.Statement;
2727
import java.util.ArrayList;
28+
import java.util.Date;
2829
import java.util.HashMap;
2930
import java.util.List;
3031
import java.util.Map;
@@ -400,7 +401,180 @@ public long getCachePageInTemplateSqlite(long id) throws IOException {
400401
return 0;
401402
}
402403

404+
public long getCacheTimeStampSqlite(long id) throws IOException {
403405

406+
InputStream inputStream = null;
407+
String dbpath = "";
408+
if (pathsqlite.containsKey(id) && Paths.get(pathsqlite.get(id)).toFile().exists()) {
409+
dbpath = pathsqlite.get(id);
410+
} else {
411+
if (this.uses3) {
412+
String fileName = "cache/" + id + ".sqlite3";
413+
try {
414+
if (this.fichierS3Service.isObjectExist(fileName)) {
415+
inputStream = this.getObject(fileName);
416+
dbpath = this.writeStreamToTempFile(inputStream, id + ".sqlite3").getAbsolutePath();
417+
pathsqlite.put(id, dbpath);
418+
}
419+
} catch (InvalidKeyException | NoSuchAlgorithmException | IllegalArgumentException e) {
420+
e.printStackTrace();
421+
return 0;
422+
}
423+
} else {
424+
String fileName = id + ".sqlite3";
425+
File customDir = new File(UPLOAD_DIR);
426+
fileName = customDir.getAbsolutePath() +
427+
File.separator + fileName;
428+
if (Paths.get(fileName).toFile().exists()) {
429+
dbpath = Paths.get(fileName).toFile().getAbsolutePath();
430+
pathsqlite.put(id, dbpath);
431+
432+
}
433+
}
434+
}
435+
if ("".equals(dbpath) || dbpath == null) {
436+
return 0;
437+
}
438+
439+
Connection conn = null;
440+
try {
441+
// db parameters
442+
String url = "jdbc:sqlite:" + dbpath;
443+
444+
// create a connection to the database
445+
conn = DriverManager.getConnection(url);
446+
String query = "select DT from exam";
447+
try (Statement stmt = conn.createStatement()) {
448+
ResultSet rs = stmt.executeQuery(query);
449+
boolean hasAline = rs.next();
450+
if (hasAline) {
451+
Date time = rs.getTime(1);
452+
return time.getTime();
453+
}
454+
455+
} catch (SQLException e) {
456+
e.printStackTrace();
457+
}
458+
459+
} catch (SQLException e) {
460+
461+
System.out.println(e.getMessage());
462+
} finally {
463+
try {
464+
if (conn != null) {
465+
conn.close();
466+
}
467+
} catch (SQLException ex) {
468+
log.error(ex.getMessage());
469+
}
470+
}
471+
return 0;
472+
}
473+
474+
475+
476+
public long getCacheTimeStamp(long id) throws IOException{
477+
InputStream inputStream = null;
478+
long timestamp = 0;
479+
if (this.uses3) {
480+
String fileName = "cache/" + id + "indexdb.json";
481+
482+
try {
483+
if (this.fichierS3Service.isObjectExist(fileName)) {
484+
inputStream = this.getObject(fileName);
485+
} else {
486+
fileName = "cache/" + id + "_exam_template_indexdb.json";
487+
488+
if (this.fichierS3Service.isObjectExist(fileName)) {
489+
490+
inputStream = this.getObject(fileName);
491+
492+
}
493+
}
494+
} catch (InvalidKeyException | NoSuchAlgorithmException | IllegalArgumentException e) {
495+
e.printStackTrace();
496+
return 0;
497+
}
498+
if (inputStream == null) {
499+
return this.getCacheTimeStampSqlite(id);
500+
501+
}
502+
503+
} else {
504+
String fileName = id + "indexdb.json";
505+
File customDir = new File(UPLOAD_DIR);
506+
fileName = customDir.getAbsolutePath() +
507+
File.separator + fileName;
508+
if (Paths.get(fileName).toFile().exists()) {
509+
510+
inputStream = Files.newInputStream(Paths.get(fileName));
511+
512+
} else {
513+
fileName = "cache/" + id + "_exam_template_indexdb.json";
514+
fileName = customDir.getAbsolutePath() +
515+
File.separator + fileName;
516+
if (Paths.get(fileName).toFile().exists()) {
517+
inputStream = Files.newInputStream(Paths.get(fileName));
518+
519+
}
520+
521+
}
522+
if (inputStream == null) {
523+
return this.getCacheTimeStampSqlite(id);
524+
525+
}
526+
527+
}
528+
529+
JsonReader reader = new JsonReader(new InputStreamReader(inputStream));
530+
reader.beginObject();
531+
reader.nextName();
532+
reader.skipValue();
533+
reader.nextName();
534+
reader.skipValue();
535+
reader.nextName();
536+
reader.beginObject();
537+
reader.nextName();
538+
reader.skipValue();
539+
reader.nextName();
540+
reader.skipValue();
541+
reader.nextName();
542+
reader.skipValue();
543+
reader.nextName();
544+
reader.beginArray();
545+
546+
// exams
547+
548+
reader.beginObject();
549+
reader.nextName();
550+
reader.nextString();
551+
reader.nextName();
552+
reader.skipValue();
553+
reader.nextName();
554+
reader.beginArray();
555+
while (reader.hasNext()){
556+
reader.beginObject();
557+
reader.nextName();
558+
reader.nextLong();
559+
if (reader.hasNext()){
560+
reader.nextName();
561+
timestamp= reader.nextLong();
562+
}
563+
reader.endObject();
564+
}
565+
reader.endArray();
566+
567+
568+
reader.endObject();
569+
570+
571+
// reader.endObject();
572+
573+
reader.close();
574+
inputStream.close();
575+
return timestamp;
576+
577+
}
404578

405579

406580
public long getCachePageInTemplate(long id) throws IOException {

src/main/java/fr/istic/web/rest/ExtendedAPI.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,23 @@ public Response getCachePageNoAlign(@PathParam("examId") long examId, @PathParam
13471347

13481348
}
13491349
}
1350+
@GET
1351+
@Path("/getCacheTimeStamp/{examId}")
1352+
@Produces(MediaType.TEXT_PLAIN)
1353+
public Response getCacheTimeStamp(@PathParam("examId") long examId) {
1354+
try {
1355+
return Response
1356+
.status(Response.Status.OK)
1357+
.entity(cacheUploadService.getCacheTimeStamp(examId))
1358+
.type(MediaType.TEXT_PLAIN)
1359+
.build();
1360+
1361+
} catch (Exception e) {
1362+
e.printStackTrace();
1363+
return Response.serverError().build();
1364+
1365+
}
1366+
}
13501367

13511368
/*
13521369
* @GET

0 commit comments

Comments
 (0)