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);
}
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);
}
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;
}
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);
}
}
}
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;
}
Aggregations