001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.tide.seam;
022    
023    import javax.servlet.http.HttpSession;
024    
025    import org.granite.config.flex.Destination;
026    import org.granite.context.GraniteContext;
027    import org.granite.messaging.webapp.HttpGraniteContext;
028    import org.granite.tide.TideServiceInvoker;
029    import org.granite.tide.hibernate.HibernateValidator;
030    import org.granite.tide.seam.lazy.SeamInitializer;
031    import org.granite.tide.validators.InvalidValue;
032    import org.hibernate.validator.ClassValidator;
033    import org.jboss.seam.Component;
034    import org.jboss.seam.core.Validators;
035    import org.jboss.seam.security.Identity;
036    
037    
038    /**
039     * @author William DRAI
040     */
041    public class SeamServiceInvoker extends TideServiceInvoker<SeamServiceFactory> {
042        
043         
044        public SeamServiceInvoker(Destination destination, SeamServiceFactory factory) {
045            super(destination, factory);
046        }
047        
048        @Override
049        protected void initValidator() {            
050        }
051        
052        
053        @Override
054        public void logout() {
055            HttpGraniteContext context = (HttpGraniteContext)GraniteContext.getCurrentInstance();
056            HttpSession session = context.getSession(false);
057            if (session != null) {
058                Identity identity = Identity.instance();
059                if (identity.isLoggedIn())
060                    identity.logout();
061            }
062        }
063        
064        @Override
065        public Object initializeObject(Object parent, String[] propertyNames) {
066            AbstractSeamServiceContext context = (AbstractSeamServiceContext)getTideContext();
067            
068            SeamInitializer s = (SeamInitializer)context.findComponent("org.granite.tide.seam.seamInitializer", SeamInitializer.class);
069            
070            return s.lazyInitialize(parent, propertyNames);
071        }
072        
073        @Override
074        public InvalidValue[] validateObject(Object entity, String propertyName, Object value) {
075            ClassValidator<?> validator = Validators.instance().getValidator(entity);
076            
077            org.hibernate.validator.InvalidValue[] invalidValues = validator.getPotentialInvalidValues(propertyName, value);
078            return HibernateValidator.convertInvalidValues(invalidValues);
079        }
080        
081        @Override
082        protected AbstractSeamServiceContext lookupContext() {
083            return (AbstractSeamServiceContext)Component.getInstance(AbstractSeamServiceContext.COMPONENT_NAME, true);
084        }
085    }