BrightSide Workbench Full Report + Source Code
StudentsReport.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2023 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.admin;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.List;
24 import java.util.function.BiConsumer;
25 import org.turro.action.IMailSender;
26 import org.turro.action.MailSenders;
27 import org.turro.auth.Authentication;
28 import org.turro.collections.CumulativeList;
29 import org.turro.contacts.BusinessRelation;
30 import org.turro.contacts.ContactType;
31 import org.turro.contacts.db.ContactsPU;
32 import org.turro.elephant.context.ElephantContext;
33 import org.turro.exporter.FieldsExporter;
34 import org.turro.i18n.I_;
35 import org.turro.log.WebLoggers;
36 import org.turro.message.MessageQueue;
37 import org.turro.plugin.contacts.IContact;
38 import org.turro.sql.SqlClause;
39 
44 public class StudentsReport implements IAdminReport {
45 
46  @Override
47  public boolean isInRole() {
49  }
50 
51  @Override
52  public String getDescription() {
53  return "Students by center";
54  }
55 
56  @Override
57  public void generate() {
60  .addContact(contact)
61  .onStart(s -> MessageQueue.pushMessage(contact, I_.get("Report will be sent by email")))
62  .onCancel(s -> MessageQueue.pushMessage(contact, I_.get("Task already running")))
63  .onBuild(s -> studentsToCSV((IMailSender) s))
64  .send(I_.get(getDescription()), I_.get(getDescription()));
65  }
66 
67  private void studentsToCSV(IMailSender sender) {
68  try {
69  sender.onFinish(x -> sender.removeAttachments());
70  File result = File.createTempFile("report", "_Students.csv");
71  FieldsExporter.of(getEntries()).export(result);
72  sender.addAttachment(result);
73  sender.onFinish(x -> sender.removeAttachments());
74  } catch (IOException ex) {
75  WebLoggers.severe(this).exception(ex).log();
76  }
77  }
78 
79  private List<StudentsEntry> getEntries() {
80  BiConsumer<StudentsEntry, BusinessRelation> consumer = (e, p) -> {
81  e.name = p.getContact().getName();
82  e.position = p.getFormattedDescription();
83  e.center = p.getBusiness().getName();
84  };
85  return CumulativeList.of(StudentsEntry.class)
86  .forEach(SqlClause.select("br").from("BusinessRelation br")
87  .where().equal("br.business.type", ContactType.CONTACT_LEARNINGCENTER)
88  .dao(new ContactsPU())
89  .resultList(BusinessRelation.class),
90  consumer).get();
91  }
92 
93 }
static IMailSender getHeavy()
static String get(String msg)
Definition: I_.java:41
static void pushMessage(IContact contact, String msg)
T addContact(IContact contact)
IMailSender onFinish(Consumer command)
IMailSender addAttachment(File file)