Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 174 additions & 0 deletions src/main/java/fr/istic/service/CacheUploadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -400,7 +401,180 @@ public long getCachePageInTemplateSqlite(long id) throws IOException {
return 0;
}

public long getCacheTimeStampSqlite(long id) throws IOException {

InputStream inputStream = null;
String dbpath = "";
if (pathsqlite.containsKey(id) && Paths.get(pathsqlite.get(id)).toFile().exists()) {
dbpath = pathsqlite.get(id);
} else {
if (this.uses3) {
String fileName = "cache/" + id + ".sqlite3";
try {
if (this.fichierS3Service.isObjectExist(fileName)) {
inputStream = this.getObject(fileName);
dbpath = this.writeStreamToTempFile(inputStream, id + ".sqlite3").getAbsolutePath();
pathsqlite.put(id, dbpath);
}
} catch (InvalidKeyException | NoSuchAlgorithmException | IllegalArgumentException e) {
e.printStackTrace();
return 0;
}
} else {
String fileName = id + ".sqlite3";
File customDir = new File(UPLOAD_DIR);
fileName = customDir.getAbsolutePath() +
File.separator + fileName;
if (Paths.get(fileName).toFile().exists()) {
dbpath = Paths.get(fileName).toFile().getAbsolutePath();
pathsqlite.put(id, dbpath);

}
}
}
if ("".equals(dbpath) || dbpath == null) {
return 0;
}

Connection conn = null;
try {
// db parameters
String url = "jdbc:sqlite:" + dbpath;

// create a connection to the database
conn = DriverManager.getConnection(url);
String query = "select DT from exam";
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery(query);
boolean hasAline = rs.next();
if (hasAline) {
Date time = rs.getTime(1);
return time.getTime();
}

} catch (SQLException e) {
e.printStackTrace();
}

} catch (SQLException e) {

System.out.println(e.getMessage());
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
log.error(ex.getMessage());
}
}
return 0;
}



public long getCacheTimeStamp(long id) throws IOException{
InputStream inputStream = null;
long timestamp = 0;
if (this.uses3) {
String fileName = "cache/" + id + "indexdb.json";

try {
if (this.fichierS3Service.isObjectExist(fileName)) {
inputStream = this.getObject(fileName);
} else {
fileName = "cache/" + id + "_exam_template_indexdb.json";

if (this.fichierS3Service.isObjectExist(fileName)) {

inputStream = this.getObject(fileName);

}
}
} catch (InvalidKeyException | NoSuchAlgorithmException | IllegalArgumentException e) {
e.printStackTrace();
return 0;
}
if (inputStream == null) {
return this.getCacheTimeStampSqlite(id);

}

} else {
String fileName = id + "indexdb.json";
File customDir = new File(UPLOAD_DIR);
fileName = customDir.getAbsolutePath() +
File.separator + fileName;
if (Paths.get(fileName).toFile().exists()) {

inputStream = Files.newInputStream(Paths.get(fileName));

} else {
fileName = "cache/" + id + "_exam_template_indexdb.json";
fileName = customDir.getAbsolutePath() +
File.separator + fileName;
if (Paths.get(fileName).toFile().exists()) {
inputStream = Files.newInputStream(Paths.get(fileName));

}

}
if (inputStream == null) {
return this.getCacheTimeStampSqlite(id);

}

}

JsonReader reader = new JsonReader(new InputStreamReader(inputStream));
reader.beginObject();
reader.nextName();
reader.skipValue();
reader.nextName();
reader.skipValue();
reader.nextName();
reader.beginObject();
reader.nextName();
reader.skipValue();
reader.nextName();
reader.skipValue();
reader.nextName();
reader.skipValue();
reader.nextName();
reader.beginArray();

// exams

reader.beginObject();
reader.nextName();
reader.nextString();
reader.nextName();
reader.skipValue();
reader.nextName();
reader.beginArray();
while (reader.hasNext()){
reader.beginObject();
reader.nextName();
reader.nextLong();
if (reader.hasNext()){
reader.nextName();
timestamp= reader.nextLong();
}
reader.endObject();
}
reader.endArray();


reader.endObject();


// reader.endObject();

reader.close();
inputStream.close();
return timestamp;

}


public long getCachePageInTemplate(long id) throws IOException {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/fr/istic/service/ExamService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.quarkus.panache.common.Page;
import fr.istic.domain.Answer2HybridGradedComment;
import fr.istic.domain.Comments;
import fr.istic.domain.Course;
import fr.istic.domain.Exam;
import fr.istic.domain.ExamSheet;
import fr.istic.domain.FinalResult;
Expand All @@ -25,6 +26,7 @@
import fr.istic.domain.enumeration.GradeType;
import fr.istic.service.customdto.answernotebooks.AnswersNoteBook;
import fr.istic.service.customdto.answernotebooks.QuestionNoteBook;
import fr.istic.service.dto.CourseDTO;
import fr.istic.service.dto.ExamDTO;
import fr.istic.service.mapper.ExamMapper;
import org.slf4j.Logger;
Expand Down Expand Up @@ -251,6 +253,17 @@ public Paged<ExamDTO> findAll(Page page) {
.map(exam -> examMapper.toDto((Exam) exam));
}

/**
* Get all the exams.
* @param page the pagination information.
* @return the list of entities.
*/
public Paged<ExamDTO> findAll4User(Page page, User u) {
log.debug("Request to get all Exams " + u.login );
return new Paged<>(Exam.findExambyLogin(u.login).page(page))
.map(exam -> examMapper.toDto((Exam) exam));
}

@Transactional
public Paged<ExamDTO> findExambyCourseId(Page page, long courseId) {
log.debug("Request to get all Exams");
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/fr/istic/web/rest/ExamResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
import fr.istic.domain.Course;
import fr.istic.domain.Exam;
import fr.istic.domain.ExamSheet;
import fr.istic.domain.Scan;
import fr.istic.domain.Student;
import fr.istic.domain.StudentResponse;
import fr.istic.domain.User;
import fr.istic.security.AuthoritiesConstants;
import fr.istic.service.ExamService;
Expand Down Expand Up @@ -208,6 +205,7 @@ public Response getAllExams(@BeanParam PageRequestVM pageRequest, @BeanParam Sor
result = examService.findExambyScanId(page, Long.parseLong("" + scanId.get(0)));

} else {

if (ctx.getUserPrincipal().getName()!= null){


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

} else {
}
else if (user.get().authorities.size() >= 1
&& user.get().authorities.stream().anyMatch(e1 -> e1.equals(new Authority("ROLE_USER")))) {
result = examService.findAll4User(page,user.get());

}
else {
return Response.status(403, "Current user cannot access to this ressource").build();
}

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/fr/istic/web/rest/ExtendedAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,23 @@ public Response getCachePageNoAlign(@PathParam("examId") long examId, @PathParam

}
}
@GET
@Path("/getCacheTimeStamp/{examId}")
@Produces(MediaType.TEXT_PLAIN)
public Response getCacheTimeStamp(@PathParam("examId") long examId) {
try {
return Response
.status(Response.Status.OK)
.entity(cacheUploadService.getCacheTimeStamp(examId))
.type(MediaType.TEXT_PLAIN)
.build();

} catch (Exception e) {
e.printStackTrace();
return Response.serverError().build();

}
}

/*
* @GET
Expand Down