Skip to main content
summaryrefslogtreecommitdiffstats
blob: e2864b1503c7f8507d1c22bd28bbac8c6b15b1d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*******************************************************************************
 * Copyright (c) 2013 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.orcs.core.internal.types.impl;

import java.util.Collection;
import org.eclipse.osee.framework.core.data.IArtifactType;
import org.eclipse.osee.framework.core.data.IRelationType;
import org.eclipse.osee.framework.core.dsl.oseeDsl.XRelationType;
import org.eclipse.osee.framework.core.enums.RelationOrderBaseTypes;
import org.eclipse.osee.framework.core.enums.RelationSide;
import org.eclipse.osee.framework.core.enums.RelationTypeMultiplicity;
import org.eclipse.osee.framework.core.exception.OseeArgumentException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.util.Conditions;
import org.eclipse.osee.orcs.data.RelationTypes;

/**
 * @author Roberto E. Escobar
 */
public class RelationTypesImpl implements RelationTypes {

   public static interface RelationTypeIndexProvider {
      RelationTypeIndex getRelationTypeIndex() throws OseeCoreException;
   }

   private final RelationTypeIndexProvider provider;

   public RelationTypesImpl(RelationTypeIndexProvider provider) {
      this.provider = provider;
   }

   private XRelationType getType(IRelationType type) throws OseeCoreException {
      Conditions.checkNotNull(type, "relationType");
      return provider.getRelationTypeIndex().getDslTypeByToken(type);
   }

   @Override
   public Collection<? extends IRelationType> getAll() throws OseeCoreException {
      return provider.getRelationTypeIndex().getAllTokens();
   }

   @Override
   public IRelationType getByUuid(Long uuid) throws OseeCoreException {
      Conditions.checkNotNull(uuid, "uuid");
      return provider.getRelationTypeIndex().getTokenByUuid(uuid);
   }

   @Override
   public RelationTypeMultiplicity getMultiplicity(IRelationType relation) throws OseeCoreException {
      XRelationType type = getType(relation);
      String multiplicityId = type.getMultiplicity().getName();
      RelationTypeMultiplicity multiplicity = RelationTypeMultiplicity.getFromString(multiplicityId);
      return multiplicity;
   }

   @Override
   public String getSideName(IRelationType relation, RelationSide relationSide) throws OseeCoreException {
      Conditions.checkNotNull(relationSide, "relationSide");
      return relationSide == RelationSide.SIDE_A ? getSideAName(relation) : getSideBName(relation);
   }

   @Override
   public String getSideAName(IRelationType relation) throws OseeCoreException {
      XRelationType type = getType(relation);
      return type.getSideAName();
   }

   @Override
   public String getSideBName(IRelationType relation) throws OseeCoreException {
      XRelationType type = getType(relation);
      return type.getSideBName();
   }

   @Override
   public boolean isSideAName(IRelationType relation, String sideName) throws OseeCoreException {
      XRelationType type = getType(relation);
      boolean isSideA = type.getSideAName().equals(sideName);
      if (!isSideA && !type.getSideBName().equals(sideName)) {
         throw new OseeArgumentException("sideName does not match either of the available side names");
      }
      return isSideA;
   }

   @Override
   public boolean isOrdered(IRelationType relation) {
      String defaultOrderTypeGuid = null;
      try {
         defaultOrderTypeGuid = getDefaultOrderTypeGuid(relation);
      } catch (OseeCoreException ex) {
         // Do nothing
      }
      return !RelationOrderBaseTypes.UNORDERED.getGuid().equals(defaultOrderTypeGuid);
   }

   @Override
   public String getDefaultOrderTypeGuid(IRelationType relation) throws OseeCoreException {
      XRelationType type = getType(relation);
      return orderTypeNameToGuid(type.getDefaultOrderType());
   }

   @Override
   public IArtifactType getArtifactTypeSideA(IRelationType relation) throws OseeCoreException {
      return getArtifactType(relation, RelationSide.SIDE_A);
   }

   @Override
   public IArtifactType getArtifactTypeSideB(IRelationType relation) throws OseeCoreException {
      return getArtifactType(relation, RelationSide.SIDE_B);
   }

   @Override
   public IArtifactType getArtifactType(IRelationType relation, RelationSide relationSide) throws OseeCoreException {
      Conditions.checkNotNull(relation, "relationType");
      Conditions.checkNotNull(relationSide, "relationSide");
      return provider.getRelationTypeIndex().getArtifactType(relation, relationSide);
   }

   @Override
   public boolean isArtifactTypeAllowed(IRelationType relation, RelationSide relationSide, IArtifactType artifactType) throws OseeCoreException {
      Conditions.checkNotNull(relation, "relationType");
      Conditions.checkNotNull(relationSide, "relationSide");
      Conditions.checkNotNull(artifactType, "artifactType");
      return provider.getRelationTypeIndex().isArtifactTypeAllowed(relation, relationSide, artifactType);
   }

   private static String orderTypeNameToGuid(String orderTypeName) throws OseeCoreException {
      Conditions.checkNotNull(orderTypeName, "orderTypeName");
      String orderType = orderTypeName.replaceAll("_", " ");
      return RelationOrderBaseTypes.getFromOrderTypeName(orderType).getGuid();
   }

   @Override
   public boolean isEmpty() throws OseeCoreException {
      return provider.getRelationTypeIndex().isEmpty();
   }

   @Override
   public int size() throws OseeCoreException {
      return provider.getRelationTypeIndex().size();
   }

   @Override
   public boolean exists(IRelationType item) throws OseeCoreException {
      return provider.getRelationTypeIndex().existsByUuid(item.getGuid());
   }
}

Back to the top