001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2007-2010 ADEQUATE SYSTEMS SARL
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        private static final long serialVersionUID = 1L;
044        
045         
046        public SeamServiceInvoker(Destination destination, SeamServiceFactory factory) {
047            super(destination, factory);
048        }
049        
050        @Override
051        protected void initValidator() {            
052        }
053        
054        
055        @Override
056        public void logout() {
057            HttpGraniteContext context = (HttpGraniteContext)GraniteContext.getCurrentInstance();
058            HttpSession session = context.getSession(false);
059            if (session != null) {
060                Identity identity = Identity.instance();
061                if (identity.isLoggedIn())
062                    identity.logout();
063            }
064        }
065        
066        @Override
067        public Object initializeObject(Object parent, String[] propertyNames) {
068            AbstractSeamServiceContext context = (AbstractSeamServiceContext)getTideContext();
069            
070            SeamInitializer s = (SeamInitializer)context.findComponent("org.granite.tide.seam.seamInitializer", SeamInitializer.class);
071            
072            return s.lazyInitialize(parent, propertyNames);
073        }
074        
075        @Override
076        public InvalidValue[] validateObject(Object entity, String propertyName, Object value) {
077            ClassValidator<?> validator = Validators.instance().getValidator(entity);
078            
079            org.hibernate.validator.InvalidValue[] invalidValues = validator.getPotentialInvalidValues(propertyName, value);
080            return HibernateValidator.convertInvalidValues(invalidValues);
081        }
082        
083        @Override
084        protected AbstractSeamServiceContext lookupContext() {
085            return (AbstractSeamServiceContext)Component.getInstance(AbstractSeamServiceContext.COMPONENT_NAME, true);
086        }
087    }