19 package org.turro.contacts.organigram;
21 import java.util.ArrayList;
22 import java.util.HashSet;
23 import java.util.List;
25 import java.util.stream.Collectors;
26 import org.turro.auth.Authentication;
27 import org.turro.contacts.Contact;
28 import org.turro.i18n.I_;
29 import org.turro.plugin.contacts.ContactRelations;
30 import org.turro.plugin.contacts.IContact;
31 import org.turro.plugin.contacts.IContactRelation;
32 import org.zkoss.bind.annotation.ExecutionArgParam;
33 import org.zkoss.bind.annotation.Init;
34 import org.zkoss.zk.ui.Executions;
35 import org.zkoss.zul.DefaultTreeModel;
36 import org.zkoss.zul.DefaultTreeNode;
37 import org.zkoss.zul.TreeModel;
38 import org.zkoss.zul.TreeNode;
51 this.contact = contact;
53 Object attrContact = Executions.getCurrent().
getAttribute(
"contact");
54 if(attrContact instanceof
Contact) {
55 this.contact = ((
Contact) attrContact).getIContact();
56 }
else if(attrContact instanceof
IContact) {
57 this.contact = (
IContact) attrContact;
67 if(contact ==
null)
return new DefaultTreeModel(
null);
68 return new DefaultTreeModel(
new DefaultTreeNode(
69 I_.
get(
"Organigram"), initTreeModel(),
true));
72 private List<TreeNode> initTreeModel() {
73 Set<String> roots =
new HashSet<>();
74 gatherRoots(contact.
getId(), roots);
75 List<TreeNode> relRoots =
new ArrayList<>();
76 addRoots(relRoots, roots);
80 private void gatherRoots(String idContact, Set<String> roots) {
81 Set<IContactRelation> rels = ContactRelations.getBusiness(idContact, ContactRelations.MODE_ALL)
82 .stream().filter(r -> r.asWorker()).collect(Collectors.toSet());
87 rels.stream().filter((rc) -> !rc.getRelatedId().equals(idContact)).forEach((rel) -> {
88 gatherRoots(rel.getRelatedId(), roots);
93 private void addRoots(List<TreeNode> relRoots, Set<String> roots) {
94 for(String root : roots) {
95 relRoots.add(
new DefaultTreeNode(emptyRootRelation(root), getRelations(root)));
99 private List<TreeNode> getRelations(String idRoot) {
100 List<TreeNode> nodes =
new ArrayList<>();
101 ContactRelations.getWorkers(idRoot, ContactRelations.MODE_ALL)
103 List<TreeNode> rels = getRelations(rel.getRelatedId());
105 nodes.add(
new DefaultTreeNode(rel));
107 nodes.add(
new DefaultTreeNode(rel, rels));
110 return nodes.isEmpty() ? null : nodes;
113 private IContactRelation emptyRootRelation(String idRoot) {
114 return new RootRelation(idRoot);
static IContact getIContact()
static String get(String msg)