Repository: metamodel Updated Branches: refs/heads/master 7cba3cb37 -> 3a0ae0930
#METAMODEL-172: Fixed Project: http://git-wip-us.apache.org/repos/asf/metamodel/repo Commit: http://git-wip-us.apache.org/repos/asf/metamodel/commit/0fba6fa5 Tree: http://git-wip-us.apache.org/repos/asf/metamodel/tree/0fba6fa5 Diff: http://git-wip-us.apache.org/repos/asf/metamodel/diff/0fba6fa5 Branch: refs/heads/master Commit: 0fba6fa5775c03704771bec81ab13531aa96d498 Parents: 7cba3cb Author: Alberto Rodriguez <[email protected]> Authored: Fri Aug 28 08:16:33 2015 +0200 Committer: Alberto Rodriguez <[email protected]> Committed: Fri Aug 28 08:16:33 2015 +0200 ---------------------------------------------------------------------- .../ElasticSearchDateConverter.java | 41 +++++++++++++ .../elasticsearch/ElasticSearchUtils.java | 16 ++++- .../ElasticSearchDateConverterTest.java | 34 +++++++++++ .../elasticsearch/ElasticSearchUtilsTest.java | 63 ++++++++++++++++++++ 4 files changed, 152 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/metamodel/blob/0fba6fa5/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchDateConverter.java ---------------------------------------------------------------------- diff --git a/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchDateConverter.java b/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchDateConverter.java new file mode 100644 index 0000000..fcf0b24 --- /dev/null +++ b/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchDateConverter.java @@ -0,0 +1,41 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.metamodel.elasticsearch; + +import org.apache.metamodel.util.TimeComparator; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * Util class to convert date strings from ElasticSearch to + * proper java Dates. + */ +final class ElasticSearchDateConverter { + + public static Date tryToConvert(String dateAsString) { + try { + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX"); + return dateFormat.parse(dateAsString); + } catch (ParseException e) { + return TimeComparator.toDate(dateAsString); + } + } +} http://git-wip-us.apache.org/repos/asf/metamodel/blob/0fba6fa5/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchUtils.java ---------------------------------------------------------------------- diff --git a/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchUtils.java b/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchUtils.java index 9cd34f7..20f56df 100644 --- a/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchUtils.java +++ b/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchUtils.java @@ -18,6 +18,7 @@ */ package org.apache.metamodel.elasticsearch; +import java.util.Date; import java.util.Map; import org.apache.metamodel.data.DataSetHeader; @@ -25,6 +26,8 @@ import org.apache.metamodel.data.DefaultRow; import org.apache.metamodel.data.Row; import org.apache.metamodel.query.SelectItem; import org.apache.metamodel.schema.Column; +import org.apache.metamodel.schema.ColumnType; +import org.apache.metamodel.util.TimeComparator; /** * Shared/common util functions for the ElasticSearch MetaModel module. @@ -44,8 +47,17 @@ final class ElasticSearchUtils { values[i] = documentId; } else { Object value = sourceMap.get(column.getName()); - - values[i] = value; + + if (column.getType() == ColumnType.DATE) { + Date valueToDate = ElasticSearchDateConverter.tryToConvert((String) value); + if (valueToDate == null) { + values[i] = value; + } else { + values[i] = valueToDate; + } + } else { + values[i] = value; + } } } http://git-wip-us.apache.org/repos/asf/metamodel/blob/0fba6fa5/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchDateConverterTest.java ---------------------------------------------------------------------- diff --git a/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchDateConverterTest.java b/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchDateConverterTest.java new file mode 100644 index 0000000..b283bd4 --- /dev/null +++ b/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchDateConverterTest.java @@ -0,0 +1,34 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.metamodel.elasticsearch; + +import junit.framework.TestCase; + +import java.util.Date; + +public class ElasticSearchDateConverterTest extends TestCase { + + public void testConvertDateOptionalTime() throws Exception { + String dateToConvert = "2013-01-04T15:55:51.217+01:00"; + Date date = ElasticSearchDateConverter.tryToConvert(dateToConvert); + + assertNotNull(date); + assertTrue(date.toString().startsWith("Fri Jan 04")); + } +} http://git-wip-us.apache.org/repos/asf/metamodel/blob/0fba6fa5/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchUtilsTest.java ---------------------------------------------------------------------- diff --git a/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchUtilsTest.java b/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchUtilsTest.java new file mode 100644 index 0000000..acece88 --- /dev/null +++ b/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchUtilsTest.java @@ -0,0 +1,63 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.metamodel.elasticsearch; + +import junit.framework.TestCase; +import org.apache.metamodel.data.DataSetHeader; +import org.apache.metamodel.data.Row; +import org.apache.metamodel.data.SimpleDataSetHeader; +import org.apache.metamodel.query.SelectItem; +import org.apache.metamodel.schema.ColumnType; +import org.apache.metamodel.schema.MutableColumn; + +import java.util.*; + +public class ElasticSearchUtilsTest extends TestCase { + + public void testAssignDocumentIdForPrimaryKeys() throws Exception { + MutableColumn primaryKeyColumn = new MutableColumn("value1", ColumnType.STRING).setPrimaryKey(true); + SelectItem primaryKeyItem = new SelectItem(primaryKeyColumn); + List<SelectItem> selectItems1 = Arrays.asList(primaryKeyItem); + String documentId = "doc1"; + DataSetHeader header = new SimpleDataSetHeader(selectItems1); + Map values = new HashMap<String, Object>(); + values.put("value1", "theValue"); + Row row = ElasticSearchUtils.createRow(values, documentId, header); + String primaryKeyValue = (String) row.getValue(primaryKeyItem); + + assertEquals(primaryKeyValue, documentId); + } + + public void testCreateRowWithParseableDates() throws Exception { + SelectItem item1 = new SelectItem(new MutableColumn("value1", ColumnType.STRING)); + SelectItem item2 = new SelectItem(new MutableColumn("value2", ColumnType.DATE)); + List<SelectItem> selectItems1 = Arrays.asList(item1, item2); + String documentId = "doc1"; + DataSetHeader header = new SimpleDataSetHeader(selectItems1); + Map values = new HashMap<String, Object>(); + values.put("value1", "theValue"); + values.put("value2", "2013-01-04T15:55:51.217+01:00"); + Row row = ElasticSearchUtils.createRow(values, documentId, header); + Object stringValue = row.getValue(item1); + Object dateValue = row.getValue(item2); + + assertTrue(stringValue instanceof String); + assertTrue(dateValue instanceof Date); + } +}
