/* * FormulaFunction_DatePart.java * * Created on May 1, 2006, 9:46 AM * * Copyright 2006 Lee Lofgern and Accounting Enhancements Inc Licensed 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.accountingenhancements.formula; import com.accountingenhancements.common.SupportParameters; import java.text.ParseException; import java.util.HashMap; /** * * @author Lee lofgren lofgren_opensource@accountingenhancements.com * @version 0.1009102006 */ public class FormulaFunction_DatePart extends FormulaFunction{ protected static String[][] requiredArguments = {{"ARG1: The Interval to be extracted. [Year, Quarter (1 through 4), Month (1 through 12), DayOfYear (1 through 366), Day (of month 1 though 31) , Weekday (1 through 7), Week (of year, 1 though 52)]","TYPE_STRING"},{"ARG2: The Date","TYPE_DATE"},{"ARG3(Optional): First day of week [(Sun)day, (Mon)day, (Tue)sday, (Wed)nesday, (Thu)rsday, (Fri)day, (Sat)urday], If not specified, Sunday is assumed. This WeekDay calculation only","TYPE_STRING"},{"ARG4(Optional): First week of year. [FirstJan1(Default), FirstFourDays (First week in which at least four days are in current year), FirstFullWeek (First full week in year)]. This can also be a number reflecting the minimum number of days required in the first week of the year where 1 = 1 day (default) and 7=full week. Affects current Week (of year) only","TYPE_STRING"}}; protected static String[] returnValueDescription = {"Long value of interval requested","TYPE_LONG"}; /** *Create a new FormulaFunction with an already defined functionArgumentStack. *@param functionArgumentStack a list of FormulaVariables that are needed by the function. */ public FormulaFunction_DatePart(FormulaVariableStack functionArgumentStack){ super(functionArgumentStack); } /** *Create a new FormulaFunction with a comma-delimited string representing the arguments needed by the function *@param functionArgumentString the string representing the functions arguments. The outer parentheses must be removed.
*Example: If the function is IIF(A *Example: If the function is IIF(A *This is the most common constructor used. *@param functionVariable a FormulaVariable of TYPE_FUNCTION *@throws ParseException if arguments can't be parsed using parseCommaDelimitedFunctionFields(String,int) */ public FormulaFunction_DatePart(FormulaVariable functionVariable) throws ParseException{ super(functionVariable); } protected FormulaVariable solve(FormulaVariableList variableList, int iteration, SupportParameters supportParameters, FormulaFunctionList functionList, int resolveEverythingAboveLevel) throws java.text.ParseException, java.lang.ArithmeticException, ClassNotFoundException{ FormulaVariable result=null; FormulaVariable date=null; FormulaVariable interval=null; FormulaVariable firstDayOfWeekArg=null; FormulaVariable minimumDaysInFirstWeekArg=null; int firstDayOfWeek; int minimumDaysInFirstWeek; int resultInt; int highestLevel=0; int slash1; int slash2; int month; int day; int year; java.util.Date dateDate=null; java.util.Calendar calendar=null; String intervalString; if(functionArgumentStack==null)throw new java.lang.ArithmeticException("functionArgumentStack is null"); interval=functionArgumentStack.get("ARG1"); if(interval==null)throw new java.lang.ArithmeticException("ARG1 is missing from functionArgumentStack"); interval=interval.solve(variableList,iteration,supportParameters,functionList,resolveEverythingAboveLevel); if (interval==null)throw new java.lang.ArithmeticException("result of solving ARG1 is invalid"); highestLevel=interval.getHighestLevel(); date=functionArgumentStack.get("ARG2"); if(date==null)throw new java.lang.ArithmeticException("ARG2 is missing from functionArgumentStack"); date=date.solve(variableList,iteration,supportParameters,functionList,resolveEverythingAboveLevel); if(date==null||date.detectVariableType(date.getString(),false)!=FormulaVariable.TYPE_DATE)throw new java.lang.ArithmeticException("ARG2 not of TYPE_DATE"); if(highestLevel7)minimumDaysInFirstWeek=7; } else if(minimumDaysInFirstWeekArg.getString().substring(0,6).equalsIgnoreCase("FIRSTJ"))minimumDaysInFirstWeek=1; else if(minimumDaysInFirstWeekArg.getString().substring(0,7).equalsIgnoreCase("FIRSTFO"))minimumDaysInFirstWeek=4; else if(minimumDaysInFirstWeekArg.getString().substring(0,7).equalsIgnoreCase("FIRSTFU"))minimumDaysInFirstWeek=7; } dateDate=date.getDate(); if(dateDate==null)throw new java.text.ParseException("ARG2 is an invalid date",0); resultInt=0; calendar = java.util.GregorianCalendar.getInstance(java.util.Locale.US); calendar.setTime(dateDate); year=calendar.get(calendar.YEAR); month=calendar.get(calendar.MONTH)-calendar.JANUARY+1; day=calendar.get(calendar.DAY_OF_MONTH); intervalString=interval.getString(); if(intervalString==null||intervalString.length()==0)throw new java.text.ParseException("interval invalid",0); if(intervalString.substring(0,1).equalsIgnoreCase("Y")){ resultInt=year; } else if(intervalString.substring(0,1).equalsIgnoreCase("Q")){ resultInt=4; if(month<4)resultInt=1; else if(month<7)resultInt=2; else if(month<10)resultInt=3; } else if(intervalString.substring(0,1).equalsIgnoreCase("M")){ resultInt=month; } else if(intervalString.length()>3&&intervalString.substring(0,4).equalsIgnoreCase("DAYO")){ resultInt=calendar.get(calendar.DAY_OF_YEAR); } else if(intervalString.substring(0,1).equalsIgnoreCase("D")){ resultInt=day; } else if(intervalString.length()>4&&intervalString.substring(0,5).equalsIgnoreCase("WEEKD")){ resultInt=calendar.get(calendar.DAY_OF_WEEK); resultInt-=firstDayOfWeek; if(resultInt<=0)resultInt+=7; } else if(intervalString.substring(0,1).equalsIgnoreCase("W")){ calendar.setMinimalDaysInFirstWeek(minimumDaysInFirstWeek); resultInt=calendar.get(calendar.WEEK_OF_YEAR); } else throw new java.lang.ArithmeticException("Invalid interval option"); result=new FormulaVariable("",(long)resultInt,highestLevel); return result; } /** *@return the name of this function */ public static String getName() { return "DatePart"; } /** *@return String[][] of needed arguments and their dataTypes which as {{"Test Argument ARG1","TYPE_BOOLEAN"},{"True Argument ARG2","TYPE_?"},{"False Argument ARG3","TYPE_?"}}
*If handing this routine a FormulaVariableStack functionArgumentStack then the FormulaVariable arguments should be named, ARG1, ARG2, etc... */ public static String[][] getRequiredArguments(){ return requiredArguments; } /** *@return String[] description of the return value as well as it's data type */ public static String[] getReturnValueDescription(){ return returnValueDescription; } }