BrightSide Workbench Full Report + Source Code
DatawarehouseUtil.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 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.dossier.dw;
20 
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;
28 
33 public class DatawarehouseUtil {
34 
35  private static final Lock lock = new ReentrantLock();
36 
37  public static void generateData() {
38  Dao dao = new DossierPU();
39  lock.lock();
40  try {
41  if(checkModifications(dao)) {
42  generateDossiers(dao);
43  generateIssues(dao);
44  }
45  } finally {
46  lock.unlock();
47  }
48  }
49 
50  // TODO: Afegir participant de categories i treue els SUBJECT
51  private static void generateDossiers(Dao dao) {
52  //dao.executeNativeUpdate("truncate table DWDossier");
53  dao.executeUpdate("delete from DWDossier where dossierId > 0");
54  //dao.executeNativeUpdate("alter table DWDossier AUTO_INCREMENT=1");
55  dao.executeUpdate(
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");
59  }
60 
61  private static void generateIssues(Dao dao) {
62  //dao.executeNativeUpdate("truncate table DWIssue");
63  dao.executeUpdate("delete from DWIssue where issueId > 0");
64  //dao.executeNativeUpdate("alter table DWIssue AUTO_INCREMENT=1");
65  dao.executeUpdate(
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");
69  dao.executeUpdate(
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)");
75  }
76 
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)");
83  /*if(count == null || count == 0) {
84  count = (Long) dao.getSingleResultOrNull(
85  "select count(i) from Issue i, DWIssue d " +
86  "where i.id = d.issueId " +
87  "and (d.startDate <> COALESCE(i.startDate, i.issueDate) " +
88  "or d.solvedDate <> i.solvedDate " +
89  "or (d.solvedDate is null and i.solvedDate is not null))");
90  }*/
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 " +
97  ")");
98  }
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 " +
105  /*" and i.publishable = d.publishable " +*/
106  ")");
107  }
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");
114  }
115  return count != null && count > 0;
116  }
117 
118  public static void remove(Dao dao, Dossier dossier) {
119  SqlClause.delete("DWDossier")
120  .where().equal("dossierId", dossier.getId())
121  .dao(dao)
122  .execute();
123  }
124 
125  public static void remove(Dao dao, Issue issue) {
126  SqlClause.delete("DWIssue")
127  .where().equal("issueId", issue.getId())
128  .dao(dao)
129  .execute();
130  }
131 
132 }
int executeUpdate(String query)
Definition: Dao.java:463