Search in sources :

Example 1 with FileType

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

the class FileService method uploadStudentFile.

@WebMethod
public void uploadStudentFile(@WebParam(name = "studentId") Long studentId, @WebParam(name = "name") String name, @WebParam(name = "fileName") String fileName, @WebParam(name = "fileTypeId") Long fileTypeId, @WebParam(name = "contentType") String contentType, @WebParam(name = "creatorId") Long creatorId, @WebParam(name = "content") DataHandler content) {
    StudentFileDAO studentFileDAO = DAOFactory.getInstance().getStudentFileDAO();
    StudentDAO studentDAO = DAOFactory.getInstance().getStudentDAO();
    Student student = studentDAO.findById(studentId);
    StaffMemberDAO userDAO = DAOFactory.getInstance().getStaffMemberDAO();
    FileTypeDAO fileTypeDAO = DAOFactory.getInstance().getFileTypeDAO();
    User creator = creatorId != null ? userDAO.findById(creatorId) : null;
    FileType fileType = fileTypeId != null ? fileTypeDAO.findById(fileTypeId) : null;
    byte[] data = null;
    try {
        InputStream inputStream = content.getInputStream();
        data = IOUtils.toByteArray(inputStream);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    studentFileDAO.create(student, name, fileName, null, fileType, contentType, data, creator);
}
Also used : StudentDAO(fi.otavanopisto.pyramus.dao.students.StudentDAO) FileTypeDAO(fi.otavanopisto.pyramus.dao.file.FileTypeDAO) StudentFileDAO(fi.otavanopisto.pyramus.dao.file.StudentFileDAO) StaffMemberDAO(fi.otavanopisto.pyramus.dao.users.StaffMemberDAO) User(fi.otavanopisto.pyramus.domainmodel.users.User) FileType(fi.otavanopisto.pyramus.domainmodel.file.FileType) InputStream(java.io.InputStream) IOException(java.io.IOException) Student(fi.otavanopisto.pyramus.domainmodel.students.Student) WebMethod(javax.jws.WebMethod)

Example 2 with FileType

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

the class UploadReportDialogViewController method process.

/**
 * Processes the page request by including the corresponding JSP page to the response.
 *
 * @param pageRequestContext Page request context
 */
public void process(PageRequestContext pageRequestContext) {
    FileTypeDAO fileTypeDAO = DAOFactory.getInstance().getFileTypeDAO();
    ReportDAO reportDAO = DAOFactory.getInstance().getReportDAO();
    StudentDAO studentDAO = DAOFactory.getInstance().getStudentDAO();
    Long studentId = pageRequestContext.getLong("studentId");
    Long reportId = pageRequestContext.getLong("reportId");
    List<FileType> fileTypes = fileTypeDAO.listUnarchived();
    Collections.sort(fileTypes, new StringAttributeComparator("getName"));
    StringBuffer reportParameters = new StringBuffer();
    Map<String, String[]> parameterMap = pageRequestContext.getRequest().getParameterMap();
    for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
        if (!reservedParameters.contains(entry.getKey())) {
            String[] values = entry.getValue();
            for (String value : values) {
                // TODO ISO-8859-1 should be UTF-8, once Birt's parameter dialog form has its accept-charset="UTF-8" set
                try {
                    reportParameters.append('&').append(entry.getKey()).append('=').append(URLEncoder.encode(value, "ISO-8859-1"));
                } catch (UnsupportedEncodingException e) {
                    throw new SmvcRuntimeException(e);
                }
            }
        }
    }
    pageRequestContext.getRequest().setAttribute("student", studentDAO.findById(studentId));
    pageRequestContext.getRequest().setAttribute("report", reportDAO.findById(reportId));
    pageRequestContext.getRequest().setAttribute("reportParameters", reportParameters);
    pageRequestContext.getRequest().setAttribute("fileTypes", fileTypes);
    pageRequestContext.setIncludeJSP("/templates/studentfiles/uploadreport.jsp");
}
Also used : FileTypeDAO(fi.otavanopisto.pyramus.dao.file.FileTypeDAO) StringAttributeComparator(fi.otavanopisto.pyramus.util.StringAttributeComparator) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SmvcRuntimeException(fi.internetix.smvc.SmvcRuntimeException) StudentDAO(fi.otavanopisto.pyramus.dao.students.StudentDAO) FileType(fi.otavanopisto.pyramus.domainmodel.file.FileType) ReportDAO(fi.otavanopisto.pyramus.dao.reports.ReportDAO) Map(java.util.Map)

Example 3 with FileType

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

the class EditFileDialogViewController method process.

/**
 * Processes the page request by including the corresponding JSP page to the response.
 *
 * @param pageRequestContext Page request context
 */
public void process(PageRequestContext pageRequestContext) {
    Long fileId = pageRequestContext.getLong("fileId");
    StudentFileDAO studentFileDAO = DAOFactory.getInstance().getStudentFileDAO();
    FileTypeDAO fileTypeDAO = DAOFactory.getInstance().getFileTypeDAO();
    List<FileType> fileTypes = fileTypeDAO.listUnarchived();
    Collections.sort(fileTypes, new StringAttributeComparator("getName"));
    pageRequestContext.getRequest().setAttribute("file", studentFileDAO.findById(fileId));
    pageRequestContext.getRequest().setAttribute("fileTypes", fileTypes);
    pageRequestContext.setIncludeJSP("/templates/studentfiles/editfile.jsp");
}
Also used : FileTypeDAO(fi.otavanopisto.pyramus.dao.file.FileTypeDAO) StudentFileDAO(fi.otavanopisto.pyramus.dao.file.StudentFileDAO) FileType(fi.otavanopisto.pyramus.domainmodel.file.FileType) StringAttributeComparator(fi.otavanopisto.pyramus.util.StringAttributeComparator)

Example 4 with FileType

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

the class UploadStudentFileJSONRequestController method process.

/**
 * Processes the request to edit a student group.
 *
 * @param requestContext
 *          The JSON request context
 */
public void process(JSONRequestContext requestContext) {
    StudentDAO studentDAO = DAOFactory.getInstance().getStudentDAO();
    StaffMemberDAO userDAO = DAOFactory.getInstance().getStaffMemberDAO();
    StudentFileDAO studentFileDAO = DAOFactory.getInstance().getStudentFileDAO();
    FileTypeDAO fileTypeDAO = DAOFactory.getInstance().getFileTypeDAO();
    Long userId = requestContext.getLoggedUserId();
    User loggedUser = userDAO.findById(userId);
    Long studentId = requestContext.getLong("studentId");
    String name = requestContext.getString("fileName");
    Long fileTypeId = requestContext.getLong("fileType");
    FileItem fileItem = requestContext.getFile("file");
    Student student = studentDAO.findById(studentId);
    FileType fileType = fileTypeId != null ? fileTypeDAO.findById(fileTypeId) : null;
    if (fileItem != null) {
        String fileId = null;
        byte[] data = fileItem.get();
        if (PyramusFileUtils.isFileSystemStorageEnabled()) {
            try {
                fileId = PyramusFileUtils.generateFileId();
                PyramusFileUtils.storeFile(student, fileId, data);
                data = null;
            } catch (IOException e) {
                fileId = null;
                logger.log(Level.SEVERE, "Store user file to file system failed", e);
            }
        }
        studentFileDAO.create(student, name, fileItem.getName(), fileId, fileType, fileItem.getContentType(), data, loggedUser);
    }
}
Also used : StudentDAO(fi.otavanopisto.pyramus.dao.students.StudentDAO) FileTypeDAO(fi.otavanopisto.pyramus.dao.file.FileTypeDAO) FileItem(org.apache.commons.fileupload.FileItem) StudentFileDAO(fi.otavanopisto.pyramus.dao.file.StudentFileDAO) StaffMemberDAO(fi.otavanopisto.pyramus.dao.users.StaffMemberDAO) User(fi.otavanopisto.pyramus.domainmodel.users.User) FileType(fi.otavanopisto.pyramus.domainmodel.file.FileType) IOException(java.io.IOException) Student(fi.otavanopisto.pyramus.domainmodel.students.Student)

Example 5 with FileType

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

the class UploadStudentReportJSONRequestController 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();
    FileTypeDAO fileTypeDAO = DAOFactory.getInstance().getFileTypeDAO();
    StudentDAO studentDAO = DAOFactory.getInstance().getStudentDAO();
    StaffMemberDAO userDAO = DAOFactory.getInstance().getStaffMemberDAO();
    ReportDAO reportDAO = DAOFactory.getInstance().getReportDAO();
    Long studentId = requestContext.getLong("studentId");
    Long reportId = requestContext.getLong("reportId");
    String reportParameters = requestContext.getString("reportParameters");
    Long userId = requestContext.getLoggedUserId();
    User loggedUser = userDAO.findById(userId);
    String name = requestContext.getString("fileName");
    Long fileTypeId = requestContext.getLong("fileType");
    String contentType;
    String format = requestContext.getString("reportFormat");
    if ("doc".equals(format)) {
        contentType = "application/msword";
    } else {
        format = "pdf";
        contentType = "application/pdf";
    }
    Student student = studentDAO.findById(studentId);
    FileType fileType = fileTypeId != null ? fileTypeDAO.findById(fileTypeId) : null;
    Report report = reportDAO.findById(reportId);
    MagicKeyDAO magicKeyDAO = DAOFactory.getInstance().getMagicKeyDAO();
    String reportsContextPath = System.getProperty("reports.contextPath");
    String reportsHost = System.getProperty("reports.host");
    String reportsProtocol = System.getProperty("reports.protocol");
    String reportsPort = System.getProperty("reports.port");
    // output or preview
    String outputMethod = "preview";
    MagicKey magicKey = magicKeyDAO.findByApplicationScope();
    StringBuilder urlBuilder = new StringBuilder().append(reportsProtocol).append("://").append(reportsHost).append(":").append(reportsPort).append(reportsContextPath).append("/").append(outputMethod).append("?magicKey=").append(magicKey.getName()).append("&__report=reports/").append(reportId).append(".rptdesign");
    urlBuilder.append("&__format=").append(format);
    urlBuilder.append(reportParameters);
    try {
        URL url = new URL(urlBuilder.toString());
        URLConnection urlConn = url.openConnection();
        InputStream inputStream = urlConn.getInputStream();
        String fileId = null;
        byte[] data = IOUtils.toByteArray(inputStream);
        if (PyramusFileUtils.isFileSystemStorageEnabled()) {
            try {
                fileId = PyramusFileUtils.generateFileId();
                PyramusFileUtils.storeFile(student, fileId, data);
                data = null;
            } catch (IOException e) {
                fileId = null;
                logger.log(Level.SEVERE, "Store user file to file system failed", e);
            }
        }
        String reportName = report.getName().toLowerCase().replaceAll("[^a-z0-9\\.]", "_");
        studentFileDAO.create(student, name, reportName + "." + format, fileId, fileType, contentType, data, loggedUser);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : FileTypeDAO(fi.otavanopisto.pyramus.dao.file.FileTypeDAO) StudentFileDAO(fi.otavanopisto.pyramus.dao.file.StudentFileDAO) MalformedURLException(java.net.MalformedURLException) User(fi.otavanopisto.pyramus.domainmodel.users.User) Report(fi.otavanopisto.pyramus.domainmodel.reports.Report) InputStream(java.io.InputStream) IOException(java.io.IOException) Student(fi.otavanopisto.pyramus.domainmodel.students.Student) MagicKeyDAO(fi.otavanopisto.pyramus.dao.base.MagicKeyDAO) URL(java.net.URL) URLConnection(java.net.URLConnection) StudentDAO(fi.otavanopisto.pyramus.dao.students.StudentDAO) StaffMemberDAO(fi.otavanopisto.pyramus.dao.users.StaffMemberDAO) FileType(fi.otavanopisto.pyramus.domainmodel.file.FileType) ReportDAO(fi.otavanopisto.pyramus.dao.reports.ReportDAO) MagicKey(fi.otavanopisto.pyramus.domainmodel.base.MagicKey)

Aggregations

FileType (fi.otavanopisto.pyramus.domainmodel.file.FileType)10 FileTypeDAO (fi.otavanopisto.pyramus.dao.file.FileTypeDAO)9 StudentFileDAO (fi.otavanopisto.pyramus.dao.file.StudentFileDAO)5 StudentDAO (fi.otavanopisto.pyramus.dao.students.StudentDAO)4 StaffMemberDAO (fi.otavanopisto.pyramus.dao.users.StaffMemberDAO)4 User (fi.otavanopisto.pyramus.domainmodel.users.User)4 StringAttributeComparator (fi.otavanopisto.pyramus.util.StringAttributeComparator)4 IOException (java.io.IOException)4 Student (fi.otavanopisto.pyramus.domainmodel.students.Student)3 ReportDAO (fi.otavanopisto.pyramus.dao.reports.ReportDAO)2 InputStream (java.io.InputStream)2 FileItem (org.apache.commons.fileupload.FileItem)2 SmvcRuntimeException (fi.internetix.smvc.SmvcRuntimeException)1 MagicKeyDAO (fi.otavanopisto.pyramus.dao.base.MagicKeyDAO)1 MagicKey (fi.otavanopisto.pyramus.domainmodel.base.MagicKey)1 StudentFile (fi.otavanopisto.pyramus.domainmodel.file.StudentFile)1 Report (fi.otavanopisto.pyramus.domainmodel.reports.Report)1 JSONArrayExtractor (fi.otavanopisto.pyramus.util.JSONArrayExtractor)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1