Skip to main content
summaryrefslogtreecommitdiffstats
blob: 20b71fb1fac8a192a695fb6cf65b7e89c8025a0a (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
/*******************************************************************************
 * Copyright (c) 2012 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.define.traceability;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.enums.CoreRelationTypes;
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.OseeSystemArtifacts;
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.framework.skynet.core.transaction.SkynetTransaction;
import org.eclipse.osee.framework.skynet.core.utility.Requirements;

/**
 * @author Roberto E. Escobar
 */
public final class HierarchyHandler {
   private static final Matcher subsystemMatcher = Pattern.compile("(\\w*)\\.ss").matcher("");
   private final Map<String, Artifact> folderNameToArtifact = new HashMap<>(50);
   private final SkynetTransaction transaction;
   private final IOseeBranch branch;
   private Artifact root;

   public HierarchyHandler(SkynetTransaction transaction) {
      this.transaction = transaction;
      this.branch = transaction.getBranch();
   }

   public void addArtifact(Artifact testUnit) throws OseeCoreException {
      Conditions.checkExpressionFailOnTrue(!testUnit.isOnBranch(branch), "Artifact [%s] must be on branch [%s]",
         testUnit.toString(), branch.toString());
      Artifact folder = null;

      if (testUnit.isOfType(CoreArtifactTypes.TestCase)) {
         folder = getOrCreateTestCaseFolder();
      } else if (testUnit.isOfType(CoreArtifactTypes.TestSupport)) {
         folder = getOrCreateTestSupportFolder();
      } else if (testUnit.isOfType(CoreArtifactTypes.CodeUnit)) {
         folder = getOrCreateCodeUnitFolder(testUnit.getName());
      } else {
         folder = getOrCreateUnknownTestUnitFolder();
      }

      addChildIfNotRelated(folder, testUnit);
   }

   private Artifact getOrCreateUnknownTestUnitFolder() throws OseeCoreException {
      return getOrCreateTestUnitsFolder("Unknown Test Unit Type", true);
   }

   private Artifact getOrCreateTestSupportFolder() throws OseeCoreException {
      return getOrCreateTestUnitsFolder(Requirements.TEST_SUPPORT_UNITS, true);
   }

   private Artifact getOrCreateTestCaseFolder() throws OseeCoreException {
      return getOrCreateTestUnitsFolder("Test Cases", true);
   }

   private Artifact getRoot() {
      if (root == null) {
         root = OseeSystemArtifacts.getDefaultHierarchyRootArtifact(branch);
      }
      return root;
   }

   private Artifact getOrCreateCodeUnitFolder(String codeUnitName) throws OseeCoreException {
      Artifact root = getRoot();
      Artifact toReturn = getOrCreateFolder("Code Units", root);

      String subSystem;
      subsystemMatcher.reset(codeUnitName);
      if (subsystemMatcher.find()) {
         subSystem = subsystemMatcher.group(1);
         subSystem = subSystem.toUpperCase();
         toReturn = getOrCreateFolder(subSystem, toReturn);
      }

      return toReturn;
   }

   private Artifact getOrCreateTestUnitsFolder(String subfolderName, boolean includesSubfolder) throws OseeCoreException {
      Artifact root = getRoot();
      Artifact testFolder = getOrCreateFolder("Test", root);
      Artifact testUnitFolder = getOrCreateFolder("Test Units", testFolder);

      if (subfolderName != null && includesSubfolder) {
         Artifact subFolder = getOrCreateFolder(subfolderName, testUnitFolder);
         return subFolder;
      }
      return testUnitFolder;
   }

   private void persistHelper(Artifact toPersist) throws OseeCoreException {
      if (transaction != null) {
         toPersist.persist(transaction);
      }
   }

   private void addChildIfNotRelated(Artifact parentFolder, Artifact childFolder) {
      boolean related = parentFolder.isRelated(CoreRelationTypes.Default_Hierarchical__Child, childFolder);
      if (!related) {
         parentFolder.addChild(childFolder);
         persistHelper(parentFolder);
      }
   }

   private Artifact getOrCreateFolder(String folderName, Artifact parentFolder) throws OseeCoreException {
      Artifact toReturn = folderNameToArtifact.get(folderName);
      if (toReturn == null) {
         List<Artifact> relatedFolders =
            ArtifactQuery.getArtifactListFromTypeAndName(CoreArtifactTypes.Folder, folderName, branch);
         if (relatedFolders.size() == 1) {
            toReturn = relatedFolders.iterator().next();
         } else if (relatedFolders.size() > 1) {
            for (Artifact folder : relatedFolders) {
               if (parentFolder.isRelated(CoreRelationTypes.Default_Hierarchical__Child, folder)) {
                  toReturn = folder;
                  break;
               }
            }
         }
         if (toReturn == null) {
            toReturn = ArtifactTypeManager.addArtifact(CoreArtifactTypes.Folder, branch, folderName);
            parentFolder.addChild(toReturn);
            toReturn.persist(transaction);
         }
         folderNameToArtifact.put(folderName, toReturn);
      }
      return toReturn;
   }
}

Back to the top