It's all about the answers!

Ask a question

How to update the time tracking section in any RTC Wok item programatically


Vaibhav S (9217) | asked Aug 30 '18, 5:58 a.m.

Hi,,

How to update the time tracking section in any RTC Wok item programmatically

If anyone has achieved this please let me now.

Thanks
Vaibhav

One answer



permanent link
Vaibhav S (9217) | answered Aug 30 '18, 7:08 a.m.

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;

import com.ibm.team.links.common.IReference;
import com.ibm.team.links.common.factory.IReferenceFactory;
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.common.TeamRepositoryException;
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.IWorkItemCommon;
import com.ibm.team.workitem.common.IWorkItemCommon.ITimeCode;
import com.ibm.team.workitem.common.model.Duration;
import com.ibm.team.workitem.common.model.IAttribute;
import com.ibm.team.workitem.common.model.IAttributeHandle;
import com.ibm.team.workitem.common.model.ILiteral;
import com.ibm.team.workitem.common.model.ITimeSheetEntry;
import com.ibm.team.workitem.common.model.ITimeSheetEntryHandle;
import com.ibm.team.workitem.common.model.IWorkItem;
import com.ibm.team.workitem.common.model.IWorkItemHandle;
import com.ibm.team.workitem.common.model.IWorkItemReferences;
import com.ibm.team.workitem.common.model.Identifier;
import com.ibm.team.workitem.common.model.WorkItemEndPoints;

public class UpdateTimeTracking {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

    public UpdateTimeTracking(IWorkItem workItem, HashMap<String, HashMap<Timestamp, ITimeSheetEntry>> trackMap,
            Duration totalDuration) {
        super();
        this.workItem = workItem;
        this.trackMap = trackMap;
        this.totalDuration = totalDuration;
    }

    private IWorkItem workItem = null;
    private HashMap<String, HashMap<Timestamp, ITimeSheetEntry>> trackMap = null;
    private Duration totalDuration = null;

    //masoom
    public void updateOrCreateTimeSheetEntry(WorkItemWorkingCopy destWorkingCopy , ITimeSheetEntry entry, IProgressMonitor monitor,
            ITeamRepository teamRepository, IProjectAreaHandle iProjectAreaHandle) throws TeamRepositoryException
    {

        UpdateTimeTracking updateTimeTracking= new UpdateTimeTracking(destWorkingCopy.getWorkItem(),null,null);
        setWorkItem(destWorkingCopy.getWorkItem());

        //pass detination repo object in this method
        ITimeCode timeCode= updateTimeTracking.calculateTimeCode(entry.getTimeCode(), teamRepository);
        ITimeSheetEntry timeSheet = findTimeSheetEntry(timeCode, entry.getStartDate(), monitor, teamRepository);
        if (timeSheet == null)
        {
            timeSheet = createTimeSheet(teamRepository);
            destWorkingCopy.getReferences().add(WorkItemEndPoints.WORK_TIME, IReferenceFactory.INSTANCE.createReferenceToItem(timeSheet));
            addEntry(timeSheet, monitor, teamRepository);
        } else {
            timeSheet = (ITimeSheetEntry) timeSheet.getWorkingCopy();
        }

        timeSheet.setContextId(entry.getContextId());
        timeSheet.setCreator(entry.getCreator());
        timeSheet.setHoursSpent(entry.getHoursSpent());
        timeSheet.setRequestedModified(entry.getRequestedModified());
        timeSheet.setRequestedStateId(entry.getRequestedStateId());
        timeSheet.setStartDate(entry.getStartDate());
        timeSheet.setTimeCode(timeCode.getTimeCodeLabel());
        timeSheet.setTimeCodeId(entry.getTimeCodeId());
        timeSheet.setTimeSpent(entry.getTimeSpent());
        timeSheet.setWorkType(entry.getWorkType());
        
        IWorkItem destWorkItem= destWorkingCopy.getWorkItem();
        
        IWorkItemClient client = (IWorkItemClient) teamRepository.getClientLibrary(IWorkItemClient.class);
        
        IAttribute attribute = updateTimeTracking.getRtcAttribute(client, iProjectAreaHandle, teamRepository, monitor, "Time Spent");
        
        long l=(long)destWorkItem.getValue(attribute)+1;
        
        double hourSpent= timeSheet.getHoursSpent();
        long l_hourSpent = (new Double(hourSpent)).longValue();
        destWorkItem.setValue(attribute, (l_hourSpent*3600000l)+l);
        
        destWorkingCopy.getDependentItems().add(timeSheet);
        destWorkingCopy.save(monitor);
    }
    
    //masoom
    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;
    }
    
    //masoom: pass destination repo object in this
    private ITimeCode calculateTimeCode(String timeCodeName, ITeamRepository teamRepository) throws TeamRepositoryException {
        // Find the time code
        ITimeCode timeCodeToSet = null;
        
        IWorkItemCommon iWorkItemCommon= getWorkItemCommon(teamRepository);
        //detination WI
        IWorkItem workItem= getWorkItem();
        //destination PA handle
        IProjectAreaHandle iProjectAreaHandle=workItem.getProjectArea();
        
        List<ITimeCode> timeCodes =null;
        timeCodes=iWorkItemCommon.getTimeCodes(iProjectAreaHandle);
        //List<ITimeCode> timeCodes = getWorkItemCommon().getTimeCodes(getWorkItem().getProjectArea());
        
        for (ITimeCode aTimeCode : timeCodes)
        {
            if (aTimeCode.getTimeCodeLabel().equals(timeCodeName))
            {
                timeCodeToSet = aTimeCode;
                break;
            }
        }
        if (timeCodeToSet == null)
        {
            throw new TeamRepositoryException("Timecode not found: "+ timeCodeName);
        }
        return timeCodeToSet;
    }
    
    //masoom
    public void setWorkItem(IWorkItem workItem) {
        // we want to preserve the time tracking data in case the work item is
        // identical with the current one.
        if (this.workItem == null) {
            initialize(workItem);
        } else if (!this.workItem.getItemId().equals(workItem.getItemId())) {
            initialize(workItem);
        }
    }
    
    //masoom
    private ITimeSheetEntry findTimeSheetEntry(ITimeCode timeCode, Timestamp startDate, IProgressMonitor monitor, ITeamRepository teamRepository) throws TeamRepositoryException
    {
        // Get the map with all the time sheets
        HashMap<String, HashMap<Timestamp, ITimeSheetEntry>> map = getTrackMap(monitor,  teamRepository);
        
        if (!map.containsKey(timeCode.getTimeCodeId())) {
            // No entry for TimeCode
            return null;
        }
        // Get all the entries for the TimeCode
        HashMap<Timestamp, ITimeSheetEntry> timeCodeEntries = map.get(timeCode
                .getTimeCodeId());
        if (timeCodeEntries.containsKey(startDate)) {
            // get the entry and return it.
            return timeCodeEntries.get(startDate);
        }
        return null;
    }
    
    //masoom
    private ITimeSheetEntry createTimeSheet(ITeamRepository teamRepository) throws TeamRepositoryException {
        ITimeSheetEntry timeSheet;
        timeSheet = getWorkItemCommon(teamRepository).createTimeSheetEntry(
                getWorkItem().getProjectArea());
        return timeSheet;
    }
    
    //masoom
    private void addEntry(ITimeSheetEntry timeSheetEntry, IProgressMonitor monitor, ITeamRepository teamRepository) throws TeamRepositoryException
    {
        HashMap<String, HashMap<Timestamp, ITimeSheetEntry>> map = getTrackMap(monitor, teamRepository);
        
        String timeCodeID = timeSheetEntry.getTimeCodeId();
        HashMap<Timestamp, ITimeSheetEntry> timeSheetMap = map.get(timeCodeID);
        if (timeSheetMap == null) {
            timeSheetMap = new HashMap<Timestamp, ITimeSheetEntry>();
            map.put(timeCodeID, timeSheetMap);
        }
        if (timeSheetMap.containsKey(timeSheetEntry.getStartDate())) {
            System.out.println("Error: entry already available");
        }
        timeSheetMap.put(timeSheetEntry.getStartDate(), timeSheetEntry);
        setTotalDuration(getTotalDuration().add(timeSheetEntry.getTimeSpent()));
    }
    
    //masoom
    private void setTotalDuration(Duration totalDuration) {
        this.totalDuration = totalDuration;
    }
    
    //masoom
    public Duration getTotalDuration() {
        return totalDuration;
    }
    
    //masoom
    public IWorkItem getWorkItem() {
        return this.workItem;
    }
    
    //masoom
    private void initialize(IWorkItem workItem) {
        this.trackMap = null;
        this.workItem = workItem;
        setTotalDuration(new Duration(0));
    }
    
    //masoom
    private HashMap<String, HashMap<Timestamp, ITimeSheetEntry>> getTrackMap(IProgressMonitor monitor, ITeamRepository teamRepository) throws TeamRepositoryException {
        if (this.trackMap == null)
        {
            this.trackMap = new HashMap<String, HashMap<Timestamp, ITimeSheetEntry>>();
            readTimeSheetEntries(monitor,  teamRepository);
        }
        return this.trackMap;
    }
    
    
    //masoom: login to destination- Pass detination repo object in this method
    private IWorkItemCommon getWorkItemCommon(ITeamRepository teamRepository) throws TeamRepositoryException {
        
        IWorkItemCommon common = (IWorkItemCommon) teamRepository.getClientLibrary(IWorkItemCommon.class);
        return common;
    }

    //masoom
    private void readTimeSheetEntries(IProgressMonitor monitor, ITeamRepository teamRepository)
            throws TeamRepositoryException {
        
        IWorkItemCommon workItemCommon = getWorkItemCommon(teamRepository);
        IWorkItemHandle iWorkItemHandle= (IWorkItemHandle)getWorkItem();
        IWorkItemReferences references = workItemCommon.resolveWorkItemReferences(iWorkItemHandle, monitor);
        List<IReference> referenceList = references.getReferences(WorkItemEndPoints.WORK_TIME);
        for (IReference aReference : referenceList)
        {
            if (aReference.isItemReference())
            {
                Object target = aReference.resolve();
                if (target instanceof ITimeSheetEntryHandle)
                {
                    ITimeSheetEntryHandle timeSheetEntry = (ITimeSheetEntryHandle) target;
                    ITimeSheetEntry tsEntry = (ITimeSheetEntry) ((ITeamRepository) getWorkItem().getOrigin()).itemManager().fetchCompleteItem(timeSheetEntry, IItemManager.REFRESH, monitor);
                    addEntry(tsEntry, monitor,teamRepository);
                }
            }
        }
    }
}






import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import org.eclipse.core.runtime.IProgressMonitor;

import com.ibm.team.links.common.IReference;
import com.ibm.team.links.common.factory.IReferenceFactory;
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.TeamPlatform;
import com.ibm.team.repository.client.ITeamRepository.ILoginHandler.ILoginInfo;
import com.ibm.team.repository.common.TeamRepositoryException;
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.IWorkItemCommon;
import com.ibm.team.workitem.common.IWorkItemCommon.ITimeCode;
import com.ibm.team.workitem.common.model.Duration;
import com.ibm.team.workitem.common.model.ILiteral;
import com.ibm.team.workitem.common.model.ITimeSheetEntry;
import com.ibm.team.workitem.common.model.ITimeSheetEntryHandle;
import com.ibm.team.workitem.common.model.IWorkItem;
import com.ibm.team.workitem.common.model.IWorkItemReferences;
import com.ibm.team.workitem.common.model.Identifier;
import com.ibm.team.workitem.common.model.WorkItemEndPoints;

public class TimeTrackingHelper {

    private static final long HOUR_IN_MILLISECONDS = 1000 * 60 * 60;

    // We store the time sheets in a structure that speeds up access
    private HashMap<String, HashMap<Timestamp, ITimeSheetEntry>> trackMap = null;
    // The work item that is currently looked at
    private IWorkItem workItem = null;
    // The total time spent based on all the time sheet entries
    //private Duration totalDuration = null;

    private HashMap<String, HashMap<Timestamp, ITimeSheetEntry>> getTrackMap(IProgressMonitor monitor) throws TeamRepositoryException
    {
        if (this.trackMap == null)
        {
            this.trackMap = new HashMap<String, HashMap<Timestamp, ITimeSheetEntry>>();
            readTimeSheetEntries(monitor);
        }
        return this.trackMap;
    }


    public void setWorkItem(IWorkItem workItem) {
        // we want to preserve the time tracking data in case the work item is identical with the current one.
        if (this.workItem == null) {
            initialize(workItem);
        } else if (!this.workItem.getItemId().equals(workItem.getItemId())) {
            initialize(workItem);
        }
    }

    public IWorkItem getWorkItem() {
        return this.workItem;
    }

    public TimeTrackingHelper() {
        super();
    }

    private void initialize(IWorkItem workItem) {
        this.trackMap = null;
        this.workItem = workItem;
        //setTotalDuration(new Duration(0));
    }

    public void refresh() {
        // Force a refresh
        initialize(null);
    }

    //masoom
    private IWorkItemCommon getWorkItemCommon() {
        return (IWorkItemCommon) ((ITeamRepository) getWorkItem().getOrigin())
                .getClientLibrary(IWorkItemCommon.class);
    }

    //masoom
    private void readTimeSheetEntries(IProgressMonitor monitor)throws TeamRepositoryException
    {
        IWorkItemCommon workItemCommon = getWorkItemCommon();
        IWorkItemReferences references = workItemCommon.resolveWorkItemReferences(getWorkItem(), monitor);
        List<IReference> referenceList = references.getReferences(WorkItemEndPoints.WORK_TIME);
        for (IReference aReference : referenceList)
        {
            if (aReference.isItemReference())
            {
                Object target = aReference.resolve();
                if (target instanceof ITimeSheetEntryHandle)
                {
                    ITimeSheetEntryHandle timeSheetEntry = (ITimeSheetEntryHandle) target;
                    ITimeSheetEntry tsEntry = (ITimeSheetEntry) ((ITeamRepository) getWorkItem().getOrigin()).itemManager().fetchCompleteItem(timeSheetEntry, IItemManager.REFRESH, monitor);
                    addEntry(tsEntry, monitor);
                }
            }
        }
    }

    //masoom
    private void addEntry(ITimeSheetEntry timeSheetEntry, IProgressMonitor monitor) throws TeamRepositoryException
    {
        HashMap<String, HashMap<Timestamp, ITimeSheetEntry>> map = getTrackMap(monitor);
        String timeCodeID = timeSheetEntry.getTimeCodeId();
        HashMap<Timestamp, ITimeSheetEntry> timeSheetMap = map.get(timeCodeID);
        if (timeSheetMap == null) {
            timeSheetMap = new HashMap<Timestamp, ITimeSheetEntry>();
            map.put(timeCodeID, timeSheetMap);
        }
        if (timeSheetMap.containsKey(timeSheetEntry.getStartDate())) {
            System.out.println("Error: entry already available");
        }
        timeSheetMap.put(timeSheetEntry.getStartDate(), timeSheetEntry);
        //setTotalDuration(getTotalDuration().add(timeSheetEntry.getTimeSpent()));
    }

    //masoom
    private void visitEntries(WorkItemWorkingCopy dest_WorkingCopy, IProgressMonitor monitor, ITeamRepository teamRepository, IProjectAreaHandle iProjectAreaHandle) throws TeamRepositoryException
    {
        HashMap<String, HashMap<Timestamp, ITimeSheetEntry>> map = getTrackMap(monitor);
       
        for (Iterator<Entry<String, HashMap<Timestamp, ITimeSheetEntry>>> timeCodeIterator = map.entrySet().iterator(); timeCodeIterator.hasNext();)
        {
            System.out.println("Inside outer map: -----------------");
            Entry<String, HashMap<Timestamp, ITimeSheetEntry>> mapEntry = (Entry<String, HashMap<Timestamp, ITimeSheetEntry>>) timeCodeIterator.next();
            System.out.println("Outer map Key(Time Code ID): "+mapEntry.getKey());
            HashMap<Timestamp, ITimeSheetEntry> values = mapEntry.getValue();
           
            for (Iterator<Entry<Timestamp, ITimeSheetEntry>> entryIterator = values.entrySet().iterator(); entryIterator.hasNext();)
            {
                System.out.println("Inside outer map: -----------------");
                Entry<Timestamp, ITimeSheetEntry> valueEntry = (Entry<Timestamp, ITimeSheetEntry>) entryIterator.next();
               
                System.out.println("Key(Start Date): "+valueEntry.getKey()+" +++++++++++ "+"Value: "+valueEntry.getValue().getStartDate());
               
                TimeTrackingHelper timeTrackingHelper = new TimeTrackingHelper();
                timeTrackingHelper.execute(dest_WorkingCopy, valueEntry.getValue(), monitor,  teamRepository,  iProjectAreaHandle);
            }
        }
    }

    //masoom
    public void execute(WorkItemWorkingCopy dest_WorkingCopy, ITimeSheetEntry entry, IProgressMonitor monitor, ITeamRepository teamRepository, IProjectAreaHandle iProjectAreaHandle){
        try {
            printTimeSheet(dest_WorkingCopy, entry, monitor,  teamRepository,  iProjectAreaHandle);
        } catch (TeamRepositoryException e) {
            e.printStackTrace();
        }
    }
   
    //masoom
    public void printTimeSheet(WorkItemWorkingCopy dest_WorkingCopy, ITimeSheetEntry timeSheetEntry, IProgressMonitor monitor, ITeamRepository teamRepository, IProjectAreaHandle iProjectAreaHandle) throws TeamRepositoryException
    {
        System.out.println("Entry: ");
       
        UpdateTimeTracking updateTimeTracking= new UpdateTimeTracking(null,null,null);
        updateTimeTracking.updateOrCreateTimeSheetEntry(dest_WorkingCopy ,timeSheetEntry, monitor,  teamRepository,  iProjectAreaHandle);
       
        System.out.println(timeSheetToString(timeSheetEntry));
    }
   
    //masoom
    public String timeSheetToString(ITimeSheetEntry timeSheetEntry)
    {
        String entry = "";
        entry += " TimeCode: " + timeSheetEntry.getTimeCode() + "\n";
        entry += " TimeCodeID: " + timeSheetEntry.getTimeCodeId() + "\n";
        entry += " WorkType: " + timeSheetEntry.getWorkType().getStringIdentifier() + "\n";
        entry += " Date: " + SimpleDateFormatUtil.getDate(timeSheetEntry.getStartDate()) + "\n";
        entry += " HoursSpent: " + timeSheetEntry.getHoursSpent() + "\n";
        entry += " TimeSpent: " + timeSheetEntry.getTimeSpent().longValue() + "\n";
        entry += " Creator ID: " + timeSheetEntry.getCreator().getItemId() + "\n";
        entry += " Creation Date: " + timeSheetEntry.getCreationDate() + "\n";
       
        return entry;
    }

    //masoom
    public void printTimeSheets(WorkItemWorkingCopy dest_WorkingCopy, IWorkItem workItem, IProgressMonitor monitor, ITeamRepository teamRepository, IProjectAreaHandle iProjectAreaHandle)
            throws TeamRepositoryException {
        setWorkItem(workItem);
        visitEntries(dest_WorkingCopy, monitor, teamRepository, iProjectAreaHandle);
    }


}

Your answer


Register or to post your answer.