Search in sources :

Example 6 with FunctionException

use of lucee.runtime.exp.FunctionException in project Lucee by lucee.

the class Some method _call.

public static boolean _call(PageContext pc, Object obj, UDF udf, boolean parallel, int maxThreads, short type) throws PageException {
    ExecutorService execute = null;
    List<Future<Data<Object>>> futures = null;
    if (parallel) {
        execute = Executors.newFixedThreadPool(maxThreads);
        futures = new ArrayList<Future<Data<Object>>>();
    }
    boolean res;
    // Array
    if (type == TYPE_ARRAY) {
        res = invoke(pc, (Array) obj, udf, execute, futures);
    } else // Query
    if (type == TYPE_QUERY) {
        res = invoke(pc, (Query) obj, udf, execute, futures);
    } else // Struct
    if (type == TYPE_STRUCT) {
        res = invoke(pc, (Struct) obj, udf, execute, futures);
    } else // Array
    if (obj instanceof Array) {
        res = invoke(pc, (Array) obj, udf, execute, futures);
    } else // Query
    if (obj instanceof Query) {
        res = invoke(pc, (Query) obj, udf, execute, futures);
    } else // Struct
    if (obj instanceof Struct) {
        res = invoke(pc, (Struct) obj, udf, execute, futures);
    } else // other Iteratorable
    if (obj instanceof Iteratorable) {
        res = invoke(pc, (Iteratorable) obj, udf, execute, futures);
    } else // Map
    if (obj instanceof java.util.Map) {
        res = invoke(pc, (java.util.Map) obj, udf, execute, futures);
    } else // List
    if (obj instanceof List) {
        res = invoke(pc, (List) obj, udf, execute, futures);
    } else // Iterator
    if (obj instanceof Iterator) {
        res = invoke(pc, (Iterator) obj, udf, execute, futures);
    } else // Enumeration
    if (obj instanceof Enumeration) {
        res = invoke(pc, (Enumeration) obj, udf, execute, futures);
    } else // String List
    if (obj instanceof StringListData) {
        res = invoke(pc, (StringListData) obj, udf, execute, futures);
    } else
        throw new FunctionException(pc, "Some", 1, "data", "cannot iterate througth this type " + Caster.toTypeName(obj.getClass()));
    if (parallel)
        res = afterCall(pc, futures, execute);
    return res;
}
Also used : Enumeration(java.util.Enumeration) Query(lucee.runtime.type.Query) FunctionException(lucee.runtime.exp.FunctionException) Struct(lucee.runtime.type.Struct) Array(lucee.runtime.type.Array) StringListData(lucee.runtime.type.util.StringListData) Iteratorable(lucee.runtime.type.Iteratorable) ExecutorService(java.util.concurrent.ExecutorService) ListIterator(java.util.ListIterator) ForEachQueryIterator(lucee.runtime.type.it.ForEachQueryIterator) Iterator(java.util.Iterator) Future(java.util.concurrent.Future) ArrayList(java.util.ArrayList) List(java.util.List)

Example 7 with FunctionException

use of lucee.runtime.exp.FunctionException in project Lucee by lucee.

the class SerializeJSON method _call.

private static String _call(PageContext pc, Object var, Object options, Charset charset) throws PageException {
    try {
        JSONConverter json = new JSONConverter(true, charset);
        if (Decision.isBoolean(options))
            return json.serialize(pc, var, Caster.toBoolean(options));
        if (Decision.isQuery(var)) {
            if (Decision.isSimpleValue(options)) {
                String opt = Caster.toString(options);
                if ("struct".equalsIgnoreCase(opt)) {
                    Array arr = new ArrayImpl();
                    ForEachQueryIterator it = new ForEachQueryIterator((Query) var, pc.getId());
                    try {
                        while (it.hasNext()) {
                            // append each record from the query as a struct
                            arr.append(it.next());
                        }
                    } finally {
                        it.reset();
                    }
                    return json.serialize(pc, arr, false);
                }
            } else if (Decision.isBoolean(options)) {
                return json.serialize(pc, var, Caster.toBoolean(options));
            } else
                throw new FunctionException(pc, SerializeJSON.class.getSimpleName(), 2, "options", "When var is a Query, argument [options] must be either a boolean value or a string with the value of [struct]");
        }
        // var is not a query so options doesn't make a difference here
        return json.serialize(pc, var, false);
    } catch (ConverterException e) {
        throw Caster.toPageException(e);
    }
}
Also used : Array(lucee.runtime.type.Array) ForEachQueryIterator(lucee.runtime.type.it.ForEachQueryIterator) ConverterException(lucee.runtime.converter.ConverterException) ArrayImpl(lucee.runtime.type.ArrayImpl) FunctionException(lucee.runtime.exp.FunctionException) JSONConverter(lucee.runtime.converter.JSONConverter)

Example 8 with FunctionException

use of lucee.runtime.exp.FunctionException in project Lucee by lucee.

the class DateCompare method call.

public static double call(PageContext pc, DateTime left, DateTime right, String datepart) throws ExpressionException {
    datepart = datepart.toLowerCase().trim();
    TimeZone tz = ThreadLocalPageContext.getTimeZone(pc);
    Calendar cLeft = JREDateTimeUtil.getThreadCalendar(tz);
    cLeft.setTime(left);
    Calendar cRight = JREDateTimeUtil.newInstance(tz, Locale.US);
    cRight.setTime(right);
    // TODO WEEEK
    int type = 0;
    if (datepart.equals("s"))
        type = Calendar.SECOND;
    else if (datepart.equals("n"))
        type = Calendar.MINUTE;
    else if (datepart.equals("h"))
        type = Calendar.HOUR;
    else if (datepart.equals("d"))
        type = Calendar.DATE;
    else if (datepart.equals("m"))
        type = Calendar.MONTH;
    else if (datepart.equals("y"))
        type = Calendar.DATE;
    else if (datepart.equals("yyyy"))
        type = Calendar.YEAR;
    else {
        throw new FunctionException(pc, "dateCompare", 3, "datePart", "invalid value [" + datepart + "], valid values has to be [s,n,h,d,m,y,yyyy]");
    }
    // Year
    int value = cLeft.get(Calendar.YEAR) - cRight.get(Calendar.YEAR);
    if (value != 0)
        return value > 0 ? 1 : -1;
    if (Calendar.YEAR == type)
        return 0;
    if (Calendar.YEAR == type)
        return 0;
    // Month
    value = cLeft.get(Calendar.MONTH) - cRight.get(Calendar.MONTH);
    if (value != 0)
        return value > 0 ? 1 : -1;
    if (Calendar.MONTH == type)
        return 0;
    // Day
    value = cLeft.get(Calendar.DATE) - cRight.get(Calendar.DATE);
    if (value != 0)
        return value > 0 ? 1 : -1;
    if (Calendar.DATE == type)
        return 0;
    // Hour
    // print.out(cLeft.get(Calendar.HOUR_OF_DAY)+"-"+cRight.get(Calendar.HOUR_OF_DAY));
    value = cLeft.get(Calendar.HOUR_OF_DAY) - cRight.get(Calendar.HOUR_OF_DAY);
    if (value != 0)
        return value > 0 ? 1 : -1;
    if (Calendar.HOUR == type)
        return 0;
    // Minute
    value = cLeft.get(Calendar.MINUTE) - cRight.get(Calendar.MINUTE);
    if (value != 0)
        return value > 0 ? 1 : -1;
    if (Calendar.MINUTE == type)
        return 0;
    // Second
    value = cLeft.get(Calendar.SECOND) - cRight.get(Calendar.SECOND);
    if (value != 0)
        return value > 0 ? 1 : -1;
    return 0;
}
Also used : TimeZone(java.util.TimeZone) Calendar(java.util.Calendar) FunctionException(lucee.runtime.exp.FunctionException)

Example 9 with FunctionException

use of lucee.runtime.exp.FunctionException in project Lucee by lucee.

the class DateDiff method call.

/**
 * @param pc
 * @param s
 * @param date
 * @param date1
 * @return
 * @throws ExpressionException
 */
public static double call(PageContext pc, String datePart, DateTime left, DateTime right) throws ExpressionException {
    long msLeft = left.getTime();
    long msRight = right.getTime();
    TimeZone tz = ThreadLocalPageContext.getTimeZone(pc);
    // Date Part
    datePart = datePart.toLowerCase().trim();
    int dp;
    if ("s".equals(datePart))
        return diffSeconds(msLeft, msRight);
    else if ("n".equals(datePart))
        return diffSeconds(msLeft, msRight) / 60L;
    else if ("h".equals(datePart))
        return diffSeconds(msLeft, msRight) / 3600L;
    else if ("d".equals(datePart))
        dp = DATEPART_D;
    else if ("y".equals(datePart))
        dp = DATEPART_Y;
    else if ("yyyy".equals(datePart))
        dp = DATEPART_YYYY;
    else if ("m".equals(datePart))
        dp = DATEPART_M;
    else if ("w".equals(datePart))
        dp = DATEPART_W;
    else if ("ww".equals(datePart))
        dp = DATEPART_WW;
    else if ("q".equals(datePart))
        dp = DATEPART_Q;
    else
        throw new FunctionException(pc, "dateDiff", 3, "datePart", "invalid value [" + datePart + "], valid values has to be [q,s,n,h,d,m,y,yyyy,w,ww]");
    // dates
    Calendar _cLeft = JREDateTimeUtil.getThreadCalendar(tz);
    _cLeft.setTimeInMillis(msLeft);
    Calendar _cRight = JREDateTimeUtil.newInstance(tz, Locale.US);
    _cRight.setTimeInMillis(msRight);
    if (msLeft > msRight)
        return -_call(pc, dp, _cRight, msRight, _cLeft, msLeft);
    return _call(pc, dp, _cLeft, msLeft, _cRight, msRight);
// }
}
Also used : TimeZone(java.util.TimeZone) Calendar(java.util.Calendar) FunctionException(lucee.runtime.exp.FunctionException)

Example 10 with FunctionException

use of lucee.runtime.exp.FunctionException in project Lucee by lucee.

the class NumberFormat method intToRoman.

private static String intToRoman(PageContext pc, int value) throws FunctionException {
    if (value == 0)
        throw new FunctionException(pc, "numberFormat", 1, "number", "a roman value can't be 0");
    if (value < 0)
        throw new FunctionException(pc, "numberFormat", 1, "number", "a roman value can't be less than 0");
    if (value > 3999)
        throw new FunctionException(pc, "numberFormat", 1, "number", "a roman value can't be greater than 3999");
    StringBuilder roman = new StringBuilder();
    while (value / 1000 >= 1) {
        roman.append('M');
        value = value - 1000;
    }
    if (value / 900 >= 1) {
        roman.append("CM");
        value = value - 900;
    }
    if (value / 500 >= 1) {
        roman.append("D");
        value = value - 500;
    }
    if (value / 400 >= 1) {
        roman.append("CD");
        value = value - 400;
    }
    while (value / 100 >= 1) {
        roman.append("C");
        value = value - 100;
    }
    if (value / 90 >= 1) {
        roman.append("XC");
        value = value - 90;
    }
    if (value / 50 >= 1) {
        roman.append("L");
        value = value - 50;
    }
    if (value / 40 >= 1) {
        roman.append("XL");
        value = value - 40;
    }
    while (value / 10 >= 1) {
        roman.append("X");
        value = value - 10;
    }
    if (value / 9 >= 1) {
        roman.append("IX");
        value = value - 9;
    }
    if (value / 5 >= 1) {
        roman.append("V");
        value = value - 5;
    }
    if (value / 4 >= 1) {
        roman.append("IV");
        value = value - 4;
    }
    while (value >= 1) {
        roman.append("I");
        value = value - 1;
    }
    return roman.toString();
}
Also used : FunctionException(lucee.runtime.exp.FunctionException)

Aggregations

FunctionException (lucee.runtime.exp.FunctionException)189 BufferedImage (java.awt.image.BufferedImage)123 Image (lucee.runtime.img.Image)17 Struct (lucee.runtime.type.Struct)13 Array (lucee.runtime.type.Array)12 Resource (lucee.commons.io.res.Resource)10 ArrayList (java.util.ArrayList)7 IOException (java.io.IOException)6 Iterator (java.util.Iterator)6 Query (lucee.runtime.type.Query)6 ForEachQueryIterator (lucee.runtime.type.it.ForEachQueryIterator)6 Enumeration (java.util.Enumeration)5 List (java.util.List)5 ListIterator (java.util.ListIterator)5 ExecutorService (java.util.concurrent.ExecutorService)5 Future (java.util.concurrent.Future)5 Iteratorable (lucee.runtime.type.Iteratorable)5 StringListData (lucee.runtime.type.util.StringListData)5 ArrayImpl (lucee.runtime.type.ArrayImpl)4 StructImpl (lucee.runtime.type.StructImpl)4