19 package org.turro.entitites;
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;
63 killDuplicatesAndOrphans(dao);
65 killMissingContacts(dao);
70 private void killDuplicatesAndOrphans(
Dao dao) {
71 killCategoryParticipantDuplicates(dao);
72 killDossierParticipantDuplicates(dao);
73 killIssueParticipantDuplicates(dao);
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])) {
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() });
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])) {
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() });
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])) {
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() });
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");
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)
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());
176 dp = dao.saveObject(dp);
177 Attachments.from(DossierPU.getObjectPath(issue))
178 .toFolder(dp.getFolder());
static void normalizeUniquePaths()
void initialize(IConstructor constructor)
WebLoggers message(String text, Object... parameters)
static WebLoggers info(Object entity)