/*
 * Copyright (c) 2009-2013 Topi Nieminen
 */
package fi.tnie.db.ent;

import fi.tnie.db.expr.ColumnReference;
import fi.tnie.db.expr.OrderBy;
import fi.tnie.db.expr.OrderBy.Order;
import fi.tnie.db.expr.OrderBy.SortKey;

/**
 * Provides a EntityQueryTemplateAttribute implementation with the capability to act as an EntityQuerySortKey.
 * 
 *  
 *  
 * @author Topi Nieminen <topi.nieminen@gmail.com>
 */

public abstract class SortKeyAttributeTemplate<A extends Attribute>
	implements EntityQuerySortKey<A> {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -6385231997652964888L;
	
	private A attribute;
	
	public static <A extends Attribute> SortKeyAttributeTemplate<A> get(A a, OrderBy.Order so) {
		so = (so == null) ? Order.ASC : so;			
		return (so == Order.ASC) ? asc(a) : desc(a);
	}
		
	public static <A extends Attribute> SortKeyAttributeTemplate<A> asc(A attribute) {
		return new Asc<A>(attribute);
	}
	
	public static <A extends Attribute> SortKeyAttributeTemplate<A> desc(A attribute) {
		return new Desc<A>(attribute);
	}
	
	protected SortKeyAttributeTemplate() {
	}
		
	public SortKeyAttributeTemplate(A attribute) {
		super();
		this.attribute = attribute;
	}



	private static class Asc<A extends Attribute>
		extends SortKeyAttributeTemplate<A> {
		
		/**
		 * 
		 */
		private static final long serialVersionUID = -8042036745392628822L;

		protected Asc(A attribute) {
			super(attribute);
		}
		
		@Override
		public Order sortOrder() {
			return Order.ASC;
		}
	}

	
	private static class Desc<A extends Attribute>
		extends SortKeyAttributeTemplate<A> {
		
		/**
		 * 
		 */
		private static final long serialVersionUID = -8042036745392628822L;

		
		/**
		 * No-argument constructor for GWT Serialization
		 */
		@SuppressWarnings("unused")
		private Desc() {	
		}		
		
		protected Desc(A attribute) {
			super(attribute);
		}

		@Override
		public Order sortOrder() {
			return Order.DESC;
		}
	}


	@Override
	public SortKey sortKey(ColumnReference cref) {
		return new OrderBy.ExprSortKey(cref, sortOrder());
	}		

	
	public abstract OrderBy.Order sortOrder();
	
	
	@Override
	public A attribute() {
		return this.attribute;
	}	
}

@@ -66,16 +66,46 @@
 b.ent;%0A%0A
+import java.io.Serializable;%0A%0A
 import f
@@ -136,16 +136,16 @@
 erence;%0A
-
 import f
@@ -538,16 +538,18 @@
 %3CA%3E %7B%0A%09%0A
+%09%0A
 %09/**%0A%09 *
@@ -606,27 +606,27 @@
  = -
-6385231997652964888
+3079799118698355746
 L;%0A%09
@@ -1225,22 +1225,21 @@
 %09%7D%0A%0A%0A%0A%09p
-rivate
+ublic
  static 
@@ -1378,37 +1378,154 @@
 nUID = -
-8042036745392628822L;
+4545159706806621845L;%0A%0A%09%09/**%0A%09%09 * No-argument constructor for GWT Serialization%0A%09%09 */%0A%09%09@SuppressWarnings(%22unused%22)%0A%09%09private Asc() %7B%09%0A%09%09%7D
 %0A%0A%09%09prot
@@ -1651,22 +1651,21 @@
 %09%7D%0A%0A%09%0A%09p
-rivate
+ublic
  static 
@@ -1730,33 +1730,35 @@
 Template%3CA%3E %7B%0A%09%09
+%09%09
 %0A
-
 %09%09/**%0A%09%09 * %0A%09%09 *
@@ -1810,34 +1810,30 @@
 D = 
--8042036745392628822L;%0A%0A%09%09
+4575239762087833095L;%0A
 %0A%09%09/

fix serialization problem