BrightSide Workbench Full Report + Source Code
bsfinancials-core/src/main/java/org/turro/financials/model/product/ProductWrapper.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.financials.model.product;
19 
20 import java.util.List;
21 import org.turro.financials.db.FinancialsPU;
22 import org.turro.financials.entity.Contract;
23 import org.turro.financials.entity.Product;
24 import org.turro.financials.entity.ProductByContractor;
25 import org.turro.jpa.Dao;
26 import org.turro.jpa.entity.DaoEntity;
27 
32 public class ProductWrapper extends DaoEntity<Product, Long> {
33 
35  super(entity);
36  }
37 
38  @Override
39  protected Dao createDao() {
40  return new FinancialsPU();
41  }
42 
43  @Override
44  public boolean canDelete() {
45  if(!super.canDelete()) return false;
46  Long count = (Long) getDao().getSingleResult(
47  "select count(*) from DocumentLine " +
48  "where product = ?",
49  new Object[] { entity });
50  if(count == 0) {
51  count = (Long) getDao().getSingleResult(
52  "select count(*) from DocumentLine " +
53  "where productByContractor.product = ?",
54  new Object[] { entity });
55  }
56  return count == 0;
57  }
58 
59  @Override
60  protected boolean shouldLog() {
61  return true;
62  }
63 
64  public Product save(List<ProductByContractor> pbc) {
65  if(pbc != null) {
66  entity.getProductByContractors().clear();
67  entity.getProductByContractors().addAll(pbc);
68  }
69  return save();
70  }
71 
72  @Override
73  public boolean canSave() {
74  Long count = entity.getId() > 0 ? 0 : (Long) getDao().getSingleResult(
75  "select count(*) from Product " +
76  "where productCode = ?",
77  new Object[] { entity.getProductCode() });
78  return count == 0;
79  }
80 
81  public static boolean hasAvailableContractorProducts(Contract contract) {
82  Dao dao = new FinancialsPU();
83  Long result = (Long) dao.getSingleResult(
84  "select count(pbp) from ProductByContractor as pbp " +
85  "where pbp.contract = ?",
86  new Object[] {
87  contract
88  }
89  );
90  if(result == null || result == 0) {
91  return false;
92  }
93  return true;
94  }
95 
96  public static boolean hasAvailableProducts() {
97  Dao dao = new FinancialsPU();
98  Long result = (Long) dao.getSingleResult(
99  "select count(p) from Product as p"
100  );
101  if(result == null || result == 0) {
102  return false;
103  }
104  return true;
105  }
106 
107 }
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380