Wednesday 14 December 2016

SOA Service Client : Download Dataset from Teamcenter

How about downloading file present in Dataset ?

Consider we have UID for dataset which is to be downloaded i.e its named reference is to be downloaded

Get the Model object from UID
ModelObject[] modelObjects = getModelObjectsFromUIDs( arrayOfUIDs,tcConnection );

private static ModelObject[] getModelObjectsFromUIDs(String[] arrayOfUIDs, Connection connection)
{
    ModelObject[] arrModelObjects = null;
    if (arrayOfUIDs.length > 0)
    {
        ArrayList<ModelObject> modelObjects = 
        new ArrayList<ModelObject>(arrayOfUIDs.length);
        DataManagementService dataManagementService = 
       DataManagementService.getService(connection);
        ServiceData loadObjects = 
        dataManagementService.loadObjects(arrayOfUIDs); //Load objects for provided UIDs

       for (int i = 0; i < loadObjects.sizeOfPlainObjects(); i++)
        {
            modelObjects.add(loadObjects.getPlainObject(i));
        }
        if (modelObjects != null && modelObjects.size() > 0)
        {
            arrModelObjects = modelObjects.toArray(new ModelObject[0]);
        }
      }
    return arrModelObjects;
}
Now Load the ref_list property on dataset which actually contain the files names
dataManagementService.getProperties( modelObjects, new String[]{ "ref_list" } );
Dataset  dataset =null;
ModelObject[] files = null;
if ( modelObject instanceof Dataset )
{
   dataset = ( Dataset )  modelObject;
   files = dataset.get_ref_list();//Gives named referenced objects
}

Check if named reference is actually the required file

ArrayList<ModelObject> al = new ArrayList<ModelObject>();

for (int i = 0; i < files.length; i++)
{
   if ( files[i] instanceof ImanFile )
   al.add( files[i] );

}

ModelObject[] newFiles = al.toArray( new ModelObject[al.size()] ); 

//Load the original name for files
dataManagementService.getProperties( newFiles, new String[]{"original_file_name"} );

Get/Download the Files
//FileManagementUtility is in package com.teamcenter.soa.client
 FileManagementUtility fileManagementUtility = null;

//Get the Fms_BootStrap_Urls Preference
SessionService localSessionService = SessionService.getService(getTcConnection());
Session.ScopedPreferenceNames[] arrayOfScopedPreferenceNames = 
new Session.ScopedPreferenceNames[1];

arrayOfScopedPreferenceNames[0] = new Session.ScopedPreferenceNames();
arrayOfScopedPreferenceNames[0].names = new String[] { "Fms_BootStrap_Urls" };
arrayOfScopedPreferenceNames[0].scope = paramString2;
Session.MultiPreferencesResponse localMultiPreferencesResponse =
localSessionService.getPreferences(arrayOfScopedPreferenceNames);

String bootStrapURL=localMultiPreferencesResponse.preferences[0].values[0];
try
{
//strDirPath is folder path /location where file will be downloaded
fileManagementUtility = new FileManagementUtility( localConnection, null,
null, new String[]{ bootStrapURL }, strDirPath);
GetFileResponse responseFiles = 
fileManagementUtility.getFiles( modelObjects );//Download the files but with Tc internal name
File[] filesFromModelObjects = responseFiles.getFiles();
}

Rename the file to Original name based on ref list model objects ->newFiles and downloaded files -> filesFromModelObjects
for(File fSrc : filesFromModelObjects)
{
    ImanFile imFle = (ImanFile) newFiles[i];
    String originalFileName = imFle.get_original_file_name();
    File fTmpDest = new File(strDirPath + File.separator + originalFileName);
    fSrc.renameTo(fTmpDest);
}
Explanation : Important API dataManagementService.loadObjects(arrayOfUIDs); dataset.get_ref_list() localSessionService.getPreferences(arrayOfScopedPreferenceNames); fileManagementUtility.getFiles( modelObjects );

2 comments:

  1. I am trying to use your sample code for dataset files download. However i am bit confused with the modelObject reference used in

    if (modelObject instanceof Dataset) {
    dataset=(Dataset) modelObject;
    files=dataset.get_ref_list();
    }
    I can't find declaration of modelObject variable inside your code. Could you please help me ?

    ReplyDelete
  2. I have same question. about modelObject

    ReplyDelete