BrightSide Workbench Full Report + Source Code
PortfolioSet.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.List;
23 import java.util.TreeSet;
24 import org.turro.action.IAcceptances;
25 import org.turro.action.Plugins;
26 import org.turro.elephant.db.WhereClause;
27 import org.turro.financials.db.FinancialsPU;
28 import org.turro.financials.entity.Document;
29 import org.turro.financials.entity.DocumentDefinition;
30 import org.turro.financials.entity.DocumentWorkflow;
31 import org.turro.jpa.Dao;
32 import org.turro.plugin.filter.IFilterValue;
33 import org.turro.zkoss.grid.GroupExtended;
34 import org.zkoss.zul.Grid;
35 import org.zkoss.zul.Group;
36 
41 public class PortfolioSet extends TreeSet<PortfolioItem> {
42 
43  private DocumentDefinition documentDefinition;
44  private long lastContract = 0, lastDocDef = 0;
45 
46  public void setDocumentDefinition(DocumentDefinition documentDefinition) {
47  this.documentDefinition = documentDefinition;
48  }
49 
50  public Collection<Document> getDocuments() {
51  ArrayList<Document> docs = new ArrayList<Document>();
52  for(PortfolioItem item : this) {
53  docs.add(item.getDocument());
54  }
55  return docs;
56  }
57 
58  public void load(RelationMode mode, List<IFilterValue> values) {
59  clear();
60  lastContract = 0;
61  lastDocDef = 0;
62  Dao dao = new FinancialsPU();
64  WhereClause wc = createCriteria(mode, values);
65  for(Object[] o : (List<Object[]>) dao.getResultList(wc)) {
66  if(acceptances.getStatusFor(FinancialsPU.getObjectPath(o[0])).isPass()) {
67  addDocument((Document) o[0], (DocumentWorkflow) o[1]);
68  }
69  }
70  }
71 
72  public void addDocument(Document document, DocumentWorkflow workflow) {
73  PortfolioItem item = getItem(document);
74  if(item == null) {
75  item = new PortfolioItem(document);
76  add(item);
77  }
78  if(workflow.getAncestor().getId() == document.getDocumentDefinition().getId()) {
79  item.addForward(workflow);
80  } else {
81  item.addBackward(workflow);
82  }
83  }
84 
85  public void checkGroup(Grid grid, Document document) {
86  if((lastContract == 0 || lastContract != document.getContract().getId()) ||
87  (lastDocDef == 0 || lastDocDef != document.getDocumentDefinition().getId())) {
88  lastContract = document.getContract().getId();
89  lastDocDef = document.getDocumentDefinition().getId();
90  Group group = new GroupExtended(
91  document.getContract().getFullDescription() +
92  " - " +
93  document.getDocumentDefinition().getName());
94  group.setOpen(true);
95  grid.getRows().appendChild(group);
96  }
97  }
98 
99  private WhereClause createCriteria(RelationMode mode, List<IFilterValue> values) {
100  WhereClause wc = new WhereClause();
101  wc.addClause("select doc, dw from Document as doc, DocumentWorkflow as dw");
102  wc.addClause("where 1=1");
103  for(IFilterValue v : values) {
104  if(!v.getLabel().equals("Document model")) {
105  v.addConstraint(wc);
106  }
107  }
108  wc.addClause("and doc.contract.contractDefinition = dw.contractDefinition");
109 
110  String sep = "";
111  wc.addClause("and (");
112  if(mode.equals(RelationMode.RELATE_FORWARD) || mode.equals(RelationMode.RELATE_BOTH)) {
113  wc.addClause(sep);
114  sep = "or";
115  wc.addClause("(");
116  wc.addClause("doc.documentDefinition = dw.ancestor");
117  for(IFilterValue v : values) {
118  if(v.getLabel().equals("Document model")) {
119  wc.getAttributes().put("mode", RelationMode.RELATE_FORWARD);
120  v.addConstraint(wc);
121  }
122  }
123  if(documentDefinition != null) {
124  wc.addClause("and dw.descendant = :docDefDes");
125  wc.addNamedValue("docDefDes", documentDefinition);
126  }
127  wc.addClause("and doc.noDescendants = FALSE");
128  wc.addClause("and not exists (");
129  wc.addClause("select dr from DocumentRelation as dr");
130  wc.addClause("where dr.ancestor = doc");
131  wc.addClause("))");
132  }
133  if(mode.equals(RelationMode.RELATE_BACKWARD) || mode.equals(RelationMode.RELATE_BOTH)) {
134  wc.addClause(sep);
135  wc.addClause("(");
136  wc.addClause("doc.documentDefinition = dw.descendant");
137  wc.addClause("and dw.allowNewDescendants = TRUE");
138  for(IFilterValue v : values) {
139  if(v.getLabel().equals("Document model")) {
140  wc.getAttributes().put("mode", RelationMode.RELATE_BACKWARD);
141  v.addConstraint(wc);
142  }
143  }
144  if(documentDefinition != null) {
145  wc.addClause("and dw.ancestor = :docDefAnc");
146  wc.addNamedValue("docDefAnc", documentDefinition);
147  }
148  wc.addClause("and doc.noAncestors = FALSE");
149  wc.addClause("and not exists (");
150  wc.addClause("select dr from DocumentRelation as dr");
151  wc.addClause("where dr.descendant = doc");
152  wc.addClause("))");
153  }
154  wc.addClause(")");
155 
156  wc.addClause("order by doc.contract.id, doc.documentDefinition.id, doc.documentDate");
157  return wc;
158  }
159 
160  private PortfolioItem getItem(Document document) {
161  for(PortfolioItem item : this) {
162  if(item.getDocument().getId() == document.getId()) {
163  return item;
164  }
165  }
166  return null;
167  }
168 
169 }
static< T > T loadImplementation(Class< T > jclass)
Definition: Plugins.java:57
Map< String, Object > getAttributes()
void addNamedValue(String name, Object value)
static String getObjectPath(Object object)
DocumentDefinition getDocumentDefinition()
Definition: Document.java:167
void checkGroup(Grid grid, Document document)
void load(RelationMode mode, List< IFilterValue > values)
void setDocumentDefinition(DocumentDefinition documentDefinition)
void addDocument(Document document, DocumentWorkflow workflow)
AcceptanceStatus getStatusFor(String path)