Search in sources :

Example 1 with MagicKey

use of fi.otavanopisto.pyramus.domainmodel.base.MagicKey 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 MagicKey

use of fi.otavanopisto.pyramus.domainmodel.base.MagicKey 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 MagicKey

use of fi.otavanopisto.pyramus.domainmodel.base.MagicKey in project pyramus by otavanopisto.

the class MagicKeyDAO method findByApplicationScope.

public MagicKey findByApplicationScope() {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<MagicKey> criteria = criteriaBuilder.createQuery(MagicKey.class);
    Root<MagicKey> root = criteria.from(MagicKey.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.equal(root.get(MagicKey_.scope), MagicKeyScope.APPLICATION));
    return getSingleResult(entityManager.createQuery(criteria));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) MagicKey(fi.otavanopisto.pyramus.domainmodel.base.MagicKey)

Example 4 with MagicKey

use of fi.otavanopisto.pyramus.domainmodel.base.MagicKey in project pyramus by otavanopisto.

the class MagicKeyDAO method findByName.

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

Example 5 with MagicKey

use of fi.otavanopisto.pyramus.domainmodel.base.MagicKey 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

MagicKey (fi.otavanopisto.pyramus.domainmodel.base.MagicKey)11 MagicKeyDAO (fi.otavanopisto.pyramus.dao.base.MagicKeyDAO)7 EntityManager (javax.persistence.EntityManager)4 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 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)3 SmvcRuntimeException (fi.internetix.smvc.SmvcRuntimeException)2 FileNotFoundException (java.io.FileNotFoundException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Date (java.util.Date)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