Search in sources :

Example 1 with MagicKeyDAO

use of fi.otavanopisto.pyramus.dao.base.MagicKeyDAO in project pyramus by otavanopisto.

the class PyramusServletContextListener method contextInitialized.

/**
 * Called when the application starts. Sets up both Hibernate and the page and JSON mappers needed
 * to serve the client requests.
 *
 * @param servletContextEvent The servlet context event
 */
public void contextInitialized(ServletContextEvent servletContextEvent) {
    try {
        userTransaction.begin();
        MagicKeyDAO magicKeyDAO = DAOFactory.getInstance().getMagicKeyDAO();
        String applicationMagicKey = UUID.randomUUID().toString();
        MagicKey magicKey = magicKeyDAO.findByApplicationScope();
        if (magicKey != null) {
            magicKeyDAO.updateName(magicKey, applicationMagicKey);
        } else {
            magicKeyDAO.create(applicationMagicKey, MagicKeyScope.APPLICATION);
        }
        Properties pageControllers = new Properties();
        Properties jsonControllers = new Properties();
        Properties binaryControllers = new Properties();
        ServletContext ctx = servletContextEvent.getServletContext();
        String webappPath = ctx.getRealPath("/");
        // Load the system settings into the system properties
        loadSystemSettings(System.getProperties());
        // Load default page mappings from properties file
        loadPropertiesFile(pageControllers, "pagemapping.properties");
        // Load default JSON mappings from properties file
        loadPropertiesFile(jsonControllers, "jsonmapping.properties");
        // Load default binary mappings from properties file
        loadPropertiesFile(binaryControllers, "binarymapping.properties");
        // Initialize the page mapper in order to serve page requests
        RequestControllerMapper.mapControllers(pageControllers, ".page");
        // Initialize the JSON mapper in order to serve JSON requests
        RequestControllerMapper.mapControllers(jsonControllers, ".json");
        // Initialize the binary mapper in order to serve binary requests
        RequestControllerMapper.mapControllers(binaryControllers, ".binary");
        // Load plugins here so that plugins can override existing controllers
        loadPlugins();
        // Sets the application directory of the application, used primarily for initial data creation
        System.getProperties().setProperty("appdirectory", webappPath);
        // Register internal authentication provider
        AuthenticationProviderVault.registerAuthenticationProviderClass("internal", InternalAuthenticationStrategy.class);
        // Initializes all configured authentication strategies
        AuthenticationProviderVault.getInstance().initializeStrategies();
        if ("development".equals(System.getProperties().getProperty("system.environment"))) {
            trustSelfSignedCerts();
        }
        if ("it".equals(System.getProperties().getProperty("system.environment"))) {
            reindexHibernateEntities();
        }
        userTransaction.commit();
    } catch (Exception e) {
        try {
            userTransaction.rollback();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        e.printStackTrace();
        throw new ExceptionInInitializerError(e);
    }
}
Also used : ServletContext(javax.servlet.ServletContext) Properties(java.util.Properties) MagicKeyDAO(fi.otavanopisto.pyramus.dao.base.MagicKeyDAO) MagicKey(fi.otavanopisto.pyramus.domainmodel.base.MagicKey) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with MagicKeyDAO

use of fi.otavanopisto.pyramus.dao.base.MagicKeyDAO in project pyramus by otavanopisto.

the class PyramusServletContextListener method contextDestroyed.

/**
 * Called when the application shuts down.
 *
 * @param ctx The servlet context event
 */
public void contextDestroyed(ServletContextEvent ctx) {
    try {
        userTransaction.begin();
        MagicKeyDAO magicKeyDAO = DAOFactory.getInstance().getMagicKeyDAO();
        MagicKey magicKey = magicKeyDAO.findByApplicationScope();
        if (magicKey != null) {
            magicKeyDAO.delete(magicKey);
        }
        userTransaction.commit();
    } catch (Exception e) {
        try {
            userTransaction.rollback();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
}
Also used : MagicKeyDAO(fi.otavanopisto.pyramus.dao.base.MagicKeyDAO) MagicKey(fi.otavanopisto.pyramus.domainmodel.base.MagicKey) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with MagicKeyDAO

use of fi.otavanopisto.pyramus.dao.base.MagicKeyDAO 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)

Example 4 with MagicKeyDAO

use of fi.otavanopisto.pyramus.dao.base.MagicKeyDAO in project pyramus by otavanopisto.

the class ViewReportContentsViewController 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) {
    MagicKeyDAO magicKeyDAO = DAOFactory.getInstance().getMagicKeyDAO();
    Long reportId = pageRequestContext.getLong("reportId");
    // output or preview
    String outputMethod = "preview";
    StringBuilder magicKeyBuilder = new StringBuilder().append(Long.toHexString(reportId)).append('-').append(Long.toHexString(System.currentTimeMillis())).append('-').append(Long.toHexString(Thread.currentThread().getId()));
    MagicKey magicKey = magicKeyDAO.create(magicKeyBuilder.toString(), MagicKeyScope.REQUEST);
    String localeAdd = "";
    Locale locale = pageRequestContext.getRequest().getLocale();
    if (locale != null) {
        localeAdd = "&__locale=";
        localeAdd += locale.getLanguage();
        if (!StringUtils.isEmpty(locale.getCountry())) {
            localeAdd += "_";
            localeAdd += locale.getCountry();
        }
    }
    StringBuilder urlBuilder = new StringBuilder().append(ReportUtils.getReportsUrl(pageRequestContext.getRequest())).append("/").append(outputMethod).append("?magicKey=").append(magicKey.getName()).append(localeAdd).append("&__report=reports/").append(reportId).append(".rptdesign");
    // TODO: Report locale is wrong.
    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 {
                    urlBuilder.append('&').append(entry.getKey()).append('=').append(URLEncoder.encode(value, "ISO-8859-1"));
                } catch (UnsupportedEncodingException e) {
                    throw new SmvcRuntimeException(e);
                }
            }
        }
    }
    pageRequestContext.setIncludeUrl(urlBuilder.toString());
}
Also used : Locale(java.util.Locale) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SmvcRuntimeException(fi.internetix.smvc.SmvcRuntimeException) MagicKeyDAO(fi.otavanopisto.pyramus.dao.base.MagicKeyDAO) MagicKey(fi.otavanopisto.pyramus.domainmodel.base.MagicKey) Map(java.util.Map)

Example 5 with MagicKeyDAO

use of fi.otavanopisto.pyramus.dao.base.MagicKeyDAO in project pyramus by otavanopisto.

the class ViewReportParametersViewController 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) {
    MagicKeyDAO magicKeyDAO = DAOFactory.getInstance().getMagicKeyDAO();
    ReportDAO reportDAO = DAOFactory.getInstance().getReportDAO();
    Long reportId = pageRequestContext.getLong("reportId");
    Report report = reportDAO.findById(reportId);
    StringBuilder magicKeyBuilder = new StringBuilder().append(Long.toHexString(reportId)).append('-').append(Long.toHexString(System.currentTimeMillis())).append('-').append(Long.toHexString(Thread.currentThread().getId()));
    MagicKey magicKey = magicKeyDAO.create(magicKeyBuilder.toString(), MagicKeyScope.REQUEST);
    String localeAdd = "";
    Locale locale = pageRequestContext.getRequest().getLocale();
    if (locale != null) {
        localeAdd = "&__locale=";
        localeAdd += locale.getLanguage();
        if (!StringUtils.isEmpty(locale.getCountry())) {
            localeAdd += "_";
            localeAdd += locale.getCountry();
        }
    }
    StringBuilder urlBuilder = new StringBuilder().append(ReportUtils.getReportsUrl(pageRequestContext.getRequest())).append("/parameter?magicKey=").append(magicKey.getName()).append(localeAdd).append("&__report=reports/").append(reportId).append(".rptdesign").append("&__masterpage=true&__nocache");
    handleContextParameters(pageRequestContext, report, urlBuilder);
    pageRequestContext.setIncludeUrl(urlBuilder.toString());
}
Also used : Locale(java.util.Locale) Report(fi.otavanopisto.pyramus.domainmodel.reports.Report) ReportDAO(fi.otavanopisto.pyramus.dao.reports.ReportDAO) MagicKeyDAO(fi.otavanopisto.pyramus.dao.base.MagicKeyDAO) MagicKey(fi.otavanopisto.pyramus.domainmodel.base.MagicKey)

Aggregations

MagicKeyDAO (fi.otavanopisto.pyramus.dao.base.MagicKeyDAO)8 MagicKey (fi.otavanopisto.pyramus.domainmodel.base.MagicKey)7 ReportDAO (fi.otavanopisto.pyramus.dao.reports.ReportDAO)3 Report (fi.otavanopisto.pyramus.domainmodel.reports.Report)3 IOException (java.io.IOException)3 Locale (java.util.Locale)3 SmvcRuntimeException (fi.internetix.smvc.SmvcRuntimeException)2 FileNotFoundException (java.io.FileNotFoundException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Map (java.util.Map)2 AccessDeniedException (fi.internetix.smvc.AccessDeniedException)1 FileTypeDAO (fi.otavanopisto.pyramus.dao.file.FileTypeDAO)1 StudentFileDAO (fi.otavanopisto.pyramus.dao.file.StudentFileDAO)1 StudentDAO (fi.otavanopisto.pyramus.dao.students.StudentDAO)1 StaffMemberDAO (fi.otavanopisto.pyramus.dao.users.StaffMemberDAO)1 FileType (fi.otavanopisto.pyramus.domainmodel.file.FileType)1 Student (fi.otavanopisto.pyramus.domainmodel.students.Student)1 User (fi.otavanopisto.pyramus.domainmodel.users.User)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1