BrightSide Workbench Full Report + Source Code
RelationType.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 package org.turro.contacts.organigram;
19 
20 import java.util.Collection;
21 import java.util.EnumSet;
22 import org.turro.string.Strings;
23 import org.turro.contacts.BusinessRelation;
24 import org.turro.elephant.util.Images;
25 import org.turro.util.CompareUtil;
26 
31 public enum RelationType {
32 
33  REL_HEADQUARTERS(1, 1, true, "b_headquarters"),
34  REL_DEPARTMENT(1, 2, true, "b_department"),
35  REL_MANAGEMENT(1, 3, true, "management"),
36  REL_ADMINISTRATION(1, 3, true, "documentation"),
37  REL_SALES(1, 3, true, "sale"),
38  REL_PURCHASING(1, 3, true, "cash"),
39  REL_TECHNICAL(1, 3, true, "technology"),
40  REL_MAINTENANCE(1, 3, true, "operator"),
41  REL_PRODUCTION(1, 3, true, "service"),
42  REL_QUALITY(1, 3, true, "qa"),
43  REL_HHRR(1, 3, true, "contacts"),
44  REL_STAFF(1, 3, true, "contact");
45 
46  private final int group, level;
47  private final boolean allowJumps;
48  private final String image;
49 
50  private RelationType(int group, int level, boolean allowJumps, String image) {
51  this.group = group;
52  this.level = level;
53  this.allowJumps = allowJumps;
54  this.image = image;
55  }
56 
57  public String getRelationKey() {
58  return "$" + toString();
59  }
60 
61  public String getImage() {
62  return Images.getImage(image);
63  }
64 
65  public int getGroup() {
66  return group;
67  }
68 
69  public int getLevel() {
70  return level;
71  }
72 
73  public Collection<RelationType> getAllowedChildren() {
74  EnumSet<RelationType> srt = EnumSet.noneOf(RelationType.class);
75  for(RelationType rt : values()) {
76  if(isAllowed(rt)) {
77  srt.add(rt);
78  }
79  }
80  return srt;
81  }
82 
83  public boolean isAllowed(RelationType rt) {
84  if(rt != null && rt.group == group) {
85  if(allowJumps && rt.level > level) {
86  return true;
87  } else if(rt.level == (level + 1)) {
88  return true;
89  }
90  }
91  return false;
92  }
93 
94  public boolean isThisType(BusinessRelation r) {
95  return CompareUtil.compare(getRelationKey(), r.getRelationType()) == 0;
96  }
97 
98  public static RelationType getFromString(String relation) {
99  for(RelationType rt : values()) {
100  if(rt.getRelationKey().equals(relation)) {
101  return rt;
102  }
103  }
104  return null;
105  }
106 
107  public static String getRelationPrefix() {
108  return "$REL_";
109  }
110 
111  public static boolean matches(String relation) {
112  return !Strings.isBlank(relation) && relation.startsWith(getRelationPrefix());
113  }
114 
115  public static Collection<RelationType> getAllowedTops() {
116  EnumSet<RelationType> srt = EnumSet.noneOf(RelationType.class);
117  for(RelationType rt : values()) {
118  if(rt.allowJumps) {
119  srt.add(rt);
120  } else if(rt.level == 1) {
121  srt.add(rt);
122  }
123  }
124  return srt;
125  }
126 
127 }
static String getImage(String image)
Definition: Images.java:36
static boolean matches(String relation)
Collection< RelationType > getAllowedChildren()
static RelationType getFromString(String relation)
static Collection< RelationType > getAllowedTops()
boolean isThisType(BusinessRelation r)