001/*
002 * (C) Copyright 2017 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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 *
016 * Contributors:
017 *     Kevin Leturc <kleturc@nuxeo.com>
018 *
019 */
020package org.nuxeo.ecm.core.query.sql.model;
021
022import java.time.ZonedDateTime;
023import java.time.temporal.Temporal;
024import java.util.Calendar;
025import java.util.Date;
026import java.util.List;
027import java.util.stream.Collectors;
028
029import org.joda.time.DateTime;
030import org.nuxeo.ecm.core.api.NuxeoException;
031import org.nuxeo.ecm.core.query.QueryParseException;
032
033/**
034 * Helper class for {@link Literal} and {@link LiteralList}.
035 *
036 * @since 9.3
037 */
038public class Literals {
039
040    public static Object valueOf(Operand operand) {
041        if (operand instanceof LiteralList) {
042            return valueOf((LiteralList) operand);
043        } else if (operand instanceof Literal) {
044            return valueOf((Literal) operand);
045        }
046        throw new QueryParseException("Operand is not a Literal neither a LiteralList, op=" + operand);
047    }
048
049    public static List<Object> valueOf(LiteralList litList) {
050        return litList.stream().map(Literals::valueOf).collect(Collectors.toList());
051    }
052
053    public static Object valueOf(Literal lit) {
054        if (lit instanceof BooleanLiteral) {
055            return valueOf((BooleanLiteral) lit);
056        } else if (lit instanceof DateLiteral) {
057            return valueOf((DateLiteral) lit);
058        } else if (lit instanceof DoubleLiteral) {
059            return valueOf((DoubleLiteral) lit);
060        } else if (lit instanceof IntegerLiteral) {
061            return valueOf((IntegerLiteral) lit);
062        } else if (lit instanceof StringLiteral) {
063            return valueOf((StringLiteral) lit);
064        }
065        throw new QueryParseException("Unknown literal: " + lit);
066    }
067
068    public static Object valueOf(BooleanLiteral lit) {
069        return Boolean.valueOf(lit.value);
070    }
071
072    public static ZonedDateTime valueOf(DateLiteral lit) {
073        return lit.value.toGregorianCalendar().toZonedDateTime(); // TODO onlyDate
074    }
075
076    public static Double valueOf(DoubleLiteral lit) {
077        return Double.valueOf(lit.value);
078    }
079
080    public static Long valueOf(IntegerLiteral lit) {
081        return Long.valueOf(lit.value);
082    }
083
084    public static String valueOf(StringLiteral lit) {
085        return lit.value;
086    }
087
088    public static Literal toLiteral(Object value) {
089        if (value instanceof Boolean) {
090            return new BooleanLiteral(((Boolean) value).booleanValue());
091        } else if (value instanceof Calendar || value instanceof Date || value instanceof Temporal
092                || value instanceof DateTime) {
093            return new DateLiteral(new DateTime(value));
094        } else if (value instanceof Double) {
095            return new DoubleLiteral((Double) value);
096        } else if (value instanceof Float) {
097            return new DoubleLiteral((Float) value);
098        } else if (value instanceof Integer) {
099            return new IntegerLiteral((Integer) value);
100        } else if (value instanceof Long) {
101            return new IntegerLiteral((Long) value);
102        } else if (value instanceof String) {
103            return new StringLiteral((String) value);
104        }
105        throw new NuxeoException("Unknown type to convert to literal, value=" + value);
106    }
107
108}