BrightSide Workbench Full Report + Source Code
ParticipationsReport.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.Optional;
25 import java.util.function.BiConsumer;
26 import org.turro.action.IMailSender;
27 import org.turro.action.MailSenders;
28 import org.turro.auth.Authentication;
29 import org.turro.collections.CumulativeList;
30 import org.turro.contacts.BusinessRelation;
31 import org.turro.contacts.Contact;
32 import org.turro.contacts.relation.Business;
33 import org.turro.dossier.db.DossierPU;
34 import org.turro.dossier.entity.Participant;
35 import org.turro.elephant.context.ElephantContext;
36 import org.turro.exporter.FieldsExporter;
37 import org.turro.i18n.I_;
38 import org.turro.log.WebLoggers;
39 import org.turro.message.MessageQueue;
40 import org.turro.plugin.contacts.IContact;
41 import org.turro.sql.SqlClause;
42 import org.turro.string.Strings;
43 import org.turro.tags.Tags;
44 
49 public class ParticipationsReport implements IAdminReport {
50 
51  @Override
52  public boolean isInRole() {
54  }
55 
56  @Override
57  public String getDescription() {
58  return "Participations in dossiers";
59  }
60 
61  @Override
62  public void generate() {
65  .addContact(contact)
66  .onStart(s -> MessageQueue.pushMessage(contact, I_.get("Report will be sent by email")))
67  .onCancel(s -> MessageQueue.pushMessage(contact, I_.get("Task already running")))
68  .onBuild(s -> participationToCSV((IMailSender) s))
69  .send(I_.get(getDescription()), I_.get(getDescription()));
70  }
71 
72  private void participationToCSV(IMailSender sender) {
73  try {
74  sender.onFinish(x -> sender.removeAttachments());
75  File result = File.createTempFile("report", "_Participation.csv");
76  FieldsExporter.of(getEntries()).export(result);
77  sender.addAttachment(result);
78  sender.onFinish(x -> sender.removeAttachments());
79  } catch (IOException ex) {
80  WebLoggers.severe(this).exception(ex).log();
81  }
82  }
83 
84  private List<ParticipationEntry> getEntries() {
85  BiConsumer<ParticipationEntry, Participant> consumer = (e, p) -> {
86  e.creation = p.getCreation();
87  e.category = p.getDossier().getCategory().getDescription();
88  e.identifier = p.getDossier().getId();
89  e.dossier = p.getDossier().getDescription();
90  e.project = Optional.ofNullable(p.getDossier().getProject()).map(c -> c.getProjectCode()).orElse("");
91  e.participation = Strings.listToCvs(p.getStringRoles());
92  e.email = p.getIContact().getEmail();
93  e.name = p.getIContact().getName();
94  Business business = new Business((Contact) p.getIContact().getContact());
95  BusinessRelation relation = business.getBusinessRelation(p.getCreation());
96  e.position = Optional.ofNullable(relation).map(r -> r.getFormattedDescription()).orElse("");
97  e.company = Optional.ofNullable(relation).map(r -> r.getBusiness()).map(b -> b.getName()).orElse("");
98  e.tags = Tags.tagsString(p.getDossier());
99  };
100  return CumulativeList.of(ParticipationEntry.class)
101  .forEach(SqlClause.select("p").from("Participant p")
102  .dao(new DossierPU())
103  .resultList(Participant.class),
104  consumer).get();
105  }
106 
107 }
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)