Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2009-2014 Pavel 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.ujorm.implementation.orm;
 
 import java.util.Set;
 import org.ujorm.Key;
 import org.ujorm.Ujo;
 import static org.ujorm.extensions.Property.UNDEFINED_INDEX;

This abstract implementation of the OrmUjo interface is situable for implementation the persistent entities.
Instances of the OrmTable can be serializable, but you need to know that only business data will be transferred, the session and property changes will not be passed.
The sample of use:
  /** Using INSERT */
  public void useCreateItem() {

      Order order = new Order();
      order.set(Order.DATE, new Date());
      order.set(Order.DESCR, "John's order");

      Item item = new Item();
      item.set(Item.ORDER, order);
      item.set(Item.DESCR, "Yellow table");

      Session session = OrmHandler.getInstance().getSession();
      session.save(order);
      session.save(item);
      session.commit();
  }

Author(s):
Pavel Ponec
See also:
RelationToMany
org.ujorm.core.UjoIterator
 
 public abstract class OrmTable<U extends OrmTableextends QuickUjo implements ExtendedOrmUjo<U> {

    
An empty array of the UJO keys
 
     @PackagePrivate
     static final Key[] EMPTY = new Key[0];
    
ORM session
 
     transient private Session session;
    
Set of changes
 
     transient private Set<Keychanges = null;
 
     public OrmTable() {
     }

    
Read a session
 
     @Override
     public Session readSession() {
         return ;
     }

    
Write a session
 
     @Override
     public void writeSession(Session session) {
         this. = session;
     }

    
A method for an internal use only.
 
     @Override
     public void writeValue(Key<?,?> keyObject value) {
         if (!=null) {
             if (==null) {
                  = new HashSet<Key>(8);
             }
             .add(key);
         }
         super.writeValue(keyvalue);
    }

    
Returns keys of changed values in a time when any session is assigned. The method is used by a SQL UPDATE statement to update assigned values only. Implementation tip: create a new property type of Set<Key> and in the method writeValue assign the current Key always.

Parameters:
clear True value clears all the key changes.
Returns:
Key array of the modified values.
    @Override
    public Key[] readChangedProperties(boolean clear) {
        final Key[] result
            = ==null || .isEmpty()
            ? 
            : .toArray(new Key[.size()])
            ;
        if (clear) {
             = null;
        }
        return result;
    }

    
Getter based on Key implemeted by a pattern UjoExt
    @SuppressWarnings("unchecked")
    public final <UJO extends U, VALUE> VALUE get(final Key<UJO, VALUE> key) {
        final VALUE result = key.of((UJO)this);
        return result;
    }

    
Setter based on Key. Type of value is checked in the runtime. The method was implemented by a pattern UjoExt.
    @SuppressWarnings({"unchecked"})
    public final <UJO extends U, VALUE> U set
        ( final Key<UJO, VALUE> key
        , final VALUE value
        ) {
        UjoManager.assertAssign(keyvalue);
        key.setValue((UJO)thisvalue);
        return (U) this;
    }

    
Test an authorization of the action.
    @Override
    public boolean readAuthorization(UjoAction actionKey keyObject value) {
        switch (action.getType()) {
            case .:
                return !(key instanceof RelationToMany);
            default:
                return super.readAuthorization(actionkeyvalue);
        }
    }

    
Read the foreign key. This is useful to obtain the foreign key value without (lazy) loading the entire object. If the lazy object is loaded, the method will need the Session to build the ForeignKey instance.
NOTE: The method is designed for developers only, the Ujorm doesn't call it newer.

Parameters:
key Must be direct key only (org.ujorm.Key.isDirect()==true)
Returns:
If no related object is available, then the result has the NULL value.
Throws:
java.lang.IllegalStateException Method throws an exception for a wrong key type.
java.lang.NullPointerException Method throws an exception if a Session is missing after a lazy initialization of the key.
    @Override
    public <UJO extends U> ForeignKey readFK(Key<UJO, ? extends OrmUjokeythrows IllegalStateException {
        final Object value = super.readValue(key);
        if (value==null || value instanceof ForeignKey) {
            return (ForeignKeyvalue;
        }
//      if (key instanceof RelationToOne) {
//          // TODO: fix the case the key is a relation:
//          final Key key = ((RelationToOne)key).getRelatedKey();
//          return value instanceof ExtendedOrmUjo
//                  ? ((ExtendedOrmUjo)value).readFK(key)
//                  : new ForeignKey(key.of((Ujo)value));
//      }
// Effectiva: toto se volá cyklicky a navíc se předává špatná key (z původního objektu místo z cizího), pak se vrací nesmysly
//        if (value instanceof ExtendedOrmUjo) {
//            return ((ExtendedOrmUjo) value).readFK(key);
//        }
        if (!=null) {
            final OrmUjo ujo = value instanceof OrmUjo
                    ? (OrmUjovalue
                    : this ;
            return .readFK(ujokey);
        }
        throw new NullPointerException("Can't get FK form the key '"+key+"' due the missing Session");
    }
    // ===== STATIC METHODS: Key Facotory =====

    
Create a factory with a cammel-case Key name generator.
Note: after declarations of all properties is recommend to call method KeyFactory.close();
    protected static <UJO extends Ujo, FACTORY extends KeyFactory<UJO>> FACTORY newCamelFactory(Class<? extends UJO> ujoClass) {
        return (FACTORY) new OrmKeyFactory(ujoClasstrue);
    }

    
Create a base factory Key name generator where key name is the same as its field name.
Note: after declarations of all properties is recommend to call method KeyFactory.close();
    protected static <UJO extends Ujo, FACTORY extends KeyFactory<UJO>> FACTORY newFactory(Class<? extends UJO> ujoClass) {
        return (FACTORY) new OrmKeyFactory(ujoClassfalse);
    }

    
A PropertyIterator Factory creates an new key and assign a next index.

Deprecated:
use the newRelation(java.lang.String) instead of this.
Hidden:
    @Deprecated
    protected static <UJO extends ExtendedOrmUjo, ITEM extends ExtendedOrmUjoRelationToMany<UJO,ITEM> newRelation(String nameClass<ITEM> type) {
        return new RelationToMany<UJO,ITEM> (nametypefalse);
    }

    
A PropertyIterator Factory creates an new key and assign a next index.

Hidden:
    protected static <UJO extends ExtendedOrmUjo, ITEM extends ExtendedOrmUjoRelationToMany<UJO,ITEM> newRelation(String name) {
        return new RelationToMany<UJO,ITEM> (namenullfalse);
    }

    
A PropertyIterator Factory creates an new key and assign a next index.

Deprecated:
use the newRelation() instead of this.
Hidden:
    @Deprecated
    protected static <UJO extends ExtendedOrmUjo, ITEM extends ExtendedOrmUjoRelationToMany<UJO,ITEM> newRelation(Class<ITEM> type) {
        return newRelation(nulltype);
    }

    
A PropertyIterator Factory creates an new key and assign a next index.

Hidden:
    protected static <UJO extends ExtendedOrmUjo, ITEM extends ExtendedOrmUjoRelationToMany<UJO,ITEM> newRelation() {
        return newRelation(nullnull);
    }

    
A Property Factory creates new key and assigns a next key index.

Hidden:
    protected static <UJO extends Ujo,VALUE> Property<UJO,VALUE> newKey() {
        return new OrmProperty();
    }

    
A Property Factory creates new key and assigns a next key index.

Hidden:
    protected static <UJO extends Ujo,VALUE> Property<UJO,VALUE> newKey(String name) {
        return new OrmProperty(namenullnull);
    }

    
A Property Factory creates new key and assigns a next key index.

Hidden:
    protected static <UJO extends Ujo,VALUE> Property<UJO,VALUE> newKey(String name, VALUE defaultValue) {
        return new OrmProperty(namedefaultValuenull);
    }

    
A Property Factory creates new key and assigns a next key index.

Hidden:
    protected static <UJO extends Ujo,VALUE> Property<UJO,VALUE> newKeyDefault(VALUE defaultValue) {
        return new OrmProperty(nulldefaultValuenull);
    }
    // --------- STATIC METHODS -------------------


    
A Property Factory creates new key and assigns a next key index.

Hidden:
    protected static <UJO extends Ujo,VALUE> Property<UJO,VALUE> newKey
    ( String name
    , Class<VALUE> type
    , VALUE defaultValue
    , int index
    , boolean lock
    ) {
        return (Property<UJO, VALUE>) new OrmProperty(indexnamedefaultValuenull);
    }

    
A Property Factory creates new key and assigns a next key index.
Warning: Method does not lock the key so you must call AbstractUjo.init(..) method after initialization!

Hidden:
    protected static <UJO extends Ujo, VALUE> Property<UJO, VALUE> newKey
    ( VALUE value
    ) {
        return newKey(nullnullvaluefalse);
    }

    
Returns a new instance of key where the default value is null.
Warning: Method does not lock the key so you must call AbstractUjo.init(..) method after initialization!

Hidden:
    @SuppressWarnings("unchecked")
    public static <UJO extends QuickUjo,VALUE> Property<UJO,VALUE> newKey(Key p) {
        return (Property<UJO, VALUE>) new OrmProperty(p.getName(), p.getDefault(), null);
    }
    // ------------- DEPRECATED METHODS ---------------------

    
A Property Factory creates new key and assigns a next key index.
Warning: Method does not lock the key so you must call AbstractUjo.init(..) method after initialization!

Deprecated:
Use rather a method org.ujorm.implementation.quick.QuickUjo.newProperty(java.lang.String) instead of this.
Hidden:
    @Deprecated
    protected static <UJO extends Ujo,VALUE> Property<UJO,VALUE> newProperty
    ( String name
    , Class<VALUE> type
    ) {
        return newProperty(nametypenullfalse);
    }

    
A Property Factory creates new key and assigns a next key index.
Warning: Method does not lock the key so you must call AbstractUjo.init(..) method after initialization!

Deprecated:
Use the method newKey(...)
Hidden:
    @Deprecated
    protected static <UJO extends Ujo,VALUE> Property<UJO,VALUE> newProperty(String name) {
        return newProperty(namenullnullfalse);
    }

    
A Property Factory creates new key and assigns a next key index.
Warning: Method does not lock the key so you must call AbstractUjo.init(..) method after initialization!

Deprecated:
Use the method newKey(...)
Hidden:
    @Deprecated
    protected static <UJO extends Ujo, VALUE> Property<UJO, VALUE> newProperty
    ( String name
    , VALUE value
    ) {
        return newProperty(namenullvaluefalse);
    }

    
A Property Factory creates new key and assigns a next key index.
Warning: Method does not lock the key so you must call AbstractUjo.init(..) method after initialization!

Deprecated:
Use rather a method org.ujorm.implementation.quick.QuickUjo.newProperty() instead of this,
Hidden:
    @Deprecated
    protected static <UJO extends Ujo,VALUE> Property<UJO,VALUE> newProperty
    ( Class<VALUE> type
    ) {
        return newProperty(nulltypenullfalse);
    }
    @Deprecated
    protected static <UJO extends Ujo,VALUE> Property<UJO,VALUE> newProperty
    ( String name
    , Class<VALUE> type
    , VALUE defaultValue
    , int index
    , boolean lock
    ) {
        return new OrmProperty(indexnamedefaultValuenull);
    }

    
A Property Factory creates new key and assigns a next key index.
Warning: Method does not lock the key so you must call AbstractUjo.init(..) method after initialization!

Deprecated:
Use the method newKey(...)
Hidden:
    @Deprecated
    protected static <UJO extends Ujo, VALUE> Property<UJO, VALUE> newProperty
    ( VALUE value
    ) {
        return newProperty(nullnullvaluefalse);
    }
    @Deprecated
    protected static <UJO extends Ujo, VALUE> Property<UJO, VALUE> newProperty
    ( Class<VALUE> type
      , VALUE value) {
        return newProperty(nullnullvaluefalse);
    }

    
A Property Factory creates new key and assigns a next key index.
Warning: Method does not lock the key so you must call AbstractUjo.init(..) method after initialization!

Deprecated:
Use the method newKey(...)
Hidden:
    @Deprecated
    protected static <UJO extends Ujo, VALUE> Property<UJO, VALUE> newProperty() {
        return newProperty(nullnullnullfalse);
    }
New to GrepCode? Check out our FAQ X