BrightSide Workbench Full Report + Source Code
CompanyTree.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 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.business;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Set;
24 import org.turro.financials.entity.Company;
25 import org.turro.financials.entity.Contract;
26 import org.turro.financials.entity.Department;
27 import org.turro.financials.entity.Headquarters;
28 import org.turro.financials.entity.Service;
29 import org.turro.financials.model.business.CompanyWrapper;
30 import org.turro.i18n.I_;
31 import org.zkoss.zk.ui.ext.AfterCompose;
32 import org.zkoss.zul.Tree;
33 import org.zkoss.zul.Treechildren;
34 import org.zkoss.zul.Treeitem;
35 
40 public class CompanyTree extends Tree implements AfterCompose {
41 
42  public CompanyTree() {
43  super();
44  }
45 
46  @Override
47  public void afterCompose() {
48  appendChild(new Treechildren());
50  Treeitem li = new Treeitem(company.getName(), company);
51  li.appendChild(new Treechildren());
52  getTreechildren().appendChild(li);
53  fillServices(li);
54  fillHeadquarters(li);
55  }
56 
57  public List<Contract> getContracts() {
58  List<Contract> contracts = new ArrayList<>();
59  processItem(getSelectedItem(), contracts);
60  return contracts;
61  }
62 
63  private void processItem(Treeitem selectedItem, List<Contract> contracts) {
64  if(selectedItem != null) {
65  Object value = selectedItem.getValue();
66  if(value instanceof Contract) {
67  contracts.add((Contract) value);
68  } else if(selectedItem.getTreechildren() != null) {
69  for(Treeitem ti : selectedItem.getTreechildren().getItems()) {
70  processItem(ti, contracts);
71  }
72  }
73  }
74  }
75 
76  private void fillServices(Treeitem root) {
77  Company company = root.getValue();
78  Treeitem li = new Treeitem(I_.get("Services"));
79  li.appendChild(new Treechildren());
80  root.getTreechildren().appendChild(li);
81  for(Service service : company.getServices()) {
82  Treeitem lis = new Treeitem(service.getName(), service);
83  lis.setTooltiptext(lis.getLabel());
84  li.getTreechildren().appendChild(lis);
85  fillContracts(lis, service.getStores());
86  }
87  }
88 
89  private void fillHeadquarters(Treeitem root) {
90  Company company = root.getValue();
91  Treeitem li = new Treeitem(I_.get("Headquarters"));
92  li.appendChild(new Treechildren());
93  root.getTreechildren().appendChild(li);
94  for(Headquarters headquarters : company.getHeadquarters()) {
95  Treeitem lis = new Treeitem(headquarters.getName(), headquarters);
96  lis.setTooltiptext(lis.getLabel());
97  li.getTreechildren().appendChild(lis);
98  fillDepartments(lis, headquarters.getDepartments());
99  }
100  }
101 
102  private void fillDepartments(Treeitem root, Set<Department> departments) {
103  root.appendChild(new Treechildren());
104  Treeitem li = new Treeitem(I_.get("Departments"));
105  li.appendChild(new Treechildren());
106  root.getTreechildren().appendChild(li);
107  for(Department department : departments) {
108  Treeitem lis = new Treeitem(department.getName(), department);
109  lis.setTooltiptext(lis.getLabel());
110  li.getTreechildren().appendChild(lis);
111  fillContracts(lis, department.getStores());
112  }
113  }
114 
115  private void fillContracts(Treeitem root, Set<Contract> stores) {
116  root.appendChild(new Treechildren());
117  for(Contract contract : stores) {
118  Treeitem lis = new Treeitem(contract.getPartialDescription(), contract);
119  lis.setTooltiptext(lis.getLabel());
120  root.getTreechildren().appendChild(lis);
121  }
122  }
123 
124 }