Attribute change scripts#
Attribute change scripts change the value of an attribute based on an event, usually a change of another attribute. These scripts are therefore event handlers.
Changing a list value#
Changes the list value of a dependent list attribute, triggered by another list attribute.
package script.attributeChange
import com.alltena.track.beans.TWorkItemBean;
import com.alltena.track.fieldType.constants.SystemFields
import com.alltena.track.fieldType.runtime.base.WorkItemContext
import com.alltena.track.item.attributeDependency.AttributeDependencyTO
import com.alltena.track.item.attributeDependency.ChangeListValueDependency
import com.alltena.track.item.attributeDependency.RemoveListEntryDependency
import com.alltena.track.item.attributeDependency.RequiredDependency
import com.alltena.track.report.execute.WorkItemBeanComparator
import com.alltena.track.util.event.IEventHandler;
/**
* Changes the list value of a dependent list attribute, triggered by another list attribute
*/
public class ChangeListValueByListValue implements IEventHandler {
//the fieldIDs can be looked up in the database directly or
//using FieldBL.loadByName() (the latter is less efficient)
private static Integer targetProjectID = 1304;
private static Integer triggerAttribut = 1476;
private static Integer dependentAttribut = SystemFields.PRIORITY;
/**
* Applies the dependency between the two lists
* The IDs can be looked up in the database directly or
* using OptionBL.loadByLabel(...) (the latter is less efficient)
*/
def public Map<String, Object> handleEvent(Map<String, Object> inputBinding) {
WorkItemContext workItemContext = (WorkItemContext)inputBinding.get("workItemContext")
TWorkItemBean workItemBean = workItemContext.getWorkItemBean()
Object[] classification = workItemBean.getAttribute(triggerAttribut)
Integer dependentValue = null
Integer value = null
if (classification!=null) {
value = (Integer)classification[0]
}
if (value!=null) {
switch (value) {
case 6474:
case 6475:
case 6476:
dependentValue = 1
break;
case 6469:
case 6470:
case 6471:
case 6472:
case 6473:
dependentValue = 2
break;
case 6465:
case 6466:
case 6467:
case 6468:
dependentValue = 3
break;
}
}
if (dependentValue!=null) {
ChangeListValueDependency.changeListEntry(workItemContext, dependentAttribut, dependentValue)
}
return inputBinding
}
/**
* Defines a dependency between two lists
* @param inputBinding
* @return
*/
List<AttributeDependencyTO> getDependencies(Map<String, Object> inputBinding) {
List<AttributeDependencyTO> dependencies = null
//uncomment this to activate this attribute change script
/*WorkItemContext workItemContext = (WorkItemContext)inputBinding.get("workItemContext")
TWorkItemBean workItemBean = workItemContext.getWorkItemBean()
Integer projectID = workItemBean.getProjectID()
if (projectID!=null && projectID.intValue()==targetProjectID) {
dependencies = new ArrayList<>()
ChangeListValueDependency.configureAttributeAsConditionallyChangeListValue(
workItemContext, dependentAttribut, triggerAttribut, dependencies)
}*/
return dependencies;
}
}
Changing a date#
Changes the value of a dependent date attribute, triggered by a list attribute.
package script.attributeChange
import com.alltena.track.beans.TWorkItemBean;
import com.alltena.track.fieldType.constants.SystemFields
import com.alltena.track.fieldType.runtime.base.IFieldTypeRT
import com.alltena.track.fieldType.runtime.base.WorkItemContext
import com.alltena.track.fieldType.types.FieldTypeManager
import com.alltena.track.item.attributeDependency.AttributeDependencyTO
import com.alltena.track.item.attributeDependency.ChangeListValueDependency
import com.alltena.track.item.attributeDependency.RemoveListEntryDependency
import com.alltena.track.item.attributeDependency.RequiredDependency
import com.alltena.track.report.execute.WorkItemBeanComparator
import com.alltena.track.util.event.IEventHandler;
/**
* Changes the value of a dependent date attribute, triggered by a list attribute
*/
public class ChangeValueByListValue implements IEventHandler {
//the fieldIDs can be looked up in the database directly or
//using FieldBL.loadByName() (the latter is less efficient)
private static Integer triggerAttribut = 1030;
private static Integer dependentAttribut = 1101;
//the dependency is valid only in this project
private static Integer targetProjectID = 1080;
/**
* This method is being called as soon as triggerAttribut has been changed (ajax call).
* It changes the value of dependentAttribut
*/
def public Map<String, Object> handleEvent(Map<String, Object> inputBinding) {
WorkItemContext workItemContext = (WorkItemContext)inputBinding.get("workItemContext")
TWorkItemBean workItemBean = workItemContext.getWorkItemBean()
workItemBean.setAttribute(dependentAttribut, new Date());
return inputBinding
}
/**
* Defines the dependency of dependentAttribut from triggerAttribut if the item is in the targetProjectID or
* targetProjectID is null
* @param inputBinding
* @return
*/
List<AttributeDependencyTO> getDependencies(Map<String, Object> inputBinding) {
List<AttributeDependencyTO> dependencies = null
//uncomment this to activate this attribute change script
/*WorkItemContext workItemContext = (WorkItemContext)inputBinding.get("workItemContext")
TWorkItemBean workItemBean = workItemContext.getWorkItemBean()
Integer projectID = workItemBean.getProjectID()
if (targetProjectID==null || projectID.intValue()==targetProjectID) {
dependencies = new ArrayList<>()
ChangeListValueDependency.configureAttributeAsConditionallyChangeListValue(
workItemContext, dependentAttribut, triggerAttribut, dependencies)
}*/
return dependencies;
}
}
Enabling manager changes by status#
Enables changing the manager and makes the attribute „Build“ required in certain statuses.
package script.attributeChange
import com.alltena.track.admin.customize.scripting.BINDING_PARAMS
import com.alltena.track.beans.TWorkItemBean
import com.alltena.track.fieldType.constants.SystemFields
import com.alltena.track.fieldType.runtime.base.WorkItemContext
import com.alltena.track.item.attributeDependency.AttributeDependencyTO
import com.alltena.track.item.attributeDependency.ReadOnlyDependency
import com.alltena.track.item.attributeDependency.RequiredDependency
import com.alltena.track.util.event.IEventHandler
/**
* Enables changing the manager and enforces the build attribute as required in certain statuses
*/
public class EnableManagerOnStatus implements IEventHandler {
/**
* Applies the constrains configured through an ajax request called on each status change
*/
def public Map<String, Object> handleEvent(Map<String, Object> inputBinding) {
WorkItemContext workItemContext = (WorkItemContext)inputBinding.get(BINDING_PARAMS.WORKITEM_CONTEXT)
TWorkItemBean workItemBean = workItemContext.getWorkItemBean()
Integer statusID = workItemBean.getStateID()
ReadOnlyDependency.configureAttributeAsReadOnly(workItemContext, SystemFields.MANAGER, statusID.intValue()!=1, true)
RequiredDependency.configureAttributeAsRequired(workItemContext, SystemFields.BUILD, statusID.intValue()!=1)
return inputBinding
}
/**
* Defines two attributes depending on a third attribute
* The manager attribute will be set as read only if the status is not 1
* The build attribute will be set as required if the status is not 1
* @param inputBinding
* @return
*/
List<AttributeDependencyTO> getDependencies(Map<String, Object> inputBinding) {
List<AttributeDependencyTO> dependencies = new ArrayList<>();
//uncomment this to activate this attribute change script
/*WorkItemContext workItemContext = (WorkItemContext)inputBinding.get(BINDING_PARAMS.WORKITEM_CONTEXT)
TWorkItemBean workItemBean = workItemContext.getWorkItemBean()
Integer statusID = workItemBean.getStateID()
ReadOnlyDependency.configureAttributeAsConditionallyReadOnly(workItemContext, SystemFields.MANAGER, SystemFields.STATE, dependencies);
ReadOnlyDependency.configureAttributeAsReadOnly(workItemContext, SystemFields.MANAGER, statusID.intValue()!=1, true)
RequiredDependency.configureAttributeAsConditionallyRequired(workItemContext, SystemFields.BUILD, SystemFields.STATE, dependencies);
RequiredDependency.configureAttributeAsRequired(workItemContext, SystemFields.BUILD, statusID.intValue()!=1)*/
return dependencies
}
}
Requiring a comment#
Makes a comment required depending on the status.
package script.attributeChange
import com.alltena.track.admin.customize.scripting.BINDING_PARAMS
import com.alltena.track.beans.TWorkItemBean;
import com.alltena.track.fieldType.constants.SystemFields
import com.alltena.track.fieldType.runtime.base.WorkItemContext
import com.alltena.track.item.attributeDependency.AttributeDependencyTO
import com.alltena.track.item.attributeDependency.RequiredDependency
import com.alltena.track.util.event.IEventHandler;
/**
* Enforces the comment as required for status changes
*/
public class RequireCommentOnStatus implements IEventHandler {
/**
* Enforce the Comment as required if status changed to a certain value
*/
def public Map<String, Object> handleEvent(Map<String, Object> inputBinding) {
WorkItemContext workItemContext = (WorkItemContext)inputBinding.get(BINDING_PARAMS.WORKITEM_CONTEXT)
TWorkItemBean workItemBean = workItemContext.getWorkItemBean()
Integer statusID = workItemBean.getStateID()
RequiredDependency.configureAttributeAsRequired(workItemContext, SystemFields.COMMENT, statusID.intValue()==1075)
return inputBinding
}
/**
* The Comment attribute will depend on status change
* @param inputBinding
* @return
*/
List<AttributeDependencyTO> getDependencies(Map<String, Object> inputBinding) {
WorkItemContext workItemContext = (WorkItemContext)inputBinding.get(BINDING_PARAMS.WORKITEM_CONTEXT)
List<AttributeDependencyTO> dependencies = new ArrayList<>();
//uncomment this to activate this attribute change script
//RequiredDependency.configureAttributeAsConditionallyRequired(workItemContext, SystemFields.COMMENT, SystemFields.STATE, dependencies);
return dependencies;
}
}
Suggesting the average processing time#
Suggests a processing time based on the average processing time by responsible and item type.
package script.attributeChange
import com.alltena.track.admin.customize.lists.systemOption.ItemTypeBL
import com.alltena.allegra.admin.item.lookups.itemType.ItemTypeFlag
import com.alltena.track.admin.customize.scripting.BINDING_PARAMS
import com.alltena.track.admin.customize.treeConfig.field.FieldBL
import com.alltena.track.admin.customize.treeConfig.field.GeneralSettingsBL
import com.alltena.track.beans.TFieldBean
import com.alltena.track.beans.TWorkItemBean
import com.alltena.track.fieldType.constants.SystemFields
import com.alltena.track.fieldType.runtime.base.WorkItemContext
import com.alltena.track.fieldType.runtime.custom.calculated.throughput.AverageThrouhputBL
import com.alltena.track.fieldType.runtime.custom.calculated.throughput.CalendarWorkingHoursUtil
import com.alltena.track.fieldType.runtime.custom.calculated.throughput.IntervalUtil
import com.alltena.track.fieldType.runtime.custom.calculated.throughput.WorkingHourSection
import com.alltena.track.fieldType.types.system.text.SystemDuration.DurationParametersCode
import com.alltena.track.item.attributeDependency.AttributeDependencyContext
import com.alltena.track.item.attributeDependency.AttributeDependencyTO
import com.alltena.track.resourceCalendar.itemDuration.ItemThoughputDurationBL
/**
* Suggests a processing time based on average processing time by responsible and item type
*
*/
public class SetAverageProcessingTime {
private static String processingTimeAttributeName = "ProcessingTime"
private static String throughputAttributeName = "Throughput"
private static int daysBefore = 30
/**
* Sets a the value of processingTimeAttributeName attribute to the average throughput time based on responsible and item type
* @param inputBinding
* @return
*/
public Map<String, Object> handleEvent(Map<String, Object> inputBinding) {
TFieldBean averageProcessingTimeFieldBean = FieldBL.loadByName(processingTimeAttributeName)
TFieldBean throughputFieldBean = FieldBL.loadByName(throughputAttributeName)
if (averageProcessingTimeFieldBean!=null && throughputFieldBean!=null) {
Integer throughputFieldID = throughputFieldBean.getObjectID()
Integer averageProcessingTimeFieldID = averageProcessingTimeFieldBean.getObjectID()
WorkItemContext workItemContext = (WorkItemContext)inputBinding.get(BINDING_PARAMS.WORKITEM_CONTEXT);
TWorkItemBean workItemBean = inputBinding.get(BINDING_PARAMS.ITEM)
Date datedFrom = IntervalUtil.getEarliestDate(daysBefore, null);
double workHours = 8.0
List<Integer> itemFilterFieldIDs = Arrays.asList(SystemFields.RESPONSIBLE, SystemFields.ISSUETYPE)
Long averageDurationInMinutes = AverageThrouhputBL.getAverageThrougputMinutes(workItemBean, itemFilterFieldIDs, throughputFieldID, datedFrom)
Double averageDurationInDays = CalendarWorkingHoursUtil.getDurationInDays(averageDurationInMinutes, workHours)
if (averageDurationInDays==null) {
averageDurationInDays = 1;
}
workItemBean.setAttribute(averageProcessingTimeFieldID, averageDurationInDays.intValue())
}
return inputBinding
}
/**
* Gets the dependencies for computing the default processing time attribute for task type items
* @param inputBinding
* @return
*/
List<AttributeDependencyTO> getDependencies(Map<String, Object> inputBinding) {
List<AttributeDependencyTO> dependencies = new ArrayList<>()
//uncomment this to activate this attribute change script
/*WorkItemContext workItemContext = (WorkItemContext)inputBinding.get(BINDING_PARAMS.WORKITEM_CONTEXT)
TWorkItemBean workItemBean = (TWorkItemBean)inputBinding.get(BINDING_PARAMS.ITEM)
if (ItemTypeBL.isOfFlag(workItemBean.getListTypeID(), ItemTypeFlag.TASK.getFlag())) {
TFieldBean averageProcessingTimeField = FieldBL.loadByName(processingTimeAttributeName)
if (averageProcessingTimeField!=null) {
Integer fieldID = averageProcessingTimeField.getObjectID()
AttributeDependencyTO responsibleDependency = new AttributeDependencyTO(fieldID, SystemFields.RESPONSIBLE, new AttributeDependencyContext(true, false, false, true))
dependencies.add(responsibleDependency);
AttributeDependencyTO itemTypeDependency = new AttributeDependencyTO(fieldID, SystemFields.ISSUETYPE, new AttributeDependencyContext(true, false, false, true))
dependencies.add(itemTypeDependency);
}
}*/
return dependencies;
}
}