BrightSide Workbench Full Report + Source Code
ProductFactory.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.product;
19 
20 import java.util.ArrayList;
21 import java.util.List;
22 import org.turro.string.Strings;
23 import org.turro.elephant.db.WhereClause;
24 import org.turro.financials.db.FinancialsPU;
25 import org.turro.financials.entity.DocumentLine;
26 import org.turro.financials.entity.LineType;
27 import org.turro.financials.entity.Product;
28 import org.turro.financials.entity.ProductByContractor;
29 import org.turro.util.Chars;
30 
35 public class ProductFactory {
36 
37  public static IProduct getProduct(long id) {
38  if(id < 1) return null;
39  return new FinancialsPU().find(Product.class, (Long) id);
40  }
41 
42  public static IProduct getProduct(String id) {
43  if(Strings.isBlank(id)) return null;
45  "select prod from Product prod " +
46  "where prod.productCode = ?",
47  new Object[] { id }
48  );
49  }
50 
51  public static IProduct getProduct(long contractorId, String contractorCode) {
52  IProduct product = null;
53  ProductByContractor pbc = getContractorProduct(contractorId, contractorCode);
54  if(pbc != null) {
55  product = pbc.getProduct();
56  product.setProductByContractor(pbc);
57  }
58  return product;
59  }
60 
61  public static IProduct getProduct(IProductKey productKey) {
62  return getProduct(productKey.getProductId(), productKey.getProviderId(),
63  productKey.getProviderProductId(), productKey.getProductDescription());
64  }
65 
66  public static IProduct getProduct(long productId, long providerId, long providerProductId, String description) {
67  if(providerProductId > 0) {
68  ProductByContractor pbc = new FinancialsPU().find(ProductByContractor.class, providerProductId);
69  if(pbc != null) {
70  return pbc.getProduct();
71  }
72  }
73  if(productId > 0) {
74  return getProduct(productId);
75  }
76  if(!Strings.isBlank(description)) {
77  return getProductFromText(providerId, description);
78  }
79  return null;
80  }
81 
82  public static ProductByContractor getContractorProduct(long contractorId, String contractorCode) {
83  if(contractorId < 1 || Strings.isBlank(contractorCode)) return null;
84  WhereClause wc = new WhereClause();
85  wc.addClause("select pbc from ProductByContractor pbc");
86  wc.addClause("where pbc.contract.id = :id");
87  wc.addNamedValue("id", contractorId);
88  wc.addClause("and pbc.contractorCode = :code");
89  wc.addNamedValue("code", contractorCode);
90  List l = new FinancialsPU().getResultList(wc, 1);
91  if(l != null && !l.isEmpty()) {
92  return (ProductByContractor) l.get(0);
93  }
94  return null;
95  }
96 
97  public static void setContractorProduct(IProduct product, long contractorId) {
98  if(contractorId < 1 || product == null) return;
99  WhereClause wc = new WhereClause();
100  wc.addClause("select pbc from ProductByContractor pbc");
101  wc.addClause("where pbc.contract.id = :id");
102  wc.addNamedValue("id", contractorId);
103  wc.addClause("and pbc.product.productCode = :code");
104  wc.addNamedValue("code", product.getProductCodeStr());
105  List l = new FinancialsPU().getResultList(wc, 1);
106  if(l != null && !l.isEmpty()) {
107  product.setProductByContractor((ProductByContractor) l.get(0));
108  }
109  }
110 
111  public static List<IProduct> getProducts(long contractorId, String input) {
112  List<IProduct> list = new ArrayList<>();
113  IProduct product;
114  // Contractor coded
115  if(input.matches("\\##.*")) {
116  product = getProduct(contractorId, input.substring(2));
117  if(product != null) list.add(product);
118  // Coded
119  } else if(input.matches("\\#.*")) {
120  product = getProduct(input.substring(1));
121  if(product != null) list.add(product);
122  // Coded or Contractor coded
123  } else {
124  product = getProduct(input);
125  setContractorProduct(product, contractorId);
126  if(product != null) list.add(product);
127  product = getProduct(contractorId, input);
128  if(product != null) list.add(product);
129  }
130  // Product description
131  if(list.isEmpty()) {
132  list.addAll(Product.getProducts(input, true));
133  }
134  // Concept description
135  if(list.isEmpty()) {
136  list.addAll(ProductUncoded.getProducts(input, true));
137  }
138  // Product similarities
139  if(list.size() < 10) {
140  list.addAll(Product.getProducts(input, false));
141  }
142  // Concept similarities
143  if(list.size() < 10) {
144  list.addAll(ProductUncoded.getProducts(input, false));
145  }
146  return list;
147  }
148 
149  public static IProduct getProductFromText(long contractorId, String text) {
150  String parts[] = text.split(Chars.forward().regexp().spaced().toString());
151  if(parts.length == 2) { // program generated
152  if(parts[0].startsWith("##")) {
153  return getProduct(contractorId, parts[0].substring(2));
154  } else if(parts[0].startsWith("#")) {
155  IProduct prod = getProduct(parts[0].substring(1));
156  setContractorProduct(prod, contractorId);
157  return prod;
158  }
159  } else if(!Strings.isBlank(text)) { // manual input
160  IProduct prod;
161  // Contractor coded
162  if(text.matches("\\##.*")) {
163  prod = getProduct(contractorId, text.substring(2));
164  // Coded
165  } else if(text.matches("\\#.*")) {
166  prod = getProduct(text.substring(1));
167  setContractorProduct(prod, contractorId);
168  // Coded or Contractor coded
169  } else {
170  prod = getProduct(text);
171  setContractorProduct(prod, contractorId);
172  if(prod == null) {
173  prod = getProduct(contractorId, text);
174  }
175  }
176  if(prod != null) {
177  return prod;
178  }
179  return new ProductUncoded(text);
180  }
181  return null;
182  }
183 
184  public static void setPriceTax(DocumentLine dl, IProduct prod) {
185  if(dl.getPrice() == 0.0 && dl.getTax() == 0.0) {
186  LineType lt = dl.getLineType();
187  if(lt != null) {
188  if(lt.getStockCoefficient() == -1) {
189  dl.setPrice(prod.getProductPrice());
190  dl.setTax(prod.getProductPriceTax());
191  } else if(lt.getStockCoefficient() == 1) {
192  dl.setPrice(prod.getProductCost());
193  dl.setTax(prod.getProductCostTax());
194  }
195  }
196  }
197  }
198 
199  private ProductFactory() {
200  }
201 
202 }
void addNamedValue(String name, Object value)
static List< Product > getProducts()
Definition: Product.java:439
static IProduct getProduct(long contractorId, String contractorCode)
static IProduct getProductFromText(long contractorId, String text)
static void setPriceTax(DocumentLine dl, IProduct prod)
static IProduct getProduct(long productId, long providerId, long providerProductId, String description)
static ProductByContractor getContractorProduct(long contractorId, String contractorCode)
static void setContractorProduct(IProduct product, long contractorId)
static List< IProduct > getProducts(long contractorId, String input)
static IProduct getProduct(IProductKey productKey)
static List< ProductUncoded > getProducts()
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419
void setProductByContractor(ProductByContractor pbc)