BrightSide Workbench Full Report + Source Code
OrganigramVM.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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.contacts.organigram;
20 
21 import java.util.ArrayList;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
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;
39 
44 public class OrganigramVM {
45 
46  private IContact contact = Authentication.getIContact();
47 
48  @Init
49  public void init(@ExecutionArgParam("contact") IContact contact) {
50  if(contact != null) {
51  this.contact = contact;
52  } else {
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;
58  }
59  }
60  }
61 
62  public IContact getContact() {
63  return contact;
64  }
65 
66  public TreeModel getModel() {
67  if(contact == null) return new DefaultTreeModel(null);
68  return new DefaultTreeModel(new DefaultTreeNode(
69  I_.get("Organigram"), initTreeModel(), true));
70  }
71 
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);
77  return relRoots;
78  }
79 
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());
83  if(rels.isEmpty()) {
84  roots.add(idContact);
85  } else {
86  // check not itself
87  rels.stream().filter((rc) -> !rc.getRelatedId().equals(idContact)).forEach((rel) -> {
88  gatherRoots(rel.getRelatedId(), roots);
89  });
90  }
91  }
92 
93  private void addRoots(List<TreeNode> relRoots, Set<String> roots) {
94  for(String root : roots) {
95  relRoots.add(new DefaultTreeNode(emptyRootRelation(root), getRelations(root)));
96  }
97  }
98 
99  private List<TreeNode> getRelations(String idRoot) {
100  List<TreeNode> nodes = new ArrayList<>();
101  ContactRelations.getWorkers(idRoot, ContactRelations.MODE_ALL)
102  .forEach((rel) -> {
103  List<TreeNode> rels = getRelations(rel.getRelatedId());
104  if(rels == null) {
105  nodes.add(new DefaultTreeNode(rel));
106  } else {
107  nodes.add(new DefaultTreeNode(rel, rels));
108  }
109  });
110  return nodes.isEmpty() ? null : nodes;
111  }
112 
113  private IContactRelation emptyRootRelation(String idRoot) {
114  return new RootRelation(idRoot);
115  }
116 
117 }
void init(@ExecutionArgParam("contact") IContact contact)
static String get(String msg)
Definition: I_.java:41