io.twipple.springframework.data.clusterpoint.mapping.BasicClusterpointPersistentEntity.java Source code

Java tutorial

Introduction

Here is the source code for io.twipple.springframework.data.clusterpoint.mapping.BasicClusterpointPersistentEntity.java

Source

/*
 * This file is part of Spring Data Clusterpoint.
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 the author or authors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package io.twipple.springframework.data.clusterpoint.mapping;

import io.twipple.springframework.data.clusterpoint.mapping.annotation.XmlDocument;
import io.twipple.springframework.data.clusterpoint.mapping.annotation.XmlSchema;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.expression.BeanFactoryAccessor;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.data.mapping.model.BasicPersistentEntity;
import org.springframework.data.util.TypeInformation;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotNull;
import javax.xml.namespace.QName;
import java.util.Comparator;
import java.util.Objects;

/**
 * The representation of a persistent entity.
 *
 * @author Olegs Briska
 */
public class BasicClusterpointPersistentEntity<T> extends BasicPersistentEntity<T, ClusterpointPersistentProperty>
        implements ClusterpointPersistentEntity<T>, ApplicationContextAware {

    /**
     * Contains the evaluation context.
     */
    private final StandardEvaluationContext context;

    private QName qualifiedName;

    public BasicClusterpointPersistentEntity(@NotNull TypeInformation<T> information) {
        this(information, null);
    }

    public BasicClusterpointPersistentEntity(@NotNull TypeInformation<T> information,
            Comparator<ClusterpointPersistentProperty> comparator) {
        super(information, comparator);

        context = new StandardEvaluationContext();
    }

    @Override
    @NotNull
    public Object getTypeAlias() {
        if (qualifiedName == null) {
            String namespaceUri = buildXmlNamespace();
            String localName = Objects.toString(super.getTypeAlias(), null);
            if (localName == null) {
                localName = buildXmlLocalName();
            }
            String prefix = buildXmlPrefix();

            qualifiedName = new QName(namespaceUri, localName, prefix);
        }
        return qualifiedName;
    }

    @Override
    public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {

        Assert.notNull(applicationContext);

        context.addPropertyAccessor(new BeanFactoryAccessor());
        context.setBeanResolver(new BeanFactoryResolver(applicationContext));
        context.setRootObject(applicationContext);
    }

    @Override
    @NotNull
    public final String getXmlNamespace() {
        return getQualifiedName().getNamespaceURI();
    }

    @Override
    @NotNull
    public final String getPreferredXmlPrefix() {
        return getQualifiedName().getPrefix();
    }

    @Override
    @NotNull
    public final String getXmlLocalName() {
        return getQualifiedName().getLocalPart();
    }

    @Override
    @NotNull
    public final QName getQualifiedName() {
        return (QName) getTypeAlias();
    }

    @NotNull
    protected String buildXmlNamespace() {
        XmlSchema schemaAnnotation = findXmlSchemaAnnotation();
        if (schemaAnnotation != null) {
            String namespaceUri = schemaAnnotation.namespace();
            if (StringUtils.hasText(namespaceUri)) {
                return namespaceUri;
            }

            namespaceUri = schemaAnnotation.value();
            if (StringUtils.hasText(namespaceUri)) {
                return namespaceUri;
            }
        }

        return buildPackageUrn();
    }

    @NotNull
    protected String buildPackageUrn() {
        return "urn:" + getType().getPackage().getName().replace('.', '-');
    }

    @NotNull
    protected String buildXmlLocalName() {
        return getType().getSimpleName();
    }

    @NotNull
    protected String buildXmlPrefix() {
        XmlSchema schemaAnnotation = findXmlSchemaAnnotation();
        if (schemaAnnotation != null) {
            String prefix = schemaAnnotation.prefix();
            if (StringUtils.hasText(prefix)) {
                return prefix;
            }
        }

        XmlDocument documentAnnotation = findAnnotation(XmlDocument.class);
        if (documentAnnotation != null) {
            String prefix = documentAnnotation.prefix();
            if (StringUtils.hasText(prefix)) {
                return prefix;
            }
        }

        return "";
    }

    protected XmlSchema findXmlSchemaAnnotation() {
        return getType().getPackage().getAnnotation(XmlSchema.class);
    }
}