Search in sources :

Example 1 with StudentFile

use of fi.otavanopisto.pyramus.domainmodel.file.StudentFile in project pyramus by otavanopisto.

the class DownloadStudentFile method process.

/**
 * Processes a binary request.
 * The request should contain the following parameters:
 * <dl>
 *   <dt><code>fileId</code></dt>
 *   <dd>The ID of the student file.</dd>
 * </dl>
 *
 * @param binaryRequestContext The context of the binary request.
 */
public void process(BinaryRequestContext binaryRequestContext) {
    StudentFileDAO studentFileDAO = DAOFactory.getInstance().getStudentFileDAO();
    Long fileId = binaryRequestContext.getLong("fileId");
    StudentFile studentFile = studentFileDAO.findById(fileId);
    if (studentFile != null) {
        binaryRequestContext.setFileName(studentFile.getFileName());
        try {
            binaryRequestContext.setResponseContent(PyramusFileUtils.getFileData(studentFile), studentFile.getContentType());
        } catch (IOException e) {
            logger.log(Level.SEVERE, String.format("Exception retrieving user file %d", studentFile.getId()), e);
        }
    }
}
Also used : StudentFile(fi.otavanopisto.pyramus.domainmodel.file.StudentFile) StudentFileDAO(fi.otavanopisto.pyramus.dao.file.StudentFileDAO) IOException(java.io.IOException)

Example 2 with StudentFile

use of fi.otavanopisto.pyramus.domainmodel.file.StudentFile in project pyramus by otavanopisto.

the class MoveStudentFilesViewController method process.

public void process(PageRequestContext pageRequestContext) {
    int count = pageRequestContext.getInteger("count");
    StudentFileDAO studentFileDAO = DAOFactory.getInstance().getStudentFileDAO();
    if (count > 0) {
        int bytes = 0;
        int totalBytes = 0;
        int totalFiles = 0;
        Long currentEntityId = PyramusFileUtils.getLastMovedEntityId();
        List<Long> entityIds = studentFileDAO.listIdsByLargerAndLimit(currentEntityId, count);
        for (Long entityId : entityIds) {
            try {
                StudentFile studentFile = studentFileDAO.findById(entityId);
                if (studentFile != null) {
                    bytes = PyramusFileUtils.relocateToFileSystem(studentFile);
                    if (bytes > 0) {
                        totalBytes += bytes;
                        totalFiles++;
                    }
                    currentEntityId = entityId;
                }
                PyramusFileUtils.setLastMovedEntityId(currentEntityId);
            } catch (IOException e) {
                logger.log(Level.SEVERE, String.format("Failed to relocate StudentFile %d", currentEntityId), e);
            }
        }
        logger.info(String.format("Moved %d files (%d bytes) with latest entity at %d", totalFiles, totalBytes, currentEntityId));
    }
}
Also used : StudentFile(fi.otavanopisto.pyramus.domainmodel.file.StudentFile) StudentFileDAO(fi.otavanopisto.pyramus.dao.file.StudentFileDAO) IOException(java.io.IOException)

Example 3 with StudentFile

use of fi.otavanopisto.pyramus.domainmodel.file.StudentFile in project pyramus by otavanopisto.

the class ListStudentFilesJSONRequestController method process.

/**
 * Processes the request to edit a student group.
 *
 * @param requestContext
 *          The JSON request context
 */
public void process(JSONRequestContext requestContext) {
    StudentFileDAO studentFileDAO = DAOFactory.getInstance().getStudentFileDAO();
    StudentDAO studentDAO = DAOFactory.getInstance().getStudentDAO();
    Long studentId = requestContext.getLong("studentId");
    Student student = studentDAO.findById(studentId);
    List<StudentFile> studentFiles = studentFileDAO.listByStudent(student);
    Collections.sort(studentFiles, new StringAttributeComparator("getName", true));
    JSONArray filesJSON = new JSONArray();
    for (StudentFile file : studentFiles) {
        JSONObject fileJSON = new JSONObject();
        fileJSON.put("id", file.getId());
        fileJSON.put("name", file.getName());
        fileJSON.put("fileTypeName", file.getFileType() != null ? file.getFileType().getName() : "");
        fileJSON.put("created", file.getCreated().getTime());
        fileJSON.put("lastModified", file.getLastModified().getTime());
        filesJSON.add(fileJSON);
    }
    requestContext.addResponseParameter("files", filesJSON.toString());
}
Also used : StudentDAO(fi.otavanopisto.pyramus.dao.students.StudentDAO) StudentFile(fi.otavanopisto.pyramus.domainmodel.file.StudentFile) StudentFileDAO(fi.otavanopisto.pyramus.dao.file.StudentFileDAO) JSONObject(net.sf.json.JSONObject) StringAttributeComparator(fi.otavanopisto.pyramus.util.StringAttributeComparator) JSONArray(net.sf.json.JSONArray) Student(fi.otavanopisto.pyramus.domainmodel.students.Student)

Example 4 with StudentFile

use of fi.otavanopisto.pyramus.domainmodel.file.StudentFile in project pyramus by otavanopisto.

the class StudentFileDAO method findByFileId.

public StudentFile findByFileId(String fileId) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<StudentFile> criteria = criteriaBuilder.createQuery(StudentFile.class);
    Root<StudentFile> root = criteria.from(StudentFile.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.equal(root.get(StudentFile_.fileId), fileId));
    return getSingleResult(entityManager.createQuery(criteria));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) StudentFile(fi.otavanopisto.pyramus.domainmodel.file.StudentFile) EntityManager(javax.persistence.EntityManager)

Example 5 with StudentFile

use of fi.otavanopisto.pyramus.domainmodel.file.StudentFile in project pyramus by otavanopisto.

the class StudentFileDAO method listIdsByLargerAndLimit.

public List<Long> listIdsByLargerAndLimit(Long id, int count) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = criteriaBuilder.createQuery(Long.class);
    Root<StudentFile> root = criteria.from(StudentFile.class);
    criteria.select(root.get(StudentFile_.id));
    criteria.where(criteriaBuilder.greaterThan(root.get(StudentFile_.id), id));
    criteria.orderBy(criteriaBuilder.asc(root.get(StudentFile_.id)));
    TypedQuery<Long> query = entityManager.createQuery(criteria);
    query.setMaxResults(count);
    return query.getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) StudentFile(fi.otavanopisto.pyramus.domainmodel.file.StudentFile) EntityManager(javax.persistence.EntityManager)

Aggregations

StudentFile (fi.otavanopisto.pyramus.domainmodel.file.StudentFile)10 StudentFileDAO (fi.otavanopisto.pyramus.dao.file.StudentFileDAO)5 IOException (java.io.IOException)4 EntityManager (javax.persistence.EntityManager)4 StudentDAO (fi.otavanopisto.pyramus.dao.students.StudentDAO)2 StaffMemberDAO (fi.otavanopisto.pyramus.dao.users.StaffMemberDAO)2 User (fi.otavanopisto.pyramus.domainmodel.users.User)2 Date (java.util.Date)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 PageRequestContext (fi.internetix.smvc.controllers.PageRequestContext)1 RequestContext (fi.internetix.smvc.controllers.RequestContext)1 Messages (fi.otavanopisto.pyramus.I18N.Messages)1 Breadcrumbable (fi.otavanopisto.pyramus.breadcrumbs.Breadcrumbable)1 DAOFactory (fi.otavanopisto.pyramus.dao.DAOFactory)1 CurriculumDAO (fi.otavanopisto.pyramus.dao.base.CurriculumDAO)1 PersonDAO (fi.otavanopisto.pyramus.dao.base.PersonDAO)1 CourseStudentDAO (fi.otavanopisto.pyramus.dao.courses.CourseStudentDAO)1