using System;
using System.Collections;
using Teamcenter.Services.Strong.Core;
using Teamcenter.Services.Strong.Query;
using Teamcenter.Soa.Client.Model;
using Teamcenter.Soa.Client.Model.Strong;
using Teamcenter.Soa.Common;
using Teamcenter.Soa.Exceptions;
using Core = Teamcenter.Services.Strong.Core;
using Query = Teamcenter.Services.Strong.Query;
using Finder = Teamcenter.Services.Strong.Query._2007_06.Finder;
namespace Teamcenter.Code
{
public class AddToFolder
{
private readonly Core.DataManagementService dmService;
private readonly Query.FinderService finderService;
private readonly String datasetUID = "wcrRIf1cLSjhKA";
private Dataset dataset;
public AddToFolder()
{
dmService = Core.DataManagementService.getService(Teamcenter.ClientX.Session.getConnection());
finderService = Query.FinderService.getService(Teamcenter.ClientX.Session.getConnection());
SetObjectPolicy();
}
public bool loadItem()
{
ServiceData sData = dmService.LoadObjects(new string[] { datasetUID });
if (sData.sizeOfPlainObjects() > 0)
{
ModelObject mObj = sData.GetPlainObject(0);
if (mObj is Dataset)
{
dataset = mObj as Dataset;
return true;
}
}
return false;
}
public bool AddFile(String folderName)
{
try
{
var find = new Finder.WSOFindCriteria();
var findSet = new Finder.WSOFindSet();
find.ObjectName = folderName;
find.ObjectType = "Folder";
find.Scope = "WSO_scope_All";
findSet.Criterias = new Finder.WSOFindCriteria[] { find };
var resp = finderService.FindWorkspaceObjects(new Finder.WSOFindSet[] { findSet });
if (!ServiceDataError(resp.ServiceData))
{
if (resp.OutputList.Length > 0 && resp.OutputList[0].FoundObjects.Length > 0)
{
var list = new ArrayList();
var map = new Hashtable();
var vec = new Core._2007_01.DataManagement.VecStruct();
var folder = (Folder)resp.OutputList[0].FoundObjects[0];
list.Add(dataset.Uid);
foreach (var content in folder.Contents)
list.Add(content.Uid);
vec.StringVec = (string[])list.ToArray(typeof(string));
map.Add("contents", vec);
var serviceData = dmService.SetProperties(new ModelObject[] { folder }, map);
if (!ServiceDataError(serviceData))
return true;
}
}
}
catch (NotLoadedException e)
{
Console.WriteLine(e.StackTrace);
}
return false;
}
protected bool ServiceDataError(ServiceData data)
{
if (data.sizeOfPartialErrors() > 0)
{
for (int i = 0; i < data.sizeOfPartialErrors(); i++)
{
foreach (string msg in data.GetPartialError(i).Messages)
Console.WriteLine(msg);
}
return true;
}
return false;
}
protected void SetObjectPolicy()
{
var session = SessionService.getService(Teamcenter.ClientX.Session.getConnection());
var policy = new ObjectPropertyPolicy();
policy.AddType(new PolicyType("Folder", new string[] { "contents" }));
session.SetObjectPropertyPolicy(policy);
}
}
}