Hi,
I created following code to update Due Date for a Work Item based on iteration End Date:
package com.rb.date;
import java.io.File;
import java.io.FileInputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.apache.log4j.Logger;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import com.ibm.team.process.client.IProcessItemService;
import com.ibm.team.process.common.IDevelopmentLine;
import com.ibm.team.process.common.IDevelopmentLineHandle;
import com.ibm.team.process.common.IIteration;
import com.ibm.team.process.common.IIterationHandle;
import com.ibm.team.process.common.IProjectArea;
import com.ibm.team.process.common.IProjectAreaHandle;
import com.ibm.team.repository.client.IItemManager;
import com.ibm.team.repository.client.ITeamRepository;
import com.ibm.team.repository.client.ITeamRepository.ILoginHandler;
import com.ibm.team.repository.client.ITeamRepository.ILoginHandler.ILoginInfo;
import com.ibm.team.repository.client.TeamPlatform;
import com.ibm.team.repository.common.IAuditableHandle;
import com.ibm.team.repository.common.TeamRepositoryException;
import com.ibm.team.repository.common.UUID;
import com.ibm.team.repository.common.json.JSONObject;
import com.ibm.team.workitem.client.IAuditableClient;
import com.ibm.team.workitem.client.IQueryClient;
import com.ibm.team.workitem.client.IWorkItemClient;
import com.ibm.team.workitem.client.IWorkItemWorkingCopyManager;
import com.ibm.team.workitem.client.WorkItemWorkingCopy;
import com.ibm.team.workitem.common.IAuditableCommon;
import com.ibm.team.workitem.common.model.IAttribute;
import com.ibm.team.workitem.common.model.IAttributeHandle;
import com.ibm.team.workitem.common.model.IWorkItem;
import com.ibm.team.workitem.common.model.ItemProfile;
import com.ibm.team.workitem.common.query.IQueryDescriptor;
import com.ibm.team.workitem.common.query.IQueryResult;
import com.ibm.team.workitem.common.query.IResult;
import com.ibm.team.workitem.common.query.QueryTypes;
public class EndDateRegeneration {
static Logger logger = Logger.getLogger(EndDateRegeneration.class.getName());
static String url;
static String username;
static String password;
static Map<String, Date> map1 = new HashMap<>();
static Map map2 = new HashMap<>();
static String projectArea ;
IProjectArea sProjectArea = null;
IProgressMonitor monitor = new NullProgressMonitor();
IProcessItemService service;
IProjectArea iProjectArea = null;
ITeamRepository repo;
IWorkItemClient workItemClient;
static IIteration iiteration;
IIterationHandle handle;
static Map<String, Date> mapIterationDetails;
List<IWorkItem> sharedQueryDetailWithWorkItemIdList;
static Date date;
static String projectAreaName;
IProjectAreaHandle iProjectAreaHandle;
static EndDateRegeneration dateHover;
static String projectAreaNameArgsInput;
static String queryName;
public static void main(String[] args) throws Exception {
File file = new File("./config.properties");
FileInputStream fileInput = new FileInputStream(file);
Properties properties = new Properties();
properties.load(fileInput);
File file2 = new File("./credentials.properties");
FileInputStream fileInput2 = new FileInputStream(file2);
Properties properties2 = new Properties();
properties2.load(fileInput2);
if (!TeamPlatform.isStarted()) {
TeamPlatform.startup();
}
if(properties != null && properties.getProperty("url") != null && properties.getProperty("projectAreaNameArgsInput") != null && properties.getProperty("queryName") != null)
{
url = properties.getProperty("url".trim());
username = properties2.getProperty("username").trim();
password = properties2.getProperty("password").trim();
projectAreaNameArgsInput = properties.getProperty("projectAreaNameArgsInput");
queryName = properties.getProperty("queryName").trim();
Timestamp time1= new Timestamp(System.currentTimeMillis());
logger.info("----------------------------------------------------------------------------");
logger.info("Execution Started at " + time1);
List<String> projectAreaList = new ArrayList<String>();
String[] projectAreaNameListSplit = projectAreaNameArgsInput.split(",");
for(int i=0; i<projectAreaNameListSplit.length; i++){
logger.info("For Project Area : "+ projectAreaNameListSplit[i]);
projectAreaName = projectAreaNameListSplit[i];
// projectAreaList.add(projectAreaNameListSplit[i]);
projectArea = projectAreaNameListSplit[i];
ArrayList<String> iterationNames = new EndDateRegeneration().getPlannedForList(username, password, url);
mapIterationDetails = new HashMap<String, Date>();
for (String s : iterationNames) {
String arr[] = s.split(",");
Date timelineWorkItemDate;
String timelineName = arr[0];
if( (!arr[1].trim().equals("null")) )
{
DateFormat timelineDateFormat = new SimpleDateFormat("yyyy-MM-dd");
date = timelineDateFormat.parse(arr[1]);
}
else {
date = null;
}
mapIterationDetails.put(timelineName.trim(), date);
}
dateHover = new EndDateRegeneration();
Set <String> set = new HashSet<String>();
List<UUID> listSet = new ArrayList<UUID>();
List<IWorkItem> result = dateHover.getTime();
dateHover.comparison(result);
logger.info("Execution Completed at " + new Timestamp(System.currentTimeMillis()));
}
}
else{
logger.info("Properties file not found ");
}
TeamPlatform.shutdown();
}
public int comparison( List<IWorkItem> iit ) throws TeamRepositoryException, ParseException{
IAuditableClient client = (IAuditableClient) repo.getClientLibrary(IAuditableClient.class);
int count=0 ;
for(int i=0; i<iit.size(); i++ )
{
IIterationHandle handle= iit.get(i).getTarget();
if(handle != null){
IIteration iteration= client.resolveAuditable(handle, ItemProfile.ITERATION_DEFAULT, monitor);
String plannedFor=iteration.getName();
if(plannedFor != null && mapIterationDetails.get(plannedFor) != null)
{
if(mapIterationDetails.get(plannedFor.trim()) != null)
{
if(!mapIterationDetails.get(plannedFor).equals(iit.get(i).getDueDate()))
{
updateRTC(iteration.getEndDate(),iit.get(i).getId());
count ++ ;
}
}
}
}
}
logger.info("Total number of worktems are :" + iit.size());;
logger.info("Total worktems updated are :" + count);
return 0;
}
public List<IWorkItem> getTime() throws TeamRepositoryException, URISyntaxException, ParseException {
repo = TeamPlatform.getTeamRepositoryService().getTeamRepository(url);
repo.registerLoginHandler(new LoginHandler(username, password));
repo.login(monitor);
// browsing through query to get work items
workItemClient = (IWorkItemClient) repo.getClientLibrary(IWorkItemClient.class);
IQueryClient queryClient = workItemClient.getQueryClient();
String queryType = QueryTypes.WORK_ITEM_QUERY;
ItemProfile<IQueryDescriptor> profile = IQueryDescriptor.FULL_PROFILE;
IQueryDescriptor queryToRun = null;
List<IQueryDescriptor> personalQueries = null;
URI uri = new URI(url);
IProcessItemService service = (IProcessItemService) repo.getClientLibrary(IProcessItemService.class);
@SuppressWarnings("unchecked")
List<IProjectArea> iprja = service.findAllProjectAreas(IProcessItemService.ALL_PROPERTIES, monitor);
Iterator<IProjectArea> iterator = iprja.iterator();
while (iterator.hasNext()) {
iProjectArea = iterator.next();
if (projectAreaName.equalsIgnoreCase(iProjectArea.getName())) {
break;
}
}
List<? extends IAuditableHandle> list = new ArrayList<IAuditableHandle>();
personalQueries = queryClient.findSharedQueries(iProjectArea, list, QueryTypes.WORK_ITEM_QUERY, profile,
monitor);
// queryName = "Test Query Please don't modify this";
Iterator<IQueryDescriptor> iterator2 = personalQueries.iterator();
while (iterator2.hasNext())
{
IQueryDescriptor iQueryDescriptor = iterator2.next();
if (iQueryDescriptor.getName().equalsIgnoreCase(queryName)) {
queryToRun = iQueryDescriptor;
break;
}
}
List<IIterationHandle> list2 = new ArrayList<IIterationHandle>();
if (queryToRun != null) {
IQueryResult<IResult> unresolvedResults = queryClient.getQueryResults(queryToRun);
unresolvedResults.setLimit(Integer.MAX_VALUE);
JSONObject wiDetails;
sharedQueryDetailWithWorkItemIdList = new ArrayList<IWorkItem>();
while (unresolvedResults.hasNext(monitor)) {
wiDetails = new JSONObject();
IResult result = (IResult) unresolvedResults.next(monitor);
IAuditableCommon auditableCommon = (IAuditableCommon) repo.getClientLibrary(IAuditableCommon.class);
IWorkItem workItem = auditableCommon.resolveAuditable((IAuditableHandle) result.getItem(),
IWorkItem.FULL_PROFILE, monitor);
for(int i=0; i<workItem.size(); i++){
if(!sharedQueryDetailWithWorkItemIdList.contains(workItem)){
sharedQueryDetailWithWorkItemIdList.add(workItem);
System.out.println("Work Item Due Date : "+workItem.getDueDate() + " for id " + workItem.getId());
}
}
}
}
return sharedQueryDetailWithWorkItemIdList;
}
public void updateRTC(Date date, int workItemId2) throws TeamRepositoryException, ParseException {
repo = TeamPlatform.getTeamRepositoryService().getTeamRepository(url);
ITeamRepository var1 = repo;;
workItemClient = (IWorkItemClient) repo.getClientLibrary(IWorkItemClient.class);
IProcessItemService service = (IProcessItemService) repo.getClientLibrary(IProcessItemService.class);
@SuppressWarnings("unchecked")
List<IProjectArea> allProjectAreas2 = service.findAllProjectAreas(IProcessItemService.ALL_PROPERTIES,
monitor);
Iterator<IProjectArea> iterator3 = allProjectAreas2.iterator();
IProjectArea pName = null;
while (iterator3.hasNext()) {
pName = iterator3.next();
if (projectArea.equals(pName.getName())) {
break;
}
}
iProjectAreaHandle = pName.getProjectArea();
String attributName = "Due Date";
IWorkItem workItem = workItemClient.findWorkItemById(workItemId2, IWorkItem.FULL_PROFILE, monitor);
IWorkItemWorkingCopyManager copyManager = workItemClient.getWorkItemWorkingCopyManager();
copyManager.connect(workItem, IWorkItem.FULL_PROFILE, monitor);
WorkItemWorkingCopy wc = copyManager.getWorkingCopy(workItem);
IWorkItem item_work = wc.getWorkItem();
IAttribute attribute = new EndDateRegeneration().getRtcAttribute(workItemClient, iProjectAreaHandle, repo, monitor,
attributName);
item_work.setValue(attribute, date);
try{
wc.save(monitor);
}
catch(Exception ex){
System.out.println("You do no have permissions to save the changes. Please check with admin.");
}
logger.info("End Date of Work Item ID: " + workItemId2 + " updated to " + wc.getWorkItem().getDueDate() + " on " + new Timestamp(System.currentTimeMillis()) );
}
public IAttribute getRtcAttribute(IWorkItemClient client, IProjectAreaHandle paHandle, ITeamRepository repo,
IProgressMonitor monitor, String attributeName) throws TeamRepositoryException {
IAttribute iattribute = null;
List<IAttribute> customAttrList = client.findAttributes(paHandle, monitor);
for (IAttributeHandle iAttribute : customAttrList) {
iattribute = (IAttribute) repo.itemManager().fetchCompleteItem(iAttribute, 0, monitor);
if (iattribute.getDisplayName().trim().equalsIgnoreCase(attributeName.trim())) {
break;
}
}
return iattribute;
}
@SuppressWarnings("unchecked")
public ArrayList<String> getPlannedForList(String username, String password, String url)
throws TeamRepositoryException {
ArrayList<String> list = new ArrayList<String>();
ArrayList<String> list1 = new ArrayList<String>();
IProgressMonitor monitor = new NullProgressMonitor();
ITeamRepository teamRepository = TeamPlatform.getTeamRepositoryService().getTeamRepository(url);
teamRepository.registerLoginHandler(new LoginHandler(username, password));
teamRepository.login(monitor);
service = (IProcessItemService) teamRepository.getClientLibrary(IProcessItemService.class);
List<IProjectArea> allProjectAreas = service.findAllProjectAreas(IProcessItemService.ALL_PROPERTIES, monitor);
java.util.Iterator<IProjectArea> iterator = allProjectAreas.iterator();
while (iterator.hasNext()) {
sProjectArea = iterator.next();
if (projectArea.equals(sProjectArea.getName())) {
break;
}
}
sProjectArea = (IProjectArea) sProjectArea.getWorkingCopy();
IDevelopmentLineHandle[] iDevelopmentLineHandles = sProjectArea.getDevelopmentLines();
IDevelopmentLine line = null;
if (iDevelopmentLineHandles != null) {
List<IProjectArea> developmentLines = null;
try {
developmentLines = teamRepository.itemManager()
.fetchCompleteItems(Arrays.asList(iDevelopmentLineHandles), IItemManager.DEFAULT, null);
} catch (TeamRepositoryException e1) {
e1.printStackTrace();
}
for (java.util.Iterator<IProjectArea> e = developmentLines.iterator(); e.hasNext();) {
line = (IDevelopmentLine) e.next();
String timelineName = line.getName();
IIterationHandle[] iterationHandles = line.getIterations();
list = PlannedForRecursive(iterationHandles, teamRepository);
if (list != null) {
String name = null;
for (int i = 0; i < list.size(); i++) {
name = list.get(i);
list1.add(name);
}
}
}
}
return list1;
}
@SuppressWarnings("unchecked")
public static ArrayList<String> PlannedForRecursive(IIterationHandle[] iterationHandles,
ITeamRepository teamRepository) {
ArrayList<String> interation_name = new ArrayList<String>();
Set<String> set = new HashSet<>();
String append = null;
if (iterationHandles != null) {
List<IProjectArea> iterationlines = null;
try {
iterationlines = teamRepository.itemManager().fetchCompleteItems(Arrays.asList(iterationHandles),
IItemManager.DEFAULT, null);
} catch (TeamRepositoryException e) {
e.printStackTrace();
}
IIteration iIteration = null;
Iterator<IProjectArea> iteration = iterationlines.iterator();
String name;
while (iteration.hasNext()) {
iIteration = (IIteration) iteration.next();
name = "";
if (iIteration.getName() != null && !iIteration.isArchived()) {
name = iIteration.getName() + "," + iIteration.getEndDate();
interation_name.add(name);
System.out.println("Iteration Name : " + name );
}
ArrayList<String> tempList = null;
if (iIteration.getChildren() != null) {
tempList = PlannedForRecursive(iIteration.getChildren(), teamRepository);
if (tempList != null) {
for (String s : tempList) {
interation_name.add(s);
set.add(s);
map1.put(iIteration.getId(), iIteration.getEndDate());
}
}
}
}
for (String string : set) {
}
for (Map.Entry<String, Date> entry : map1.entrySet()) {
}
}
return interation_name;
}
private static class LoginHandler implements ILoginHandler, ILoginInfo {
private String fUserId;
private String fPassword;
LoginHandler(String userId, String password) {
fUserId = userId;
fPassword = password;
}
public String getUserId() {
return fUserId;
}
public String getPassword() {
return fPassword;
}
public ILoginInfo challenge(ITeamRepository repository) {
return (ILoginInfo) this;
}
}
}
Comments
Do you perhaps mean "due date"? Or is "end date" some custom property that your project lead has introduced?