BrightSide Workbench Full Report + Source Code
PortfolioProcess.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.portfolio;
19 
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Date;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.TreeSet;
27 import org.turro.command.Command;
28 import org.turro.command.Context;
29 import org.turro.elephant.zkoss.Modal;
30 import org.turro.elephant.zkoss.ModalWindow;
31 import org.turro.financials.document.logic.DocumentWrapper;
32 import org.turro.financials.entity.Contract;
33 import org.turro.financials.entity.Document;
34 import org.turro.financials.entity.DocumentDefinition;
35 import org.turro.financials.entity.DocumentLine;
36 import org.turro.financials.menu.FinancialsMenu;
37 import org.zkoss.zul.Grid;
38 
43 public class PortfolioProcess extends TreeSet<PortfolioAction> {
44 
45  private boolean wasCancelled = false;
46  private PortfolioOptions po = null;
47  private ModalWindow portForm = null;
48 
49  Contract contract = null;
50  DocumentDefinition documentDefinition = null;
51  Boolean direction = null;
52  List<PortfolioAction> list = new ArrayList<>();
53 
54  public void doIt(Grid grid) {
55 
56  wasCancelled = false;
57 
58  contract = null;
59  documentDefinition = null;
60  direction = null;
61  list = new ArrayList<>();
62 
63  Iterator<PortfolioAction> it = iterator();
64 
65  if(it.hasNext()) {
66  doNext(it);
67  } else {
68  processList(null);
69  }
70  // fill the list until there is a break, then process
71 // for(PortfolioAction pa : this) {
72 // if(isBreak(pa, contract, direction, documentDefinition)) {
73 // processList(list, contract, documentDefinition);
74 // }
75 // contract = pa.getDocument().getContract();
76 // direction = pa.isForward();
77 // documentDefinition = pa.getDocumentDefinition();
78 // list.add(pa);
79 // }
80 
81  }
82 
83  private void doNext(final Iterator<PortfolioAction> it) {
84  final PortfolioAction pa = it.next();
85  if(isBreak(pa)) {
86  processList(new Command() {
87  @Override
88  public Object execute(Context context) {
89  contract = pa.getDocument().getContract();
90  direction = pa.isForward();
91  documentDefinition = pa.getDocumentDefinition();
92  list.add(pa);
93  if(it.hasNext()) {
94  doNext(it);
95  } else {
96  processList(null);
97  }
98  return null;
99  }
100  });
101  return;
102  }
103  contract = pa.getDocument().getContract();
104  direction = pa.isForward();
105  documentDefinition = pa.getDocumentDefinition();
106  list.add(pa);
107  if(it.hasNext()) {
108  doNext(it);
109  } else {
110  processList(null);
111  }
112  }
113 
114  private boolean isBreak(PortfolioAction pa) {
115  if(contract != null && contract.getId() != pa.getDocument().getContract().getId() && (po == null || po.isStopOnContract())) {
116  return true;
117  }
118  if(direction != null && !direction.equals(pa.isForward())) {
119  return true;
120  }
121  if(documentDefinition != null && documentDefinition.getId() != pa.getDocumentDefinition().getId()) {
122  return true;
123  }
124  return false;
125  }
126 
127  private void processList(final Command command) {
128  if(list.isEmpty() || wasCancelled) return;
129  portForm = null;
130  final Date now = new Date();
131  if(po == null || po.isStopOnContract()) {
132  HashMap args = new HashMap();
133  Collection<Document> docs = getDocuments(list);
134  args.put("portfolioDocs", docs);
135  args.put("portfolioOptions", new PortfolioOptions(documentDefinition, contract));
136  portForm = Modal.getModal("/WEB-INF/_zul/financials/document/portfolioAction.zul", null, args);
137  Modal.doModal(portForm, new Command() {
138  @Override
139  public Object execute(Context context) {
140  processResult(portForm.getResult(), portForm, list, now);
141  list.clear();
142  portForm.detach();
143  if(command != null) command.execute(context);
144  return null;
145  }
146  });
147  } else {
148  processResult(1, portForm, list, now);
149  list.clear();
150  }
151  }
152 
153  private boolean processResult(int result, ModalWindow portForm, List<PortfolioAction> list, Date now) {
154  if (result == 2) {
155  wasCancelled = true;
156  return true;
157  } else if (result == 1) {
158  if(po == null || po.isStopOnContract()) {
159  po = (PortfolioOptions) portForm.getResultValue();
160  }
161  Document nd = null;
162  if(po.isAccumulated()) {
163  nd = new Document();
164  }
165  for(PortfolioAction pa : list) {
166  if(!po.isAccumulated()) {
167  nd = new Document();
168  }
169  nd.conciliateRegister = po.isConciliateRegister();
170  if(pa.isForward()) {
171  nd.flowFrom(pa.getDocument(), new DocumentWrapper(pa.getDocument()).getDocDescription(), pa.getWorkflow());
172  nd.setDocumentNumber(pa.getDocument().getDocumentNumber() + "/1");
173  } else {
174  nd.backFrom(pa.getDocument(), new DocumentWrapper(pa.getDocument()).getDocDescription(), pa.getWorkflow());
175  nd.setDocumentNumber(pa.getDocument().getDocumentNumber() + "\\1");
176  }
177  if(po.getDateMode() == 0) {
178  nd.setDocumentDate(now);
179  nd.setReceiptDate(now);
180  } else if(po.getDateMode() == 2) {
181  nd.setDocumentDate(po.getDate());
182  nd.setReceiptDate(po.getDate());
183  }
184  if(po.getStoreMode() == 2) {
185  for(DocumentLine dl : nd.getDocumentLines()) {
186  dl.setStore(po.getStore());
187  }
188  }
189  if(!po.isAccumulated()) {
190  if(po.isAutosave()) {
191  new DocumentWrapper(nd).save(new ArrayList(nd.getDocumentLines()), null);
192  } else {
193  FinancialsMenu.showDocument(nd);
194  }
195  }
196  pa.getRow().detach();
197  }
198  if(po.isAccumulated()) {
199  if(po.isAutosave()) {
200  new DocumentWrapper(nd).save(new ArrayList(nd.getDocumentLines()), null);
201  } else {
202  FinancialsMenu.showDocument(nd);
203  }
204  }
205  return true;
206  }
207  return false;
208  }
209 
210  private Collection<Document> getDocuments(List<PortfolioAction> list) {
211  ArrayList<Document> docs = new ArrayList<>();
212  for(PortfolioAction pa : list) {
213  docs.add(pa.getDocument());
214  }
215  return docs;
216  }
217 
218 }