001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.uif.util;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.util.type.TypeUtils;
020
021import java.util.List;
022import java.util.Map;
023import java.util.Set;
024
025/**
026 * Utility class for generating JavaScript
027 *
028 * @author Kuali Rice Team (rice.collab@kuali.org)
029 */
030public class ScriptUtils {
031
032    /**
033     * Translates an Object to a String for representing the given Object as
034     * a JavaScript value
035     *
036     * <p>
037     * Handles null, List, Map, and Set collections, along with non quoting for numeric and
038     * boolean types. Complex types are treated as a String value using toString
039     * </p>
040     *
041     * @param value - Object instance to translate
042     * @return String JS value
043     */
044    public static String translateValue(Object value) {
045        String jsValue = "";
046
047        if (value == null) {
048            jsValue = "null";
049            return jsValue;
050        }
051
052        if (value instanceof List) {
053            jsValue = "[";
054
055            List<Object> list = (List<Object>) value;
056            for (Object listItem : list) {
057                jsValue += translateValue(listItem);
058                jsValue += ",";
059            }
060            jsValue = StringUtils.removeEnd(jsValue, ",");
061
062            jsValue += "]";
063        } else if (value instanceof Set) {
064            jsValue = "[";
065
066            Set<Object> set = (Set<Object>) value;
067            for (Object setItem : set) {
068                jsValue += translateValue(setItem);
069                jsValue += ",";
070            }
071            jsValue = StringUtils.removeEnd(jsValue, ",");
072
073            jsValue += "]";
074        } else if (value instanceof Map) {
075            jsValue = "{";
076
077            Map<Object, Object> map = (Map<Object, Object>) value;
078            for (Map.Entry<Object, Object> mapEntry : map.entrySet()) {
079                jsValue += mapEntry.getKey().toString() + ":";
080                jsValue += translateValue(mapEntry.getValue());
081                jsValue += ",";
082            }
083            jsValue = StringUtils.removeEnd(jsValue, ",");
084
085            jsValue += "}";
086        } else {
087            boolean quoteValue = true;
088
089            Class<?> valueClass = value.getClass();
090            if (TypeUtils.isBooleanClass(valueClass) || TypeUtils.isDecimalClass(valueClass) || TypeUtils
091                    .isIntegralClass(valueClass)) {
092                quoteValue = false;
093            }
094
095            if (quoteValue) {
096                jsValue = "\"";
097            }
098
099            // TODO: should this go through property editors?
100            jsValue += value.toString();
101
102            if (quoteValue) {
103                jsValue += "\"";
104            }
105        }
106
107        return jsValue;
108    }
109
110    /**
111     * Escapes the ' character present in collection names so it can be properly used in js without causing
112     * javascript errors due to an early completion of a ' string.
113     * @param name
114     * @return
115     */
116    public static String escapeName(String name) {
117        name = name.replace("'", "\\'");
118        return name;
119    }
120}