/*
 * JBoss, Home of Professional Open Source.
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * Modified by
 * @author  Data Virtuality GmbH
 * @date    $Date: 2016-01-06 11:06:51 +0200 (Wed, 06 Jan 2016) $
 *  
 */

package org.teiid.translator.jdbc.oracle;

import org.teiid.core.util.StringUtil;
import org.teiid.translator.jdbc.ParseFormatFunctionModifier;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

// EXTENSION
// ISSUE: DVCORE-2789
// FIXED BY: Salvatore Raunich
// DATE: 2015.04.20
//public final class OracleFormatFunctionModifier extends
public abstract class AbstractFormatFunctionModifier extends
// END EXTENSION
		ParseFormatFunctionModifier {

	public AbstractFormatFunctionModifier(String prefix) {
		super(prefix);
	}

	public abstract Pattern getTokenPattern();

	@Override
	protected Object translateFormat(String format) {
		Matcher m = getTokenPattern().matcher(format);
		// BUG: formatTimestamp and formatDate functions are not pushed down in Oracle
		// ISSUE: DVCORE-776
		// SOLUTION: added missing quotes when the literal is translated
		// FIXED by Salvatore Raunich
		// date 2013.11.06
		// StringBuilder sb = new StringBuilder();
		StringBuilder sb = new StringBuilder("'");
		// BUG END
		while (m.find()) {
			if (m.group().length() == 0) {
				continue;
			}
			String group = m.group();
			sb.append(convertToken(group));
		} 
		// BUG: formatTimestamp and formatDate functions are not pushed down in Oracle
		// ISSUE: DVCORE-776
		// SOLUTION: added missing quotes when the literal is translated
		// FIXED by Salvatore Raunich
		// date 2013.11.06
		sb.append("'");
		// BUG END
		return sb.toString();
	}

	protected Object convertToken(String group) {
		switch (group.charAt(0)) {
		case 'G':
			return "AD"; //$NON-NLS-1$
		case 'y':
			if (group.length() == 4) {
				return "YYYY"; //$NON-NLS-1$
			}
			return "YY"; //$NON-NLS-1$
		case 'M':
			if (group.length() <= 2) {
				return "MM"; //$NON-NLS-1$
			}
			if (group.length() == 3) {
				return "Mon"; //$NON-NLS-1$
			}
			return "Month"; //$NON-NLS-1$
		case 'D':
			return "DDD"; //$NON-NLS-1$
		case 'd':
			return "DD"; //$NON-NLS-1$
		case 'E':
			if (group.length() == 4) {
				return "Day"; //$NON-NLS-1$
			}
			return "Dy"; //$NON-NLS-1$
		case 'a':
			return "PM"; //$NON-NLS-1$
		case 'H':
			return "HH24"; //$NON-NLS-1$
		case 'h':
			return "HH"; //$NON-NLS-1$
		case 'm':
			return "MI"; //$NON-NLS-1$
		case 's':
			return "SS"; //$NON-NLS-1$
		case 'S':
			// EXTENSION
			// ISSUE: DVCORE-2789
			// FIXED BY: Salvatore Raunich
			// DATE: 2015.04.20
//			return "FF" + group.length(); //$NON-NLS-1$
			return getMillisecondsPattern(group);
			// END EXTENSIONs
		case 'Z':
			return "TZHTZM"; //$NON-NLS-1$
		case '\'':
			return '"' + StringUtil.replace(group.substring(1, group.length() - 1), "''", "'") + '"'; //$NON-NLS-1$ //$NON-NLS-2$
		case ' ':
		case '-':
		case '/':
		case ',':
		case '.':
		case ';':
		case ':':
			return group;
		default:
			return '"' + group + '"';
		}
	}
	// EXTENSION
	// ISSUE: DVCORE-2789
	// FIXED BY: Salvatore Raunich
	// DATE: 2015.04.20
	protected String getMillisecondsPattern(String group) {
		String length = group!=null?""+group.length():"";
		return "FF" + length;
	}
	// END EXTENSION
}