BrightSide Workbench Full Report + Source Code
HandshakeDocument.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.financials.handshake;
20 
21 import com.google.gson.Gson;
22 import com.google.gson.GsonBuilder;
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Objects;
30 import java.util.Set;
31 import java.util.logging.Level;
32 import java.util.logging.Logger;
33 import org.turro.elephant.context.ElephantContext;
34 import org.turro.elephant.impl.util.FileUtil;
35 import org.turro.financials.db.FinancialsPU;
36 import org.turro.financials.entity.Company;
37 import org.turro.financials.entity.Contract;
38 import org.turro.financials.entity.Document;
39 import org.turro.financials.entity.DocumentLine;
40 import org.turro.financials.entity.DocumentRelation;
41 import org.turro.financials.model.business.CompanyWrapper;
42 import org.turro.jpa.Dao;
43 import org.turro.util.IdGenerator;
44 
49 public class HandshakeDocument {
50 
51  public final static String
52  HANDSHAKE_FOLDER = "/WEB-INF/handshake/document";
53 
55  public String documentName;
56  public String documentNumber;
57  public Date documentDate;
58  public long definitionId;
59  public List<HandshakeDocLine> lines = new ArrayList<>();
60  public List<HandshakeDocument> expiries = new ArrayList<>();
61 
62  public static String createJsonFromDocument(Document document) {
63  Gson gson = new GsonBuilder().setDateFormat("yyyy/MM/dd").create();
65  if(hd != null) {
66  return gson.toJson(hd);
67  }
68  return null;
69  }
70 
73  if(company != null) {
76  hd.documentName = document.getDocumentDefinition().getName();
77  hd.documentNumber = document.getDocumentNumber();
78  hd.documentDate = document.getDocumentDate();
79  hd.definitionId = mapDocumentDefinition(document);
80  for(DocumentLine dl : document.getDocumentLines()) {
81  if(!dl.isEmpty()) {
83  hdl.concept = dl.getConcept();
84  hdl.discountMoney = dl.getDiscountMoney();
85  hdl.discountPerCent = dl.getDiscountPerCent();
86  hdl.equivalenceSurcharge = dl.getEquivalenceSurcharge();
87  hdl.price = dl.getPrice();
88  if(dl.getProduct() != null) {
90  }
91  hdl.quantity = dl.getQuantity();
92  hdl.retention = dl.getRetention();
93  hdl.tax = dl.getTax();
94  hd.lines.add(hdl);
95  }
96  }
97  for(DocumentRelation dr : document.getDescendants()) {
98  Document doc = dr.getDescendant();
99  if(!doc.isEmpty()) {
101  }
102  }
103  return hd;
104  }
105  return null;
106  }
107 
108  public static HandshakeDocument createHandshakeFromJson(String value) {
109  Gson gson = new GsonBuilder().setDateFormat("yyyy/MM/dd").create();
110  return gson.fromJson(value, HandshakeDocument.class);
111  }
112 
113  public static HandshakeDocument createHandshakeFromFile(File file) {
114  try {
116  } catch (IOException ex) {
117  Logger.getLogger(HandshakeDocument.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
118  }
119  return null;
120  }
121 
122  public static void saveHandshakeToFile(File file, HandshakeDocument hd) {
123  Gson gson = new GsonBuilder().setDateFormat("yyyy/MM/dd").create();
124  try {
125  FileUtil.setContent(file, gson.toJson(hd));
126  } catch (IOException ex) {
127  Logger.getLogger(HandshakeContract.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
128  }
129  }
130 
132  Dao dao = new FinancialsPU();
133  return (Document) dao.getSingleResultOrNull(
134  "select d from Document d " +
135  "where d.contract.id = ? " +
136  "and d.documentNumber = ? " +
137  "and d.documentDate = ?",
138  new Object[] {
139  contract.getId(),
140  hd.documentNumber,
141  hd.documentDate
142  });
143  }
144 
145  public static void saveHandshakeDocument(HandshakeDocument hd) {
146  if(!existsHandshake(hd)) {
147  File file = new File(ElephantContext.getRealPath(HANDSHAKE_FOLDER) + "/doc" + IdGenerator.generate() + ".handshake");
148  saveHandshakeToFile(file, hd);
149  }
150  }
151 
152  public static boolean existsHandshake(HandshakeDocument hd) {
153  File folder = new File(ElephantContext.getRealPath(HANDSHAKE_FOLDER));
154  if(!folder.exists()) { folder.mkdirs(); }
155  for(File file : folder.listFiles()) {
157  if(hd.equals(fhd)) {
158  return true;
159  }
160  }
161  return false;
162  }
163 
164  private static long mapDocumentDefinition(Document document) {
165  int docdef = (int) document.getDocumentDefinition().getId();
166  switch(docdef) {
167  case 1:
168  return 2;
169  case 44:
170  return 43;
171  case 6:
172  return 7;
173  default:
174  return 0;
175  }
176  }
177 
178  @Override
179  public int hashCode() {
180  int hash = 5;
181  hash = 61 * hash + Objects.hashCode(this.contract);
182  hash = 61 * hash + Objects.hashCode(this.documentNumber);
183  hash = 61 * hash + Objects.hashCode(this.documentDate);
184  return hash;
185  }
186 
187  @Override
188  public boolean equals(Object obj) {
189  if (obj == null) {
190  return false;
191  }
192  if (getClass() != obj.getClass()) {
193  return false;
194  }
195  final HandshakeDocument other = (HandshakeDocument) obj;
196  if (!Objects.equals(this.contract, other.contract)) {
197  return false;
198  }
199  if (!Objects.equals(this.documentNumber, other.documentNumber)) {
200  return false;
201  }
202  if (!Objects.equals(this.documentDate, other.documentDate)) {
203  return false;
204  }
205  return true;
206  }
207 
208  public Iterable<HandshakeProduct> getProducts() {
209  Set<HandshakeProduct> products = new HashSet<>();
210  addProducts(products, this);
211  return products;
212  }
213 
214  private void addProducts(Set<HandshakeProduct> products, HandshakeDocument hd) {
215  for(HandshakeDocLine hdl : hd.lines) {
216  if(hdl.product != null) {
217  products.add(hdl.product);
218  }
219  }
220  for(HandshakeDocument dhd : hd.expiries) {
221  addProducts(products, dhd);
222  }
223  }
224 
225 }
static void setContent(File file, String value)
Definition: FileUtil.java:274
static String getContent(File file)
Definition: FileUtil.java:245
Set< DocumentRelation > getDescendants()
Definition: Document.java:139
Set< DocumentLine > getDocumentLines()
Definition: Document.java:180
DocumentDefinition getDocumentDefinition()
Definition: Document.java:167
static HandshakeContract createHandshakeFromContract(Contract contract)
static HandshakeDocument createHandshakeFromFile(File file)
static HandshakeDocument createHandshakeFromDocument(Document document)
static HandshakeDocument createHandshakeFromJson(String value)
static void saveHandshakeDocument(HandshakeDocument hd)
static boolean existsHandshake(HandshakeDocument hd)
static String createJsonFromDocument(Document document)
static Document getDocumentFromHandshake(Contract contract, HandshakeDocument hd)
static void saveHandshakeToFile(File file, HandshakeDocument hd)
static HandshakeProduct createHandshakeFromProduct(Product product)
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419