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 static org.nuxeo.common.utils.DateUtils.toZonedDateTime; 023 024import java.time.ZonedDateTime; 025import java.time.temporal.Temporal; 026import java.util.Calendar; 027import java.util.Date; 028import java.util.List; 029import java.util.stream.Collectors; 030 031import org.nuxeo.ecm.core.api.NuxeoException; 032import org.nuxeo.ecm.core.query.QueryParseException; 033 034/** 035 * Helper class for {@link Literal} and {@link LiteralList}. 036 * 037 * @since 9.3 038 */ 039public class Literals { 040 041 public static Object valueOf(Operand operand) { 042 if (operand instanceof LiteralList) { 043 return valueOf((LiteralList) operand); 044 } else if (operand instanceof Literal) { 045 return valueOf((Literal) operand); 046 } 047 throw new QueryParseException("Operand is not a Literal neither a LiteralList, op=" + operand); 048 } 049 050 public static List<Object> valueOf(LiteralList litList) { 051 return litList.stream().map(Literals::valueOf).collect(Collectors.toList()); 052 } 053 054 public static Object valueOf(Literal lit) { 055 if (lit instanceof BooleanLiteral) { 056 return valueOf((BooleanLiteral) lit); 057 } else if (lit instanceof DateLiteral) { 058 return valueOf((DateLiteral) lit); 059 } else if (lit instanceof DoubleLiteral) { 060 return valueOf((DoubleLiteral) lit); 061 } else if (lit instanceof IntegerLiteral) { 062 return valueOf((IntegerLiteral) lit); 063 } else if (lit instanceof StringLiteral) { 064 return valueOf((StringLiteral) lit); 065 } 066 throw new QueryParseException("Unknown literal: " + lit); 067 } 068 069 public static Object valueOf(BooleanLiteral lit) { 070 return Boolean.valueOf(lit.value); 071 } 072 073 public static ZonedDateTime valueOf(DateLiteral lit) { 074 return lit.value; // TODO onlyDate 075 } 076 077 public static Double valueOf(DoubleLiteral lit) { 078 return Double.valueOf(lit.value); 079 } 080 081 public static Long valueOf(IntegerLiteral lit) { 082 return Long.valueOf(lit.value); 083 } 084 085 public static String valueOf(StringLiteral lit) { 086 return lit.value; 087 } 088 089 public static Literal toLiteral(Object value) { 090 if (value instanceof Boolean) { 091 return new BooleanLiteral(((Boolean) value).booleanValue()); 092 } else if (value instanceof Calendar) { 093 return new DateLiteral(toZonedDateTime((Calendar) value)); 094 } else if (value instanceof Date) { 095 return new DateLiteral(toZonedDateTime((Date) value)); 096 } else if (value instanceof ZonedDateTime) { 097 return new DateLiteral((ZonedDateTime) value); 098 } else if (value instanceof Temporal) { 099 return new DateLiteral(ZonedDateTime.from((Temporal) value)); 100 } else if (value instanceof Double) { 101 return new DoubleLiteral((Double) value); 102 } else if (value instanceof Float) { 103 return new DoubleLiteral((Float) value); 104 } else if (value instanceof Integer) { 105 return new IntegerLiteral((Integer) value); 106 } else if (value instanceof Long) { 107 return new IntegerLiteral((Long) value); 108 } else if (value instanceof String) { 109 return new StringLiteral((String) value); 110 } 111 throw new NuxeoException("Unknown type to convert to literal, value=" + value); 112 } 113 114}