001    /*
002     * Copyright © 2008, 2009 Pedro Agulló Soliveres.
003     * 
004     * This file is part of DirectJNgine.
005     *
006     * DirectJNgine is free software: you can redistribute it and/or modify
007     * it under the terms of the GNU General Public License as published by
008     * the Free Software Foundation, either version 3 of the License.
009     *
010     * DirectJNgine is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with DirectJNgine.  If not, see <http://www.gnu.org/licenses/>.
017     * 
018     * This software uses the ExtJs library (http://extjs.com), which is 
019     * distributed under the GPL v3 license (see http://extjs.com/license).
020     */
021    
022    package com.softwarementors.extjs.djn.config;
023    
024    import java.lang.reflect.Method;
025    import java.util.Map;
026    
027    import org.apache.log4j.Logger;
028    
029    import com.softwarementors.extjs.djn.StringUtils;
030    
031    public class RegisteredMethod {
032    
033      private static Logger logger = Logger.getLogger( RegisteredMethod.class);
034      
035      private RegisteredAction action;
036      private String name;
037      private Method method;
038      private boolean formHandler;
039      
040      /* package */ RegisteredMethod( RegisteredAction action, String name, Method method, boolean formHandler ) {
041        assert action != null;
042        assert !StringUtils.isEmpty(name);
043        assert method != null;
044        
045        this.action = action;
046        this.name = name;
047        this.method = method;
048        this.formHandler = formHandler;
049        
050        if( this.formHandler ) {
051          if( method.getParameterTypes().length != 2 || !method.getParameterTypes()[0].equals( Map.class) || !method.getParameterTypes()[1].equals( Map.class) ) {
052            ApiConfigurationException ex = ApiConfigurationException.forMethodHasWrongParametersForAFormHandler( this );
053            logger.fatal( ex.getMessage(), ex );
054            throw ex;
055          }
056        }
057      }
058      
059      public RegisteredAction getAction() {
060        return this.action;
061      }
062      
063      public String getName() {
064        return this.name;
065      }
066    
067      public Method getMethod() {
068        return this.method;
069      }
070    
071      public Class<?>[] getParameterTypes() {
072        return this.method.getParameterTypes();
073      }
074      
075      public int getParameterCount() {
076        return getParameterTypes().length;
077      }
078    
079      public Class<?> getReturnType() {
080        return this.method.getReturnType();
081      }
082      
083      public boolean getFormHandler() {
084        return this.formHandler;
085      }
086    
087      public String getActionName() {
088        return getAction().getName();
089      }
090      
091      public String getFullName() {
092        return getActionName() + "." + getName();
093      }
094    
095      public Class<?> getActionClass() {
096        return getAction().getActionClass();
097      }
098    
099      public int getClientParameterCount() {
100        // If this is a form handler, then the client must pass just one parameter: 'myForm.getForm().el',
101        // else we expecte as many client call parameters as the Java method expects
102        if( getFormHandler() ) {
103          return 1;
104        }
105        return getParameterCount();
106      }
107    }