BrightSide Workbench Full Report + Source Code
bsfinancials-core/src/main/java/org/turro/financials/model/document/DocumentWrapper.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.document;
19 
20 import java.util.*;
21 import org.turro.financials.db.FinancialsPU;
22 import org.turro.financials.entity.*;
23 import org.turro.financials.model.contract.ContractWrapper;
24 import org.turro.financials.model.register.RegisterWrapper;
25 import org.turro.financials.model.register.ViewWrapper;
26 import org.turro.jpa.Dao;
27 import org.turro.jpa.entity.DaoEntity;
28 import org.turro.usual.UsualUsages;
29 
34 public class DocumentWrapper extends DaoEntity<Document, Long> {
35 
36  private List<DocumentLine> lines;
37  private List<DocumentRelation> descendants;
38 
40  super(entity);
41  for(Register r : entity.getRegisters()) {
42  entity.setRegularizeVAT(r.isRegularizeVAT());
43  entity.setRegularizeIRPF(r.isRegularizeIRPF());
44  }
45  }
46 
47  @Override
48  protected Dao createDao() {
49  return new FinancialsPU();
50  }
51 
52  public Set<RegisterWrapper> getRegisters() {
53  Set<RegisterWrapper> s = new HashSet<RegisterWrapper>();
54  for(RegisterView rv : ViewWrapper.getViews()) {
55  boolean done = false;
56  for(Register r : entity.getRegisters()) {
57  if(r.getView().getId() == rv.getId()) {
58  r.setRegisterDate(entity.getReceiptDate());
59  s.add(new RegisterWrapper(r));
60  done = true;
61  break;
62  }
63  }
64  if(!done) {
65  Register r = new Register();
66  r.setView(rv);
67  r.setRegisterDate(entity.getReceiptDate());
69  entity.getRegisters().add(r);
70  s.add(new RegisterWrapper(r));
71  }
72  }
73  return s;
74  }
75 
76  public void setPriorSavingValues(List<DocumentLine> lines, List<DocumentRelation> descendants) {
77  this.lines = lines;
78  this.descendants = descendants;
79  }
80 
81  @Override
82  public Document save() {
83  return save(lines, descendants);
84  }
85 
86  public Document save(List<DocumentLine> lines, List<DocumentRelation> descendants) {
87  /*
88  * Save documents and assign ancestor and descendant,
89  * then save relations.
90  */
91  if(descendants != null) {
92  DocumentWrapper dw;
93  for(DocumentRelation dr : descendants) {
94  if(!dr.isEmpty()) {
95  if(!dr.getDescendant().isEmpty()) {
96  dr.getDescendant().setForcedView(entity.getForcedView());
97  if(dr.getDescendant().getId() == 0) {
98  if(entity.isDraft()) {
99  dr.getDescendant().setDraft(true);
100  }
101  } else {
102  if(!entity.isDraft()) {
103  dr.getDescendant().setDraft(false);
104  }
105  }
106  for(DocumentLine l : dr.getDescendant().getDocumentLines()) {
107  if(!l.isEmpty() && l.getStore() != null) {
108  UsualUsages.addUsage(entity.getUsualPath() + "df:" + l.getDocument().getDocumentDefinition().getId());
109  UsualUsages.addUsage(dr.getDescendant().getUsualPath() + "st:" + l.getStore().getId());
110  UsualUsages.addUsage(dr.getDescendant().getUsualPath() + "cp:" + l.getConcept());
111  }
112  }
113  dw = new DocumentWrapper(dr.getDescendant());
114  dr.setDescendant(dw.save(null, null));
115  }
116  }
117  }
118  }
119  if(lines != null) {
120  entity.assignTableOrder(lines);
121  for(DocumentLine l : lines) {
122  if(!l.isEmpty() && l.getStore() != null) {
123  UsualUsages.addUsage(entity.getUsualPath() + "st:" + l.getStore().getId());
124  UsualUsages.addUsage(entity.getUsualPath() + "cp:" + l.getConcept());
125  }
126  }
127  }
128  entity.clearEmpties();
129  if(entity.isDraft()) {
130  entity.getRegisters().clear();
131  } else {
132  getRegisters();
133  Iterator<Register> ir = entity.getRegisters().iterator();
134  while(ir.hasNext()) {
135  Register r = ir.next();
136  r.removeEmpties();
137  r.setRegisterDate(entity.getReceiptDate());
138  if(r.isEmpty()) {
139  r.setDocument(null);
140  ir.remove();
141  } else if(entity.getForcedView() != null) {
142  if(r.getView().getId() != entity.getForcedView().getId()) {
143  r.setView(entity.getForcedView());
144  r.setIdRegister(0);
145  }
146  }
147  }
148  }
149 
150  /* Save relations for future saving */
151  ArrayList<DocumentRelation> relancestors = new ArrayList<DocumentRelation>(entity.getAncestors());
152  ArrayList<DocumentRelation> reldescendants = new ArrayList<DocumentRelation>(entity.getDescendants());
153  entity.getAncestors().clear();
154  entity.getDescendants().clear();
155 
156  entity.prepareSave();
157  super.save();
158 
159  /* Save relations from editing entity */
160  if(lines != null) {
161  for(DocumentRelation dr : relancestors) {
162  if(!dr.isEmpty()) {
163  dr.setDescendant(entity);
164  getDao().saveObject(dr);
165  }
166  }
167  for(DocumentRelation dr : reldescendants) {
168  if(!dr.isEmpty() && dr.getDescendant().getId() != 0) {
169  dr.setAncestor(entity);
170  getDao().saveObject(dr);
171  }
172  }
173  }
174  return entity;
175  }
176 
177  @Override
178  public boolean delete() {
179  // Check if possible and notify
181  "delete from DocumentRelation " +
182  "where ancestor = ? " +
183  "or descendant = ?",
184  new Object[] { entity, entity });
185  entity.getAncestors().clear();
186  entity.getDescendants().clear();
187  return super.delete();
188  }
189 
190  @Override
191  protected boolean shouldLog() {
192  return true;
193  }
194 
195  public List<Document> getRelatedAncestor(DocumentDefinition documentDefinition) {
196  return getDao().getResultList(
197  "select doc from Document as doc " +
198  "where doc.descendants is empty " +
199  "and doc.documentDefinition = ? " +
200  "and doc.contract = ?",
201  new Object[] { documentDefinition, entity.getContract() });
202  }
203 
204  public List<Document> getRelatedDescendant(DocumentDefinition documentDefinition) {
205  return getDao().getResultList(
206  "select doc from Document as doc " +
207  "where doc.ancestors is empty " +
208  "and doc.documentDefinition = ? " +
209  "and doc.contract = ?",
210  new Object[] { documentDefinition, entity.getContract() });
211  }
212 
213  public double getAdvance() {
214  if(entity.getContract() != null) {
215  return new ContractWrapper(entity.getContract()).getAdvance(entity.getForcedView());
216  }
217  return 0.0d;
218  }
219 
220 }
void setView(RegisterView view)
Definition: Register.java:163
void setIdRegister(long idRegister)
Definition: Register.java:119
void setDocument(Document document)
Definition: Register.java:95
void setRegisterDate(Date registerDate)
Definition: Register.java:127
static List< RegisterView > getViews()
int executeUpdate(String query)
Definition: Dao.java:463
static Usually addUsage(String path)