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

Java tutorial

Introduction

Here is the source code for io.twipple.springframework.data.clusterpoint.mapping.BasicClusterpointPersistentProperty.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.XmlAttribute;
import io.twipple.springframework.data.clusterpoint.mapping.annotation.XmlElement;
import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.util.Assert;

import javax.validation.constraints.NotNull;
import javax.xml.namespace.QName;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;

/**
 * Implements annotated property representations of a given entity field instance.
 * This object is used to gather information out of properties on objects that need to be persisted. For example, it
 * supports overriding of the actual property name by providing custom annotations.
 *
 * @author Olegs Briska
 */
public class BasicClusterpointPersistentProperty
        extends AnnotationBasedPersistentProperty<ClusterpointPersistentProperty>
        implements ClusterpointPersistentProperty {

    private Boolean isXmlElement;
    private Boolean isXmlAttribute;
    private QName qualifiedName;

    /**
     * Creates a new {@link BasicClusterpointPersistentProperty}.
     *
     * @param field               must not be {@literal null}.
     * @param propertyDescriptor  can be {@literal null}.
     * @param owner               must not be {@literal null}.
     * @param simpleTypeHolder    must not be {@literal null}.
     */
    public BasicClusterpointPersistentProperty(@NotNull Field field, PropertyDescriptor propertyDescriptor,
            @NotNull ClusterpointPersistentEntity<?> owner, @NotNull SimpleTypeHolder simpleTypeHolder) {
        super(field, propertyDescriptor, owner, simpleTypeHolder);
    }

    @Override
    @NotNull
    protected Association<ClusterpointPersistentProperty> createAssociation() {
        return new Association<ClusterpointPersistentProperty>(this, null);
    }

    @Override
    public boolean isXmlElement() {
        if (this.isXmlElement == null) {
            this.isXmlElement = !isTransient() && (isAnnotationPresent(XmlElement.class) || isEntity());
        }
        return this.isXmlElement;
    }

    @Override
    public boolean isXmlAttribute() {
        if (this.isXmlAttribute == null) {
            this.isXmlAttribute = !isTransient() && (isAnnotationPresent(XmlAttribute.class) || !isEntity());
        }
        return this.isXmlAttribute;
    }

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

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

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

    @Override
    @NotNull
    public QName getQualifiedName() {
        if (qualifiedName == null) {
            ClusterpointPersistentEntity<?> ownerEntity = (ClusterpointPersistentEntity<?>) getOwner();

            Assert.notNull(ownerEntity);

            String namespaceUri = ownerEntity.getXmlNamespace();
            String prefix = ownerEntity.getPreferredXmlPrefix();
            String localName = getName();

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