From a57b7d0541cbaf8e0f41ff3428292385bedc8e30 Mon Sep 17 00:00:00 2001 From: Christian Amend Date: Tue, 20 Oct 2015 16:28:09 +0200 Subject: [PATCH] [OLINGO-786] Added terms to metadata document --- .../xml/MetadataDocumentXmlSerializer.java | 59 +++++++++++++++++++ .../MetadataDocumentXmlSerializerTest.java | 43 ++++++++++++-- 2 files changed, 98 insertions(+), 4 deletions(-) diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentXmlSerializer.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentXmlSerializer.java index b761374e2..52432ca2e 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentXmlSerializer.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentXmlSerializer.java @@ -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.EdmSingleton; 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.EdmTypeDefinition; 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.EdmCast; 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_QUALIFIER_ATT = "Qualifier"; 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 Map namespaceToAlias = new HashMap(); @@ -209,6 +213,8 @@ public class MetadataDocumentXmlSerializer { // Functions appendFunctions(writer, schema.getFunctions()); + appendTerms(writer, schema.getTerms()); + // EntityContainer appendEntityContainer(writer, schema.getEntityContainer()); @@ -220,6 +226,59 @@ public class MetadataDocumentXmlSerializer { writer.writeEndElement(); } + private void appendTerms(XMLStreamWriter writer, List 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 annotationGroups) throws XMLStreamException { for (EdmAnnotations annotationGroup : annotationGroups) { diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentXmlSerializerTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentXmlSerializerTest.java index 365da7a68..e750aadb4 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentXmlSerializerTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentXmlSerializerTest.java @@ -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.CsdlCollection; 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.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.CsdlLabeledElementReference; 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.CsdlNavigationPropertyPath; 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.CsdlPropertyValue; import org.apache.olingo.commons.api.edm.provider.annotation.CsdlRecord; import org.apache.olingo.commons.api.edm.provider.annotation.CsdlUrlRef; //CHECKSTYLE:ON @@ -273,6 +273,17 @@ public class MetadataDocumentXmlSerializerTest { "Function=\"Alias.UFNRTInt16\" IncludeInServiceDocument=\"true\"/>")); } + @Test + public void terms() throws Exception { + String metadata = localMetadata(); + assertTrue(metadata.contains("")); + assertTrue(metadata + .contains("")); + assertTrue(metadata.contains("")); + assertTrue(metadata.contains("")); + } + @Test public void annotationsTest() throws Exception { String metadata = localMetadata(); @@ -587,6 +598,15 @@ public class MetadataDocumentXmlSerializerTest { // EntityContainer schema.setEntityContainer(getEntityContainer()); + //Terms + List terms = new ArrayList(); + 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 List annotationGroups = new ArrayList(); annotationGroups.add(getAnnotationsGroup(new FullQualifiedName("Alias.ETAbstract"), "Tablett")); @@ -641,6 +661,21 @@ public class MetadataDocumentXmlSerializerTest { public CsdlTerm getTerm(FullQualifiedName termName) throws ODataException { if (new FullQualifiedName("ns.term").equals(termName)) { 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; }