BrightSide Workbench Full Report + Source Code
FinancialsWarnings.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.warnings;
20 
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.stream.Stream;
25 import org.turro.annotation.ElephantWarning;
26 import org.turro.elephant.context.Application;
27 import org.turro.financials.db.FinancialsPU;
28 import org.turro.financials.entity.Account;
29 import org.turro.financials.entity.MajorAccount;
30 import org.turro.i18n.I_;
31 import org.turro.jpa.Dao;
32 
38 public class FinancialsWarnings implements IElephantWarning {
39 
40  @Override
41  public String getHeader() {
42  return "Financials";
43  }
44 
45  @Override
46  public List<IWarning> getWarnings() {
47  if(Application.getApplication().isInRole("finan-model:list")) {
48  return createModel();
49  }
50  return Collections.EMPTY_LIST;
51  }
52 
53  private List<IWarning> createModel() {
54  Dao dao = new FinancialsPU();
55  List<IWarning> warnings = new ArrayList<>();
56  try(Stream<MajorAccount> majors = dao.stream(MajorAccount.class, "select m from MajorAccount m")) {
57  majors.forEach((major) -> {
58  checkMajor(warnings, major);
59  });
60  }
61  try(Stream<Account> accounts = dao.stream(Account.class, "select a from Account a")) {
62  accounts.forEach((account) -> {
63  checkAccount(warnings, account);
64  });
65  }
66  return warnings;
67  }
68 
69  private void checkMajor(List<IWarning> warnings, MajorAccount major) {
70  Warning warning = new Warning();
71  warning.setEntity(major);
72  if(major.getAccount().length() > 1 && major.getParent() == null) {
73  warning.addMessage(I_.format("%s has no parent",
74  major.getAccount() + " - " + major.getDescription()));
75  }
76  if(!warning.getMessages().isEmpty()) {
77  warnings.add(warning);
78  }
79  }
80 
81  private void checkAccount(List<IWarning> warnings, Account account) {
82  Warning warning = new Warning();
83  warning.setEntity(account);
84  MajorAccount ma = account.getParent();
85  if(ma == null) {
86  warning.addMessage(I_.format("%s has no parent",
87  account.getId()+ " - " + account.getDescription()));
88  } else if(ma.getAccount().length() < 3) {
89  warning.addMessage(I_.format("%s has a wrong parent",
90  account.getId()+ " - " + account.getDescription()));
91  }
92  if(!warning.getMessages().isEmpty()) {
93  warnings.add(warning);
94  }
95  }
96 
97 }