/*
 *  Copyright 2009 Paul Ponec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.ujoframework.orm.metaModel;

import java.util.ArrayList;
import org.ujoframework.UjoProperty;
import org.ujoframework.orm.AbstractMetaModel;
import org.ujoframework.extensions.ListProperty;
import org.ujoframework.extensions.Property;
import org.ujoframework.orm.OrmUjo;
import org.ujoframework.orm.Session;

/**
 * The table primary key.
 * @author Pavel Ponec
 * @composed 1 - * MetaColumn
 */
public class MetaPKey extends AbstractMetaModel {
    private static final Class CLASS = MetaPKey.class;

    /** DB table */
    public static final Property<MetaPKey,MetaTable> TABLE = newProperty("table", MetaTable.class);

    /** DB columns */
    public static final ListProperty<MetaPKey,MetaColumn> COLUMNS = newListProperty("columns", MetaColumn.class);

    /** The property initialization */
    static{init(CLASS);}

    /** Database */
    final private MetaDatabase database;

    public MetaPKey(MetaTable table) {
        this.database = MetaTable.DATABASE.of(table);
        TABLE.setValue(this, table);
        COLUMNS.setValue(this, new ArrayList<MetaColumn>(0));
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(10);

        for (MetaColumn column : COLUMNS.getList(this)) {
            if (sb.length()>0) { sb.append(','); }
            sb.append(column.toString());
        }
        return sb.toString();
    }

    /** Assign a PK from framework in case the PK generator is type of MEMO_SEQUENCE.
     * @return Value 'true' means, that primary key was assigned successfully
     *         and 'false' means that a primary key was defined earlier.
     * @throws java.lang.IllegalArgumentException The PK can't be assigned.
     */
    @SuppressWarnings("unchecked")
    public boolean assignPrimaryKey(final OrmUjo bo, final Session session) throws IllegalArgumentException {
        int count = COLUMNS.getItemCount(this);
        if (count==1) {

            MetaColumn column = COLUMNS.getItem(this, 0);
            UjoProperty property = column.getProperty();
            if (property.of(bo)!=null) {
                return false;
            }

            final long value = TABLE.of(this).getSequencer().nextValue(session);
            if (Long.class==property.getType()) {
                property.setValue(bo, value);
                return true;
            }
            if (Integer.class==property.getType()) {
                property.setValue(bo, (int) value);
                return true;
            }

            return false;
        } else {
            String msg = "Table " + bo + " must have defined only one primary key type of Long or Integer";
            throw new IllegalArgumentException(msg);
        }
    }

    /** Returns the first column. */
    public MetaColumn getFirstColumn() {
        return getColumn(0);
    }

    /** Returns column on the selected position. */
    public MetaColumn getColumn(int i) {
        return COLUMNS.of(this).get(i);
    }
}

@@ -639,16 +639,45 @@
 Model;%0A%0A
+import java.math.BigInteger;%0A
 import j
@@ -946,16 +946,54 @@
 Session;
+%0Aimport org.ujoframework.orm.TypeBook;
 %0A%0A/**%0A *
@@ -2382,43 +2382,8 @@
  */%0A
-    @SuppressWarnings(%22unchecked%22)%0A
     
@@ -2691,16 +2691,29 @@
      if 
+(bo.readValue
 (propert
@@ -2717,14 +2717,8 @@
 erty
-.of(bo
 )!=n
@@ -2850,16 +2850,17 @@
 ssion);%0A
+%0A
         
@@ -2867,40 +2867,30 @@
     
-if (Long.class==property
+switch (column
 .getType
 ()) 
@@ -2877,32 +2877,36 @@
  (column.getType
+Code
 ()) %7B%0A          
@@ -2915,53 +2915,83 @@
     
-property.setValue(bo, value);%0A               
+case TypeBook.BIG_INTE: bo.writeValue(property, BigInteger.valueOf(value));
  ret
@@ -3016,105 +3016,151 @@
     
-%7D%0A
     
-        if (Integer.class==property.getType()) %7B%0A                property.setValue(bo
+case TypeBook.LONG    : bo.writeValue(property, value); return true;%0A                case TypeBook.INT     : bo.writeValue(property
 , (int
+  
 ) va
@@ -3164,16 +3164,29 @@
  value);
+ return true;
 %0A       
@@ -3185,32 +3185,96 @@
 %0A               
+ case TypeBook.SHORT   : bo.writeValue(property, (short) value);
  return true;%0A  
@@ -3283,30 +3283,28 @@
         
-%7D%0A%0A
     
-       
+default:
  return 
@@ -3302,32 +3302,46 @@
 : return false;%0A
+            %7D%0A
         %7D else %7B

The internal sequence support works for the next Java types: - BigInteger - Long - Integer - Short