/**
 * 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 com.pinterest.secor.parser;

import java.text.SimpleDateFormat;
import java.util.TimeZone;
import java.util.Date;

import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.pinterest.secor.common.SecorConfig;
import com.pinterest.secor.message.Message;
import com.pinterest.secor.message.ParsedMessage;


/**
 * PerfMetricsDateParser extracts timestamp field (specified by 'message.timestamp.name')
 * and creates custom output dirs based on a custom formatter, defined by defaultFormatter
 * the output dir will look like s3://<bucket>/<topic>/yyyy/MM/dd/HH/<file_name>
 *
 * @see http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
 *
 * 
 *
 */
public class PerfMetricsDateParser extends MessageParser {
    private static final Logger LOG = LoggerFactory.getLogger(PerfMetricsDateParser.class);
    protected static final String defaultFormatter = "yyyy/MM/dd/HH";

    public PerfMetricsDateParser(SecorConfig config) {
        super(config);
    }

    @Override
    public String[] extractPartitions(Message message) {
        JSONObject jsonObject = (JSONObject) JSONValue.parse(message.getPayload());
        String result[]={};
        if (jsonObject != null) {
            Object fieldValue = jsonObject.get(mConfig.getMessageTimestampName());
            Object inputPattern = mConfig.getMessageTimestampInputPattern();
            if (fieldValue != null && inputPattern != null) {
                try {
                    SimpleDateFormat inputFormatter = new SimpleDateFormat(inputPattern.toString());
                    SimpleDateFormat outputFormatter = new SimpleDateFormat(defaultFormatter);
                    Date dateFormat = inputFormatter.parse(fieldValue.toString());
                    result[] = {outputFormatter.format(dateFormat)};
                    return result;
                } catch (Exception e) {
                    LOG.warn("Impossible to convert date = " + fieldValue.toString()
                            + " for the input pattern = " + inputPattern.toString());
                }
            }
        }

        return result;
    }
}

@@ -2633,13 +2633,13 @@
 ult%5B
+0
 %5D = 
-%7B
 outp
@@ -2668,17 +2668,16 @@
 eFormat)
-%7D
 ;%0A      
@@ -2721,17 +2721,38 @@
         
-%7D
+    %7D %0A               
  catch (
@@ -2979,21 +2979,16 @@
       %7D%0A
-%0A    
     retu

fix the return value bug