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.seam;
023    
024    import javax.servlet.http.HttpSession;
025    
026    import org.granite.config.flex.Destination;
027    import org.granite.context.GraniteContext;
028    import org.granite.messaging.webapp.HttpGraniteContext;
029    import org.granite.tide.TideServiceInvoker;
030    import org.granite.tide.hibernate.HibernateValidator;
031    import org.granite.tide.seam.lazy.SeamInitializer;
032    import org.granite.tide.validators.InvalidValue;
033    import org.hibernate.validator.ClassValidator;
034    import org.jboss.seam.Component;
035    import org.jboss.seam.core.Validators;
036    import org.jboss.seam.security.Identity;
037    
038    
039    /**
040     * @author William DRAI
041     */
042    public class SeamServiceInvoker extends TideServiceInvoker<SeamServiceFactory> {
043        
044         
045        public SeamServiceInvoker(Destination destination, SeamServiceFactory factory) {
046            super(destination, factory);
047        }
048        
049        @Override
050        protected void initValidator() {            
051        }
052        
053        
054        @Override
055        public void logout() {
056            HttpGraniteContext context = (HttpGraniteContext)GraniteContext.getCurrentInstance();
057            HttpSession session = context.getSession(false);
058            if (session != null) {
059                Identity identity = Identity.instance();
060                if (identity.isLoggedIn())
061                    identity.logout();
062            }
063        }
064        
065        @Override
066        public Object initializeObject(Object parent, String[] propertyNames) {
067            AbstractSeamServiceContext context = (AbstractSeamServiceContext)getTideContext();
068            
069            SeamInitializer s = (SeamInitializer)context.findComponent("org.granite.tide.seam.seamInitializer", SeamInitializer.class);
070            
071            return s.lazyInitialize(parent, propertyNames);
072        }
073        
074        @Override
075        public InvalidValue[] validateObject(Object entity, String propertyName, Object value) {
076            ClassValidator<?> validator = Validators.instance().getValidator(entity);
077            
078            org.hibernate.validator.InvalidValue[] invalidValues = validator.getPotentialInvalidValues(propertyName, value);
079            return HibernateValidator.convertInvalidValues(invalidValues);
080        }
081        
082        @Override
083        protected AbstractSeamServiceContext lookupContext() {
084            return (AbstractSeamServiceContext)Component.getInstance(AbstractSeamServiceContext.COMPONENT_NAME, true);
085        }
086    }