BrightSide Workbench Full Report + Source Code
TimeTrackerExporter.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.timetracker;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.Arrays;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import org.turro.action.IMailSender;
30 import org.turro.action.MailSenders;
31 import org.turro.auth.Authentication;
32 import org.turro.elephant.context.ElephantContext;
33 import org.turro.elephant.db.ElephantPU;
34 import org.turro.elephant.entities.db.TimeTrackerType;
35 import org.turro.entities.Entities;
36 import org.turro.i18n.I_;
37 import org.turro.jpa.Dao;
38 import org.turro.jpa.export.ExportAsStream;
39 import org.turro.message.MessageQueue;
40 import org.turro.plugin.contacts.IContact;
41 import org.turro.sql.SqlClause;
42 import org.turro.time.Time;
43 
48 public class TimeTrackerExporter {
49 
50  public static void exportTimeTracker() {
53  .addContact(contact)
54  .onStart(s -> MessageQueue.pushMessage(contact, I_.get("Report will be sent by email")))
55  .onCancel(s -> MessageQueue.pushMessage(contact, I_.get("Task already running")))
56  .onBuild(s -> timeTrackerToExcel((IMailSender) s))
57  .send(I_.get("Export time tracker"), I_.get("Export time tracker"));
58  }
59 
60  private static void timeTrackerToExcel(IMailSender sender) {
61  Map<String, String> cache = new HashMap<>();
62  List<String> l = Arrays.asList(new String[] {
63  I_.get("Entity"),
64  I_.get("Tracked"),
65  I_.get("Moment"),
66  I_.get("Type"),
67  I_.get("Hours"),
68  I_.get("Validated")
69  });
70  try {
71  Dao dao = new ElephantPU();
72  ExportAsStream eq = new ExportAsStream(dao, l,
73  SqlClause.select("tt.entityPath, tt.trackedPath, tt.timeTrack, tt.trackType, tt.hours, tt.validated")
74  .from("TimeTracker tt")
75  .where().greaterOrEqual("timeTrack", Time.now().addYears(-1).getDate())
76  .orderBy("tt.entityPath, tt.trackedPath, tt.timeTrack"));
77  eq.fieldReplacement((col, obj) -> {
78  if(col == 0 || col == 1) {
79  if(cache.containsKey((String) obj)) {
80  obj = cache.get((String) obj);
81  } else {
82  String name = Entities.getController(obj).getName();
83  cache.put((String) obj, name);
84  obj = name;
85  }
86  } else if(col == 3) {
87  obj = ((TimeTrackerType) obj).toString();
88  }
89  return obj;
90  });
91  File result = File.createTempFile("report", "_TimeTracker.xls");
92  eq.generateExcel(result);
93  sender.addAttachment(result);
94  sender.onFinish(x -> sender.removeAttachments());
95  } catch (IOException ex) {
96  Logger.getLogger(TimeTrackerExporter.class.getName()).log(Level.SEVERE, null, ex);
97  }
98  }
99 
100 }
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)