Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2129e7e8e54db4fc41ae487f90f6f38dc1849251 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*******************************************************************************
 * 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.attribute;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.exception.OseeTypeDoesNotExist;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.OseeEnumEntry;
import org.eclipse.osee.framework.core.model.cache.AbstractOseeCache;
import org.eclipse.osee.framework.core.model.cache.BranchCache;
import org.eclipse.osee.framework.core.model.type.ArtifactType;
import org.eclipse.osee.framework.core.model.type.AttributeType;
import org.eclipse.osee.framework.core.services.IOseeCachingService;
import org.eclipse.osee.framework.jdk.core.type.OseeArgumentException;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Conditions;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactTypeManager;
import org.eclipse.osee.framework.skynet.core.artifact.Attribute;
import org.eclipse.osee.framework.skynet.core.attribute.providers.IAttributeDataProvider;
import org.eclipse.osee.framework.skynet.core.internal.ServiceUtil;

/**
 * @author Ryan D. Brooks
 */
public class AttributeTypeManager {

   private static IOseeCachingService getCacheService() throws OseeCoreException {
      return ServiceUtil.getOseeCacheService();
   }

   public static AbstractOseeCache<AttributeType> getCache() throws OseeCoreException {
      return getCacheService().getAttributeTypeCache();
   }

   public static BranchCache getBranchCache() throws OseeCoreException {
      return getCacheService().getBranchCache();
   }

   public static Collection<IAttributeType> getValidAttributeTypes(IOseeBranch branchToken) throws OseeCoreException {
      Branch branch = getBranchCache().get(branchToken);
      Set<IAttributeType> attributeTypes = new HashSet<>(100);
      for (ArtifactType artifactType : ArtifactTypeManager.getAllTypes()) {
         attributeTypes.addAll(artifactType.getAttributeTypes(branch));
      }
      return attributeTypes;
   }

   public static Collection<AttributeType> getAllTypes() throws OseeCoreException {
      return getCache().getAll();
   }

   public static Collection<IAttributeType> getTaggableTypes() throws OseeCoreException {
      Collection<IAttributeType> taggableTypes = new ArrayList<>();
      for (AttributeType type : getAllTypes()) {
         if (type.isTaggable()) {
            taggableTypes.add(type);
         }
      }
      return taggableTypes;
   }

   public static Collection<IAttributeType> getSingleMultiplicityTypes() throws OseeCoreException {
      Collection<IAttributeType> types = new ArrayList<>();
      for (AttributeType type : getAllTypes()) {
         if (type.getMaxOccurrences() == 1) {
            types.add(type);
         }
      }
      return types;
   }

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

   /**
    * @return Returns the attribute type matching the guid
    * @param guid attribute type guid to match
    */
   public static AttributeType getTypeByGuid(Long guid) throws OseeCoreException {
      if (guid == null) {
         throw new OseeArgumentException("[%s] is not a valid guid", guid);
      }
      AttributeType attributeType = getCache().getByGuid(guid);
      if (attributeType == null) {
         getCacheService().reloadTypes();
         attributeType = getCache().getByGuid(guid);
         if (attributeType == null) {
            throw new OseeTypeDoesNotExist("Attribute Type [%s] is not available.", guid);
         }
      }
      return attributeType;
   }

   public static AttributeType getType(IAttributeType type) throws OseeCoreException {
      return getTypeByGuid(type.getGuid());
   }

   /**
    * @return the attribute type with the given name or throws an OseeTypeDoesNotExist if it does not exist.
    */
   public static AttributeType getType(String name) throws OseeCoreException {
      AttributeType attributeType = getCache().getUniqueByName(name);
      if (attributeType == null) {
         throw new OseeTypeDoesNotExist("Attribute Type with name [%s] does not exist.", name);
      }
      return attributeType;
   }

   private static Set<String> getEnumerationValues(AttributeType attributeType) throws OseeCoreException {
      return attributeType.getOseeEnumType().valuesAsOrderedStringSet();
   }

   public static Set<String> getEnumerationValues(IAttributeType attributeType) throws OseeCoreException {
      return getEnumerationValues(getType(attributeType));
   }

   public static Map<String, String> getEnumerationValueDescriptions(IAttributeType attributeType) throws OseeCoreException {
      Map<String, String> values = new HashMap<>();
      for (OseeEnumEntry entry : AttributeTypeManager.getType(attributeType).getOseeEnumType().values()) {
         values.put(entry.getName(), entry.getDescription());
      }
      return values;
   }

   public static int getMinOccurrences(IAttributeType attributeType) throws OseeCoreException {
      return getType(attributeType).getMinOccurrences();
   }

   public static int getMaxOccurrences(IAttributeType attributeType) throws OseeCoreException {
      return getType(attributeType).getMaxOccurrences();
   }

   public static Set<String> getEnumerationValues(String attributeName) throws OseeCoreException {
      AttributeType type = getType(attributeName);
      Conditions.checkNotNull(type, "Attribute Type");
      return getEnumerationValues(type);
   }

   @SuppressWarnings("rawtypes")
   public static boolean isBaseTypeCompatible(Class<? extends Attribute> baseType, IAttributeType attributeType) throws OseeCoreException {
      return baseType.isAssignableFrom(getAttributeBaseClass(attributeType));
   }

   public static Class<? extends Attribute<?>> getAttributeBaseClass(IAttributeType attributeType) throws OseeCoreException {
      return AttributeExtensionManager.getAttributeClassFor(getType(attributeType).getBaseAttributeTypeId());
   }

   public static Class<? extends IAttributeDataProvider> getAttributeProviderClass(AttributeType attributeType) throws OseeCoreException {
      return AttributeExtensionManager.getAttributeProviderClassFor(attributeType.getAttributeProviderId());
   }

   public static boolean checkIfRemovalAllowed(IAttributeType attributeType, Collection<? extends Artifact> artifacts) {
      boolean removalAllowed = false;
      if (getType(attributeType).getMinOccurrences() == 0) {
         removalAllowed = true;
      }
      // if there is any artifact that allows the type, then removal is not allowed
      boolean notAllowed = false;
      for (Artifact art : artifacts) {
         notAllowed = art.isAttributeTypeValid(attributeType);
         if (notAllowed) {
            break;
         }
      }

      return removalAllowed || !notAllowed;
   }
}

Back to the top