Tuesday 13 December 2016

SOA Service client: Change Object Ownership

How about changing project ownership ?
In data Migration you create the Teamcenter object using DBA user due to all access but it may required that created object should belong to other user and group then we need to change the ownership

To change the object ownership we need
1) Object whose ownership to be changed //Get the object by UIDs using  API loadObjects belong to class DataManagementService
2) User->User object
3) Group-> Group object

How do you find the User and Group ?
To find the User and Group you need savedQuery which should be available in Teamcenter Query Builder,If not available create it which will query User and Group for you.






Note: Must read code comment

//Find user using User ID ->String user represent User ID
 public ModelObject findUser(Connection connection, String user)

 {

  ImanQuery query = null

  //SavedQueryService class is in package com.teamcenter.services.strong.query
  SavedQueryService queryService = SavedQueryService.getService(connection);
  try
  {
   GetSavedQueriesResponse savedQueries = queryService.getSavedQueries();
   if (savedQueries.queries.length == 0)
   {
    return null;
   }
   for (int i = 0; i < savedQueries.queries.length; i++)
   {
    //__WEB_find_user -> Query in Query builder to find the user
    if (savedQueries.queries[i].name.equals("__WEB_find_user"))
    {
     query = savedQueries.queries[i].query;
     break;
    }

   }

  }
  catch (ServiceException e)
  {
   System.out.println(e.getMessage());
   return null;
  }
  if (query == null)
  {
   System.out.println("There is not an 'Item Name' query.");
   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[]{"User ID" };//Attribute in Query to search by
   savedQueryInput[0].values = new String[1];
   savedQueryInput[0].values[0] = user;//Value for the search attribute
   savedQueryInput[0].maxNumToInflate = 25;

   //Execute Query to find the User
   ExecuteSavedQueriesResponse savedQueryResult = queryService.executeSavedQueries(savedQueryInput);
   SavedQueryResults found = savedQueryResult.arrayOfResults[0];

   // System.out.println("Found Users:");

   ModelObject[] modelObjs = found.objects;

   return modelObjs[0]; //User object

  }
  catch (Exception e)
  {
   System.out.println(e.getMessage());
   return null;
  }                     

 }

 public ModelObject findGroup(Connection connection, String groupName)
 {

  ImanQuery query = null;

  SavedQueryService queryService = SavedQueryService.getService(connection);

  try
  {

   GetSavedQueriesResponse savedQueries = queryService.getSavedQueries();

   if (savedQueries.queries.length == 0)
   {
    return null;
   }

   for (int i = 0; i < savedQueries.queries.length; i++)
   {
   //__WEB_group -> Query in Query builder to find the group

    if (savedQueries.queries[i].name.equals("__WEB_group"))
    {
     query = savedQueries.queries[i].query;
     break;
    }

   }

  }
  catch (ServiceException e)
  {

   System.out.println(e.getMessage());
   return null;
  }

  if (query == null)
  {
   System.out.println("__WEB_group 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[]{"Name" };//Attribute in Query to search by
   savedQueryInput[0].values = new String[1];
   savedQueryInput[0].values[0] = groupName;//Value for the search attribute
   savedQueryInput[0].maxNumToInflate = 25;

   //Execute Query to find the Group

   ExecuteSavedQueriesResponse savedQueryResult = queryService.executeSavedQueries(savedQueryInput);

   SavedQueryResults found = savedQueryResult.arrayOfResults[0];

   // System.out.println("Found Users:");


   ModelObject[] modelObjs = found.objects;

   return modelObjs[0];//Group object

  }
  catch (Exception e)
  {

   System.out.println(e.getMessage());

   return null;

  }                     

 }

//Change the ownership of object modl

public void changeOwnershipModelObject(Connection localConnection,String userName,String groupName,ModelObject modl) throws Exception
 {

  ModelObject user  = findUser(localConnection, userName ) ;

  if( user != null )
  {
   System.out.println("................................owning user found..................................");

  } else {


   //The configured EPI-Owning-user is not found in Teamcenter.

   System.out.println("................  EPI-owning user not found ................");

   throw new Exception("Change ownership service: 003.","Change ownership service - The configured EPI-owning user is not found in Teamcenter.");

  }

  ModelObject userGroup  = findGroup(localConnection, groupName ) ;

  if( userGroup != null )
  {

   System.out.println("................................owning group found..................................");

  }else{

   System.out.println("................  EPI-owning group not found ................");

   throw new Exception("Change ownership service: 004.","Change ownership service - The configured EPI-owning group is not found in Teamcenter.");

  }

  DataManagementService ser1 = com.teamcenter.services.strong.core.DataManagementService.getService( localConnection );

  ObjectOwner[] ownerData = new ObjectOwner[1];

  ObjectOwner ownrObj = new com.teamcenter.services.strong.core._2006_03.DataManagement.ObjectOwner();

  ownrObj.object = modl;
  ownrObj.group = (Group) userGroup;
  ownrObj.owner = (User) user;
  ownerData[0] = ownrObj;

  ServiceData returnData = ser1.changeOwnership( ownerData );

  System.out.println( "****************************** Ownership changed successfully ******************************" );

  if(returnData.sizeOfPartialErrors() > 0){

   throw new Exception("Change ownership service: 005", "Change ownership service - "+returnData.getPartialError(0).getMessages()[0]);

  }

 }
Explanation : Important API
ServiceData returnData = ser1.changeOwnership( ownerData );

2 comments:

  1. Does the author have any code descriptions related to Schedule objects and Schedule deliverables?

    ReplyDelete
  2. How we can take ownership of replica object through code ,is there any API ,without data share command

    ReplyDelete