0

There are fields named 'date' on both of general and metadata of a schema. And on entity class, 'date' field is mapped to a property by using SemanticProperty.

Then metadata date field's value is set to the property(date). How can I set general field's value to the property?

Is there any method to explicitly map general or metadata field to Java's property?

My entity class is following.

import static com.sdl.webapp.common.api.mapping.semantic.config.SemanticVocabulary.*;

import com.sdl.webapp.common.api.mapping.semantic.annotations.SemanticEntity;
import com.sdl.webapp.common.api.mapping.semantic.annotations.SemanticMappingIgnore;
import com.sdl.webapp.common.api.mapping.semantic.annotations.SemanticProperty;


@SemanticEntity(entityName = "NewsandEvents", vocabulary = SDL_CORE)
@Data
@EqualsAndHashCode(callSuper = true)
public class NewsandEvents extends AbstractEntityModel implements LineupEntity {
    @SemanticProperty(propertyName = "date")
    private DateTime date;

2 Answers 2

0

By default, CM fields are mapped to Semantic Properties by their (unqualified) field name. So, by default you won't be able to distinguish content and metadata fields with the same name.

However, DXA TBBs allow customization of the Semantic Property mappings using mapping:property elements in the Schema XSD (inside xsd:appinfo/tcm:ExtensionXml). So, you can define additional Semantic Properties for a CM Schema field. This allows you to map only the content or metadata field to a distinct Semantic Property.

For details, you may want to have a look at the Publish Mappings TBB implementation: https://github.com/RWS/dxa-content-management/blob/develop/Sdl.Web.Tridion.Templates/Templates/PublishMappings.cs

In particular, see method PublishMappings.GetSemanticProperties

1

Solution

Add <mapping:property> element in <tcm:ExtensionXml>.

Namespace mapped to prefix 'mapping' must be http://www.sdl.com/tridion/SemanticMapping.This namespace string is written in TBB Publish Mappings's source code.

Also value of <mapping:property> element must be <prefix>:<value> format, oherwise TBB throws error. So I use value tri:date_general. I don't know why prefix is 'tri', but I saw schemas.json which is created on server, all prefixes were 'tri' so I use same value as prefix.

schemas.json :

"Semantics":[{"Prefix":"tri","Entity":"ProductOverview","Property":"certification_icons"}]

Schema's source:

      <xsd:element name="date" minOccurs="1" maxOccurs="1" type="xsd:dateTime">
        <xsd:annotation>
          <xsd:appinfo>
            <tcm:IsPublishable xmlns:tcm="http://www.tridion.com/ContentManager/5.0">true</tcm:IsPublishable>
            <tcm:IsIndexable xmlns:tcm="http://www.tridion.com/ContentManager/5.0">true</tcm:IsIndexable>
            <tcm:CustomURL xmlns:tcm="http://www.tridion.com/ContentManager/5.0">/Extensions/ServerTimeGetter/ServerTimeGetterPopup.html</tcm:CustomURL>
            <tcm:ExtensionXml xmlns:mapping="http://www.sdl.com/tridion/SemanticMapping" xmlns:tcm="http://www.tridion.com/ContentManager/5.0">
              <mapping:property>tri:date_general</mapping:property>
            </tcm:ExtensionXml>
          </xsd:appinfo>
        </xsd:annotation>
      </xsd:element>

Then specify the mapping:property's value as propertyName on Java entity, the schema's field's value was set in Java property(date).

 @SemanticProperty(propertyName = "date_general")
    private DateTime date;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.