Search in sources :

Example 1 with TranscriptOfRecordsFile

use of fi.otavanopisto.muikku.plugins.transcriptofrecords.model.TranscriptOfRecordsFile in project muikku by otavanopisto.

the class TranscriptOfRecordsFileController method outputFileToStream.

public void outputFileToStream(TranscriptOfRecordsFile torFile, OutputStream stream) {
    String fileUuid = torFile.getFileName();
    if (!UUID_PATTERN.matcher(fileUuid).matches()) {
        throw new RuntimeException("File name is not a valid UUID");
    }
    File file = Paths.get(getFileUploadBasePath(), fileUuid).toFile();
    try {
        FileUtils.copyFile(file, stream);
    } catch (IOException e) {
        // Wrap with unchecked exception to adhere to StreamingOutput interface
        throw new RuntimeException(e);
    }
}
Also used : IOException(java.io.IOException) TranscriptOfRecordsFile(fi.otavanopisto.muikku.plugins.transcriptofrecords.model.TranscriptOfRecordsFile) File(java.io.File)

Example 2 with TranscriptOfRecordsFile

use of fi.otavanopisto.muikku.plugins.transcriptofrecords.model.TranscriptOfRecordsFile in project muikku by otavanopisto.

the class TranscriptOfRecordsFileUploadServlet method doPost.

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    if (!sessionController.isLoggedIn()) {
        sendResponse(resp, "Must be logged in", HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    if (!sessionController.hasEnvironmentPermission(TranscriptofRecordsPermissions.TRANSCRIPT_OF_RECORDS_FILE_UPLOAD)) {
        sendResponse(resp, "Insufficient permissions", HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    Part userIdentifierPart = req.getPart("userIdentifier");
    if (userIdentifierPart == null) {
        sendResponse(resp, "Missing userIdentifier", HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    String userIdentifier = "";
    try (InputStream is = userIdentifierPart.getInputStream()) {
        userIdentifier = IOUtils.toString(is, StandardCharsets.UTF_8);
    }
    SchoolDataIdentifier schoolDataIdentifier = SchoolDataIdentifier.fromId(userIdentifier);
    if (schoolDataIdentifier == null) {
        sendResponse(resp, "Invalid userIdentifier", HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    UserEntity userEntity = userEntityController.findUserEntityByUserIdentifier(schoolDataIdentifier);
    if (userEntity == null) {
        sendResponse(resp, "User entity not found", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return;
    }
    Part titlePart = req.getPart("title");
    if (titlePart == null) {
        sendResponse(resp, "Missing title", HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    String title = "";
    try (InputStream is = titlePart.getInputStream()) {
        title = IOUtils.toString(is, StandardCharsets.UTF_8);
    }
    Part descriptionPart = req.getPart("description");
    if (descriptionPart == null) {
        sendResponse(resp, "Missing description", HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    String description = "";
    try (InputStream is = descriptionPart.getInputStream()) {
        description = IOUtils.toString(is, StandardCharsets.UTF_8);
    }
    Part uploadPart = req.getPart("upload");
    if (uploadPart == null) {
        sendResponse(resp, "Missing file", HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    String contentType = uploadPart.getContentType();
    long fileSizeLimit = systemSettingsController.getUploadFileSizeLimit();
    if (uploadPart.getSize() > fileSizeLimit) {
        sendResponse(resp, "File too large", HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE);
        return;
    }
    try (InputStream is = uploadPart.getInputStream()) {
        TranscriptOfRecordsFile file = transcriptOfRecordsFileController.attachFile(userEntity, is, contentType, title, description);
        String result = (new ObjectMapper()).writeValueAsString(file);
        sendResponse(resp, result, HttpServletResponse.SC_OK);
    }
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) Part(javax.servlet.http.Part) InputStream(java.io.InputStream) TranscriptOfRecordsFile(fi.otavanopisto.muikku.plugins.transcriptofrecords.model.TranscriptOfRecordsFile) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with TranscriptOfRecordsFile

use of fi.otavanopisto.muikku.plugins.transcriptofrecords.model.TranscriptOfRecordsFile in project muikku by otavanopisto.

the class TranscriptofRecordsBackingBean method init.

@RequestAction
public String init() {
    if (!sessionController.hasEnvironmentPermission(TranscriptofRecordsPermissions.TRANSCRIPT_OF_RECORDS_VIEW)) {
        return NavigationRules.ACCESS_DENIED;
    }
    Map<String, Grade> grades = new HashMap<>();
    List<GradingScale> gradingScales = gradingController.listGradingScales();
    for (GradingScale gradingScale : gradingScales) {
        List<GradingScaleItem> scaleItems = gradingController.listGradingScaleItems(gradingScale);
        for (GradingScaleItem scaleItem : scaleItems) {
            String id = StringUtils.join(new String[] { gradingScale.getSchoolDataSource(), gradingScale.getIdentifier(), scaleItem.getSchoolDataSource(), scaleItem.getIdentifier() }, '-');
            String grade = scaleItem.getName();
            String scale = gradingScale.getName();
            Boolean passing = scaleItem.isPassingGrade();
            grades.put(id, new Grade(grade, scale, passing));
        }
    }
    try {
        this.grades = new ObjectMapper().writeValueAsString(grades);
    } catch (JsonProcessingException e) {
        logger.log(Level.SEVERE, "Failed to serialize grades", e);
        return NavigationRules.INTERNAL_ERROR;
    }
    UserEntity loggedEntity = sessionController.getLoggedUserEntity();
    User user = userController.findUserByDataSourceAndIdentifier(sessionController.getLoggedUserSchoolDataSource(), sessionController.getLoggedUserIdentifier());
    studyStartDate = user.getStudyStartDate();
    studyTimeEnd = user.getStudyTimeEnd();
    studyTimeLeftStr = "";
    if (studyTimeEnd != null) {
        OffsetDateTime now = OffsetDateTime.now();
        Locale locale = sessionController.getLocale();
        if (now.isBefore(studyTimeEnd)) {
            long studyTimeLeftYears = now.until(studyTimeEnd, ChronoUnit.YEARS);
            now = now.plusYears(studyTimeLeftYears);
            if (studyTimeLeftYears > 0) {
                studyTimeLeftStr += studyTimeLeftYears + " " + localeController.getText(locale, "plugin.records.studyTimeEndShort.y");
            }
            long studyTimeLeftMonths = now.until(studyTimeEnd, ChronoUnit.MONTHS);
            now = now.plusMonths(studyTimeLeftMonths);
            if (studyTimeLeftMonths > 0) {
                if (studyTimeLeftStr.length() > 0)
                    studyTimeLeftStr += " ";
                studyTimeLeftStr += studyTimeLeftMonths + " " + localeController.getText(locale, "plugin.records.studyTimeEndShort.m");
            }
            long studyTimeLeftDays = now.until(studyTimeEnd, ChronoUnit.DAYS);
            now = now.plusDays(studyTimeLeftDays);
            if (studyTimeLeftDays > 0) {
                if (studyTimeLeftStr.length() > 0)
                    studyTimeLeftStr += " ";
                studyTimeLeftStr += studyTimeLeftDays + " " + localeController.getText(locale, "plugin.records.studyTimeEndShort.d");
            }
        }
    }
    List<TranscriptOfRecordsFile> transcriptOfRecordsFiles;
    if (loggedEntity != null) {
        transcriptOfRecordsFiles = transcriptOfRecordsFileController.listFiles(loggedEntity);
    } else {
        transcriptOfRecordsFiles = Collections.emptyList();
    }
    try {
        files = new ObjectMapper().writeValueAsString(transcriptOfRecordsFiles);
    } catch (JsonProcessingException e) {
        logger.log(Level.SEVERE, "Failed to serialize files", e);
        return NavigationRules.INTERNAL_ERROR;
    }
    return null;
}
Also used : Locale(java.util.Locale) GradingScale(fi.otavanopisto.muikku.schooldata.entity.GradingScale) User(fi.otavanopisto.muikku.schooldata.entity.User) HashMap(java.util.HashMap) GradingScaleItem(fi.otavanopisto.muikku.schooldata.entity.GradingScaleItem) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) OffsetDateTime(java.time.OffsetDateTime) TranscriptOfRecordsFile(fi.otavanopisto.muikku.plugins.transcriptofrecords.model.TranscriptOfRecordsFile) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RequestAction(org.ocpsoft.rewrite.annotation.RequestAction)

Example 4 with TranscriptOfRecordsFile

use of fi.otavanopisto.muikku.plugins.transcriptofrecords.model.TranscriptOfRecordsFile in project muikku by otavanopisto.

the class GuiderRESTService method deleteTranscriptOfRecordsFile.

@DELETE
@Path("/files/{ID}")
@RESTPermit(GuiderPermissions.GUIDER_DELETE_TORFILE)
public Response deleteTranscriptOfRecordsFile(@PathParam("ID") Long fileId) {
    TranscriptOfRecordsFile file = torFileController.findFileById(fileId);
    if (file == null) {
        return Response.status(Status.NOT_FOUND).entity("file not found").build();
    }
    torFileController.delete(file);
    return Response.status(Status.NO_CONTENT).build();
}
Also used : TranscriptOfRecordsFile(fi.otavanopisto.muikku.plugins.transcriptofrecords.model.TranscriptOfRecordsFile) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) RESTPermit(fi.otavanopisto.security.rest.RESTPermit)

Example 5 with TranscriptOfRecordsFile

use of fi.otavanopisto.muikku.plugins.transcriptofrecords.model.TranscriptOfRecordsFile in project muikku by otavanopisto.

the class TranscriptofRecordsRESTService method getFileContent.

@GET
@Path("/files/{ID}/content")
@RESTPermit(handling = Handling.INLINE)
@Produces("*/*")
public Response getFileContent(@PathParam("ID") Long fileId) {
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.FORBIDDEN).entity("Must be logged in").build();
    }
    UserEntity loggedUserEntity = sessionController.getLoggedUserEntity();
    TranscriptOfRecordsFile file = transcriptOfRecordsFileController.findFileById(fileId);
    if (file == null) {
        return Response.status(Status.NOT_FOUND).entity("File not found").build();
    }
    boolean isLoggedUser = Objects.equals(file.getUserEntityId(), loggedUserEntity.getId());
    if (!isLoggedUser) {
        return Response.status(Status.FORBIDDEN).entity("Not your file").build();
    }
    StreamingOutput output = s -> transcriptOfRecordsFileController.outputFileToStream(file, s);
    String contentType = file.getContentType();
    return Response.ok().type(contentType).entity(output).build();
}
Also used : PluginSettingsController(fi.otavanopisto.muikku.controller.PluginSettingsController) Arrays(java.util.Arrays) Produces(javax.ws.rs.Produces) EnvironmentRoleArchetype(fi.otavanopisto.muikku.model.users.EnvironmentRoleArchetype) Path(javax.ws.rs.Path) StringUtils(org.apache.commons.lang3.StringUtils) User(fi.otavanopisto.muikku.schooldata.entity.User) Consumes(javax.ws.rs.Consumes) PermissionController(fi.otavanopisto.muikku.controller.PermissionController) Map(java.util.Map) TranscriptofRecordsUserProperties(fi.otavanopisto.muikku.plugins.transcriptofrecords.TranscriptofRecordsUserProperties) CourseMetaController(fi.otavanopisto.muikku.schooldata.CourseMetaController) DELETE(javax.ws.rs.DELETE) TranscriptOfRecordsFile(fi.otavanopisto.muikku.plugins.transcriptofrecords.model.TranscriptOfRecordsFile) StudiesViewCourseChoice(fi.otavanopisto.muikku.plugins.transcriptofrecords.model.StudiesViewCourseChoice) UserGroupEntityController(fi.otavanopisto.muikku.users.UserGroupEntityController) SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) StreamingOutput(javax.ws.rs.core.StreamingOutput) EnvironmentUser(fi.otavanopisto.muikku.model.users.EnvironmentUser) VopsLister(fi.otavanopisto.muikku.plugins.transcriptofrecords.VopsLister) TranscriptofRecordsPermissions(fi.otavanopisto.muikku.plugins.transcriptofrecords.TranscriptofRecordsPermissions) GradingController(fi.otavanopisto.muikku.schooldata.GradingController) Objects(java.util.Objects) StudiesViewCourseChoiceController(fi.otavanopisto.muikku.plugins.transcriptofrecords.StudiesViewCourseChoiceController) List(java.util.List) UserEntityController(fi.otavanopisto.muikku.users.UserEntityController) Response(javax.ws.rs.core.Response) UserController(fi.otavanopisto.muikku.users.UserController) EnvironmentRoleEntity(fi.otavanopisto.muikku.model.users.EnvironmentRoleEntity) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceController(fi.otavanopisto.muikku.schooldata.WorkspaceController) PathParam(javax.ws.rs.PathParam) GET(javax.ws.rs.GET) Subject(fi.otavanopisto.muikku.schooldata.entity.Subject) EnvironmentUserController(fi.otavanopisto.muikku.users.EnvironmentUserController) Handling(fi.otavanopisto.security.rest.RESTPermit.Handling) ArrayList(java.util.ArrayList) SessionController(fi.otavanopisto.muikku.session.SessionController) Inject(javax.inject.Inject) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) Status(javax.ws.rs.core.Response.Status) POST(javax.ws.rs.POST) TransferCredit(fi.otavanopisto.muikku.schooldata.entity.TransferCredit) WorkspaceAssessment(fi.otavanopisto.muikku.schooldata.entity.WorkspaceAssessment) IOException(java.io.IOException) TranscriptOfRecordsFileController(fi.otavanopisto.muikku.plugins.transcriptofrecords.TranscriptOfRecordsFileController) Stateful(javax.ejb.Stateful) RestCatchSchoolDataExceptions(fi.otavanopisto.muikku.schooldata.RestCatchSchoolDataExceptions) PluginRESTService(fi.otavanopisto.muikku.plugin.PluginRESTService) WorkspaceUserEntityController(fi.otavanopisto.muikku.users.WorkspaceUserEntityController) RequestScoped(javax.enterprise.context.RequestScoped) PUT(javax.ws.rs.PUT) TranscriptOfRecordsController(fi.otavanopisto.muikku.plugins.transcriptofrecords.TranscriptOfRecordsController) Comparator(java.util.Comparator) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) TranscriptOfRecordsFile(fi.otavanopisto.muikku.plugins.transcriptofrecords.model.TranscriptOfRecordsFile) StreamingOutput(javax.ws.rs.core.StreamingOutput) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

TranscriptOfRecordsFile (fi.otavanopisto.muikku.plugins.transcriptofrecords.model.TranscriptOfRecordsFile)9 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)5 SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)4 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)4 Path (javax.ws.rs.Path)4 IOException (java.io.IOException)3 DELETE (javax.ws.rs.DELETE)3 GET (javax.ws.rs.GET)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)2 PluginRESTService (fi.otavanopisto.muikku.plugin.PluginRESTService)2 TranscriptOfRecordsFileController (fi.otavanopisto.muikku.plugins.transcriptofrecords.TranscriptOfRecordsFileController)2 RestCatchSchoolDataExceptions (fi.otavanopisto.muikku.schooldata.RestCatchSchoolDataExceptions)2 SessionController (fi.otavanopisto.muikku.session.SessionController)2 UserEntityController (fi.otavanopisto.muikku.users.UserEntityController)2 WorkspaceUserEntityController (fi.otavanopisto.muikku.users.WorkspaceUserEntityController)2 Handling (fi.otavanopisto.security.rest.RESTPermit.Handling)2 List (java.util.List)2 Map (java.util.Map)2 Stateful (javax.ejb.Stateful)2