| author | David W. Miller | 2012-05-03 16:19:23 (EDT) |
|---|---|---|
| committer | Roberto E. Escobar | 2012-05-03 16:19:23 (EDT) |
| commit | f5af88baf1b4d20701f5074fe2cc4e2321c14da3 (patch) (side-by-side diff) | |
| tree | 6bffe6c8f6ff527679e3cec6e6aeafd294bb6801 | |
| parent | edebd23b8302281b68635625f6f45969886ed133 (diff) | |
| download | org.eclipse.osee-f5af88baf1b4d20701f5074fe2cc4e2321c14da3.zip org.eclipse.osee-f5af88baf1b4d20701f5074fe2cc4e2321c14da3.tar.gz org.eclipse.osee-f5af88baf1b4d20701f5074fe2cc4e2321c14da3.tar.bz2 | |
refinement: Add getSoleAttributeValue method to server side Artifact
2 files changed, 33 insertions, 1 deletions
diff --git a/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/artifact/Artifact.java b/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/artifact/Artifact.java index 048bb55..1a64570 100644 --- a/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/artifact/Artifact.java +++ b/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/artifact/Artifact.java @@ -19,6 +19,9 @@ import org.eclipse.osee.framework.core.data.IRelationTypeSide; import org.eclipse.osee.framework.core.data.NamedIdentity; import org.eclipse.osee.framework.core.enums.CoreAttributeTypes; import org.eclipse.osee.framework.core.enums.ModificationType; +import org.eclipse.osee.framework.core.exception.AttributeDoesNotExist; +import org.eclipse.osee.framework.core.exception.MultipleAttributesExist; +import org.eclipse.osee.framework.core.exception.OseeArgumentException; import org.eclipse.osee.framework.core.exception.OseeCoreException; import org.eclipse.osee.framework.core.model.Branch; import org.eclipse.osee.framework.core.model.cache.RelationTypeCache; @@ -152,7 +155,34 @@ public class Artifact extends NamedIdentity<String> implements ReadableArtifact @Override public String getSoleAttributeAsString(IAttributeType attributeType) throws OseeCoreException { - return String.valueOf(attributeContainer.getAttributes(attributeType).iterator().next().getValue()); + return String.valueOf(getSoleAttributeValue(attributeType)); } + /** + * Return sole attribute value for given attribute type name. Will throw exceptions if "Sole" nature of attribute is + * invalid.<br> + * <br> + * Used for quick access to attribute value that should only have 0 or 1 instances of the attribute. + */ + @Override + public final <T> T getSoleAttributeValue(IAttributeType attributeType) throws OseeCoreException { + List<ReadableAttribute<T>> soleAttributes = attributeContainer.getAttributes(attributeType); + if (soleAttributes.isEmpty()) { + if (!isAttributeTypeValid(attributeType)) { + throw new OseeArgumentException("The attribute type %s is not valid for artifacts of type [%s]", + attributeType, artifactType.getName()); + } + throw new AttributeDoesNotExist("Attribute of type [%s] could not be found on artifact [%s] on branch [%s]", + attributeType.getName(), getGuid(), getBranch().getGuid()); + } else if (soleAttributes.size() > 1) { + throw new MultipleAttributesExist( + "Attribute [%s] must have exactly one instance. It currently has %d for artifact [%s]", attributeType, + soleAttributes.size(), getGuid()); + } + return soleAttributes.iterator().next().getValue(); + } + + public final boolean isAttributeTypeValid(IAttributeType attributeType) throws OseeCoreException { + return artifactType.isValidAttributeType(attributeType, branch); + } } diff --git a/plugins/org.eclipse.osee.orcs/src/org/eclipse/osee/orcs/data/ReadableArtifact.java b/plugins/org.eclipse.osee.orcs/src/org/eclipse/osee/orcs/data/ReadableArtifact.java index a244a5e..4d68cc1 100644 --- a/plugins/org.eclipse.osee.orcs/src/org/eclipse/osee/orcs/data/ReadableArtifact.java +++ b/plugins/org.eclipse.osee.orcs/src/org/eclipse/osee/orcs/data/ReadableArtifact.java @@ -49,6 +49,8 @@ public interface ReadableArtifact extends Readable, HasVersion, Identifiable { String getSoleAttributeAsString(IAttributeType attributeType, String defaultValue) throws OseeCoreException; + <T> T getSoleAttributeValue(IAttributeType attributeType) throws OseeCoreException; + @Override String toString(); |

