The following topic describes how to use triggers to customize task processing.
To map triggers to Add Message action:
The following trigger complains if the Deadline task property has not been filled in for the task:
if(task.getDeadline() == null){ throw new gran.exception.UserMessageException("Please fill in the Deadline task property"); } return task;
The following trigger chooses a random handler for the task:
String group = task.getHandlerGroupId(); String handler= task.getHandlerUserId(); if (task.getHandlerGroupId()!=null){ ArrayList hand = AdapterManager.getInstance().getSecuredStepAdapterManager() .getTaskEditHandlerList(task.getSecure(), task.getId(), task.getCategoryId()); int size = hand.size(); if (size>0){ int pointer = new Random().nextInt(size); handler= ((SecuredUserBean)hand.get(pointer)).getId(); group = null; } } if (task.getParentId()!=null){ SecuredTaskTriggerBean nb = new SecuredTaskTriggerBean(task.getId(),task.getDescription(), task.getName(), task.getShortname(), task.getSubmitdate(), task.getUpdatedate(), task.getClosedate(), task.getActualBudget(), task.getBudget(), task.getDeadline(), task.getNumber(), task.getSubmitterId(), task.getHandlerId(), handler, group, task.getParentId(), task.getCategoryId(), task.getWorkflowId(), task.getStatusId(), task.getResolutionId(), task.getPriorityId(),task.getUdfValues(), task.getSecure()); return nb.create(false); } else return nb;
The following script fills in the custom field See Also of type Task, with a list of tasks similar to the created one:
HashMap set = AdapterManager.getInstance().getSecuredTaskAdapterManager() .findSimilar(task.getSecure(),task.getId()); String similar=""; if (set!=null && !set.isEmpty()) { for (Iterator i = set.keySet().iterator(); i.hasNext();) { SecuredTaskBean s = (SecuredTaskBean) i.next(); Float ratio = (Float) set.get(s); if (ratio.floatValue()>0.75f) { similar+="#"+s.getNumber()+"; " ; } } } if (similar.length()>0) { similar=similar.substring(0,similar.length()-2); AdapterManager.getInstance().getSecuredUDFAdapterManager().setTaskUDFValueSimple( task.getSecure(), task.getId(), "See Also", similar); } return task;
The following trigger prevents users from saving a task with Budgeted Time less than Actual Time:
SecuredTaskBean currentState = new SecuredTaskBean(task.getId(),task.getSecure()); if (currentState.getActualBudget()!=null) { if ((task.getBudget()==null && currentState.getBudget()!=null) || (task.getBudget()!=null && task.getBudget().compareTo(currentState.getActualBudget())<0)) throw new UserMessageException("Budgeted Time must be greater than Actual Time"); } return task;
The following trigger creates messages of the type log for each deadline change using the Current Task -> Task... -> Edit link. Create message type log before using this trigger.
SecuredTaskBean oldOne = new SecuredTaskBean(task.getId(),task.getSecure()); Object oldDeadline=oldOne.getDeadline(); Object newDeadline = task.getDeadline(); String actionType=null; if (oldDeadline==null && newDeadLine!=null) { actionType="Deadline was set."; } else if (oldDeadline!=null && newDeadLine==null) { actionType="Deadline was reset."; } else if (oldDeadline!=null && newDeadLine!=null) { if (newDeadline.after(oldDeadline)) { actionType="Deadline was extended."; } else if (newDeadline.before(oldDeadline)) { actionType="Deadline was moved up."; } } Object newOne = task.update(false); if (actionType!=null) { SecuredMessageTriggerBean bean = new SecuredMessageTriggerBean(null,actionType,null,null, task.getDeadline(), task.getBudget(), task.getId(), task.getSecure().getUserId(), null, task.getPriorityId(), task.getHandlerId(), task.getHandlerUserId(), task.getHandlerGroupId(), CSVImport.findMessageTypeIdByName("log", task.getCategory().getName()), null, task.getSecure()).create(); } return newOne;
The following trigger prevents users from closing a task that contains non-closed subtasks:
SecuredStatusBean ssb = AdapterManager.getInstance().getSecuredStepAdapterManager() .getNextStatus(message.getSecure(), message.getTaskId(), message.getMstatusId()); if(ssb != null && ssb.isFinish()) { Set childredIdSet =message.getTask().getAllowedChildrenWithSubtasksMap().keySet(); for(Iterator it = childredIdSet.iterator(); it.hasNext();) { String ch = (String)it.next(); if (!AdapterManager.getInstance().getSecuredFindAdapterManager() .findTaskById(sc,ch).getStatus().isFinish()) { throw new UserMessageException("Subtask status is not final."); } } } return message;
The following trigger implements the ability to vote for a task by adding a message. To use this script, create a custom field votesCounter of type Integer that will be used to count votes. Assign this trigger for a message type used to vote.
String udfValue = AdapterManager.getInstance().getSecuredUDFAdapterManager() .getTaskUDFValue(message.getSecure(), message.getTaskId(),"votesCounter"); String vote="0"; if (udfValue!=null && udfValue.length()>0) vote = udfValue; int v = Integer.parseInt(vote); v++; AdapterManager.getInstance().getSecuredUDFAdapterManager().setTaskUDFValueSimple( message.getSecure(), message.getTaskId(),"votesCounter", v+""); return message;
The following trigger copies a task to the Knowledge Base (task #2 in this example) when user adds a 003 Close message.
String copyTo = "2"; if(message.getMstatus().getName().equals("003 Close")) { AdapterManager.getInstance().getSecuredTaskAdapterManager().pasteTasks(message.getSecure(), AdapterManager.getInstance().getSecuredTaskAdapterManager().findTaskIdByQuickGo( message.getSecure(), copyTo), new String[]{message.getTaskId()},"SINGLE_COPY"); } return message;
The following trigger tracks custom field value changes in the message description:
if (message.getUdfValues()!=null && !message.getUdfValues().isEmpty()) { String changes=""; for (Iterator it=message.getUdfValues().keySet().iterator();it.hasNext();) { String key = it.next().toString(); String value = message.getUdfValues().get(key).toString(); String oldValue = AdapterManager.getInstance().getSecuredUDFAdapterManager() .getTaskUDFValue(message.getSecure(),message.getTaskId(),key); if ((oldValue!=null && !oldValue.equals(value)) || (oldValue==null && value!=null)) { changes+=key+" changed to "+value+"\n"; } } String newDescription = changes+message.getDescription(); return new SecuredMessageTriggerBean(message.getId(), newDescription, message.getTime(), message.getHrs(), message.getDeadline(), message.getBudget(), message.getTaskId(), message.getSubmitterId(), message.getResolutionId(), message.getPriorityId(), null, message.getHandlerUserId(), message.getHandlerGroupId(), message.getMstatusId(), message.getUdfValues(), message.getSecure()); } return message;
Copyright (c) 2002-2006. All rights reserved.
|
What do you think about this topic? Send feedback!
|