Skip to content

Commit 1fbf550

Browse files
authored
Merge pull request #167 from correctexam/develop
Develop
2 parents f87a7ea + 5ed81ce commit 1fbf550

4 files changed

Lines changed: 212 additions & 4 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/service/ExamService.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.quarkus.panache.common.Page;
1010
import fr.istic.domain.Answer2HybridGradedComment;
1111
import fr.istic.domain.Comments;
12+
import fr.istic.domain.Course;
1213
import fr.istic.domain.Exam;
1314
import fr.istic.domain.ExamSheet;
1415
import fr.istic.domain.FinalResult;
@@ -25,6 +26,7 @@
2526
import fr.istic.domain.enumeration.GradeType;
2627
import fr.istic.service.customdto.answernotebooks.AnswersNoteBook;
2728
import fr.istic.service.customdto.answernotebooks.QuestionNoteBook;
29+
import fr.istic.service.dto.CourseDTO;
2830
import fr.istic.service.dto.ExamDTO;
2931
import fr.istic.service.mapper.ExamMapper;
3032
import org.slf4j.Logger;
@@ -251,6 +253,17 @@ public Paged<ExamDTO> findAll(Page page) {
251253
.map(exam -> examMapper.toDto((Exam) exam));
252254
}
253255

256+
/**
257+
* Get all the exams.
258+
* @param page the pagination information.
259+
* @return the list of entities.
260+
*/
261+
public Paged<ExamDTO> findAll4User(Page page, User u) {
262+
log.debug("Request to get all Exams " + u.login );
263+
return new Paged<>(Exam.findExambyLogin(u.login).page(page))
264+
.map(exam -> examMapper.toDto((Exam) exam));
265+
}
266+
254267
@Transactional
255268
public Paged<ExamDTO> findExambyCourseId(Page page, long courseId) {
256269
log.debug("Request to get all Exams");

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
import fr.istic.domain.Course;
77
import fr.istic.domain.Exam;
88
import fr.istic.domain.ExamSheet;
9-
import fr.istic.domain.Scan;
10-
import fr.istic.domain.Student;
11-
import fr.istic.domain.StudentResponse;
129
import fr.istic.domain.User;
1310
import fr.istic.security.AuthoritiesConstants;
1411
import fr.istic.service.ExamService;
@@ -208,6 +205,7 @@ public Response getAllExams(@BeanParam PageRequestVM pageRequest, @BeanParam Sor
208205
result = examService.findExambyScanId(page, Long.parseLong("" + scanId.get(0)));
209206

210207
} else {
208+
211209
if (ctx.getUserPrincipal().getName()!= null){
212210

213211

@@ -223,7 +221,13 @@ public Response getAllExams(@BeanParam PageRequestVM pageRequest, @BeanParam Sor
223221
&& user.get().authorities.stream().anyMatch(e1 -> e1.equals(new Authority("ROLE_ADMIN")))) {
224222
result = examService.findAll(page);
225223

226-
} else {
224+
}
225+
else if (user.get().authorities.size() >= 1
226+
&& user.get().authorities.stream().anyMatch(e1 -> e1.equals(new Authority("ROLE_USER")))) {
227+
result = examService.findAll4User(page,user.get());
228+
229+
}
230+
else {
227231
return Response.status(403, "Current user cannot access to this ressource").build();
228232
}
229233

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)