use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswerFile in project muikku by otavanopisto.
the class WorkspaceFileFieldIOHandler method retrieve.
@Override
public String retrieve(WorkspaceMaterialField field, WorkspaceMaterialReply reply) throws WorkspaceFieldIOException {
List<File> result = new ArrayList<>();
WorkspaceMaterialFileFieldAnswer fieldAnswer = workspaceMaterialFieldAnswerController.findWorkspaceMaterialFileFieldAnswerByFieldAndReply(field, reply);
if (fieldAnswer != null) {
List<WorkspaceMaterialFileFieldAnswerFile> answerFiles = workspaceMaterialFieldAnswerController.listWorkspaceMaterialFileFieldAnswerFilesByFieldAnswer(fieldAnswer);
for (WorkspaceMaterialFileFieldAnswerFile answerFile : answerFiles) {
result.add(new File(answerFile.getFileId(), answerFile.getFileName(), answerFile.getContentType()));
}
}
try {
return new ObjectMapper().writeValueAsString(result);
} catch (IOException e) {
throw new WorkspaceFieldIOException("Could not marshal file file response", e);
}
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswerFile in project muikku by otavanopisto.
the class WorkspaceMaterialFileFieldAnswerFileDAO method findByFileId.
public WorkspaceMaterialFileFieldAnswerFile findByFileId(String fileId) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<WorkspaceMaterialFileFieldAnswerFile> criteria = criteriaBuilder.createQuery(WorkspaceMaterialFileFieldAnswerFile.class);
Root<WorkspaceMaterialFileFieldAnswerFile> root = criteria.from(WorkspaceMaterialFileFieldAnswerFile.class);
criteria.select(root);
criteria.where(criteriaBuilder.equal(root.get(WorkspaceMaterialFileFieldAnswerFile_.fileId), fileId));
return getSingleResult(entityManager.createQuery(criteria));
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswerFile in project muikku by otavanopisto.
the class WorkspaceMaterialFileFieldAnswerFileDAO method listByFieldAnswer.
public List<WorkspaceMaterialFileFieldAnswerFile> listByFieldAnswer(WorkspaceMaterialFileFieldAnswer fieldAnswer) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<WorkspaceMaterialFileFieldAnswerFile> criteria = criteriaBuilder.createQuery(WorkspaceMaterialFileFieldAnswerFile.class);
Root<WorkspaceMaterialFileFieldAnswerFile> root = criteria.from(WorkspaceMaterialFileFieldAnswerFile.class);
criteria.select(root);
criteria.where(criteriaBuilder.equal(root.get(WorkspaceMaterialFileFieldAnswerFile_.fieldAnswer), fieldAnswer));
return entityManager.createQuery(criteria).getResultList();
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswerFile in project muikku by otavanopisto.
the class WorkspaceRESTService method getAllFileAnswers.
@GET
@Produces("application/zip")
@Path("/allfileanswers/{FILEID}")
@RESTPermit(handling = Handling.INLINE)
public Response getAllFileAnswers(@PathParam("FILEID") String fileId, @QueryParam("archiveName") String archiveName) {
if (!sessionController.isLoggedIn()) {
return Response.status(Status.UNAUTHORIZED).build();
}
// Find the initial file
WorkspaceMaterialFileFieldAnswerFile answerFile = workspaceMaterialFieldAnswerController.findWorkspaceMaterialFileFieldAnswerFileByFileId(fileId);
if (answerFile == null) {
return Response.status(Status.NOT_FOUND).build();
}
// Access check
fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialReply workspaceMaterialReply = answerFile.getFieldAnswer().getReply();
if (workspaceMaterialReply == null) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find reply from answer file %d", answerFile.getId())).build();
}
WorkspaceMaterial workspaceMaterial = workspaceMaterialReply.getWorkspaceMaterial();
if (workspaceMaterial == null) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find workspace material from reply %d", workspaceMaterialReply.getId())).build();
}
WorkspaceRootFolder workspaceRootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceNode(workspaceMaterial);
if (workspaceRootFolder == null) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find workspace root folder for material %d", workspaceMaterial.getId())).build();
}
WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceRootFolder.getWorkspaceEntityId());
if (workspaceEntity == null) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find workspace entity for root folder %d", workspaceRootFolder.getId())).build();
}
if (!workspaceMaterialReply.getUserEntityId().equals(sessionController.getLoggedUserEntity().getId())) {
if (!sessionController.hasWorkspacePermission(MuikkuPermissions.ACCESS_STUDENT_ANSWERS, workspaceEntity)) {
return Response.status(Status.FORBIDDEN).build();
}
}
// Fetch all files belonging to the same answer as the initial file
List<WorkspaceMaterialFileFieldAnswerFile> answerFiles = workspaceMaterialFieldAnswerController.listWorkspaceMaterialFileFieldAnswerFilesByFieldAnswer(answerFile.getFieldAnswer());
if (CollectionUtils.isEmpty(answerFiles)) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("answerFiles not found")).build();
}
try {
Set<String> fileNames = new HashSet<String>();
StreamingOutput output = new StreamingOutput() {
@Override
public void write(OutputStream out) throws IOException {
ZipOutputStream zout = new ZipOutputStream(out);
for (WorkspaceMaterialFileFieldAnswerFile file : answerFiles) {
// Prevent duplicate file names
String fileName = file.getFileName();
if (fileNames.contains(fileName)) {
int counter = 1;
String name = file.getFileName();
String prefix = "";
if (StringUtils.contains(name, ".")) {
prefix = StringUtils.substringAfterLast(name, ".");
name = StringUtils.substringBeforeLast(name, ".");
}
if (!StringUtils.isEmpty(prefix)) {
prefix = String.format(".%s", prefix);
}
while (fileNames.contains(fileName)) {
fileName = String.format("%s (%s)%s", name, counter++, prefix);
}
}
fileNames.add(fileName);
// Zip file
ZipEntry ze = new ZipEntry(fileName);
zout.putNextEntry(ze);
InputStream inputStream = new ByteArrayInputStream(file.getContent());
IOUtils.copy(inputStream, zout);
zout.closeEntry();
}
zout.flush();
zout.close();
}
};
archiveName = StringUtils.defaultIfEmpty(archiveName, "files.zip");
return Response.ok(output).header("Content-Disposition", "attachment; filename=\"" + archiveName.replaceAll("\"", "\\\"") + "\"").build();
} catch (Exception e) {
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
}
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswerFile in project muikku by otavanopisto.
the class WorkspaceRESTService method getFileAnswer.
@GET
@Path("/fileanswer/{FILEID}")
@RESTPermit(handling = Handling.INLINE)
public Response getFileAnswer(@PathParam("FILEID") String fileId) {
if (!sessionController.isLoggedIn()) {
return Response.status(Status.UNAUTHORIZED).build();
}
WorkspaceMaterialFileFieldAnswerFile answerFile = workspaceMaterialFieldAnswerController.findWorkspaceMaterialFileFieldAnswerFileByFileId(fileId);
if (answerFile == null) {
return Response.status(Status.NOT_FOUND).build();
}
fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialReply workspaceMaterialReply = answerFile.getFieldAnswer().getReply();
if (workspaceMaterialReply == null) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find reply from answer file %d", answerFile.getId())).build();
}
WorkspaceMaterial workspaceMaterial = workspaceMaterialReply.getWorkspaceMaterial();
if (workspaceMaterial == null) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find workspace material from reply %d", workspaceMaterialReply.getId())).build();
}
WorkspaceRootFolder workspaceRootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceNode(workspaceMaterial);
if (workspaceRootFolder == null) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find workspace root folder for material %d", workspaceMaterial.getId())).build();
}
WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceRootFolder.getWorkspaceEntityId());
if (workspaceEntity == null) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find workspace entity for root folder %d", workspaceRootFolder.getId())).build();
}
if (!workspaceMaterialReply.getUserEntityId().equals(sessionController.getLoggedUserEntity().getId())) {
if (!sessionController.hasWorkspacePermission(MuikkuPermissions.ACCESS_STUDENT_ANSWERS, workspaceEntity)) {
return Response.status(Status.FORBIDDEN).build();
}
}
return Response.ok(answerFile.getContent()).type(answerFile.getContentType()).header("Content-Disposition", "attachment; filename=\"" + answerFile.getFileName().replaceAll("\"", "\\\"") + "\"").build();
}
Aggregations