/** * Gets the generic relationship from the artifact (by type). * @param artifact the s-ramp artifact * @param relationshipType the relationship type * @return the Relationship or null if not found */ public static Relationship getGenericRelationship(BaseArtifactType artifact, String relationshipType) { for (Relationship relationship : artifact.getRelationship()) { if (relationship.getRelationshipType().equals(relationshipType)) { return relationship; } } return null; }
/** * Gets the generic relationship from the artifact (by type). * @param artifact the s-ramp artifact * @param relationshipType the relationship type * @return the Relationship or null if not found */ public static Relationship getGenericRelationship(BaseArtifactType artifact, String relationshipType) { for (Relationship relationship : artifact.getRelationship()) { if (relationship.getRelationshipType().equals(relationshipType)) { return relationship; } } return null; }
/** * Updates the generic artifact relationships. * @param artifact * @throws RepositoryException */ private void updateGenericRelationships(BaseArtifactType artifact) throws Exception { // Create/Update all the relationships included in the artifact Set<String> updatedRelationshipTypes = new HashSet<String>(); for (Relationship relationship : artifact.getRelationship()) { setRelationships(relationship.getRelationshipType(), -1, 0, null, true, relationship.getRelationshipTarget()); updatedRelationshipTypes.add(relationship.getRelationshipType()); } // Now remove any relationships that weren't just updated or created (the ones // not included on the artifact but that have existing JCR nodes). NodeIterator existingNodes = this.jcrNode.getNodes(); while (existingNodes.hasNext()) { Node node = existingNodes.nextNode(); // Only roemove generic relationships if (node.isNodeType("sramp:relationship") && node.hasProperty("sramp:generic") //$NON-NLS-1$ //$NON-NLS-2$ && node.getProperty("sramp:generic").getBoolean()) { //$NON-NLS-1$ String type = node.getProperty("sramp:relationshipType").getString(); //$NON-NLS-1$ // If this relationship type was *not* updated above, then remove it because // it's not included in the latest artifact meta-data if (!updatedRelationshipTypes.contains(type)) { node.remove(); } } } }
/** * Updates the generic artifact relationships. * @param artifact * @throws Exception */ private void updateGenericRelationships(BaseArtifactType artifact) throws Exception { for (Relationship relationship : artifact.getRelationship()) { if (relationship.getRelationshipTarget().size() > 0) { setRelationships(relationship.getRelationshipType(), RelationshipType.GENERIC, relationship.getRelationshipTarget(), relationship.getOtherAttributes()); } } }
/** * @see org.overlord.sramp.common.visitors.HierarchicalArtifactVisitorAdapter#visitBase(org.oasis_open.docs.s_ramp.ns.s_ramp_v1.BaseArtifactType) */ @Override protected void visitBase(BaseArtifactType artifact) { List<Relationship> relationships = artifact.getRelationship(); if (relationships != null) { for (Relationship relationship : relationships) { String type = relationship.getRelationshipType(); List<Target> targets = relationship.getRelationshipTarget(); visitRelationships(type, targets); } } super.visitBase(artifact); }
/** * @see HierarchicalArtifactVisitor#visitBase(org.oasis_open.docs.s_ramp.ns.s_ramp_v1.BaseArtifactType) */ @Override protected void visitBase(BaseArtifactType artifact) { // expansion String archiveUuid = artifact.getOtherAttributes().get(ArtificerConstants.ARTIFICER_EXPANDED_FROM_ARCHIVE_UUID_QNAME); if (StringUtils.isNotBlank(archiveUuid)) { visitRelationship("expandedFromArchive", archiveUuid, false); } List<Relationship> relationships = artifact.getRelationship(); if (relationships != null) { for (Relationship relationship : relationships) { String type = relationship.getRelationshipType(); List<Target> targets = relationship.getRelationshipTarget(); visitRelationships(type, targets, true); } } super.visitBase(artifact); }
/** * Adds a new generic {@link Relationship} to the artifact. * @param artifact * @param relationshipType * @param targetUUID * @return the created {@link Relationship} */ public static Relationship addGenericRelationship(BaseArtifactType artifact, String relationshipType, String targetUUID) { Relationship relationship = null; for (Relationship r : artifact.getRelationship()) { if (r.getRelationshipType().equals(relationshipType)) { relationship = r; break; } } if (relationship == null) { relationship = new Relationship(); relationship.setRelationshipType(relationshipType); artifact.getRelationship().add(relationship); } // TODO check for duplicates first? if (targetUUID != null) { Target target = new Target(); target.setValue(targetUUID); relationship.getRelationshipTarget().add(target); } return relationship; }
Relationship relationship = null; for (Relationship r : artifact.getRelationship()) { if (r.getRelationshipType().equals(relationshipType)) { relationship = r; break;
relationshipNames.add(relationship.getRelationshipType());
if (relationship.getRelationshipType().equals(relationshipType.relationshipType())) { relationshipFound = true; final List<Target> targets = relationship.getRelationshipTarget();
for (Relationship relationship : artifact.getRelationship()) { List<Target> targets = relationship.getRelationshipTarget(); printRelationship(relationship.getRelationshipType(), targets);
Assert.assertEquals("NoTargetRelationship", artifact.getRelationship().get(0).getRelationshipType()); //$NON-NLS-1$ Assert.assertEquals(Collections.EMPTY_LIST, artifact.getRelationship().get(0).getRelationshipTarget()); Relationship relationship = SrampModelUtils.getGenericRelationship(artifact, "NoTargetRelationship"); //$NON-NLS-1$ Assert.assertNotNull(relationship); Assert.assertEquals("NoTargetRelationship", relationship.getRelationshipType()); //$NON-NLS-1$ Assert.assertEquals(Collections.EMPTY_LIST, relationship.getRelationshipTarget()); relationship = SrampModelUtils.getGenericRelationship(artifact, "TargetedRelationship"); //$NON-NLS-1$ Assert.assertNotNull(relationship); Assert.assertEquals("TargetedRelationship", relationship.getRelationshipType()); //$NON-NLS-1$ Assert.assertEquals(1, relationship.getRelationshipTarget().size()); // has only one target Assert.assertEquals(uuid2, relationship.getRelationshipTarget().get(0).getValue()); relationship = SrampModelUtils.getGenericRelationship(artifact, "NoTargetRelationship"); //$NON-NLS-1$ Assert.assertNotNull(relationship); Assert.assertEquals("NoTargetRelationship", relationship.getRelationshipType()); //$NON-NLS-1$ Assert.assertEquals(Collections.EMPTY_LIST, relationship.getRelationshipTarget()); relationship = SrampModelUtils.getGenericRelationship(artifact, "TargetedRelationship"); //$NON-NLS-1$ Assert.assertNotNull(relationship); Assert.assertEquals("TargetedRelationship", relationship.getRelationshipType()); //$NON-NLS-1$ Assert.assertEquals(2, relationship.getRelationshipTarget().size()); Set<String> expected = new HashSet<String>();