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.impl;
036
037import java.util.ArrayList;
038import java.util.List;
039import java.util.Map;
040import java.util.Map.Entry;
041import java.util.concurrent.ConcurrentHashMap;
042
043import org.granite.client.tide.Resettable;
044import org.granite.client.tide.ViewScope;
045
046/**
047 * @author William DRAI
048 */
049public class DefaultViewScope implements ViewScope {
050        
051        private Map<String, Object> instanceCache = new ConcurrentHashMap<String, Object>();
052                
053        private GlobalResetter resetter = null;
054        private Map<String, BeanResetter> resettersMap = new ConcurrentHashMap<String, BeanResetter>();
055        
056        private String viewId = null;
057        
058        public DefaultViewScope() {
059        }
060        
061        public String getViewId() {
062                return viewId;
063        }
064        public void setViewId(String viewId) {
065                this.viewId = viewId;
066        }
067        
068        public Object get(String name) {
069                return instanceCache.get(name);
070        }
071        
072        public void put(String name, Object instance) {
073                instanceCache.put(name, instance);
074        }
075        
076        public Object remove(String name) {
077                Object instance = instanceCache.remove(name);
078                
079                if (instance instanceof Resettable)
080                        ((Resettable)instance).reset();
081                
082                if (resettersMap.containsKey(name)) {
083                        resettersMap.get(name).reset(instance);
084                        resettersMap.remove(name);
085                }
086                        
087                if (resetter != null)
088                        resetter.reset(name, instance);
089                
090                return instance;
091        }
092        
093        public void reset(Class<?> type) {
094                List<String> names = new ArrayList<String>();
095                
096                for (Entry<String, Object> entry : instanceCache.entrySet()) {
097                        if (type.isInstance(entry.getValue()))
098                                names.add(entry.getKey());
099                }
100                
101                for (String name : names)
102                        remove(name);
103        }
104        
105        public void reset() {
106                for (Entry<String, Object> entry : instanceCache.entrySet()) {
107                        if (entry.getValue() instanceof Resettable)
108                                ((Resettable)entry.getValue()).reset();
109                        
110                        if (resettersMap.containsKey(entry.getKey()))
111                                resettersMap.get(entry.getKey()).reset(entry.getValue());
112                                
113                        if (resetter != null)
114                                resetter.reset(entry.getKey(), entry.getValue());
115                }
116                
117                instanceCache.clear();
118                resettersMap.clear();
119        }
120        
121        public void setResetter(GlobalResetter resetter) {
122                this.resetter = resetter;
123        }
124
125        public void addResetter(String name, BeanResetter resetter) {
126                this.resettersMap.put(name, resetter);
127        }
128        
129}