Tuesday, 13 December 2016

SOA Serive client: Assign Project to object

How about assigning project to object ?

What you need is
1) Object to which project to be assigned //Get the object by UIDs using  API loadObjects belong to class DataManagementService
2) Project -> TC_Project object

 How do you find the project using Project ID ?
 To find the project you need savedQuery which should be available  in Teamcenter Query Builder,If not available create it which will  query using Project ID
 Lets find TC project object by executing savedqueries (in our case __rr_get_projects)


Note: Must read code comment
public ModelObject findProject(Connection connection, String projectIds)
{
 ImanQuery query = null;
//SavedQueryService class is in package com.teamcenter.services.strong.query
 SavedQueryService queryService = SavedQueryService.getService(connection);
 try
 {

  GetSavedQueriesResponse savedQueries = queryService.getSavedQueries(); //Get all the savedQueries from Teamcenter
  if (savedQueries.queries.length == 0)
  {
   return null;
  }
  for (int i = 0; i < savedQueries.queries.length; i++)
  {
   if (savedQueries.queries[i].name.equals("__rr_get_projects")) //Find the query which we need
   {
    query = savedQueries.queries[i].query;//Query found
    break;
   }
  }
}
catch (ServiceException e)
{
  System.out.println(e.getMessage());
  return null;
}
if (query == null)
{
 System.out.println("__rr_get_projects query not found");
 return null;
}
try
{
  SavedQueryInput savedQueryInput[] = new SavedQueryInput[1];
  savedQueryInput[0] = new SavedQueryInput();
  savedQueryInput[0].query = query;
  savedQueryInput[0].maxNumToReturn = 25;
  savedQueryInput[0].limitListCount = 0;
  savedQueryInput[0].limitList = new ModelObject[0];
  savedQueryInput[0].entries = new String[]{"Project ID" };//Query Condition attribute
  savedQueryInput[0].values = new String[1];
  savedQueryInput[0].values[0] = projectIds;//Value for Project ID Attribute
  savedQueryInput[0].maxNumToInflate = 25;

  //Execute query
  ExecuteSavedQueriesResponse savedQueryResult = queryService.executeSavedQueries(savedQueryInput);
  SavedQueryResults found = savedQueryResult.arrayOfResults[0];
  //  System.out.println("Found Users:");

  //Found output objects on query execution
  ModelObject[] modelObjs = found.objects;
  return modelObjs[0];//Project object we need
}
catch (Exception e)
{
  System.out.println(e.getMessage());
  return null;
}                     
}
Assign project  to object modl
public void assignProjectToItem(Connection localConnection,String projectId,ModelObject modl) throws Exception
 {
  ModelObject[] modelObjectsItems = new ModelObject[1];
  modelObjectsItems[0] = modl;
  TC_Project[] projects = new TC_Project[1];   //Create array of TC_Project 
  ModelObject project  = findProject(localConnection, projectId ) ;
  if( project != null )
  {
   //System.out.println("................................owning project found..................................");
    projects[0] = (TC_Project) project; //Add project found to the Project array

  }
 else
  {
   System.out.println("................ Project not found ................");
   throw new eQMIException("The configured EPI-Project is not found in Teamcenter.","Assigning the project - The configured EPI-Project is not found in Teamcenter.");
  } 
  //ProjectLevelSecurityService is from package com.teamcenter.services.strong.core
  ProjectLevelSecurityService projectLevelSecurityService = ProjectLevelSecurityService.getService(localConnection);
  AssignedOrRemovedObjects[] assignedOrRemovedObjects = new AssignedOrRemovedObjects[1];
  AssignedOrRemovedObjects assignedOrRemovedObject = new AssignedOrRemovedObjects();
  assignedOrRemovedObject.objectToAssign = modelObjectsItems;
  assignedOrRemovedObject.projects = projects;
  assignedOrRemovedObjects[0] = assignedOrRemovedObject;
  ServiceData serviceData =  projectLevelSecurityService.assignOrRemoveObjects(assignedOrRemovedObjects);
  
  for(int i=0 ; i < serviceData.sizeOfPartialErrors() ; i++)
 {
   ErrorStack errorStack = serviceData.getPartialError(i);
   String errorMsg = errorStack.getMessages()[0];
   int errorCode = errorStack.getCodes()[0];
   throw new eQMIException("Assigning the project: " +errorCode+" - ", "Assigning the project - "+errorMsg);
  }
  System.out.println( "****************************** Project modified successfuly ******************************");
 }

Explanation : Important API
ServiceData serviceData =  projectLevelSecurityService.assignOrRemoveObjects(assignedOrRemoved Objects);

No comments:

Post a Comment