BrightSide Workbench Full Report + Source Code
CommonsActivity.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.activity;
20 
21 import org.turro.elephant.db.ElephantPU;
22 import org.turro.elephant.entities.db.EntityParticipation;
23 import org.turro.elephant.entities.db.Poll;
24 import org.turro.elephant.entities.db.Post;
25 import org.turro.elephant.entities.db.Topic;
26 import org.turro.entities.Entities;
27 import org.turro.entities.IElephantEntity;
28 import org.turro.jpa.Dao;
29 import org.turro.participation.ParticipationReason;
30 import org.turro.sql.SqlClause;
31 import org.turro.util.Cached;
32 
37 @ElephantActivity
38 public class CommonsActivity implements IElephantActivity {
39 
40  @Override
41  public void generate() {
42  Activities topacts = Activities.of("New topic");
43  SqlClause.select("topic").from("Topic topic")
44  .where().greater("topic.creation", topacts.lastDate())
45  .dao(dao.get()).forEach(Topic.class, topic -> {
46  if(!topic.getEntity().isEmpty()) {
47  IElephantEntity itopic = Entities.getController(topic);
48  if(!itopic.isEmpty()) {
49  topacts.add(topic.getCreation(), itopic.getPath(), topic.getEntityPath(), itopic.getHierarchicalPath());
50  }
51  }
52  });
53  Activities ansacts = Activities.of("New answer");
54  SqlClause.select("post").from("Post post")
55  .where().greater("topic.creation", ansacts.lastDate())
56  .dao(dao.get()).forEach(Post.class, post -> {
57  if(!post.getTopic().getEntity().isEmpty()) {
58  IElephantEntity ipost = Entities.getController(post);
59  if(!ipost.isEmpty()) {
60  ansacts.add(post.getCreation(), ipost.getPath(), post.getTopic().getEntityPath(), ipost.getHierarchicalPath());
61  }
62  }
63  });
64  Activities pollacts = Activities.of("Related polls");
65  SqlClause.select("poll").from("Poll poll")
66  .where().greater("poll.deadline", pollacts.lastDate())
67  .dao(dao.get()).forEach(Poll.class, poll -> {
68  if(!poll.getEntity().isEmpty()) {
69  IElephantEntity ipoll = Entities.getController(poll);
70  if(!ipoll.isEmpty()) {
71  pollacts.add(poll.getDeadline(), ipoll.getPath(), poll.getEntityPath(), ipoll.getHierarchicalPath());
72  }
73  }
74  });
75  for(ParticipationReason reason : ParticipationReason.values()) {
76  Activities epacts = Activities.of(reason.toString());
77  SqlClause.select("ep").from("EntityParticipation ep")
78  .where().greater("ep.participationDate", epacts.lastDate())
79  .and().equal("ep.reason", reason)
80  .dao(dao.get()).forEach(EntityParticipation.class, ep -> {
81  if(!ep.getEntity().isEmpty()) {
82  String path = ElephantPU.getObjectPath(ep);
83  epacts.add(ep.getParticipationDate(), path, ep.getEntityPath(), ep.getEntity().getHierarchicalPath());
84  }
85  });
86  }
87  }
88 
89  /* Dao */
90 
91  private final Cached<Dao> dao = Cached.instance(() -> new ElephantPU());
92 
93 }
static Activities of(String reason)