Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5a6513cd1dacbed31a420ee602e151244856031e (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
/*******************************************************************************
 * Copyright (c) 2004, 2007 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.framework.skynet.core.relation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.data.IRelationType;
import org.eclipse.osee.framework.core.enums.RelationSide;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeTypeDoesNotExist;
import org.eclipse.osee.framework.core.model.cache.AbstractOseeCache;
import org.eclipse.osee.framework.core.model.type.ArtifactType;
import org.eclipse.osee.framework.core.model.type.RelationType;
import org.eclipse.osee.framework.skynet.core.internal.Activator;

/**
 * @author Ryan D. Brooks
 * @author Andrew M. Finkbeiner
 */
public class RelationTypeManager {

   public static AbstractOseeCache<RelationType> getCache() {
      return Activator.getInstance().getOseeCacheService().getRelationTypeCache();
   }

   public static List<RelationType> getValidTypes(ArtifactType artifactType, IOseeBranch branch) throws OseeCoreException {
      Collection<RelationType> relationTypes = getAllTypes();
      List<RelationType> validRelationTypes = new ArrayList<RelationType>();
      for (RelationType relationType : relationTypes) {
         int sideAMax = getRelationSideMax(relationType, artifactType, RelationSide.SIDE_A);
         int sideBMax = getRelationSideMax(relationType, artifactType, RelationSide.SIDE_B);
         boolean onSideA = sideBMax > 0;
         boolean onSideB = sideAMax > 0;
         if (onSideA || onSideB) {
            validRelationTypes.add(relationType);
         }
      }
      return validRelationTypes;
   }

   public static int getRelationSideMax(RelationType relationType, ArtifactType artifactType, RelationSide relationSide) throws OseeCoreException {
      int toReturn = 0;
      if (relationType.isArtifactTypeAllowed(relationSide, artifactType)) {
         toReturn = relationType.getMultiplicity().getLimit(relationSide);
      }
      return toReturn;
   }

   /**
    * @return all the relation types that are valid for the given branch
    */
   public static Collection<RelationType> getValidTypes(IOseeBranch branch) throws OseeCoreException {
      return getAllTypes();
   }

   /**
    * @return all Relation types
    */
   public static Collection<RelationType> getAllTypes() throws OseeCoreException {
      return getCache().getAll();
   }

   public static RelationType getType(int relationTypeId) throws OseeCoreException {
      RelationType relationType = getCache().getById(relationTypeId);
      if (relationType == null) {
         throw new OseeTypeDoesNotExist("The relation with type id[" + relationTypeId + "] does not exist");
      }
      return relationType;
   }

   public static RelationType getTypeByGuid(String guid) throws OseeCoreException {
      RelationType relationType = getCache().getByGuid(guid);
      if (relationType == null) {
         throw new OseeTypeDoesNotExist("The relation with type guid [%s] does not exist", guid);
      }
      return relationType;
   }

   public static RelationType getType(IRelationType relationType) throws OseeCoreException {
      return getCache().get(relationType);
   }

   public static int getTypeId(IRelationType relationType) throws OseeCoreException {
      return getCache().get(relationType).getId();
   }

   public static RelationType getType(String typeName) throws OseeCoreException {
      RelationType relationType = getCache().getUniqueByName(typeName);
      if (relationType == null) {
         throw new OseeTypeDoesNotExist("The relation type [%s] does not exist", typeName);
      }
      return relationType;
   }

   public static boolean typeExists(String name) throws OseeCoreException {
      return !getCache().getByName(name).isEmpty();
   }

   public static void persist() throws OseeCoreException {
      getCache().storeAllModified();
   }
}

Back to the top