BrightSide Workbench Full Report + Source Code
RelationshipViewer.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2012 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.relation;
20 
21 import java.util.List;
22 import org.turro.financials.entity.Document;
23 import org.turro.financials.entity.DocumentDefinition;
24 import org.turro.financials.entity.DocumentRelation;
25 import org.turro.financials.entity.DocumentWorkflow;
26 import org.turro.relationship.Graph;
27 import org.turro.relationship.Node;
28 import org.turro.relationship.Relationship;
29 import org.zkoss.zk.ui.event.Event;
30 import org.zkoss.zk.ui.event.Events;
31 import org.zkoss.zul.Hbox;
32 
37 public class RelationshipViewer extends Hbox {
38 
39  private Document document;
40  private Graph graph;
41 
42  public Document getDocument() {
43  return document;
44  }
45 
46  public void setDocument(Document document) {
47  this.document = document;
48  }
49 
50  public void notifyChange() {
51  Events.postEvent(new Event(Events.ON_CHANGE, this));
52  updateGraph();
53  }
54 
55  public void updateGraph() {
56  getChildren().clear();
57  if(document != null && document.getContract() != null) {
59  appendChild(new RelationshipColumn(dd, getRelationMode(dd)));
60  }
61  }
62  generateGraph();
63  }
64 
65  private void generateGraph() {
66  graph = new Graph();
67  Node center = new Node("/document/" + document.getId(), document);
68  addToGraph(center);
69  addDescendants(document);
70  addAncestors(document);
71  graph.generateGraph();
72  }
73 
74  private void addDescendants(Document document) {
75  for(DocumentRelation dr : document.getDescendants()) {
76  Node start = new Node("/document/" + dr.getAncestor().getId(), dr.getAncestor()),
77  end = new Node("/document/" + dr.getDescendant().getId(), dr.getDescendant());
78  Relationship relationship = new Relationship("/document-relation/" + dr.getId(), dr, start, end);
79  if(graph.addRelationship(relationship)) {
80  addToGraph(start);
81  addToGraph(end);
82  addDescendants(dr.getDescendant());
83  addAncestors(dr.getDescendant());
84  }
85  }
86  }
87 
88  private void addAncestors(Document document) {
89  for(DocumentRelation dr : document.getAncestors()) {
90  Node start = new Node("/document/" + dr.getAncestor().getId(), dr.getAncestor()),
91  end = new Node("/document/" + dr.getDescendant().getId(), dr.getDescendant());
92  Relationship relationship = new Relationship("/document-relation/" + dr.getId(), dr, start, end);
93  if(graph.addRelationship(relationship)) {
94  addToGraph(start);
95  addToGraph(end);
96  addDescendants(dr.getAncestor());
97  addAncestors(dr.getAncestor());
98  }
99  }
100  }
101 
102  private void addToGraph(Node node) {
103  Document doc = (Document) node.getData();
104  if(doc.isBlank()) return;
105  if(graph.addNode(node)) {
106  List l = getChildren();
107  for(int i = 0; i < l.size(); i++) {
108  RelationshipColumn rc = (RelationshipColumn) l.get(i);
109  if(rc.getDocumentDefinition().getId() == doc.getDocumentDefinition().getId()) {
110  rc.appendChild(new DocumentBox(doc));
111  node.setX(i);
112  break;
113  }
114  }
115  }
116  }
117 
118  private DocumentRelationMode getRelationMode(DocumentDefinition docDef) {
119  for(DocumentWorkflow dw : document.getContract().getContractDefinition().getDocumentWorkflows()) {
120  if(dw.getAncestor().getId() == docDef.getId() &&
121  dw.getDescendant().getId() == document.getDocumentDefinition().getId()) {
122  return DocumentRelationMode.RELATE_TO_ANCESTORS;
123  } else if(dw.getDescendant().getId() == docDef.getId() &&
124  dw.getAncestor().getId() == document.getDocumentDefinition().getId()) {
125  return DocumentRelationMode.RELATE_TO_DESCENDANTS;
126  }
127  }
128  return DocumentRelationMode.RELATE_TO_NONE;
129  }
130 
131 }
ContractDefinition getContractDefinition()
Definition: Contract.java:125