Search in sources :

Example 1 with CertificateImpl

use of org.olat.course.certificate.model.CertificateImpl in project OpenOLAT by OpenOLAT.

the class CertificatesManagerImpl method doCertificate.

private void doCertificate(JmsCertificateWork workUnit) {
    CertificateImpl certificate = getCertificateById(workUnit.getCertificateKey());
    CertificateTemplate template = null;
    if (workUnit.getTemplateKey() != null) {
        template = getTemplateById(workUnit.getTemplateKey());
    }
    OLATResource resource = certificate.getOlatResource();
    Identity identity = certificate.getIdentity();
    RepositoryEntry entry = repositoryService.loadByResourceKey(resource.getKey());
    String dir = usersStorage.generateDir();
    File dirFile = new File(getCertificateRoot(), dir);
    dirFile.mkdirs();
    Float score = workUnit.getScore();
    String lang = identity.getUser().getPreferences().getLanguage();
    Locale locale = i18nManager.getLocaleOrDefault(lang);
    Boolean passed = workUnit.getPassed();
    Date dateCertification = certificate.getCreationDate();
    Date dateFirstCertification = getDateFirstCertification(identity, resource.getKey());
    Date dateNextRecertification = certificate.getNextRecertificationDate();
    File certificateFile;
    // File name with user name
    StringBuilder sb = new StringBuilder();
    sb.append(identity.getUser().getProperty(UserConstants.LASTNAME, locale)).append("_").append(identity.getUser().getProperty(UserConstants.FIRSTNAME, locale)).append("_").append(entry.getDisplayname()).append("_").append(Formatter.formatShortDateFilesystem(dateCertification));
    String filename = FileUtils.normalizeFilename(sb.toString()) + ".pdf";
    // External URL to certificate as short as possible for QR-Code
    sb = new StringBuilder();
    sb.append(Settings.getServerContextPathURI()).append("/certificate/").append(certificate.getUuid()).append("/certificate.pdf");
    String certUrl = sb.toString();
    if (template == null || template.getPath().toLowerCase().endsWith("pdf")) {
        CertificatePDFFormWorker worker = new CertificatePDFFormWorker(identity, entry, score, passed, dateCertification, dateFirstCertification, dateNextRecertification, certUrl, locale, userManager, this);
        certificateFile = worker.fill(template, dirFile, filename);
        if (certificateFile == null) {
            certificate.setStatus(CertificateStatus.error);
        } else {
            certificate.setStatus(CertificateStatus.ok);
        }
    } else {
        CertificatePhantomWorker worker = new CertificatePhantomWorker(identity, entry, score, passed, dateCertification, dateFirstCertification, dateNextRecertification, certUrl, locale, userManager, this);
        certificateFile = worker.fill(template, dirFile, filename);
        if (certificateFile == null) {
            certificate.setStatus(CertificateStatus.error);
        } else {
            certificate.setStatus(CertificateStatus.ok);
        }
    }
    certificate.setPath(dir + certificateFile.getName());
    if (dateFirstCertification != null) {
        // not the first certification, reset the last of the others certificates
        removeLastFlag(identity, resource.getKey());
    }
    MailerResult result = sendCertificate(identity, entry, certificateFile);
    if (result.isSuccessful()) {
        certificate.setEmailStatus(EmailStatus.ok);
    } else {
        certificate.setEmailStatus(EmailStatus.error);
    }
    dbInstance.getCurrentEntityManager().merge(certificate);
    dbInstance.commit();
    CertificateEvent event = new CertificateEvent(identity.getKey(), certificate.getKey(), resource.getKey());
    coordinatorManager.getCoordinator().getEventBus().fireEventToListenersOf(event, ORES_CERTIFICATE_EVENT);
}
Also used : Locale(java.util.Locale) CertificateTemplate(org.olat.course.certificate.CertificateTemplate) MailerResult(org.olat.core.util.mail.MailerResult) OLATResource(org.olat.resource.OLATResource) CertificateImpl(org.olat.course.certificate.model.CertificateImpl) RepositoryEntry(org.olat.repository.RepositoryEntry) Date(java.util.Date) CertificateEvent(org.olat.course.certificate.CertificateEvent) Identity(org.olat.core.id.Identity) TransientIdentity(org.olat.admin.user.imp.TransientIdentity) File(java.io.File)

Example 2 with CertificateImpl

use of org.olat.course.certificate.model.CertificateImpl in project openolat by klemens.

the class CertificatesManagerImpl method doCertificate.

private void doCertificate(JmsCertificateWork workUnit) {
    CertificateImpl certificate = getCertificateById(workUnit.getCertificateKey());
    CertificateTemplate template = null;
    if (workUnit.getTemplateKey() != null) {
        template = getTemplateById(workUnit.getTemplateKey());
    }
    OLATResource resource = certificate.getOlatResource();
    Identity identity = certificate.getIdentity();
    RepositoryEntry entry = repositoryService.loadByResourceKey(resource.getKey());
    String dir = usersStorage.generateDir();
    File dirFile = new File(getCertificateRoot(), dir);
    dirFile.mkdirs();
    Float score = workUnit.getScore();
    String lang = identity.getUser().getPreferences().getLanguage();
    Locale locale = i18nManager.getLocaleOrDefault(lang);
    Boolean passed = workUnit.getPassed();
    Date dateCertification = certificate.getCreationDate();
    Date dateFirstCertification = getDateFirstCertification(identity, resource.getKey());
    Date dateNextRecertification = certificate.getNextRecertificationDate();
    File certificateFile;
    // File name with user name
    StringBuilder sb = new StringBuilder();
    sb.append(identity.getUser().getProperty(UserConstants.LASTNAME, locale)).append("_").append(identity.getUser().getProperty(UserConstants.FIRSTNAME, locale)).append("_").append(entry.getDisplayname()).append("_").append(Formatter.formatShortDateFilesystem(dateCertification));
    String filename = FileUtils.normalizeFilename(sb.toString()) + ".pdf";
    // External URL to certificate as short as possible for QR-Code
    sb = new StringBuilder();
    sb.append(Settings.getServerContextPathURI()).append("/certificate/").append(certificate.getUuid()).append("/certificate.pdf");
    String certUrl = sb.toString();
    if (template == null || template.getPath().toLowerCase().endsWith("pdf")) {
        CertificatePDFFormWorker worker = new CertificatePDFFormWorker(identity, entry, score, passed, dateCertification, dateFirstCertification, dateNextRecertification, certUrl, locale, userManager, this);
        certificateFile = worker.fill(template, dirFile, filename);
        if (certificateFile == null) {
            certificate.setStatus(CertificateStatus.error);
        } else {
            certificate.setStatus(CertificateStatus.ok);
        }
    } else {
        CertificatePhantomWorker worker = new CertificatePhantomWorker(identity, entry, score, passed, dateCertification, dateFirstCertification, dateNextRecertification, certUrl, locale, userManager, this);
        certificateFile = worker.fill(template, dirFile, filename);
        if (certificateFile == null) {
            certificate.setStatus(CertificateStatus.error);
        } else {
            certificate.setStatus(CertificateStatus.ok);
        }
    }
    certificate.setPath(dir + certificateFile.getName());
    if (dateFirstCertification != null) {
        // not the first certification, reset the last of the others certificates
        removeLastFlag(identity, resource.getKey());
    }
    MailerResult result = sendCertificate(identity, entry, certificateFile);
    if (result.isSuccessful()) {
        certificate.setEmailStatus(EmailStatus.ok);
    } else {
        certificate.setEmailStatus(EmailStatus.error);
    }
    dbInstance.getCurrentEntityManager().merge(certificate);
    dbInstance.commit();
    CertificateEvent event = new CertificateEvent(identity.getKey(), certificate.getKey(), resource.getKey());
    coordinatorManager.getCoordinator().getEventBus().fireEventToListenersOf(event, ORES_CERTIFICATE_EVENT);
}
Also used : Locale(java.util.Locale) CertificateTemplate(org.olat.course.certificate.CertificateTemplate) MailerResult(org.olat.core.util.mail.MailerResult) OLATResource(org.olat.resource.OLATResource) CertificateImpl(org.olat.course.certificate.model.CertificateImpl) RepositoryEntry(org.olat.repository.RepositoryEntry) Date(java.util.Date) CertificateEvent(org.olat.course.certificate.CertificateEvent) Identity(org.olat.core.id.Identity) TransientIdentity(org.olat.admin.user.imp.TransientIdentity) File(java.io.File)

Example 3 with CertificateImpl

use of org.olat.course.certificate.model.CertificateImpl in project openolat by klemens.

the class CertificatesManagerImpl method uploadCertificate.

@Override
public Certificate uploadCertificate(Identity identity, Date creationDate, OLATResource resource, File certificateFile) {
    CertificateImpl certificate = new CertificateImpl();
    certificate.setOlatResource(resource);
    certificate.setArchivedResourceKey(resource.getKey());
    if (creationDate != null) {
        certificate.setCreationDate(creationDate);
    }
    RepositoryEntry entry = repositoryService.loadByResourceKey(resource.getKey());
    if (entry != null) {
        certificate.setCourseTitle(entry.getDisplayname());
    }
    certificate.setLastModified(certificate.getCreationDate());
    certificate.setIdentity(identity);
    certificate.setUuid(UUID.randomUUID().toString());
    certificate.setLast(true);
    certificate.setStatus(CertificateStatus.ok);
    String dir = usersStorage.generateDir();
    try (InputStream in = Files.newInputStream(certificateFile.toPath())) {
        File dirFile = new File(getCertificateRoot(), dir);
        dirFile.mkdirs();
        File storedCertificateFile = new File(dirFile, "Certificate.pdf");
        Files.copy(in, storedCertificateFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
        certificate.setPath(dir + storedCertificateFile.getName());
        Date dateFirstCertification = getDateFirstCertification(identity, resource.getKey());
        if (dateFirstCertification != null) {
            removeLastFlag(identity, resource.getKey());
        }
        dbInstance.getCurrentEntityManager().persist(certificate);
    } catch (Exception e) {
        log.error("", e);
    }
    return certificate;
}
Also used : InputStream(java.io.InputStream) CertificateImpl(org.olat.course.certificate.model.CertificateImpl) RepositoryEntry(org.olat.repository.RepositoryEntry) File(java.io.File) Date(java.util.Date) CorruptedCourseException(org.olat.course.CorruptedCourseException) IOException(java.io.IOException) JMSException(javax.jms.JMSException)

Example 4 with CertificateImpl

use of org.olat.course.certificate.model.CertificateImpl in project openolat by klemens.

the class CertificatesManagerImpl method deleteCertificate.

@Override
public void deleteCertificate(Certificate certificate) {
    File certificateFile = getCertificateFile(certificate);
    if (certificateFile != null && certificateFile.exists()) {
        try {
            FileUtils.deleteDirsAndFiles(certificateFile.getParentFile().toPath());
        } catch (IOException e) {
            log.error("", e);
        }
    }
    CertificateImpl relaodedCertificate = dbInstance.getCurrentEntityManager().getReference(CertificateImpl.class, certificate.getKey());
    dbInstance.getCurrentEntityManager().remove(relaodedCertificate);
    // reorder the last flag
    List<Certificate> certificates = getCertificates(relaodedCertificate.getIdentity(), relaodedCertificate.getOlatResource());
    certificates.remove(relaodedCertificate);
    if (certificates.size() > 0) {
        boolean hasLast = false;
        for (Certificate cer : certificates) {
            if (((CertificateImpl) cer).isLast()) {
                hasLast = true;
            }
        }
        if (!hasLast) {
            CertificateImpl newLastCertificate = (CertificateImpl) certificates.get(0);
            newLastCertificate.setLast(true);
            dbInstance.getCurrentEntityManager().merge(newLastCertificate);
        }
    }
}
Also used : IOException(java.io.IOException) CertificateImpl(org.olat.course.certificate.model.CertificateImpl) File(java.io.File) Certificate(org.olat.course.certificate.Certificate)

Example 5 with CertificateImpl

use of org.olat.course.certificate.model.CertificateImpl in project OpenOLAT by OpenOLAT.

the class CertificatesManagerImpl method persistCertificate.

private Certificate persistCertificate(CertificateInfos certificateInfos, RepositoryEntry entry, CertificateTemplate template, boolean sendMail) {
    OLATResource resource = entry.getOlatResource();
    Identity identity = certificateInfos.getAssessedIdentity();
    CertificateImpl certificate = new CertificateImpl();
    certificate.setOlatResource(resource);
    certificate.setArchivedResourceKey(resource.getKey());
    if (certificateInfos.getCreationDate() != null) {
        certificate.setCreationDate(certificateInfos.getCreationDate());
    } else {
        certificate.setCreationDate(new Date());
    }
    certificate.setLastModified(certificate.getCreationDate());
    certificate.setIdentity(identity);
    certificate.setUuid(UUID.randomUUID().toString());
    certificate.setLast(true);
    certificate.setCourseTitle(entry.getDisplayname());
    certificate.setStatus(CertificateStatus.pending);
    Date nextCertification = getDateNextRecertification(certificate, entry);
    certificate.setNextRecertificationDate(nextCertification);
    dbInstance.getCurrentEntityManager().persist(certificate);
    dbInstance.commit();
    // send message
    sendJmsCertificateFile(certificate, template, certificateInfos.getScore(), certificateInfos.getPassed(), sendMail);
    return certificate;
}
Also used : OLATResource(org.olat.resource.OLATResource) CertificateImpl(org.olat.course.certificate.model.CertificateImpl) Identity(org.olat.core.id.Identity) TransientIdentity(org.olat.admin.user.imp.TransientIdentity) Date(java.util.Date)

Aggregations

CertificateImpl (org.olat.course.certificate.model.CertificateImpl)8 File (java.io.File)6 Date (java.util.Date)6 IOException (java.io.IOException)4 TransientIdentity (org.olat.admin.user.imp.TransientIdentity)4 Identity (org.olat.core.id.Identity)4 RepositoryEntry (org.olat.repository.RepositoryEntry)4 OLATResource (org.olat.resource.OLATResource)4 InputStream (java.io.InputStream)2 Locale (java.util.Locale)2 JMSException (javax.jms.JMSException)2 MailerResult (org.olat.core.util.mail.MailerResult)2 CorruptedCourseException (org.olat.course.CorruptedCourseException)2 Certificate (org.olat.course.certificate.Certificate)2 CertificateEvent (org.olat.course.certificate.CertificateEvent)2 CertificateTemplate (org.olat.course.certificate.CertificateTemplate)2