Skip to main content
summaryrefslogtreecommitdiffstats
blob: daa1c390744b9767f726605a73c0caf22e580d78 (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
/*******************************************************************************
 * 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.ote.define.artifacts;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
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.model.Branch;
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.logging.OseeLog;
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.search.ArtifactQuery;
import org.eclipse.osee.ote.define.OteDefinePlugin;

/**
 * @author Roberto E. Escobar
 */
public class OteArtifactFetcher<T extends Artifact> {
   private final IArtifactType oteArtifactType;

   protected OteArtifactFetcher(IArtifactType oteArtifactType) {
      this.oteArtifactType = oteArtifactType;
   }

   /**
    * Creates a new Artifact in the specified branch
    */
   @SuppressWarnings("unchecked")
   public T getNewArtifact(IOseeBranch branch) throws OseeCoreException {
      checkForNull(branch);
      return (T) ArtifactTypeManager.addArtifact(oteArtifactType, branch);
   }

   /**
    * Retrieves a unique artifact matching input parameter
    * 
    * @param typeName Attribute type name
    * @param value attribute value to match
    * @param branch to search in
    * @return the unique artifact
    */
   @SuppressWarnings("unchecked")
   public T searchForUniqueArtifactMatching(IAttributeType attributeType, String attributeValue, IOseeBranch branch) throws OseeCoreException {
      Conditions.checkNotNull(attributeType, "attributeType");
      Conditions.checkNotNull(attributeValue, "attributeValue");
      Conditions.checkNotNull(branch, "branch");
      return (T) ArtifactQuery.getArtifactFromTypeAndAttribute(oteArtifactType, attributeType, attributeValue, branch);
   }

   /**
    * Returns all artifact instances found in branch matching the type <b>T</b>
    * 
    * @param branch to search in
    * @return artifact instances
    */
   @SuppressWarnings("unchecked")
   public Set<T> getAllArtifacts(IOseeBranch branch) throws OseeArgumentException {
      checkForNull(branch);
      Set<T> toReturn = new HashSet<>();
      try {
         Collection<Artifact> artifacts = ArtifactQuery.getArtifactListFromType(oteArtifactType, branch);
         for (Artifact artifact : artifacts) {
            toReturn.add((T) artifact);
         }
      } catch (OseeCoreException ex) {
         OseeLog.logf(OteDefinePlugin.class, Level.WARNING, ex, "Search for all artifacts failed [%s, %s]",
            oteArtifactType.getName(), branch.getName());
      }
      return toReturn;
   }

   /**
    * Returns all artifact instances found in branch matching the type <b>T</b> Results are indexed by artifact
    * descriptive name.
    * 
    * @param branch to search in
    * @return artifact instances indexed by descriptive name
    */
   public Map<String, T> getAllArtifactsIndexedByName(Branch branch) throws OseeArgumentException {
      Map<String, T> toReturn = new HashMap<>();
      Set<T> testScripts = getAllArtifacts(branch);
      for (T artifact : testScripts) {
         toReturn.put(artifact.getName(), artifact);
      }
      return toReturn;
   }

   private void checkForNull(Object object) throws OseeArgumentException {
      if (object == null) {
         throw new OseeArgumentException("Object was null");
      }
   }
}

Back to the top