You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@oodt.apache.org by "Mattmann, Chris A (3980)" <ch...@jpl.nasa.gov> on 2014/05/02 18:40:27 UTC

Re: svn commit: r1591965 - in /oodt/trunk/filemgr/src: main/java/org/apache/oodt/cas/filemgr/metadata/extractors/examples/DateTimeExpandMetExtractor.java test/org/apache/oodt/cas/filemgr/metadata/extractors/examples/TestDateTimeExpandMetExtractor.java

Brian, you are awesome!

Can you create JIRA issues for some of these so they show up in
CHANGES.txt? I know being at Google didn't make you forget that! :)

Cheers,
Chris



-----Original Message-----
From: "bfoster@apache.org" <bf...@apache.org>
Reply-To: "dev@oodt.apache.org" <de...@oodt.apache.org>
Date: Friday, May 2, 2014 9:32 AM
To: "commits@oodt.apache.org" <co...@oodt.apache.org>
Subject: svn commit: r1591965 - in /oodt/trunk/filemgr/src:
main/java/org/apache/oodt/cas/filemgr/metadata/extractors/examples/DateTime
ExpandMetExtractor.java
test/org/apache/oodt/cas/filemgr/metadata/extractors/examples/TestDateTimeE
xpandMetExtractor.java

>Author: bfoster
>Date: Fri May  2 16:32:32 2014
>New Revision: 1591965
>
>URL: http://svn.apache.org/r1591965
>Log:
>-Added new Filemgr MetExtractor which expands a given metadata date time
>field out into multiple metadata fields (i.e. year, month, day, hour,
>minute, and second) to enable more fine tune queries.
>
>Added:
>    
>oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/metadata/extr
>actors/examples/DateTimeExpandMetExtractor.java   (with props)
>    
>oodt/trunk/filemgr/src/test/org/apache/oodt/cas/filemgr/metadata/extractor
>s/examples/TestDateTimeExpandMetExtractor.java   (with props)
>
>Added: 
>oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/metadata/extr
>actors/examples/DateTimeExpandMetExtractor.java
>URL: 
>http://svn.apache.org/viewvc/oodt/trunk/filemgr/src/main/java/org/apache/o
>odt/cas/filemgr/metadata/extractors/examples/DateTimeExpandMetExtractor.ja
>va?rev=1591965&view=auto
>==========================================================================
>====
>--- 
>oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/metadata/extr
>actors/examples/DateTimeExpandMetExtractor.java (added)
>+++ 
>oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/metadata/extr
>actors/examples/DateTimeExpandMetExtractor.java Fri May  2 16:32:32 2014
>@@ -0,0 +1,112 @@
>+/*
>+ * 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.oodt.cas.filemgr.metadata.extractors.examples;
>+
>+// OODT imports
>+import 
>org.apache.oodt.cas.filemgr.metadata.extractors.AbstractFilemgrMetExtracto
>r;
>+import org.apache.oodt.cas.filemgr.structs.Product;
>+import org.apache.oodt.cas.metadata.Metadata;
>+import org.apache.oodt.cas.metadata.exceptions.MetExtractionException;
>+import org.apache.oodt.cas.metadata.util.PathUtils;
>+
>+// Google imports
>+import com.google.common.base.Strings;
>+
>+/**
>+ * Expands a given metadata date time field out into multiple metadata
>fields (i.e. year, month,
>+ * day, hour, minute, and second).
>+ * 
>+ * @author bfoster@apache.org (Brian Foster)
>+ */
>+public class DateTimeExpandMetExtractor extends
>AbstractFilemgrMetExtractor {
>+
>+  private static final String FULL_DATE_TIME_KEY = "FullDateTimeKey";
>+  private static final String FULL_DATE_TIME_FORMAT =
>"FullDateTimeFormat";
>+
>+  private static final String YEAR_KEY = "YearKey";
>+  private static final String MONTH_KEY = "MonthKey";
>+  private static final String DAY_KEY = "DayKey";
>+  private static final String HOUR_KEY = "HourKey";
>+  private static final String MINUTE_KEY = "MinuteKey";
>+  private static final String SECOND_KEY = "SecondKey";
>+
>+  private String fullDateTimeKey;
>+  private String fullDateTimeFormat;
>+  private String yearKey;
>+  private String monthKey;
>+  private String dayKey;
>+  private String hourKey;
>+  private String minuteKey;
>+  private String secondKey;
>+
>+  @Override
>+  public void doConfigure() {
>+    fullDateTimeKey = getKey(FULL_DATE_TIME_KEY);
>+    fullDateTimeFormat = getKey(FULL_DATE_TIME_FORMAT);
>+    yearKey = getKey(YEAR_KEY);
>+    monthKey = getKey(MONTH_KEY);
>+    dayKey = getKey(DAY_KEY);
>+    hourKey = getKey(HOUR_KEY);
>+    minuteKey = getKey(MINUTE_KEY);
>+    secondKey = getKey(SECOND_KEY);
>+  }
>+
>+  @Override
>+  public Metadata doExtract(Product product, Metadata metadata) throws
>MetExtractionException {
>+    String fullDateTime = getFullDateTime(metadata);
>+
>+    Metadata extractMetadata = new Metadata();
>+    createDateField(extractMetadata, yearKey, fullDateTime,
>fullDateTimeFormat, "yyyy");
>+    createDateField(extractMetadata, monthKey, fullDateTime,
>fullDateTimeFormat, "MM");
>+    createDateField(extractMetadata, dayKey, fullDateTime,
>fullDateTimeFormat, "dd");
>+    createDateField(extractMetadata, hourKey, fullDateTime,
>fullDateTimeFormat, "HH");
>+    createDateField(extractMetadata, minuteKey, fullDateTime,
>fullDateTimeFormat, "mm");
>+    createDateField(extractMetadata, secondKey, fullDateTime,
>fullDateTimeFormat, "ss");
>+    return extractMetadata;
>+  }
>+
>+  private String getKey(String key) {
>+    if (configuration.containsKey(key)) {
>+      return configuration.getProperty(key);
>+    } else {
>+      return null;
>+    }
>+  }
>+
>+  private String getFullDateTime(Metadata metadata)
>+       throws MetExtractionException {
>+    if (!Strings.isNullOrEmpty(fullDateTimeKey)) {
>+      return metadata.getMetadata(fullDateTimeKey);
>+    } else {
>+      throw new MetExtractionException("Failed to find DateTimeKey " +
>fullDateTimeKey);
>+    }
>+  }
>+
>+  private void createDateField(Metadata metadata, String fieldKey,
>String fullDateTime,
>+      String fullDateTimeFormat, String fieldFormat) throws
>MetExtractionException {
>+    if (!Strings.isNullOrEmpty(fieldKey)) {
>+      try {
>+        metadata.addMetadata(fieldKey,
>PathUtils.doDynamicReplacement(String.format(
>+            "[FORMAT(%s,%s,%s)]", fullDateTimeFormat, fullDateTime,
>fieldFormat)));
>+      } catch (Exception e) {
>+        throw new MetExtractionException(String.format(
>+            "Failed to create field for key %s from fullDateTime %s and
>fullDateTimeFormat %s",
>+            fieldKey, fullDateTime, fullDateTimeFormat), e);
>+      }
>+    }
>+  }
>+}
>
>Propchange: 
>oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/metadata/extr
>actors/examples/DateTimeExpandMetExtractor.java
>--------------------------------------------------------------------------
>----
>    svn:mime-type = text/plain
>
>Added: 
>oodt/trunk/filemgr/src/test/org/apache/oodt/cas/filemgr/metadata/extractor
>s/examples/TestDateTimeExpandMetExtractor.java
>URL: 
>http://svn.apache.org/viewvc/oodt/trunk/filemgr/src/test/org/apache/oodt/c
>as/filemgr/metadata/extractors/examples/TestDateTimeExpandMetExtractor.jav
>a?rev=1591965&view=auto
>==========================================================================
>====
>--- 
>oodt/trunk/filemgr/src/test/org/apache/oodt/cas/filemgr/metadata/extractor
>s/examples/TestDateTimeExpandMetExtractor.java (added)
>+++ 
>oodt/trunk/filemgr/src/test/org/apache/oodt/cas/filemgr/metadata/extractor
>s/examples/TestDateTimeExpandMetExtractor.java Fri May  2 16:32:32 2014
>@@ -0,0 +1,125 @@
>+/*
>+ * 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.oodt.cas.filemgr.metadata.extractors.examples;
>+
>+// JUnit static imports
>+import static org.hamcrest.Matchers.is;
>+import static org.junit.Assert.assertThat;
>+import static org.mockito.Mockito.when;
>+
>+// JDK imports
>+import java.util.Properties;
>+
>+// OODT imports
>+import org.apache.oodt.cas.metadata.Metadata;
>+import org.apache.oodt.cas.metadata.exceptions.MetExtractionException;
>+
>+// JUnit imports
>+import org.junit.Before;
>+import org.junit.Rule;
>+import org.junit.Test;
>+import org.junit.rules.ExpectedException;
>+import org.junit.runner.RunWith;
>+import org.junit.runners.JUnit4;
>+
>+// Mockito imports
>+import org.mockito.Mock;
>+import org.mockito.MockitoAnnotations;
>+
>+/**
>+ * Test class for {@link DateTimeExpandMetExtractor}.
>+ * 
>+ * @author bfoster@apache.org (Brian Foster)
>+ */
>+@RunWith(JUnit4.class)
>+public class TestDateTimeExpandMetExtractor {
>+
>+  @Rule public ExpectedException expectedException =
>ExpectedException.none();
>+
>+  @Mock private Properties configuration;
>+  @Mock private Metadata metadata;
>+
>+  private DateTimeExpandMetExtractor extractor;
>+
>+  @Before
>+  public void setUp() {
>+    MockitoAnnotations.initMocks(this);
>+
>+    
>when(configuration.getProperty("FullDateTimeKey")).thenReturn("StartDateTi
>me");
>+    
>when(configuration.getProperty("FullDateTimeFormat")).thenReturn("yyyy-MM-
>dd HH:mm:ss");
>+    when(configuration.getProperty("YearKey")).thenReturn("StartYear");
>+    when(configuration.getProperty("MonthKey")).thenReturn("StartMonth");
>+    when(configuration.getProperty("DayKey")).thenReturn("StartDay");
>+    when(configuration.getProperty("HourKey")).thenReturn("StartHour");
>+    
>when(configuration.getProperty("MinuteKey")).thenReturn("StartMinute");
>+    
>when(configuration.getProperty("SecondKey")).thenReturn("StartSecond");
>+
>+    when(metadata.getMetadata("StartDateTime")).thenReturn("2013-05-23
>03:02:01");
>+
>+    extractor = new DateTimeExpandMetExtractor();
>+  }
>+
>+  @Test
>+  public void testExpandAll() throws MetExtractionException {
>+    when(configuration.containsKey("FullDateTimeKey")).thenReturn(true);
>+    
>when(configuration.containsKey("FullDateTimeFormat")).thenReturn(true);
>+    when(configuration.containsKey("YearKey")).thenReturn(true);
>+    when(configuration.containsKey("MonthKey")).thenReturn(true);
>+    when(configuration.containsKey("DayKey")).thenReturn(true);
>+    when(configuration.containsKey("HourKey")).thenReturn(true);
>+    when(configuration.containsKey("MinuteKey")).thenReturn(true);
>+    when(configuration.containsKey("SecondKey")).thenReturn(true);
>+
>+    extractor.configure(configuration);
>+    Metadata extractedMetadata = extractor.doExtract(null, metadata);
>+
>+    assertThat(extractedMetadata.getMetadata("StartYear"), is("2013"));
>+    assertThat(extractedMetadata.getMetadata("StartMonth"), is("05"));
>+    assertThat(extractedMetadata.getMetadata("StartDay"), is("23"));
>+    assertThat(extractedMetadata.getMetadata("StartHour"), is("03"));
>+    assertThat(extractedMetadata.getMetadata("StartMinute"), is("02"));
>+    assertThat(extractedMetadata.getMetadata("StartSecond"), is("01"));
>+  }
>+
>+  @Test
>+  public void testExpandSome() throws MetExtractionException {
>+    when(configuration.containsKey("FullDateTimeKey")).thenReturn(true);
>+    
>when(configuration.containsKey("FullDateTimeFormat")).thenReturn(true);
>+    when(configuration.containsKey("YearKey")).thenReturn(true);
>+    when(configuration.containsKey("MonthKey")).thenReturn(true);
>+    when(configuration.containsKey("DayKey")).thenReturn(true);
>+
>+    extractor.configure(configuration);
>+    Metadata extractedMetadata = extractor.doExtract(null, metadata);
>+
>+    assertThat(extractedMetadata.getMetadata("StartYear"), is("2013"));
>+    assertThat(extractedMetadata.getMetadata("StartMonth"), is("05"));
>+    assertThat(extractedMetadata.getMetadata("StartDay"), is("23"));
>+    assertThat(extractedMetadata.containsKey("StartHour"), is(false));
>+    assertThat(extractedMetadata.containsKey("StartMinute"), is(false));
>+    assertThat(extractedMetadata.containsKey("StartSecond"), is(false));
>+  }
>+
>+  @Test
>+  public void testFailure() throws MetExtractionException {
>+    when(configuration.containsKey("FullDateTimeKey")).thenReturn(false);
>+
>+    extractor.configure(configuration);
>+    expectedException.expect(MetExtractionException.class);
>+    extractor.doExtract(null, metadata);
>+  }
>+}
>
>Propchange: 
>oodt/trunk/filemgr/src/test/org/apache/oodt/cas/filemgr/metadata/extractor
>s/examples/TestDateTimeExpandMetExtractor.java
>--------------------------------------------------------------------------
>----
>    svn:mime-type = text/plain
>
>