How to get a WorkingCopy of a hierarchy
I am working on re-creating a team hierarchy from one project area to another.
I have gotten down to getting an IWorkingCopyManager as well as creating a new ITeamAreaHandle and getting the ITeamAreaHierarchy from the project area.
However, I am getting the ImmutablePropertyException on the teamHierarchy.addRoot(teamAreaHandle, timeline).
I assume this is because I have the hierarchy and not a hierarchyWorkingCopy (if one exists).
Has anyone done this that they could share a snippet?
Susan
I have gotten down to getting an IWorkingCopyManager as well as creating a new ITeamAreaHandle and getting the ITeamAreaHierarchy from the project area.
However, I am getting the ImmutablePropertyException on the teamHierarchy.addRoot(teamAreaHandle, timeline).
I assume this is because I have the hierarchy and not a hierarchyWorkingCopy (if one exists).
Has anyone done this that they could share a snippet?
Susan
3 answers
here is some code I have for that copy team area from src project (and server maybe) to dest project (and server maybe)
<code>
// note, this function ASSUMES it has the destination projectarea working copy on input
ITeamAreaHierarchy srchierarchy = iprja_src.getTeamAreaHierarchy();
ITeamAreaHierarchy desthierarchy = iprja_dest.getTeamAreaHierarchy();
// allocate work area for list of changed/created timelines and
// iterations
// will be used to update the repository all at once when we are
// done.
ArrayList<IProcessItem> newitemlist = new ArrayList<IProcessItem>();
// add the project to the list
newitemlist.add((new IProcessItem[]
{ iprja_dest })[0]);
// loop thru the team areas
for (ITeamAreaHandle baseteam : new ArrayList<ITeamAreaHandle>(srchierarchy.getRoots()))
{
processchildTeamAreas(newitemlist, srcauditableClient, destauditableClient, iprja_src, iprja_dest,
srchierarchy, desthierarchy, baseteam, null);
}
// allocate space for the array
IProcessItem[] ilist = new IProcessItem[newitemlist.size()];
// build the array
ilist = newitemlist.toArray(ilist);
// save all the new/modified objects to the repository
try
{
destprocessItemClient.save(ilist, null);
}
catch (Exception ex)
{
if (!ex.getMessage().contains("Cannot have two team areas with the name"))
System.out.println("Exception=" + ex.getMessage());
}
/////
private static void processchildTeamAreas(ArrayList<IProcessItem> newitemlist,
IAuditableClient srcauditableClient, IAuditableClient destauditableClient, IProjectArea srcproject,
IProjectArea destproject, ITeamAreaHierarchy srchierarchy, ITeamAreaHierarchy desthierarchy,
ITeamAreaHandle baseteam, ITeamAreaHandle parent)
{
try
{
// get the full object to see the name field
IDevelopmentLine srcdevline = (IDevelopmentLine) srcauditableClient.fetchCurrentAuditable(
srchierarchy.getDevelopmentLine(baseteam),
ItemProfile.createFullProfile(IDevelopmentLine.ITEM_TYPE), null);
// get the full object to see the name field
ITeamArea srcteamarea = (ITeamArea) srcauditableClient.fetchCurrentAuditable(baseteam,
ItemProfile.createFullProfile(ITeamArea.ITEM_TYPE), null);
// don't copy archived team areas
if(!srcteamarea.isArchived())
{
System.out.println("src timeline and team area = '"+srcdevline.getId()+"' and '"+srcteamarea.getName()+"'");
ITeamArea destTeamArea = (ITeamArea) ITeamArea.ITEM_TYPE.createItem();
destTeamArea.setName(srcteamarea.getName());
destTeamArea.setProcessName(srcteamarea.getProcessName());
destTeamArea.setProcessSummary(srcteamarea.getProcessSummary());
destTeamArea.setProjectArea(destproject);
destTeamArea.getDescription().setSummary(srcteamarea.getDescription().getSummary()); //new
destTeamArea.getDescription().setDetails(copycontent(srcteamarea.getDescription().getDetails(),((ITeamRepository)srcproject.getOrigin()).contentManager())); //new
// add the new team area to the list of modified objects
newitemlist.add((new IProcessItem[]
{ destTeamArea })[0]);
// find the matching development line on the target system
IDevelopmentLineHandle destline = getmatchingDevline(srcdevline.getName(), destproject, destauditableClient);
if (destline != null)
{
// desthierarchy.setDevelopmentLine(destTeamArea, destline);
// is this a root timeline?
if (parent == null)
{
// use it.
// desthierarchy.setDevelopmentLine(destTeamArea, destline);
desthierarchy.addRoot(destTeamArea, destline);
}
// nope add us to the parent
else
desthierarchy.addChild(parent, destTeamArea);
// get the list of members in the src team area
for (IContributorHandle srcteammember : srcteamarea.getMembers())
{
// get the full object to see the name field
IContributor srcmember = (IContributor) srcauditableClient.fetchCurrentAuditable(srcteammember,
ItemProfile.createFullProfile(IContributor.ITEM_TYPE), null);
// find the matching dest system user
try
{
IContributorHandle destuser = destauditableClient.getTeamRepository().contributorManager()
.fetchContributorByUserId(srcmember.getEmailAddress(), null);
// if found, add to the team area
destTeamArea.addMember(destuser);
RolePersistence.setPersistedRoleData(destTeamArea.getTeamData(), destuser,
RolePersistence.getPersistentRoleData(srcteamarea.getTeamData(), srcteammember));
}
catch (Exception ex)
{
System.out.println("unable to find user with email="+srcmember.getEmailAddress());
}
}
}
if (srchierarchy.getChildren(baseteam).size() > 0)
{
// loop thru the list of team areas
for (ITeamAreaHandle childteam: (ITeamAreaHandle[])srchierarchy.getChildren(baseteam).toArray(new ITeamAreaHandle[1]))
{
// process for any children, recursively
processchildTeamAreas(newitemlist, srcauditableClient, destauditableClient, srcproject,
destproject, srchierarchy, desthierarchy, childteam, destTeamArea);
}
}
}
}
catch (TeamRepositoryException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
private static IContent copycontent(IContent in, IContentManager icm)
{
IContent out = null;
// get the content of the content object.
try
{
if (in != null)
{
IAuditableCommon auditable = (IAuditableCommon) destauditableClient.getTeamRepository().getClientLibrary(
IAuditableCommon.class);
// make an output stream for the content manager
OutputStream os = new ByteArrayOutputStream();
// get the content of the content object.
icm.retrieveContent(in, os, null);
// extract the bytes, need the object twice
byte[] bytes = os.toString().getBytes(in.getCharacterEncoding());
// make a content object on the destination system
out=auditable.storeContent(in.getContentType(), in.getCharacterEncoding(), new ByteArrayInputStream(bytes),
bytes.length, null);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return out;
}
private static IDevelopmentLineHandle getmatchingDevline(String prodname, IProjectArea destproject,
IAuditableClient destauditableClient)
{
IDevelopmentLineHandle foundline = null;
// loop thru the list of timelines
for (IDevelopmentLineHandle destlineh: destproject.getDevelopmentLines())
{
try
{
// get the full profile, so we can get the name field
IDevelopmentLine destline = (IDevelopmentLine) destauditableClient.fetchCurrentAuditable(destlineh,
ItemProfile.createFullProfile(IDevelopmentLine.ITEM_TYPE), null);
// if the target project timeline name matches the one in
// the prod team area
if (destline.getName().equalsIgnoreCase(prodname))
{
foundline = destlineh;
break;
}
}
catch (TeamRepositoryException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return foundline;
}
</code>
<code>
// note, this function ASSUMES it has the destination projectarea working copy on input
ITeamAreaHierarchy srchierarchy = iprja_src.getTeamAreaHierarchy();
ITeamAreaHierarchy desthierarchy = iprja_dest.getTeamAreaHierarchy();
// allocate work area for list of changed/created timelines and
// iterations
// will be used to update the repository all at once when we are
// done.
ArrayList<IProcessItem> newitemlist = new ArrayList<IProcessItem>();
// add the project to the list
newitemlist.add((new IProcessItem[]
{ iprja_dest })[0]);
// loop thru the team areas
for (ITeamAreaHandle baseteam : new ArrayList<ITeamAreaHandle>(srchierarchy.getRoots()))
{
processchildTeamAreas(newitemlist, srcauditableClient, destauditableClient, iprja_src, iprja_dest,
srchierarchy, desthierarchy, baseteam, null);
}
// allocate space for the array
IProcessItem[] ilist = new IProcessItem[newitemlist.size()];
// build the array
ilist = newitemlist.toArray(ilist);
// save all the new/modified objects to the repository
try
{
destprocessItemClient.save(ilist, null);
}
catch (Exception ex)
{
if (!ex.getMessage().contains("Cannot have two team areas with the name"))
System.out.println("Exception=" + ex.getMessage());
}
/////
private static void processchildTeamAreas(ArrayList<IProcessItem> newitemlist,
IAuditableClient srcauditableClient, IAuditableClient destauditableClient, IProjectArea srcproject,
IProjectArea destproject, ITeamAreaHierarchy srchierarchy, ITeamAreaHierarchy desthierarchy,
ITeamAreaHandle baseteam, ITeamAreaHandle parent)
{
try
{
// get the full object to see the name field
IDevelopmentLine srcdevline = (IDevelopmentLine) srcauditableClient.fetchCurrentAuditable(
srchierarchy.getDevelopmentLine(baseteam),
ItemProfile.createFullProfile(IDevelopmentLine.ITEM_TYPE), null);
// get the full object to see the name field
ITeamArea srcteamarea = (ITeamArea) srcauditableClient.fetchCurrentAuditable(baseteam,
ItemProfile.createFullProfile(ITeamArea.ITEM_TYPE), null);
// don't copy archived team areas
if(!srcteamarea.isArchived())
{
System.out.println("src timeline and team area = '"+srcdevline.getId()+"' and '"+srcteamarea.getName()+"'");
ITeamArea destTeamArea = (ITeamArea) ITeamArea.ITEM_TYPE.createItem();
destTeamArea.setName(srcteamarea.getName());
destTeamArea.setProcessName(srcteamarea.getProcessName());
destTeamArea.setProcessSummary(srcteamarea.getProcessSummary());
destTeamArea.setProjectArea(destproject);
destTeamArea.getDescription().setSummary(srcteamarea.getDescription().getSummary()); //new
destTeamArea.getDescription().setDetails(copycontent(srcteamarea.getDescription().getDetails(),((ITeamRepository)srcproject.getOrigin()).contentManager())); //new
// add the new team area to the list of modified objects
newitemlist.add((new IProcessItem[]
{ destTeamArea })[0]);
// find the matching development line on the target system
IDevelopmentLineHandle destline = getmatchingDevline(srcdevline.getName(), destproject, destauditableClient);
if (destline != null)
{
// desthierarchy.setDevelopmentLine(destTeamArea, destline);
// is this a root timeline?
if (parent == null)
{
// use it.
// desthierarchy.setDevelopmentLine(destTeamArea, destline);
desthierarchy.addRoot(destTeamArea, destline);
}
// nope add us to the parent
else
desthierarchy.addChild(parent, destTeamArea);
// get the list of members in the src team area
for (IContributorHandle srcteammember : srcteamarea.getMembers())
{
// get the full object to see the name field
IContributor srcmember = (IContributor) srcauditableClient.fetchCurrentAuditable(srcteammember,
ItemProfile.createFullProfile(IContributor.ITEM_TYPE), null);
// find the matching dest system user
try
{
IContributorHandle destuser = destauditableClient.getTeamRepository().contributorManager()
.fetchContributorByUserId(srcmember.getEmailAddress(), null);
// if found, add to the team area
destTeamArea.addMember(destuser);
RolePersistence.setPersistedRoleData(destTeamArea.getTeamData(), destuser,
RolePersistence.getPersistentRoleData(srcteamarea.getTeamData(), srcteammember));
}
catch (Exception ex)
{
System.out.println("unable to find user with email="+srcmember.getEmailAddress());
}
}
}
if (srchierarchy.getChildren(baseteam).size() > 0)
{
// loop thru the list of team areas
for (ITeamAreaHandle childteam: (ITeamAreaHandle[])srchierarchy.getChildren(baseteam).toArray(new ITeamAreaHandle[1]))
{
// process for any children, recursively
processchildTeamAreas(newitemlist, srcauditableClient, destauditableClient, srcproject,
destproject, srchierarchy, desthierarchy, childteam, destTeamArea);
}
}
}
}
catch (TeamRepositoryException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
private static IContent copycontent(IContent in, IContentManager icm)
{
IContent out = null;
// get the content of the content object.
try
{
if (in != null)
{
IAuditableCommon auditable = (IAuditableCommon) destauditableClient.getTeamRepository().getClientLibrary(
IAuditableCommon.class);
// make an output stream for the content manager
OutputStream os = new ByteArrayOutputStream();
// get the content of the content object.
icm.retrieveContent(in, os, null);
// extract the bytes, need the object twice
byte[] bytes = os.toString().getBytes(in.getCharacterEncoding());
// make a content object on the destination system
out=auditable.storeContent(in.getContentType(), in.getCharacterEncoding(), new ByteArrayInputStream(bytes),
bytes.length, null);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return out;
}
private static IDevelopmentLineHandle getmatchingDevline(String prodname, IProjectArea destproject,
IAuditableClient destauditableClient)
{
IDevelopmentLineHandle foundline = null;
// loop thru the list of timelines
for (IDevelopmentLineHandle destlineh: destproject.getDevelopmentLines())
{
try
{
// get the full profile, so we can get the name field
IDevelopmentLine destline = (IDevelopmentLine) destauditableClient.fetchCurrentAuditable(destlineh,
ItemProfile.createFullProfile(IDevelopmentLine.ITEM_TYPE), null);
// if the target project timeline name matches the one in
// the prod team area
if (destline.getName().equalsIgnoreCase(prodname))
{
foundline = destlineh;
break;
}
}
catch (TeamRepositoryException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return foundline;
}
</code>
Comments
showing 5 of 6
show 1 more comments
Please have a look into the snippets that come with the Plain Java Client Libraries. There is some explanation here too: https://rsjazz.wordpress.com/2013/09/18/deploying-templates-and-creating-projects-using-the-plain-java-clients-library/
The code looks funny.....
The code looks funny.....
Comments
this should be the new code.. (tested on same repo) (my code assumes different rtc repos all the time)
destTeamArea.setName(srcteamarea.getName());
destTeamArea.setProcessName(srcteamarea.getProcessName());
destTeamArea.setProcessSummary(srcteamarea.getProcessSummary());
destTeamArea.setProjectArea(destproject);
destTeamArea.getDescription().setSummary(srcteamarea.getDescription().getSummary()); //new
destTeamArea.getDescription().setDetails(copycontent(srcteamarea.getDescription().getDetails(),((ITeamRepository)srcproject.getOrigin()).contentManager())); //new
////
worker function
private static IContent copycontent(IContent in, IContentManager icm)
{
IContent out = null;
// get the content of the content object.
try
{
if (in != null)
{
IAuditableCommon auditable = (IAuditableCommon) destauditableClient.getTeamRepository().getClientLibrary(
IAuditableCommon.class);
// make an output stream for the content manager
OutputStream os = new ByteArrayOutputStream();
// get the content of the content object.
icm.retrieveContent(in, os, null);
// extract the bytes, need the object twice
byte[] bytes = os.toString().getBytes(in.getCharacterEncoding());
// make a content object on the destination system
out=auditable.storeContent(in.getContentType(), in.getCharacterEncoding(), new ByteArrayInputStream(bytes),
bytes.length, null);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return out;
}
destTeamArea.setName(srcteamarea.getName());
destTeamArea.setProcessName(srcteamarea.getProcessName());
destTeamArea.setProcessSummary(srcteamarea.getProcessSummary());
destTeamArea.setProjectArea(destproject);
destTeamArea.getDescription().setSummary(srcteamarea.getDescription().getSummary()); //new
destTeamArea.getDescription().setDetails(copycontent(srcteamarea.getDescription().getDetails(),((ITeamRepository)srcproject.getOrigin()).contentManager())); //new
////
worker function
private static IContent copycontent(IContent in, IContentManager icm)
{
IContent out = null;
// get the content of the content object.
try
{
if (in != null)
{
IAuditableCommon auditable = (IAuditableCommon) destauditableClient.getTeamRepository().getClientLibrary(
IAuditableCommon.class);
// make an output stream for the content manager
OutputStream os = new ByteArrayOutputStream();
// get the content of the content object.
icm.retrieveContent(in, os, null);
// extract the bytes, need the object twice
byte[] bytes = os.toString().getBytes(in.getCharacterEncoding());
// make a content object on the destination system
out=auditable.storeContent(in.getContentType(), in.getCharacterEncoding(), new ByteArrayInputStream(bytes),
bytes.length, null);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return out;
}