001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *                               ***
008 *
009 *   Community License: GPL 3.0
010 *
011 *   This file is free software: you can redistribute it and/or modify
012 *   it under the terms of the GNU General Public License as published
013 *   by the Free Software Foundation, either version 3 of the License,
014 *   or (at your option) any later version.
015 *
016 *   This file is distributed in the hope that it will be useful, but
017 *   WITHOUT ANY WARRANTY; without even the implied warranty of
018 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019 *   GNU General Public License for more details.
020 *
021 *   You should have received a copy of the GNU General Public License
022 *   along with this program. If not, see <http://www.gnu.org/licenses/>.
023 *
024 *                               ***
025 *
026 *   Available Commercial License: GraniteDS SLA 1.0
027 *
028 *   This is the appropriate option if you are creating proprietary
029 *   applications and you are not prepared to distribute and share the
030 *   source code of your application under the GPL v3 license.
031 *
032 *   Please visit http://www.granitedataservices.com/license for more
033 *   details.
034 */
035package org.granite.client.tide.collection;
036
037import org.granite.client.persistence.Loader;
038import org.granite.client.persistence.collection.PersistentCollection;
039import org.granite.client.persistence.collection.PersistentCollection.InitializationCallback;
040import org.granite.client.tide.data.EntityManager;
041import org.granite.client.tide.data.PersistenceManager;
042import org.granite.client.tide.server.ServerSession;
043
044
045/**
046 *  Internal implementation of persistent collection handling automatic lazy loading.<br/>
047 *  Used for wrapping persistent collections received from the server.<br/>
048 *  Should not be used directly.
049 * 
050 *  @author William DRAI
051 */
052public class CollectionLoader implements Loader<PersistentCollection> {
053    
054    private final ServerSession serverSession;
055    
056    private final Object entity;
057        private final String propertyName;
058    
059    private boolean localInitializing = false;
060    private boolean initializing = false;
061    @SuppressWarnings("unused")
062    private InitializationCallback initializationCallback = null;
063    
064    
065        public CollectionLoader(ServerSession serverSession, Object entity, String propertyName) {
066        this.serverSession = serverSession;
067        this.entity = entity;
068        this.propertyName = propertyName;
069    }
070    
071    public boolean isInitializing() {
072        return initializing;
073    }
074    
075    public void onInitializing() {
076        localInitializing = true;
077    }
078    
079    public void onInitialize() {
080        localInitializing = false;
081    }
082    
083    public void onUninitialize() {
084        initializing = false;
085        localInitializing = false;
086        initializationCallback = null;
087    }
088    
089    public void load(PersistentCollection collection, InitializationCallback callback) {
090        if (localInitializing)
091            return;
092        
093        this.initializationCallback = callback;
094        
095        EntityManager entityManager = PersistenceManager.getEntityManager(entity);
096        if (!initializing && entityManager.initializeObject(serverSession, entity, propertyName, collection))                
097            initializing = true;
098    }
099}