BrightSide Workbench Full Report + Source Code
DossierInitializer.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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.entitites;
20 
21 import java.util.Collection;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import org.turro.action.IElephantInitializer;
25 import org.turro.annotation.ElephantInitializer;
26 import org.turro.attach.search.Attachments;
27 import org.turro.contacts.ContactManagers;
28 import org.turro.contacts.IContactManager;
29 import org.turro.dossier.db.DossierPU;
30 import org.turro.dossier.dossier.ParticipantSet;
31 import org.turro.dossier.entity.Category;
32 import org.turro.dossier.entity.CategoryParticipant;
33 import org.turro.dossier.entity.Dossier;
34 import org.turro.dossier.entity.DossierOffer;
35 import org.turro.dossier.entity.Issue;
36 import org.turro.dossier.entity.IssueParticipant;
37 import org.turro.dossier.entity.IssueType;
38 import org.turro.dossier.entity.Participant;
39 import org.turro.dossier.search.CategoryResults;
40 import org.turro.elephant.context.IConstructor;
41 import org.turro.jpa.Dao;
42 import org.turro.jpa.DaoUtil;
43 import org.turro.jpa.query.JpaCriteria;
44 import org.turro.jpa.query.JpaCriteriaDelete;
45 import org.turro.jpa.query.JpaQuery;
46 import org.turro.jpa.query.JpaRoot;
47 import org.turro.log.WebLoggers;
48 import org.turro.sql.SqlClause;
49 
54 @ElephantInitializer
55 public class DossierInitializer implements IElephantInitializer {
56 
57  @Override
58  public void initialize(IConstructor constructor) {
59  WebLoggers.info(this).message("CategoryResults started").log();
61  Dao dao = new DossierPU();
62  WebLoggers.info(this).message("killDuplicatesAndOrphans started").log();
63  killDuplicatesAndOrphans(dao);
64  WebLoggers.info(this).message("killMissingContacts started").log();
65  killMissingContacts(dao);
66  WebLoggers.info(this).message("migrateOffers started").log();
67  migrateOffers(dao);
68  }
69 
70  private void killDuplicatesAndOrphans(Dao dao) {
71  killCategoryParticipantDuplicates(dao);
72  killDossierParticipantDuplicates(dao);
73  killIssueParticipantDuplicates(dao);
74  }
75 
76  private void killCategoryParticipantDuplicates(Dao dao) {
77  JpaCriteria jc = new JpaCriteria(dao);
78  JpaQuery<Object[]> jqcp = jc.query(Object[].class);
79  JpaRoot<CategoryParticipant> participant = jqcp.root(CategoryParticipant.class);
80  jqcp.multiselect(participant.get("idContact"), participant.get("category"), jc.count(participant.get("id")))
81  .groupBy(participant.get("idContact"), participant.get("category"))
82  .having(jc.greaterThan(jc.count(participant.get("id")), 1L));
83  dao.getResultList(jqcp).forEach((results) -> {
84  Category category = (Category) results[1];
85  boolean exists = false;
86  for(CategoryParticipant cp : new ParticipantSet<>(category.getParticipants())) {
87  if(cp.getIdContact().equals(results[0])) {
88  if(exists) {
89  JpaCriteriaDelete<CategoryParticipant> juc = jc.createCriteriaDelete(CategoryParticipant.class);
90  dao.executeDelete(juc.where(jc.equal(juc.field("id"), cp.getId())));
91  Logger.getLogger(DossierInitializer.class.getName()).log(
92  Level.INFO, "CategoryParticipant deleted {0} {1} on {2}",
93  new Object[] { cp.getName(), cp.getRole().name(), cp.getCategory().getDescription() });
94  }
95  exists = true;
96  }
97  }
98  });
99  }
100 
101  private void killDossierParticipantDuplicates(Dao dao) {
102  JpaCriteria jc = new JpaCriteria(dao);
103  JpaQuery<Object[]> jqcp = jc.query(Object[].class);
104  JpaRoot<Participant> participant = jqcp.root(Participant.class);
105  jqcp.multiselect(participant.get("idContact"), participant.get("dossier"), jc.count(participant.get("id")))
106  .groupBy(participant.get("idContact"), participant.get("dossier"))
107  .having(jc.greaterThan(jc.count(participant.get("id")), 1L));
108  dao.getResultList(jqcp).forEach((results) -> {
109  Dossier dossier = (Dossier) results[1];
110  boolean exists = false;
111  for(Participant dp : new ParticipantSet<>(dossier.getParticipants())) {
112  if(dp.getIdContact().equals(results[0])) {
113  if(exists) {
114  JpaCriteriaDelete<Participant> juc = jc.createCriteriaDelete(Participant.class);
115  dao.executeDelete(juc.where(jc.equal(juc.field("id"), dp.getId())));
116  Logger.getLogger(DossierInitializer.class.getName()).log(
117  Level.INFO, "DossierParticipant deleted {0} {1} on {2}",
118  new Object[] { dp.getName(), dp.getRole().name(), dp.getDossier().getDescription() });
119  }
120  exists = true;
121  }
122  }
123  });
124  }
125 
126  private void killIssueParticipantDuplicates(Dao dao) {
127  JpaCriteria jc = new JpaCriteria(dao);
128  JpaQuery<Object[]> jqcp = jc.query(Object[].class);
129  JpaRoot<IssueParticipant> participant = jqcp.root(IssueParticipant.class);
130  jqcp.multiselect(participant.get("idContact"), participant.get("role"), participant.get("issue"), jc.count(participant.get("id")))
131  .groupBy(participant.get("idContact"), participant.get("role"), participant.get("issue"))
132  .having(jc.greaterThan(jc.count(participant.get("id")), 1L));
133  dao.getResultList(jqcp).forEach((results) -> {
134  Issue issue = (Issue) results[2];
135  boolean exists = false;
136  for(IssueParticipant ip : new ParticipantSet<>(issue.getParticipants())) {
137  if(ip.getIdContact().equals(results[0]) && ip.getRole().equals(results[1])) {
138  if(exists) {
139  JpaCriteriaDelete<IssueParticipant> juc = jc.createCriteriaDelete(IssueParticipant.class);
140  dao.executeDelete(juc.where(jc.equal(juc.field("id"), ip.getId())));
141  Logger.getLogger(DossierInitializer.class.getName()).log(
142  Level.INFO, "IssueParticipant deleted {0} {1} on {2}",
143  new Object[] { ip.getName(), ip.getRole().name(), ip.getIssue().getDescription() });
144  }
145  exists = true;
146  }
147  }
148  });
149  }
150 
151  private void killMissingContacts(Dao dao) {
152  IContactManager manager = ContactManagers.instance();
153  manager.removeMissingFrom(dao, "CategoryParticipant", "idContact");
154  manager.removeMissingFrom(dao, "Participant", "idContact");
155  manager.removeMissingFrom(dao, "IssueParticipant", "idContact");
156  }
157 
158  private void migrateOffers(Dao dao) {
159  if(DaoUtil.isEmpty(dao, "DossierOffer") &&
160  !DaoUtil.isEmpty(dao, "Issue", "where type = ?", IssueType.TYPE_PROPOSITION)) {
161  SqlClause.select("i").from("Issue i")
162  .where().equal("i.type", IssueType.TYPE_PROPOSITION)
163  .dao(dao)
164  .resultList(Issue.class).forEach(issue -> {
165  Collection<IssueParticipant> reporters = issue.getIssueParticipants().getReporters();
166  if(!reporters.isEmpty()) {
167  DossierOffer dp = new DossierOffer();
168  dp.setDescription(issue.getDescription());
169  dp.setCreation(issue.getIssueDate());
170  dp.setBidderId(reporters.iterator().next().getIdContact());
171  dp.setDossier(issue.getDossier());
172  if(issue.getStatus().isFinished()) {
173  dp.setAccepted(issue.getResolution().isSolved());
174  dp.setDeclined(!dp.isAccepted());
175  }
176  dp = dao.saveObject(dp);
177  Attachments.from(DossierPU.getObjectPath(issue))
178  .toFolder(dp.getFolder());
179  }
180  });
181  }
182  }
183 
184 }
void initialize(IConstructor constructor)
WebLoggers message(String text, Object... parameters)
Definition: WebLoggers.java:34
static WebLoggers info(Object entity)
Definition: WebLoggers.java:43