Search in sources :

Example 1 with WorkspaceEntityFile

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceEntityFile in project muikku by otavanopisto.

the class WorkspaceRESTService method createWorkspaceFile.

@POST
@Path("/workspaces/{WORKSPACEID}/workspacefile/")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response createWorkspaceFile(@PathParam("WORKSPACEID") Long workspaceId, WorkspaceEntityFileRESTModel entity) {
    WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceId);
    if (workspaceEntity == null)
        return Response.status(Status.BAD_REQUEST).build();
    if (!sessionController.hasWorkspacePermission(MuikkuPermissions.MANAGE_WORKSPACE, workspaceEntity)) {
        return Response.status(Status.FORBIDDEN).build();
    }
    if (StringUtils.isBlank(entity.getContentType())) {
        return Response.status(Status.BAD_REQUEST).entity("contentType is missing").build();
    }
    if (StringUtils.isBlank(entity.getFileIdentifier())) {
        return Response.status(Status.BAD_REQUEST).entity("identifier is missing").build();
    }
    byte[] content = null;
    if (StringUtils.isNotBlank(entity.getTempFileId())) {
        try {
            content = TempFileUtils.getTempFileData(entity.getTempFileId());
        } catch (IOException e) {
            return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
        }
    } else if (StringUtils.isNotBlank(entity.getBase64Data())) {
        String base64File = entity.getBase64Data().split(",")[1];
        content = DatatypeConverter.parseBase64Binary(base64File);
    }
    if (content == null) {
        return Response.status(Status.BAD_REQUEST).entity("no content was found").build();
    }
    try {
        WorkspaceEntityFile workspaceEntityFile = workspaceEntityFileController.findWorkspaceEntityFile(workspaceEntity, entity.getFileIdentifier());
        ByteArrayInputStream contentStream = new ByteArrayInputStream(content);
        if (workspaceEntityFile == null) {
            String diskName = fileController.createFile("workspace", contentStream);
            workspaceEntityFile = workspaceEntityFileController.createWorkspaceEntityFile(workspaceEntity, entity.getFileIdentifier(), diskName, entity.getContentType(), new Date());
        } else {
            fileController.updateFile("workspace", workspaceEntityFile.getDiskName(), contentStream);
            workspaceEntityFile = workspaceEntityFileController.updateWorkspaceEntityFile(workspaceEntityFile, entity.getContentType(), new Date());
        }
        return Response.ok(createRestModel(workspaceEntityFile)).build();
    } catch (IOException e) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
    }
}
Also used : WorkspaceEntityFile(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceEntityFile) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) WorkspaceFieldIOException(fi.otavanopisto.muikku.plugins.workspace.fieldio.WorkspaceFieldIOException) IOException(java.io.IOException) Date(java.util.Date) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) POST(javax.ws.rs.POST)

Example 2 with WorkspaceEntityFile

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceEntityFile in project muikku by otavanopisto.

the class WorkspaceIndexBackingBean method init.

@RequestAction
public String init() {
    String urlName = getWorkspaceUrlName();
    if (StringUtils.isBlank(urlName)) {
        return NavigationRules.NOT_FOUND;
    }
    WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityByUrlName(urlName);
    if (workspaceEntity == null) {
        return NavigationRules.NOT_FOUND;
    }
    canPublish = sessionController.hasWorkspacePermission(MuikkuPermissions.PUBLISH_WORKSPACE, workspaceEntity);
    workspaceEntityId = workspaceEntity.getId();
    published = workspaceEntity.getPublished();
    if (!published) {
        if (!sessionController.hasWorkspacePermission(MuikkuPermissions.ACCESS_UNPUBLISHED_WORKSPACE, workspaceEntity)) {
            return NavigationRules.NOT_FOUND;
        }
    }
    try {
        WorkspaceMaterial frontPage = workspaceMaterialController.ensureWorkspaceFrontPageExists(workspaceEntity);
        contentNodes = Arrays.asList(workspaceMaterialController.createContentNode(frontPage));
    } catch (WorkspaceMaterialException e) {
        logger.log(Level.SEVERE, "Error loading materials", e);
        return NavigationRules.INTERNAL_ERROR;
    }
    workspaceBackingBean.setWorkspaceUrlName(urlName);
    schoolDataBridgeSessionController.startSystemSession();
    try {
        Workspace workspace = workspaceController.findWorkspace(workspaceEntity);
        if (workspace == null) {
            logger.warning(String.format("Could not find workspace for workspaceEntity #%d", workspaceEntity.getId()));
            return NavigationRules.NOT_FOUND;
        }
        WorkspaceType workspaceType = workspaceController.findWorkspaceType(workspace.getWorkspaceTypeId());
        EducationType educationTypeObject = workspace.getEducationTypeIdentifier() == null ? null : courseMetaController.findEducationType(workspace.getEducationTypeIdentifier());
        Subject subjectObject = courseMetaController.findSubject(workspace.getSchoolDataSource(), workspace.getSubjectIdentifier());
        CourseLengthUnit lengthUnit = null;
        if ((workspace.getLength() != null) && (workspace.getLengthUnitIdentifier() != null)) {
            lengthUnit = courseMetaController.findCourseLengthUnit(workspace.getSchoolDataSource(), workspace.getLengthUnitIdentifier());
        }
        workspaceId = workspaceEntity.getId();
        workspaceName = workspace.getName();
        workspaceNameExtension = workspace.getNameExtension();
        subject = subjectObject != null ? subjectObject.getName() : null;
        educationType = educationTypeObject != null ? educationTypeObject.getName() : null;
        if (lengthUnit != null) {
            courseLength = workspace.getLength();
            courseLengthSymbol = lengthUnit.getSymbol();
        }
        beginDate = workspace.getBeginDate() != null ? Date.from(workspace.getBeginDate().toInstant()) : null;
        endDate = workspace.getEndDate() != null ? Date.from(workspace.getEndDate().toInstant()) : null;
        if (workspaceType != null) {
            this.workspaceType = workspaceType.getName();
        }
    } finally {
        schoolDataBridgeSessionController.endSystemSession();
    }
    WorkspaceEntityFile customFrontImage = workspaceEntityFileController.findWorkspaceEntityFile(workspaceEntity, "workspace-frontpage-image-cropped");
    hasCustomFrontPageImage = customFrontImage != null;
    customFrontPageImageUrl = hasCustomFrontPageImage ? String.format("/rest/workspace/workspaces/%d/workspacefile/workspace-frontpage-image-cropped", workspaceEntity.getId()) : null;
    materialsBaseUrl = String.format("/workspace/%s/materials", workspaceUrlName);
    announcementsBaseUrl = String.format("/workspace/%s/announcements", workspaceUrlName);
    workspaceVisitController.visit(workspaceEntity);
    return null;
}
Also used : WorkspaceType(fi.otavanopisto.muikku.schooldata.entity.WorkspaceType) WorkspaceEntityFile(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceEntityFile) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) EducationType(fi.otavanopisto.muikku.schooldata.entity.EducationType) CourseLengthUnit(fi.otavanopisto.muikku.schooldata.entity.CourseLengthUnit) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) Subject(fi.otavanopisto.muikku.schooldata.entity.Subject) Workspace(fi.otavanopisto.muikku.schooldata.entity.Workspace) RequestAction(org.ocpsoft.rewrite.annotation.RequestAction)

Example 3 with WorkspaceEntityFile

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceEntityFile in project muikku by otavanopisto.

the class WorkspaceEntityFileDAO method findByWorkspaceAndIdentifier.

public WorkspaceEntityFile findByWorkspaceAndIdentifier(WorkspaceEntity workspaceEntity, String fileIdentifier) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<WorkspaceEntityFile> criteria = criteriaBuilder.createQuery(WorkspaceEntityFile.class);
    Root<WorkspaceEntityFile> root = criteria.from(WorkspaceEntityFile.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(WorkspaceEntityFile_.workspaceEntity), workspaceEntity.getId()), criteriaBuilder.equal(root.get(WorkspaceEntityFile_.fileIdentifier), fileIdentifier)));
    return getSingleResult(entityManager.createQuery(criteria));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) WorkspaceEntityFile(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceEntityFile)

Example 4 with WorkspaceEntityFile

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceEntityFile in project muikku by otavanopisto.

the class WorkspaceManagementBackingBean method init.

@RequestAction
public String init() {
    String urlName = getWorkspaceUrlName();
    if (StringUtils.isBlank(urlName)) {
        return NavigationRules.NOT_FOUND;
    }
    WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityByUrlName(urlName);
    if (workspaceEntity == null) {
        return NavigationRules.NOT_FOUND;
    }
    if (!sessionController.hasWorkspacePermission(MuikkuPermissions.MANAGE_WORKSPACE, workspaceEntity)) {
        return NavigationRules.ACCESS_DENIED;
    }
    workspaceEntityId = workspaceEntity.getId();
    workspaceBackingBean.setWorkspaceUrlName(urlName);
    WorkspaceEntityFile customFrontImage = workspaceEntityFileController.findWorkspaceEntityFile(workspaceEntity, "workspace-frontpage-image-cropped");
    hasCustomFrontPageImage = customFrontImage != null;
    customFrontPageImageUrl = hasCustomFrontPageImage ? String.format("/rest/workspace/workspaces/%d/workspacefile/workspace-frontpage-image-cropped", workspaceEntity.getId()) : null;
    return null;
}
Also used : WorkspaceEntityFile(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceEntityFile) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) RequestAction(org.ocpsoft.rewrite.annotation.RequestAction)

Example 5 with WorkspaceEntityFile

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceEntityFile in project muikku by otavanopisto.

the class WorkspaceEntityFileDAO method listByWorkspaceEntity.

public List<WorkspaceEntityFile> listByWorkspaceEntity(WorkspaceEntity workspaceEntity) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<WorkspaceEntityFile> criteria = criteriaBuilder.createQuery(WorkspaceEntityFile.class);
    Root<WorkspaceEntityFile> root = criteria.from(WorkspaceEntityFile.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.equal(root.get(WorkspaceEntityFile_.workspaceEntity), workspaceEntity.getId()));
    return entityManager.createQuery(criteria).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) WorkspaceEntityFile(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceEntityFile)

Aggregations

WorkspaceEntityFile (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceEntityFile)9 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)6 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)4 Path (javax.ws.rs.Path)4 WorkspaceFieldIOException (fi.otavanopisto.muikku.plugins.workspace.fieldio.WorkspaceFieldIOException)3 IOException (java.io.IOException)3 Date (java.util.Date)3 POST (javax.ws.rs.POST)3 DELETE (javax.ws.rs.DELETE)2 MessagingWidget (fi.otavanopisto.muikku.controller.messaging.MessagingWidget)1 TempFileUtils (fi.otavanopisto.muikku.files.TempFileUtils)1 BooleanPredicate (fi.otavanopisto.muikku.model.base.BooleanPredicate)1 EnvironmentRoleArchetype (fi.otavanopisto.muikku.model.users.EnvironmentRoleArchetype)1 Flag (fi.otavanopisto.muikku.model.users.Flag)1 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)1 UserSchoolDataIdentifier (fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier)1 WorkspaceMaterialProducer (fi.otavanopisto.muikku.model.workspace.WorkspaceMaterialProducer)1 WorkspaceRoleArchetype (fi.otavanopisto.muikku.model.workspace.WorkspaceRoleArchetype)1 WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)1 PluginRESTService (fi.otavanopisto.muikku.plugin.PluginRESTService)1