BrightSide Workbench Full Report + Source Code
BatchOf.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.entity;
19 
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.Date;
23 import java.util.HashSet;
24 import java.util.Set;
25 import javax.persistence.Column;
26 import javax.persistence.Entity;
27 import javax.persistence.FetchType;
28 import javax.persistence.GeneratedValue;
29 import javax.persistence.GenerationType;
30 import javax.persistence.Id;
31 import javax.persistence.OneToMany;
32 import javax.persistence.OneToOne;
33 import org.turro.financials.db.FinancialsPU;
34 import org.turro.financials.model.document.BatchOfType;
35 
40 @Entity
41 public class BatchOf implements java.io.Serializable {
42 
43  @Id
44  @GeneratedValue(strategy=GenerationType.IDENTITY)
45  @Column(name="IDENTIFIER")
46  private long id;
47 
48  private BatchOfType type;
49 
50  @OneToOne
51  private Document batch;
52 
53  @OneToOne
54  private Contract store;
55 
56  @OneToMany(fetch = FetchType.EAGER)
57  private Set<Document> documents = new HashSet<Document>();
58 
59  public Document getBatch() {
60  return batch;
61  }
62 
63  public void setBatch(Document batch) {
64  this.batch = batch;
65  }
66 
67  public Set<Document> getDocuments() {
68  return documents;
69  }
70 
71  public void setDocuments(Set<Document> documents) {
72  this.documents = documents;
73  }
74 
75  public long getId() {
76  return id;
77  }
78 
79  public void setId(long id) {
80  this.id = id;
81  }
82 
83  public BatchOfType getType() {
84  return type;
85  }
86 
87  public void setType(BatchOfType type) {
88  this.type = type;
89  }
90 
91  public Contract getStore() {
92  return store;
93  }
94 
95  public void setStore(Contract store) {
96  this.store = store;
97  for(Document d : documents) {
98  for(DocumentLine dl : d.getDocumentLines()) {
99  dl.setStore(store);
100  }
101  }
102  }
103 
104  public void setDocumentDate(Date date) {
105  if(batch != null) {
106  batch.setDocumentDate(date);
107  }
108  for(Document doc : documents) {
109  if(!BatchOfType.BATCH_OF_DISCOUNTING.equals(type)) {
110  doc.setDocumentDate(date);
111  }
112  }
113  }
114 
115  public void setReceiptDate(Date date) {
116  if(batch != null) {
117  batch.setReceiptDate(date);
118  }
119  for(Document doc : documents) {
120  if(!BatchOfType.BATCH_OF_DISCOUNTING.equals(type)) {
121  doc.setReceiptDate(date);
122  }
123  }
124  }
125 
126  /* Helpers */
127 
128  public boolean isValid() {
129  if(store == null || batch == null || !(batch.isValid())) {
130  return false;
131  }
132  return true;
133  }
134 
135  public static Collection<BatchOf> getFrom(Document document) {
136  return (document == null || document.getId() == 0) ? Collections.EMPTY_LIST :
137  new FinancialsPU().getResultList(
138  "select batch from BatchOf as batch " +
139  "where batch.batch = ? " +
140  "or ? member of batch.documents",
141  new Object[] {
142  document, document
143  });
144  }
145 
146 }
void setType(BatchOfType type)
Definition: BatchOf.java:87
void setStore(Contract store)
Definition: BatchOf.java:95
static Collection< BatchOf > getFrom(Document document)
Definition: BatchOf.java:135
void setDocuments(Set< Document > documents)
Definition: BatchOf.java:71
void setBatch(Document batch)
Definition: BatchOf.java:63
Set< Document > getDocuments()
Definition: BatchOf.java:67