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/extractors/examples/DateTimeExpandMetExtractor.java
   (with props)
    
oodt/trunk/filemgr/src/test/org/apache/oodt/cas/filemgr/metadata/extractors/examples/TestDateTimeExpandMetExtractor.java
   (with props)

Added: 
oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/metadata/extractors/examples/DateTimeExpandMetExtractor.java
URL: 
http://svn.apache.org/viewvc/oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/metadata/extractors/examples/DateTimeExpandMetExtractor.java?rev=1591965&view=auto
==============================================================================
--- 
oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/metadata/extractors/examples/DateTimeExpandMetExtractor.java
 (added)
+++ 
oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/metadata/extractors/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.AbstractFilemgrMetExtractor;
+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 [email protected] (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/extractors/examples/DateTimeExpandMetExtractor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
oodt/trunk/filemgr/src/test/org/apache/oodt/cas/filemgr/metadata/extractors/examples/TestDateTimeExpandMetExtractor.java
URL: 
http://svn.apache.org/viewvc/oodt/trunk/filemgr/src/test/org/apache/oodt/cas/filemgr/metadata/extractors/examples/TestDateTimeExpandMetExtractor.java?rev=1591965&view=auto
==============================================================================
--- 
oodt/trunk/filemgr/src/test/org/apache/oodt/cas/filemgr/metadata/extractors/examples/TestDateTimeExpandMetExtractor.java
 (added)
+++ 
oodt/trunk/filemgr/src/test/org/apache/oodt/cas/filemgr/metadata/extractors/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 [email protected] (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("StartDateTime");
+    
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/extractors/examples/TestDateTimeExpandMetExtractor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to