19 package org.turro.dossier.dw;
21 import java.util.concurrent.locks.Lock;
22 import java.util.concurrent.locks.ReentrantLock;
23 import org.turro.dossier.db.DossierPU;
24 import org.turro.dossier.entity.Dossier;
25 import org.turro.dossier.entity.Issue;
26 import org.turro.jpa.Dao;
27 import org.turro.sql.SqlClause;
35 private static final Lock lock =
new ReentrantLock();
41 if(checkModifications(dao)) {
42 generateDossiers(dao);
51 private static void generateDossiers(
Dao dao) {
53 dao.
executeUpdate(
"delete from DWDossier where dossierId > 0");
56 "insert into DWDossier(dossierId, versionId, creation, description, categoryPath, participantId, type, status, publishable, phase) " +
57 "select distinct d.id, v.id, d.creation, cast(d.description as string), d.category.fullDescription, p.idContact, d.type, d.status, d.publishable, pr.phase " +
58 "from Dossier d left join d.participants p left join d.versions v left join d.project pr");
61 private static void generateIssues(Dao dao) {
63 dao.executeUpdate(
"delete from DWIssue where issueId > 0");
66 "insert into DWIssue(issueId, dossierId, versionId, startDate, solvedDate, description, participantId, participantRole, categoryPath, type, priority, status, resolution, pexpenses, phours, pprice, commentCount, rexpenses, rhours, rprice) " +
67 "select i.id, i.dossier.id, i.version.id, COALESCE(i.startDate, i.issueDate), i.solvedDate, cast(i.description as string), p.idContact, p.role, i.dossier.category.fullDescription, i.type, i.priority, i.status, i.resolution, i.expenses, i.hours, i.price, 0L, 0.0, 0.0, 0.0 " +
68 "from Issue i left join i.participants p");
70 "update DWIssue as i " +
71 "set commentCount = (select count(id) from IssueComment where issue.id = i.issueId), " +
72 "rexpenses = coalesce((select sum(coalesce(expenses,0)) from IssueComment where issue.id = i.issueId),0), " +
73 "rhours = coalesce((select sum(coalesce(hours,0)) from IssueComment where issue.id = i.issueId),0), " +
74 "rprice = coalesce((select sum(coalesce(price,0)) from IssueComment where issue.id = i.issueId),0)");
77 private static boolean checkModifications(Dao dao) {
78 Long count = (Long) dao.getSingleResultOrNull(
79 "select count(distinct d.id) from DWIssue d " +
80 "where commentCount <> (" +
81 "select count(ic.id) from IssueComment ic " +
82 "where ic.issue.id = d.issueId)");
91 if(count ==
null || count == 0) {
92 count = (Long) dao.getSingleResultOrNull(
93 "select count(i) from Issue i " +
94 "where not exists( " +
95 " select d from DWIssue d where i.id = d.issueId " +
96 " and i.status = d.status " +
99 if(count ==
null || count == 0) {
100 count = (Long) dao.getSingleResultOrNull(
101 "select count(i) from Dossier i " +
102 "where not exists( " +
103 " select d from DWDossier d where i.id = d.dossierId " +
104 " and i.category.fullDescription = d.categoryPath " +
108 if(count ==
null || count == 0) {
109 count = (Long) dao.getSingleResultOrNull(
110 "select count(i) from Project i " +
111 "where not exists( " +
112 " select d from DWDossier d where i.dossier.id = d.dossierId and i.phase = d.phase " +
113 ") and i.dossier is not null");
115 return count !=
null && count > 0;
119 SqlClause.delete(
"DWDossier")
120 .where().equal(
"dossierId", dossier.getId())
126 SqlClause.delete(
"DWIssue")
127 .where().equal(
"issueId", issue.getId())
static void generateData()
int executeUpdate(String query)