001    /*
002     * Copyright © 2008, 2012 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 Lesser General Public License as published by
008     * the Free Software Foundation, either version 3 of the License.
009     *
010     * Commercial use is permitted to the extent that the code/component(s)
011     * do NOT become part of another Open Source or Commercially developed
012     * licensed development library or toolkit without explicit permission.
013     *
014     * DirectJNgine is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017     * GNU Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public License
020     * along with DirectJNgine.  If not, see <http://www.gnu.org/licenses/>.
021     * 
022     * This software uses the ExtJs library (http://extjs.com), which is 
023     * distributed under the GPL v3 license (see http://extjs.com/license).
024     */
025    
026    package com.softwarementors.extjs.djn.api;
027    
028    import java.lang.reflect.Method;
029    import java.util.ArrayList;
030    import java.util.HashMap;
031    import java.util.List;
032    import java.util.Map;
033    
034    import com.softwarementors.extjs.djn.StringUtils;
035    
036    import edu.umd.cs.findbugs.annotations.NonNull;
037    
038    public class RegisteredAction {
039    
040      @NonNull private Map<String, RegisteredPollMethod> pollMethods = new HashMap<String, RegisteredPollMethod>();
041      @NonNull private Class<?> actionClass;
042      @NonNull private String name;
043      @NonNull private Map<String, RegisteredStandardMethod> standardMethodsByName = new HashMap<String,RegisteredStandardMethod>();
044      @NonNull private RegisteredApi api;
045      
046      /* package */ RegisteredAction( RegisteredApi api, Class<?> actionClass, String name ) {
047        assert api != null;
048        assert actionClass != null;
049        assert !StringUtils.isEmpty(name);
050        
051        this.actionClass = actionClass;
052        this.name = name;
053        this.api = api;
054      }
055      
056      public RegisteredApi getApi() {
057        return this.api;
058      }
059      
060      public Registry getRegistry() {
061        return getApi().getRegistry();
062      }
063      
064      public RegisteredPollMethod addPollMethod(String name, Method method) {
065        assert !StringUtils.isEmpty(name);
066        assert method != null;
067        assert RegisteredPollMethod.isValidPollMethod(method);
068        
069        assert !getRegistry().hasPollMethod(name);
070        RegisteredPollMethod result = new RegisteredPollMethod( this, name, method );
071        this.pollMethods.put( name, result );
072        getRegistry().registerPollMethod( result );
073        if( Registry.logger.isDebugEnabled()) {
074          Registry.logger.debug( "  - Registered new Poll Method. Name: '" + name + "', Java method: '" + result.getActionClass() + "." + result.getMethod().getName() + "'" );
075        }
076        return result;
077      }
078      
079      public RegisteredPollMethod getPollMethod(String eventName) {
080        assert !StringUtils.isEmpty(eventName);
081    
082        return this.pollMethods.get(eventName);
083      }
084      
085      public List<RegisteredPollMethod> getPollMethods() {
086        return new ArrayList<RegisteredPollMethod>(this.pollMethods.values());
087      }
088    
089      public RegisteredStandardMethod addStandardMethod( String name, Method method, boolean formHandler) {
090        assert !StringUtils.isEmpty(name);
091        assert method != null;
092        
093        RegisteredStandardMethod result = new RegisteredStandardMethod( this, name, method, formHandler);
094        this.standardMethodsByName.put( name, result );
095        
096        
097        if( Registry.logger.isDebugEnabled() ) {
098          String type = "Standard";
099          if( formHandler) {
100            type = "Form";
101          }
102            
103          Registry.logger.debug( "  - Registered new " + type + " Method. Name: '" + result.getFullName() + "'. Java method: '" + getActionClass().getName() + "." + method.getName() + "'" );
104        }
105        return result;
106      }
107      
108      public RegisteredStandardMethod getStandardMethod(String methodName) {
109        assert !StringUtils.isEmpty(methodName);
110        
111        return this.standardMethodsByName.get( methodName );
112      }
113    
114      public boolean hasStandardMethod(String method) {
115        assert !StringUtils.isEmpty(method);
116        
117        return this.standardMethodsByName.containsKey(method);
118      }
119    
120      public List<RegisteredStandardMethod> getStandardMethods() {
121        return new ArrayList<RegisteredStandardMethod>( this.standardMethodsByName.values() );
122      }
123    
124      public Class<?> getActionClass() {
125        return this.actionClass;
126      }
127      
128      public static final String NAME_PROPERTY = "name";
129      
130      public String getName() {
131        return this.name;
132      }
133    
134      /*package*/ void addStandardMethod(RegisteredStandardMethod method) {
135        assert method != null;
136        assert !this.standardMethodsByName.containsKey(method.getName());
137        
138        this.standardMethodsByName.put( method.getName(), method );
139      }
140    
141      public String getFullJavaClassName() {
142        return getActionClass().getName();
143      }
144      
145    }