[OLINGO-786] Added terms to metadata document

This commit is contained in:
Christian Amend 2015-10-20 16:28:09 +02:00
parent 15164da8f1
commit a57b7d0541
2 changed files with 98 additions and 4 deletions

View File

@ -51,9 +51,11 @@ import org.apache.olingo.commons.api.edm.EdmReturnType;
import org.apache.olingo.commons.api.edm.EdmSchema; import org.apache.olingo.commons.api.edm.EdmSchema;
import org.apache.olingo.commons.api.edm.EdmSingleton; import org.apache.olingo.commons.api.edm.EdmSingleton;
import org.apache.olingo.commons.api.edm.EdmStructuredType; import org.apache.olingo.commons.api.edm.EdmStructuredType;
import org.apache.olingo.commons.api.edm.EdmTerm;
import org.apache.olingo.commons.api.edm.EdmType; import org.apache.olingo.commons.api.edm.EdmType;
import org.apache.olingo.commons.api.edm.EdmTypeDefinition; import org.apache.olingo.commons.api.edm.EdmTypeDefinition;
import org.apache.olingo.commons.api.edm.FullQualifiedName; import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.api.edm.TargetType;
import org.apache.olingo.commons.api.edm.annotation.EdmApply; import org.apache.olingo.commons.api.edm.annotation.EdmApply;
import org.apache.olingo.commons.api.edm.annotation.EdmCast; import org.apache.olingo.commons.api.edm.annotation.EdmCast;
import org.apache.olingo.commons.api.edm.annotation.EdmConstantExpression; import org.apache.olingo.commons.api.edm.annotation.EdmConstantExpression;
@ -147,6 +149,8 @@ public class MetadataDocumentXmlSerializer {
private static final String XML_TERM_ATT = "Term"; private static final String XML_TERM_ATT = "Term";
private static final String XML_QUALIFIER_ATT = "Qualifier"; private static final String XML_QUALIFIER_ATT = "Qualifier";
private static final String XML_PROPERTY_Value = "PropertyValue"; private static final String XML_PROPERTY_Value = "PropertyValue";
private static final String XML_BASE_TERM = "BaseTerm";
private static final String XML_APPLIES_TO = "AppliesTo";
private final ServiceMetadata serviceMetadata; private final ServiceMetadata serviceMetadata;
private final Map<String, String> namespaceToAlias = new HashMap<String, String>(); private final Map<String, String> namespaceToAlias = new HashMap<String, String>();
@ -209,6 +213,8 @@ public class MetadataDocumentXmlSerializer {
// Functions // Functions
appendFunctions(writer, schema.getFunctions()); appendFunctions(writer, schema.getFunctions());
appendTerms(writer, schema.getTerms());
// EntityContainer // EntityContainer
appendEntityContainer(writer, schema.getEntityContainer()); appendEntityContainer(writer, schema.getEntityContainer());
@ -220,6 +226,59 @@ public class MetadataDocumentXmlSerializer {
writer.writeEndElement(); writer.writeEndElement();
} }
private void appendTerms(XMLStreamWriter writer, List<EdmTerm> terms) throws XMLStreamException {
for (EdmTerm term : terms) {
writer.writeStartElement(XML_TERM_ATT);
writer.writeAttribute(XML_NAME, term.getName());
writer.writeAttribute(XML_TYPE, getAliasedFullQualifiedName(term.getType(), false));
if (term.getBaseTerm() != null) {
writer.writeAttribute(XML_BASE_TERM, getAliasedFullQualifiedName(term.getBaseTerm().getFullQualifiedName(),
false));
}
if(term.getAppliesTo() != null && !term.getAppliesTo().isEmpty()){
String appliesToString = "";
boolean first = true;
for(TargetType target : term.getAppliesTo()){
if(first){
first = false;
appliesToString = target.toString();
}else{
appliesToString = appliesToString + " " + target.toString();
}
}
writer.writeAttribute(XML_APPLIES_TO, appliesToString);
}
// Facets
if (!term.isNullable()) {
writer.writeAttribute(XML_NULLABLE, "" + term.isNullable());
}
if (term.getDefaultValue() != null) {
writer.writeAttribute(XML_DEFAULT_VALUE, term.getDefaultValue());
}
if (term.getMaxLength() != null) {
writer.writeAttribute(XML_MAX_LENGTH, "" + term.getMaxLength());
}
if (term.getPrecision() != null) {
writer.writeAttribute(XML_PRECISION, "" + term.getPrecision());
}
if (term.getScale() != null) {
writer.writeAttribute(XML_SCALE, "" + term.getScale());
}
appendAnnotations(writer, term);
writer.writeEndElement();
}
}
private void appendAnnotationGroups(XMLStreamWriter writer, List<EdmAnnotations> annotationGroups) private void appendAnnotationGroups(XMLStreamWriter writer, List<EdmAnnotations> annotationGroups)
throws XMLStreamException { throws XMLStreamException {
for (EdmAnnotations annotationGroup : annotationGroups) { for (EdmAnnotations annotationGroup : annotationGroups) {

View File

@ -66,12 +66,10 @@ import org.apache.olingo.commons.api.edm.provider.annotation.CsdlApply;
import org.apache.olingo.commons.api.edm.provider.annotation.CsdlCast; import org.apache.olingo.commons.api.edm.provider.annotation.CsdlCast;
import org.apache.olingo.commons.api.edm.provider.annotation.CsdlCollection; import org.apache.olingo.commons.api.edm.provider.annotation.CsdlCollection;
import org.apache.olingo.commons.api.edm.provider.annotation.CsdlConstantExpression; import org.apache.olingo.commons.api.edm.provider.annotation.CsdlConstantExpression;
import org.apache.olingo.commons.api.edm.provider.annotation.CsdlIf;
import org.apache.olingo.commons.api.edm.provider.annotation.CsdlIsOf;
import org.apache.olingo.commons.api.edm.provider.annotation.CsdlPath;
import org.apache.olingo.commons.api.edm.provider.annotation.CsdlPropertyValue;
import org.apache.olingo.commons.api.edm.provider.annotation.CsdlConstantExpression.ConstantExpressionType; import org.apache.olingo.commons.api.edm.provider.annotation.CsdlConstantExpression.ConstantExpressionType;
import org.apache.olingo.commons.api.edm.provider.annotation.CsdlExpression; import org.apache.olingo.commons.api.edm.provider.annotation.CsdlExpression;
import org.apache.olingo.commons.api.edm.provider.annotation.CsdlIf;
import org.apache.olingo.commons.api.edm.provider.annotation.CsdlIsOf;
import org.apache.olingo.commons.api.edm.provider.annotation.CsdlLabeledElement; import org.apache.olingo.commons.api.edm.provider.annotation.CsdlLabeledElement;
import org.apache.olingo.commons.api.edm.provider.annotation.CsdlLabeledElementReference; import org.apache.olingo.commons.api.edm.provider.annotation.CsdlLabeledElementReference;
import org.apache.olingo.commons.api.edm.provider.annotation.CsdlLogicalOrComparisonExpression; import org.apache.olingo.commons.api.edm.provider.annotation.CsdlLogicalOrComparisonExpression;
@ -79,7 +77,9 @@ import org.apache.olingo.commons.api.edm.provider.annotation.CsdlLogicalOrCompar
import org.apache.olingo.commons.api.edm.provider.annotation.CsdlLogicalOrComparisonExpression.LogicalOrComparisonExpressionType; import org.apache.olingo.commons.api.edm.provider.annotation.CsdlLogicalOrComparisonExpression.LogicalOrComparisonExpressionType;
import org.apache.olingo.commons.api.edm.provider.annotation.CsdlNavigationPropertyPath; import org.apache.olingo.commons.api.edm.provider.annotation.CsdlNavigationPropertyPath;
import org.apache.olingo.commons.api.edm.provider.annotation.CsdlNull; import org.apache.olingo.commons.api.edm.provider.annotation.CsdlNull;
import org.apache.olingo.commons.api.edm.provider.annotation.CsdlPath;
import org.apache.olingo.commons.api.edm.provider.annotation.CsdlPropertyPath; import org.apache.olingo.commons.api.edm.provider.annotation.CsdlPropertyPath;
import org.apache.olingo.commons.api.edm.provider.annotation.CsdlPropertyValue;
import org.apache.olingo.commons.api.edm.provider.annotation.CsdlRecord; import org.apache.olingo.commons.api.edm.provider.annotation.CsdlRecord;
import org.apache.olingo.commons.api.edm.provider.annotation.CsdlUrlRef; import org.apache.olingo.commons.api.edm.provider.annotation.CsdlUrlRef;
//CHECKSTYLE:ON //CHECKSTYLE:ON
@ -273,6 +273,17 @@ public class MetadataDocumentXmlSerializerTest {
"Function=\"Alias.UFNRTInt16\" IncludeInServiceDocument=\"true\"/>")); "Function=\"Alias.UFNRTInt16\" IncludeInServiceDocument=\"true\"/>"));
} }
@Test
public void terms() throws Exception {
String metadata = localMetadata();
assertTrue(metadata.contains("<Term Name=\"Term1\" Type=\"Edm.String\"/>"));
assertTrue(metadata
.contains("<Term Name=\"Term2\" Type=\"Edm.String\" Nullable=\"false\" DefaultValue=\"default\" "
+ "MaxLength=\"1\" Precision=\"2\" Scale=\"3\"/>"));
assertTrue(metadata.contains("<Term Name=\"Term3\" Type=\"Edm.String\" AppliesTo=\"Property EntitySet Schema\"/>"));
assertTrue(metadata.contains("<Term Name=\"Term4\" Type=\"Edm.String\" BaseTerm=\"Alias.Term1\"/>"));
}
@Test @Test
public void annotationsTest() throws Exception { public void annotationsTest() throws Exception {
String metadata = localMetadata(); String metadata = localMetadata();
@ -587,6 +598,15 @@ public class MetadataDocumentXmlSerializerTest {
// EntityContainer // EntityContainer
schema.setEntityContainer(getEntityContainer()); schema.setEntityContainer(getEntityContainer());
//Terms
List<CsdlTerm> terms = new ArrayList<CsdlTerm>();
terms.add(getTerm(new FullQualifiedName("ns.term")));
terms.add(getTerm(new FullQualifiedName("namespace.Term1")));
terms.add(getTerm(new FullQualifiedName("ns.Term2")));
terms.add(getTerm(new FullQualifiedName("ns.Term3")));
terms.add(getTerm(new FullQualifiedName("ns.Term4")));
schema.setTerms(terms);
// Annotationgroups // Annotationgroups
List<CsdlAnnotations> annotationGroups = new ArrayList<CsdlAnnotations>(); List<CsdlAnnotations> annotationGroups = new ArrayList<CsdlAnnotations>();
annotationGroups.add(getAnnotationsGroup(new FullQualifiedName("Alias.ETAbstract"), "Tablett")); annotationGroups.add(getAnnotationsGroup(new FullQualifiedName("Alias.ETAbstract"), "Tablett"));
@ -641,6 +661,21 @@ public class MetadataDocumentXmlSerializerTest {
public CsdlTerm getTerm(FullQualifiedName termName) throws ODataException { public CsdlTerm getTerm(FullQualifiedName termName) throws ODataException {
if (new FullQualifiedName("ns.term").equals(termName)) { if (new FullQualifiedName("ns.term").equals(termName)) {
return new CsdlTerm().setType("Edm.String").setName("term"); return new CsdlTerm().setType("Edm.String").setName("term");
} else if(new FullQualifiedName("namespace.Term1").equals(termName)){
return new CsdlTerm().setType("Edm.String").setName("Term1");
} else if(new FullQualifiedName("ns.Term2").equals(termName)){
return new CsdlTerm().setType("Edm.String").setName("Term2")
.setNullable(false).setDefaultValue("default").setMaxLength(1).setPrecision(2).setScale(3);
} else if(new FullQualifiedName("ns.Term3").equals(termName)){
return new CsdlTerm().setType("Edm.String").setName("Term3")
.setAppliesTo(Arrays.asList("Property", "EntitySet", "Schema"));
} else if(new FullQualifiedName("ns.Term4").equals(termName)){
return new CsdlTerm().setType("Edm.String").setName("Term4").setBaseTerm("namespace.Term1");
} }
return null; return null;
} }