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     *   Granite Data Services is free software; you can redistribute it and/or
008     *   modify it under the terms of the GNU Lesser General Public
009     *   License as published by the Free Software Foundation; either
010     *   version 2.1 of the License, or (at your option) any later version.
011     *
012     *   Granite Data Services is distributed in the hope that it will be useful,
013     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015     *   General Public License for more details.
016     *
017     *   You should have received a copy of the GNU Lesser General Public
018     *   License along with this library; if not, write to the Free Software
019     *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020     *   USA, or see <http://www.gnu.org/licenses/>.
021     */
022    package org.granite.tide.cdi;
023    
024    import java.util.ArrayList;
025    import java.util.List;
026    
027    import org.granite.tide.invocation.ContextEvent;
028    import org.granite.tide.invocation.ContextUpdate;
029    
030    
031    /**
032     * @author William DRAI
033     */
034    public class TideInvocation {
035        
036        private static ThreadLocal<TideInvocation> invocation = new ThreadLocal<TideInvocation>();
037        
038        public static TideInvocation get() {
039            return invocation.get();
040        }
041        
042        public static TideInvocation init() {
043            TideInvocation ti = new TideInvocation();
044            invocation.set(ti);
045            return ti;
046        }
047        
048        public static void remove() {
049            invocation.remove();
050        }
051        
052        private boolean locked = false;
053        private boolean enabled = false;
054        private boolean updated = false;
055        private boolean evaluated = false;
056        private final List<ContextUpdate> updates = new ArrayList<ContextUpdate>();
057        private final List<ContextUpdate> results = new ArrayList<ContextUpdate>();
058        private final List<ContextEvent> events = new ArrayList<ContextEvent>();
059    
060        
061        public List<ContextUpdate> getUpdates() {
062            return updates;
063        }
064        
065        public List<ContextUpdate> getResults() {
066            return results;
067        }
068        
069        public List<ContextEvent> getEvents() {
070            return events;
071        }
072        
073        public void update(List<ContextUpdate> updates) {
074            this.enabled = true;
075            this.updated = false;
076            this.updates.clear();
077            if (updates != null)
078                this.updates.addAll(updates);
079        }
080        public void updated() {
081            this.updated = true;
082            this.updates.clear();
083        }
084        public boolean isUpdated() {
085            return this.updated;
086        }
087        
088        public void evaluate() {
089            this.evaluated = false;
090            this.results.clear();
091        }
092        public void evaluated(List<ContextUpdate> results) {
093            this.evaluated = true;
094    //        this.results.clear();
095            this.results.addAll(results);
096            this.updated = false;
097            this.updates.clear();
098        }
099        public boolean isEvaluated() {
100            return this.evaluated;
101        }
102        
103        public void addEvent(ContextEvent event) {
104            events.add(event);
105        }
106        
107        public void lock() {
108            this.locked = true;
109        }
110        public void unlock() {
111            this.locked = false;
112        }
113        public boolean isLocked() {
114            return this.locked;
115        }
116        
117        public boolean isEnabled() {
118            return enabled;
119        }
120    }