Created
May 22, 2016 17:06
-
-
Save jechlin/25557469af8b8013f62c6e4d3852be27 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.onresolve.jira.groovy.jql | |
import com.atlassian.jira.JiraDataType | |
import com.atlassian.jira.JiraDataTypes | |
import com.atlassian.jira.component.ComponentAccessor | |
import com.atlassian.jira.jql.operand.QueryLiteral | |
import com.atlassian.jira.jql.query.QueryCreationContext | |
import com.atlassian.jira.timezone.TimeZoneManager | |
import com.atlassian.jira.user.ApplicationUser | |
import com.atlassian.jira.util.MessageSet | |
import com.atlassian.query.clause.TerminalClause | |
import com.atlassian.query.operand.FunctionOperand | |
import groovy.util.logging.Log4j | |
@Log4j | |
class BusinessMonth extends AbstractScriptedJqlFunction implements JqlFunction { | |
@Override | |
MessageSet validate(ApplicationUser user, FunctionOperand operand, TerminalClause terminalClause) { | |
def messageSet = super.validate(user, operand, terminalClause) | |
if (operand.args) { | |
def offset = operand.args[0] as String | |
try { | |
Integer.parseInt(offset) | |
} | |
catch (NumberFormatException ignored) { | |
messageSet.addErrorMessage("Could not parse $offset as an integer") | |
} | |
} | |
messageSet | |
} | |
@Override | |
List<QueryLiteral> getValues(QueryCreationContext queryCreationContext, FunctionOperand operand, TerminalClause terminalClause) { | |
def cal = Calendar.getInstance() | |
def offset = 0 | |
if (operand.args) { | |
offset = operand.args.first() as Integer | |
} | |
def timeZoneManager = ComponentAccessor.getComponent(TimeZoneManager) | |
cal.setTimeZone(timeZoneManager.getLoggedInUserTimeZone()); | |
cal.set(GregorianCalendar.MONTH, cal.get(GregorianCalendar.MONTH) + offset) | |
cal.set(GregorianCalendar.DAY_OF_WEEK, Calendar.FRIDAY) | |
cal.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, -1) | |
cal.set(GregorianCalendar.HOUR, cal.getActualMaximum(GregorianCalendar.HOUR)) | |
cal.set(GregorianCalendar.MINUTE, cal.getActualMaximum(GregorianCalendar.MINUTE)) | |
cal.set(GregorianCalendar.AM_PM, GregorianCalendar.PM) | |
return Collections.singletonList(new QueryLiteral(operand, cal.timeInMillis)) | |
} | |
@Override | |
Integer getMinimumNumberOfExpectedArguments() { | |
0 | |
} | |
@Override | |
JiraDataType getDataType() { | |
JiraDataTypes.DATE | |
} | |
@Override | |
String getDescription() { | |
"Returns midnight (ish) on last day of business month" | |
} | |
@Override | |
List<Map> getArguments() { | |
[ | |
["description": "offset", "optional": true], | |
] | |
} | |
@Override | |
boolean isList() { | |
false | |
} | |
@Override | |
String getFunctionName() { | |
"endOfBusinessMonth" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment