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;

It is an thread safe implementation of the OrmUjo for the multi-thread use. The special feature are:
  • some critical method are synchronized
  • Session is saved to the ThreadLocal instance so object in different thread must assign a new Session
    See the OrmTable javadoc for basic information.

    Author(s):
    Pavel Ponec
    See also:
    OrmTable
 
 public abstract class OrmTableSynchronized<UJO_IMPL extends Ujoextends QuickUjo implements ExtendedOrmUjo<UJO_IMPL> {

    
Orm session
 
     transient private ThreadLocal<Sessionsession;
    
Set of changes
 
     transient private Set<Keychanges = null;
 
     public OrmTableSynchronized() {
     }

    
Read a session
 
     @Override
     public Session readSession() {
         return !=null ? .get() : null ;
     }

    
Write a session
 
     @Override
     public void writeSession(Session session) {
         if (this.==null) {
             if (session==null) {
                 return;
             }
             this. = new ThreadLocal<Session>();
         }
         this..set(session);
     }

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

    
A method for an internal use only.
 
     @Override
     synchronized public Object readValue(final Key key) {
         return super.readValue(key);
     }

    
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 key 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
    synchronized 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 UJO_IMPL, 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 UJO_IMPL, VALUE> UJO_IMPL set
        ( final Key<UJO, VALUE> key
        , final VALUE value
        ) {
        UjoManager.assertAssign(keyvalue);
        key.setValue((UJO)thisvalue);
        return (UJO_IMPL) 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
    synchronized public <UJO extends UJO_IMPL> ForeignKey readFK(Key<UJO, ? extends OrmUjokeythrows IllegalStateException {
        final Object value = super.readValue(key);
        if (value==null || value instanceof ForeignKey) {
            return (ForeignKeyvalue;
        }
// 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);
//        }
        final Session session = readSession();
        if (session!=null) {
            final OrmUjo ujo = value instanceof OrmUjo
                    ? (OrmUjovalue
                    : this ;
            return session.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();
In case of OrmUjo the method is called by a Ujorm framework, so the newCamelFactory
    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.of(nametypedefaultValueindexlock);
    }

    
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.of(p.getName(), p.getType(), p.getDefault(), -1, false);
    }
    // ------------- 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);
    }

    
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