001    /*
002     * Licensed under the Apache License, Version 2.0 (the "License");
003     * you may not use this file except in compliance with the License.
004     * You may obtain a copy of the License at
005     *
006     * http://www.apache.org/licenses/LICENSE-2.0
007     *
008     * Unless required by applicable law or agreed to in writing, software
009     * distributed under the License is distributed on an "AS IS" BASIS,
010     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011     * See the License for the specific language governing permissions and
012     * limitations under the License.
013     * 
014     * See the NOTICE file distributed with this work for additional
015     * information regarding copyright ownership.
016     */
017    
018    package com.osbcp.apbs;
019    
020    import java.io.Serializable;
021    
022    import com.osbcp.apbs.events.AjaxPostBackEvent;
023    import com.osbcp.jjsw.JavaScript;
024    
025    /**
026     * A specific JavaScript, used to call the <i>$apb</i> JavaScript function that performs the AJAX request.
027     * 
028     * @author <a href="mailto:christoffer@christoffer.me">Christoffer Pettersson</a>
029     */
030    
031    public abstract class AjaxPostBackCaller extends JavaScript {
032    
033            private static final long serialVersionUID = 1L;
034    
035            /**
036             * Creates a new instance of an AjaxPostBackCaller instance, used to call the <i>$apb</i> JavaScript function that performs an AJAX request.
037             * 
038             * @param preScript Any JavaScript that should be executed before the AJAX request.
039             * @param postScript Any JavaScript that should be executed before the event's own JavaScript response is executed.
040             * @param eventClass The event that should be triggered.
041             * @param arguments Any arguments that should be sent to the event, should be the same argument types as the event's constructor.
042             */
043    
044            public AjaxPostBackCaller(final JavaScript preScript, final JavaScript postScript, final Class<? extends AjaxPostBackEvent> eventClass, final Serializable... arguments) {
045    
046                    String preScriptString = !JavaScript.isEmpty(preScript) ? preScript.toString(TYPE.DOWNGRADE) : "";
047                    String postScriptString = !JavaScript.isEmpty(postScript) ? postScript.toString(TYPE.DOWNGRADE) : "";
048                    String eventName = eventClass.getName();
049                    String argumentString = argumentsToString(arguments);
050    
051                    append("$apb('" + preScriptString + "','" + postScriptString + "','" + eventName + "',[" + argumentString + "]);");
052    
053            }
054    
055            /**
056             * Takes in an array of method arguments, and returns a String comma separated JavaScript friendly list
057             * 
058             * @param arguments Argument values
059             * @return Returns a String comma separated JavaScript friendly list
060             */
061    
062            private static String argumentsToString(final Serializable... arguments) {
063    
064                    StringBuilder output = new StringBuilder();
065    
066                    // For each argument
067                    for (int i = 0; i < arguments.length; i++) {
068    
069                            // Get the argument
070                            Object argument = arguments[i];
071    
072                            if (argument == null) {
073    
074                                    // If the argument is null, encode it into a 'null' String to be decoded later
075                                    output.append("'null'");
076    
077                            } else if (argument instanceof JavaScript) {
078    
079                                    // Since it's a JavaScrip, conjugate it into the JavaScript variable
080                                    output.append(argument);
081    
082                            } else {
083    
084                                    // URL encode the argument
085                                    String escaped = argument.toString();
086                                    //                              try {
087                                    //                                      escaped = URLEncoder.encode(argument.toString(), "UTF-8");
088                                    //                              } catch (UnsupportedEncodingException e) {
089                                    //                              }
090    
091                                    // Append it to the URL
092                                    output.append("'" + escaped + "'");
093    
094                            }
095    
096                            // Add a colon unless it is the last item
097                            if (i < (arguments.length - 1)) {
098                                    output.append(", ");
099                            }
100    
101                    }
102    
103                    // Return the trimmed list
104                    return output.toString().trim();
105    
106            }
107    
108    }