/*
 * Copyright (c) 2009-2013 Topi Nieminen
 */
package com.appspot.relaxe.pg.pagila;

import com.appspot.relaxe.ent.Attribute;
import com.appspot.relaxe.ent.EntityRuntimeException;
import com.appspot.relaxe.ent.value.AbstractOtherKey;
import com.appspot.relaxe.rpc.AbstractPrimitiveHolder;
import com.appspot.relaxe.types.OtherType;
import com.appspot.relaxe.types.AbstractPrimitiveType;

public class MPAARatingKey<
	A extends Attribute,
	E extends HasMPAARating<A, E>	
>
	extends AbstractOtherKey<A, E, MPAARating, MPAARatingType, MPAARatingHolder, MPAARatingKey<A, E>>
{	
	/**
	 *
	 */
	private static final long serialVersionUID = 3465654564903987460L;
	
	/**
	 * No-argument constructor for GWT Serialization
	 */	
	private MPAARatingKey() {
	}
	
	private MPAARatingKey(HasMPAARatingKey<A, E> meta, A name) {
		super(name);
		meta.register(this);
	}
	
	public static <
		X extends Attribute,
		T extends HasMPAARating<X, T>
	>
	MPAARatingKey<X, T> get(HasMPAARatingKey<X, T> meta, X a) {
		MPAARatingKey<X, T> k = meta.getMPAARatingKey(a);
		
		if (k == null) {
			MPAARatingType kt = MPAARatingType.TYPE;
			OtherType<?> t = a.type().asOtherType();
			
			if (t != null && t.getSqlType() == AbstractPrimitiveType.OTHER && kt.getName().equals(t.getName())) {
				k = new MPAARatingKey<X, T>(meta, a);
			}			
		}
				
		return k;
	}	

			
	@Override
	public MPAARatingType type() {
		return MPAARatingType.TYPE;
	}
	
	@Override
	public void set(E e, MPAARatingHolder newValue) 
		throws EntityRuntimeException {
		e.setMPAARating(this, newValue);
	}
	
	@Override
	public MPAARatingHolder get(E e) 
		throws EntityRuntimeException {
		return e.getMPAARating(this);
	}
	
	@Override
	public MPAARatingHolder newHolder(MPAARating newValue) {
		return MPAARatingHolder.valueOf(newValue);
	}

	@Override
	public void copy(E src, E dest) {
		dest.setMPAARating(this, src.getMPAARating(this));
	}

	@Override
	public MPAARatingKey<A, E> self() {
		return this;
	}

	@Override
	public MPAARatingHolder as(AbstractPrimitiveHolder<?, ?, ?> holder) {
		return MPAARatingHolder.of(holder);
	}
}

@@ -259,24 +259,16 @@
 axe.rpc.
-Abstract
 Primitiv
@@ -276,16 +276,16 @@
 Holder;%0A
+
 import c
@@ -373,16 +373,63 @@
 iveType;
+%0Aimport com.appspot.relaxe.types.PrimitiveType;
 %0A%0Apublic
@@ -1230,24 +1230,16 @@
 pe() == 
-Abstract
 Primitiv
@@ -1990,32 +1990,32 @@
 %0A%09%7D%0A%0A%09@Override%0A
+
 %09public MPAARati
@@ -2026,24 +2026,16 @@
 lder as(
-Abstract
 Primitiv

replace AbstractPrimitive* with Primitive*