summaryrefslogtreecommitdiffstats
blob: ef50949330ec847db88cc0b2a19c9d9d65eb1246 (plain)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*******************************************************************************
 * 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.artifact;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.osee.framework.core.data.IArtifactToken;
import org.eclipse.osee.framework.core.data.IArtifactType;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.enums.DeletionFlag;
import org.eclipse.osee.framework.core.exception.OseeArgumentException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeStateException;
import org.eclipse.osee.framework.core.exception.OseeTypeDoesNotExist;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.cache.ArtifactTypeCache;
import org.eclipse.osee.framework.core.model.event.DefaultBasicGuidArtifact;
import org.eclipse.osee.framework.core.model.type.ArtifactType;
import org.eclipse.osee.framework.core.services.IOseeCachingService;
import org.eclipse.osee.framework.database.core.ConnectionHandler;
import org.eclipse.osee.framework.skynet.core.artifact.factory.ArtifactFactoryManager;
import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery;
import org.eclipse.osee.framework.skynet.core.internal.ServiceUtil;

/**
 * Contains methods specific to artifact types. All artifact methods will eventually be moved from the
 * ConfigurationPersistenceManager to here.
 * 
 * @author Donald G. Dunne
 */
public class ArtifactTypeManager {

   private final static ArtifactFactoryManager factoryManager = new ArtifactFactoryManager();

   private static ArtifactTypeCache getCache() throws OseeCoreException {
      return getCacheService().getArtifactTypeCache();
   }

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

   public static Collection<ArtifactType> getArtifactTypesFromAttributeType(IAttributeType attributeType, IOseeBranch branchToken) throws OseeCoreException {
      Branch branch = getCacheService().getBranchCache().get(branchToken);
      List<ArtifactType> artifactTypes = new ArrayList<ArtifactType>();
      for (ArtifactType artifactType : getAllTypes()) {
         if (artifactType.isValidAttributeType(attributeType, branch)) {
            artifactTypes.add(artifactType);
         }
      }
      return artifactTypes;
   }

   public static Collection<ArtifactType> getValidArtifactTypes(IOseeBranch branch) throws OseeCoreException {
      // TODO Filter artifact types by branch
      return getAllTypes();
   }

   public static Collection<ArtifactType> getConcreteArtifactTypes(IOseeBranch branch) throws OseeCoreException {
      Collection<ArtifactType> types = getAllTypes();
      Iterator<ArtifactType> iterator = types.iterator();
      while (iterator.hasNext()) {
         ArtifactType type = iterator.next();
         if (type.isAbstract()) {
            iterator.remove();
         }
      }
      return types;
   }

   /**
    * @return Returns all of the descriptors.
    */
   public static Collection<ArtifactType> getAllTypes() throws OseeCoreException {
      return getCache().getAll();
   }

   public static ArtifactType getType(DefaultBasicGuidArtifact guidArt) throws OseeCoreException {
      return getTypeByGuid(guidArt.getArtTypeGuid());
   }

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

   /**
    * @return Returns the artifact type matching the name
    * @param name artifact type name to match
    */
   public static ArtifactType getType(String name) throws OseeCoreException {
      ArtifactType artifactType = getCache().getUniqueByName(name);
      if (artifactType == null) {
         throw new OseeTypeDoesNotExist("Artifact type [%s] is not available.", name);
      }
      return artifactType;
   }

   public static ArtifactType getType(IArtifactType artifactType) throws OseeCoreException {
      return getTypeByGuid(artifactType.getGuid());
   }

   /**
    * @return Returns the descriptor with a particular name, null if it does not exist.
    */
   public static ArtifactType getType(int artTypeId) throws OseeCoreException {
      ArtifactType artifactType = getCache().getById(artTypeId);
      if (artifactType == null) {
         throw new OseeTypeDoesNotExist("Atrifact type: %d is not available.", artTypeId);
      }
      return artifactType;
   }

   public static boolean inheritsFrom(IArtifactType artifactType, IArtifactType... parentTypes) throws OseeCoreException {
      return getType(artifactType).inheritsFrom(parentTypes);
   }

   /**
    * Get a new instance of type artifactTypeName
    */
   public static Artifact addArtifact(IArtifactType artifactType, IOseeBranch branch) throws OseeCoreException {
      return getFactory(artifactType).makeNewArtifact(branch, artifactType, null, null, null);
   }

   /**
    * Get a new instance of type artifactTypeName and set it's name.
    */
   public static Artifact addArtifact(IArtifactType artifactType, IOseeBranch branch, String name) throws OseeCoreException {
      Artifact artifact = addArtifact(artifactType, branch);
      artifact.setName(name);
      return artifact;
   }

   /**
    * Get a new instance of the type of artifact. This is just a convenience method that calls makeNewArtifact on the
    * known factory with this descriptor for the descriptor parameter, and the supplied branch.
    * 
    * @see ArtifactFactory#makeNewArtifact(Branch, IArtifactType, String, String, ArtifactProcessor)
    */
   public static Artifact addArtifact(IArtifactType artifactType, IOseeBranch branch, String guid, String humandReadableId) throws OseeCoreException {
      return getFactory(artifactType).makeNewArtifact(branch, artifactType, guid, humandReadableId);
   }

   public static Artifact addArtifact(IArtifactType artifactType, IOseeBranch branch, String name, String guid, String humandReadableId) throws OseeCoreException {
      return getFactory(artifactType).makeNewArtifact(branch, artifactType, name, guid, humandReadableId);
   }

   public static Artifact addArtifact(IArtifactToken artifactToken, IOseeBranch branch) throws OseeCoreException {
      return getFactory(artifactToken.getArtifactType()).makeNewArtifact(branch, artifactToken.getArtifactType(),
         artifactToken.getName(), artifactToken.getGuid(), null);
   }

   private static final String COUNT_ARTIFACT_OCCURRENCE =
      "select count(1) from (select DISTINCT(art_id) FROM osee_artifact where art_type_id = ?) t1";

   public static void purgeArtifactType(IArtifactType artifactType) throws OseeCoreException {
      final int artifactTypeId = ServiceUtil.getIdentityService().getLocalId(artifactType);
      int artifactCount = ConnectionHandler.runPreparedQueryFetchInt(0, COUNT_ARTIFACT_OCCURRENCE, artifactTypeId);

      if (artifactCount != 0) {
         throw new OseeArgumentException(
            "Can not delete artifact type [%s] because there are %d existing artifacts of this type.", artifactType,
            artifactCount);
      }
   }

   /**
    * Given a set of artifact types, they will be converted to the new artifact type and the old artifact types will be
    * purged
    * 
    * @param purgeArtifactTypes types to be converted and purged
    * @param newArtifactType new type to convert any existing artifacts of the old type
    */
   public static void purgeArtifactTypesWithCheck(Collection<? extends IArtifactType> purgeArtifactTypes, IArtifactType newArtifactType) throws CoreException {
      for (IArtifactType purgeArtifactType : purgeArtifactTypes) {
         // find all artifact of this type on all branches and make a unique list for type change (since it is not by branch)
         List<Artifact> artifacts =
            ArtifactQuery.getArtifactListFromType(purgeArtifactType, DeletionFlag.INCLUDE_DELETED);
         if (artifacts.size() > 0) {
            HashMap<Integer, Artifact> artifactMap = new HashMap<Integer, Artifact>();
            for (Artifact artifact : artifacts) {
               artifactMap.put(artifact.getArtId(), artifact);
            }
            if (newArtifactType == null) {
               HashSet<IOseeBranch> branches = new HashSet<IOseeBranch>();
               for (Artifact artifact : artifacts) {
                  branches.add(artifact.getBranch());
               }
               throw new OseeStateException("Found %d artifact references of type [%s] on branches [%s]",
                  artifacts.size(), purgeArtifactType, branches);
            } else {
               ChangeArtifactType.changeArtifactType(artifactMap.values(), newArtifactType);
            }
         }
         purgeArtifactType(purgeArtifactType);
      }
   }

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

   /**
    * @return Returns the ArtifactType factory.
    */
   public static ArtifactFactory getFactory(IArtifactType artifactType) throws OseeCoreException {
      if (artifactType == null) {
         throw new OseeArgumentException("Artifact Type cannot be null");
      }
      return factoryManager.getFactory(artifactType);
   }
}