BrightSide Workbench Full Report + Source Code
ResponsesReport.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.elephant.context.ElephantContext;
30 import org.turro.exporter.FieldsExporter;
31 import org.turro.html.HtmlContent;
32 import org.turro.i18n.I_;
33 import org.turro.log.WebLoggers;
34 import org.turro.message.MessageQueue;
35 import org.turro.plugin.contacts.IContact;
36 import org.turro.sql.SqlClause;
37 import org.turro.students.db.StudentsPU;
38 import org.turro.students.entities.Response;
39 
44 public class ResponsesReport implements IAdminReport {
45 
46  @Override
47  public boolean isInRole() {
49  }
50 
51  @Override
52  public String getDescription() {
53  return "Responses to challenges by student";
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", "_Responses.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<CumulativeList<ResponsesEntry>, Response> consumer = (l, r) -> {
81  for(IContact student : r.getStudentList()) {
82  ResponsesEntry e = new ResponsesEntry();
83  e.challengeId = r.getChallenge().getId();
84  e.creation = r.getCreation();
85  e.challenge = r.getChallenge().getQuestion();
86  e.student = student.getFullName();
87  e.response = HtmlContent.plain(r.getText(), 255);
88  l.add(e);
89  }
90  };
91  return CumulativeList.of(ResponsesEntry.class)
92  .forEachMultiCreate(SqlClause.select("r").from("Response r")
93  .dao(new StudentsPU())
94  .resultList(Response.class),
95  consumer).get();
96  }
97 
98 }
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)