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.router;
023    
024    
025    import java.lang.reflect.Method;
026    import java.util.List;
027    
028    import com.softwarementors.extjs.djn.DirectJNgineException;
029    import com.softwarementors.extjs.djn.StringUtils;
030    
031    public class RequestException extends DirectJNgineException {
032      private RequestException( String message ) {
033        super(message);
034        
035        assert !StringUtils.isEmpty(message);
036      }
037    
038      private RequestException( String message, Throwable cause ) {
039        super(message, cause);
040        
041        assert !StringUtils.isEmpty(message);
042        assert cause != null;
043      }
044    
045      public static RequestException forJsonElementMustBeAJsonArray(String elementName, String jsonString ) {
046        assert !StringUtils.isEmpty(elementName);
047        assert !StringUtils.isEmpty(jsonString);
048        
049        return new RequestException( "The method arguments must be a json array, but it is not. Json=" + jsonString );
050      }
051    
052      public static RequestException forActionNotFound(String actionName) {
053        assert !StringUtils.isEmpty( actionName );
054        
055        return new RequestException( "No action registered as '" + actionName + "'" );
056      }
057    
058      public static RequestException forActionMethodNotFound(String actionName, String methodName) {
059        assert !StringUtils.isEmpty( actionName );
060        assert !StringUtils.isEmpty( methodName );
061    
062        return new RequestException( "No method registered as '" + methodName + "' in action '" + actionName + "'" );
063      }
064    
065      public static RequestException forRequestBatchMustHaveAtLeastOneRequest() {
066        return new RequestException( "A batched request must have at least one request, but the json array had no elements." );
067      }
068    
069      public static RequestException forRequestBatchItemMustBeAValidJsonObject(int itemPosition) {
070        assert itemPosition >= 0;
071        
072        return new RequestException( "Item " + itemPosition + " in the batched request json array is not a valid json object");
073      }
074    
075      public static RequestException forRequestMustBeAValidJsonObjectOrArray() {
076        return new RequestException( "The request must be a valid json object or array");
077      }
078    
079      public static RequestException forRequestFormatNotRecognized() {
080        return new RequestException( "Unable to recognize the request format.");
081      }
082    
083      public static RequestException forJsonElementMustBeANonNullOrEmptyValue(String elementName, Class<?> primitiveType) {
084        assert !StringUtils.isEmpty( elementName);
085        assert primitiveType != null;
086    
087        return new RequestException( "The json '" + elementName + "' element is missing, null or emtpy, or it is not of type " + primitiveType.getName() + ".");
088      }
089    
090      public static RequestException forJsonElementMissing(String elementName) {
091        assert !StringUtils.isEmpty( elementName);
092        
093        return new RequestException( "The json '" + elementName + "' element is missing.");
094      }
095    
096      public static RequestException forWrongMethodArgumentCount(Method method,
097          int expectedArgumentCount, int realArgumentCount) {
098        assert expectedArgumentCount >= 0;
099        assert realArgumentCount >= 0;
100        
101        return new RequestException( "Wrong call for method '" + method.getName() + "'. Expected '" + expectedArgumentCount + "' arguments, but found '" + realArgumentCount + "'. Note: this can happen sometimes when passing 'undefined' values or just because the JavaScript call was missing some parameters");
102      }
103    
104      public static RequestException forFormPostMissingParameters(List<String> missingParameters) {
105        assert missingParameters != null;
106        assert !missingParameters.isEmpty();
107        
108        return new RequestException( "Form post request is missing the following parameters: " + StringUtils.concatWithSeparator( missingParameters, ", " ) );
109      }
110    
111      public static RequestException forPollEventNotFound(String eventName) {
112        assert !StringUtils.isEmpty( eventName );
113    
114        return new RequestException( "No method registered for poll event '" + eventName + "'" );
115      }
116    
117    }