diff options
2 files changed, 31 insertions, 0 deletions
diff --git a/plugins/org.eclipse.osee.framework.skynet.core.test/src/org/eclipse/osee/framework/skynet/core/test/cases/ArtifactQueryTestDemo.java b/plugins/org.eclipse.osee.framework.skynet.core.test/src/org/eclipse/osee/framework/skynet/core/test/cases/ArtifactQueryTestDemo.java index d93ad95a47e..b1dcd3f60ed 100644 --- a/plugins/org.eclipse.osee.framework.skynet.core.test/src/org/eclipse/osee/framework/skynet/core/test/cases/ArtifactQueryTestDemo.java +++ b/plugins/org.eclipse.osee.framework.skynet.core.test/src/org/eclipse/osee/framework/skynet/core/test/cases/ArtifactQueryTestDemo.java @@ -18,6 +18,8 @@ import org.eclipse.osee.framework.core.enums.CoreArtifactTypes; import org.eclipse.osee.framework.core.enums.DeletionFlag; import org.eclipse.osee.framework.core.exception.ArtifactDoesNotExist; import org.eclipse.osee.framework.core.exception.OseeCoreException; +import org.eclipse.osee.framework.core.model.Branch; +import org.eclipse.osee.framework.jdk.core.util.GUID; 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.BranchManager; @@ -92,4 +94,15 @@ public class ArtifactQueryTestDemo { } Assert.assertTrue("No artifacts on multiple branches found", pass); } + + @Test + public void testGetOrCreate() throws OseeCoreException { + String guid = GUID.create(); + Branch branch = BranchManager.createTopLevelBranch("test branch"); + Artifact artifact1 = ArtifactQuery.getOrCreate(guid, null, CoreArtifactTypes.GeneralData, branch); + Assert.assertNotNull(artifact1); + Artifact artifact2 = ArtifactQuery.getOrCreate(guid, null, CoreArtifactTypes.GeneralData, branch); + Assert.assertEquals(artifact1, artifact2); + BranchManager.deleteBranch(branch); + } }
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.framework.skynet.core/src/org/eclipse/osee/framework/skynet/core/artifact/search/ArtifactQuery.java b/plugins/org.eclipse.osee.framework.skynet.core/src/org/eclipse/osee/framework/skynet/core/artifact/search/ArtifactQuery.java index 53e7d0c8a3d..620ed6ac50e 100644 --- a/plugins/org.eclipse.osee.framework.skynet.core/src/org/eclipse/osee/framework/skynet/core/artifact/search/ArtifactQuery.java +++ b/plugins/org.eclipse.osee.framework.skynet.core/src/org/eclipse/osee/framework/skynet/core/artifact/search/ArtifactQuery.java @@ -33,6 +33,7 @@ import org.eclipse.osee.framework.core.exception.OseeArgumentException; import org.eclipse.osee.framework.core.exception.OseeCoreException; import org.eclipse.osee.framework.core.message.SearchOptions; import org.eclipse.osee.framework.core.message.SearchRequest; +import org.eclipse.osee.framework.core.model.Branch; import org.eclipse.osee.framework.core.model.TransactionRecord; import org.eclipse.osee.framework.core.model.event.IBasicGuidArtifact; import org.eclipse.osee.framework.core.model.type.ArtifactType; @@ -510,4 +511,21 @@ public class ArtifactQuery { OseeEventManager.kickLocalArtifactReloadEvent(query, reloadedArts); return reloadedArts; } + + public static Artifact getOrCreate(String guid, String hrid, IArtifactType type, IOseeBranch branch) throws OseeCoreException { + Artifact artifact = null; + try { + artifact = ArtifactQuery.getArtifactFromId(guid, branch); + } catch (ArtifactDoesNotExist ex) { + //do nothing since this is expected if the artifact does not exist + } + if (artifact == null) { + Branch fullBranch = BranchManager.getBranch(branch); + artifact = ArtifactTypeManager.addArtifact(type, fullBranch, guid, hrid); + } + if (artifact == null) { + throw new ArtifactDoesNotExist("Artifact of type [%s] does not exist on branch [%s]", type, branch); + } + return artifact; + } } |