All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.quinsoft.zeidon.domains.GeneratedKeyDomain Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
/**
    This file is part of the Zeidon Java Object Engine (Zeidon JOE).

    Zeidon JOE is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Zeidon JOE is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Zeidon JOE.  If not, see .

    Copyright 2009-2015 QuinSoft
 */
package com.quinsoft.zeidon.domains;

import java.util.Map;

import com.quinsoft.zeidon.Application;
import com.quinsoft.zeidon.AttributeInstance;
import com.quinsoft.zeidon.Blob;
import com.quinsoft.zeidon.GeneratedKey;
import com.quinsoft.zeidon.InvalidAttributeConversionException;
import com.quinsoft.zeidon.InvalidAttributeValueException;
import com.quinsoft.zeidon.Task;
import com.quinsoft.zeidon.objectdefinition.AttributeDef;
import com.quinsoft.zeidon.objectdefinition.DataField;
import com.quinsoft.zeidon.objectdefinition.DataRecord;
import com.quinsoft.zeidon.standardoe.GeneratedKeyImpl;
import com.quinsoft.zeidon.utils.JoeUtils;

/**
 * This is a domain for storing keys generated by the DB.  The domain is constructed
 * in a way that it can hold keys for any DB.  Internally the keys are stored as
 * strings and it is up to the DBHandler to convert it to the correct type.
 *
 */
public class GeneratedKeyDomain extends AbstractDomain
{

    /**
     * @param app
     * @param domainProperties
     * @param task
     */
    public GeneratedKeyDomain( Application app, Map domainProperties, Task task )
    {
        super( app, domainProperties, task );
    }

    /* (non-Javadoc)
     * @see com.quinsoft.zeidon.domains.Domain#convertExternalValue(com.quinsoft.zeidon.Task, com.quinsoft.zeidon.AttributeInstance, com.quinsoft.zeidon.objectdefinition.AttributeDef, java.lang.String, java.lang.Object)
     */
    @SuppressWarnings("unchecked")
    @Override
    public Object convertExternalValue( Task task,
                                        AttributeInstance attributeInstance,
                                        AttributeDef attributeDef,
                                        String contextName,
                                        Object externalValue ) throws InvalidAttributeValueException
    {
        // If external value is an AttributeInstance then get *its* internal value.
        if ( externalValue instanceof AttributeInstance )
            externalValue = ((AttributeInstance) externalValue).getValue();

        if ( externalValue == null )
            return null;

        if ( externalValue instanceof GeneratedKey )
            return externalValue;

        if ( ! ( externalValue instanceof Comparable ) )
            throw new InvalidAttributeValueException( attributeDef, externalValue,
                                                      "Value of GeneratedKeyDomaing must implement Comparable" );

        return new GeneratedKeyImpl( (Comparable) externalValue );
    }

    @Override
    public Object convertValueForDb( Task task,
                                     AttributeDef attributeDef,
                                     Object value ) throws InvalidAttributeValueException
    {
        if ( ! ( value instanceof GeneratedKey ) )
            return value;

        GeneratedKey key = (GeneratedKey) value;
        DataRecord dataRecord = attributeDef.getEntityDef().getDataRecord();
        DataField  dataField = dataRecord.getDataField( attributeDef );

        if ( dataField.getType().equals( "L" ) )
            return JoeUtils.convertObjectToInteger( key.getNativeValue() );

        return key.getNativeValue();
    }

    @Override
    public void validateInternalValue( Task task, AttributeDef attributeDef, Object internalValue ) throws InvalidAttributeValueException
    {
        if ( internalValue instanceof GeneratedKey )
            return;

        throw new InvalidAttributeValueException( attributeDef, internalValue, "Attribute is expecting a GeneratedKey, got %s", internalValue.getClass() );
    }

    @Override
    public boolean isNull( Task task, AttributeDef attributeDef, Object value )
    {
        if ( value == null )
            return true;

        assert value instanceof GeneratedKey : "Unexpected class found: " + value.getClass().getCanonicalName();
        return ((GeneratedKey) value).isNull();
    }


    @Override
    public String convertToString(Task task, AttributeDef attributeDef, Object internalValue)
    {
        if ( isNull( task, attributeDef, internalValue ) )
            return null;

        return ((GeneratedKey) internalValue).getString();
    }

    @Override
    public String convertToString( Task task, AttributeDef attributeDef, Object internalValue, String contextName )
    {
        if ( isNull( task, attributeDef, internalValue ) )
            return null;

        return ((GeneratedKey) internalValue).getString();
    }

    @Override
    public Blob convertToBlob( Task task, AttributeDef attributeDef, Object internalValue )
    {
        // We don't allow genkeys to be converted to boolean.
        throw new InvalidAttributeConversionException( attributeDef, "Cannot convert GeneratedKeyDomain to Blob", attributeDef.toString() );
    }

    @Override
    public Double convertToDouble( Task task, AttributeDef attributeDef, Object internalValue )
    {
        // We don't allow genkeys to be converted to Double.
        throw new InvalidAttributeConversionException( attributeDef, "Cannot convert GeneratedKeyDomain to Double", attributeDef.toString() );
    }

    /**
     * We don't allow GeneratedKeyDomains to be automatically converted into integers.
     * This prevents the accidental usage of keys as integers.
     */
    @Override
    public Integer convertToInteger( Task task, AttributeDef attributeDef, Object internalValue )
    {
        // We don't allow genkeys to be converted to integer.
        throw new InvalidAttributeConversionException( attributeDef, "Cannot convert GeneratedKeyDomain to Integer", attributeDef.toString() );
    }

    @Override
    public Boolean convertToBoolean(Task task, AttributeDef attributeDef, Object internalValue )
    {
        // We don't allow genkeys to be converted to boolean.
        throw new InvalidAttributeConversionException( attributeDef, "Cannot convert GeneratedKeyDomain to Boolean", attributeDef.toString() );
    }

    @Override
    public int compare( Task task,
                        AttributeInstance attributeInstance,
                        AttributeDef attributeDef,
                        Object internalValue,
                        Object externalValue )
    {
        GeneratedKey externalKey = (GeneratedKey) convertExternalValue( task, attributeInstance, attributeDef, null, externalValue );
        Integer rc = compareNull( task, attributeDef, internalValue, externalKey );
        if ( rc != null )
            return rc;

        GeneratedKey internalKey = (GeneratedKey) internalValue;
        return internalKey.getString().compareTo( externalKey.getString() );
    }
}