Below Windchill logic takes Hashtable input details for creating a Supplier, creates supplier organization if one doesn't exist and returns the created supplier.
protected WTObject create( Hashtable recordData, Hashtable ibaAttrMap )
throws WTException {
String methodName = CLASSNAME + ".create: ";
Supplier supplier = null;
String supplierType = (String) getValue(recordData,"SUPPLIERTYPE", true);
String supplierorg = (String) getValue(recordData,"SUPPLIERORG", true);
String supplierorg_id = (String) getValue(recordData,"SUPPLIERORG_ID", true);
String container_organization_name = (String) getValue(recordData,"CONTAINER_ORGANIZATION_NAME", true);
String lifecycle = (String) getValue(recordData,"LIFECYCLE", true);
String state = (String) getValue(recordData,"LIFECYCLESTATE", false);
String type = (String) getValue(recordData,"TYPE", true);
String container = getValue(recordData, "CONTAINER", true);
String containertype = getValue(recordData, "CONTAINERTYPE", true);
if (!(containertype.equalsIgnoreCase("PRODUCT")
|| containertype.equalsIgnoreCase("ORGANIZATION")
|| containertype.equalsIgnoreCase("SITE") || containertype
.equalsIgnoreCase("LIBRARY"))) {
throw new WTException(
"Wrong ContainerType:"
+ containertype
+ ".ContainerType should be either SITE/LIBRARY/ORGANIZATION/PRODUCT");
}
if (!(supplierType.equalsIgnoreCase("VENDOR")
|| supplierType.equalsIgnoreCase("MANUFACTURER"))) {
throw new WTException(
"Wrong SupplierType:"
+ supplierType
+ ".SupplierType should be either VENDOR/MANUFACTURER");
}
// Return and Inflated Manufacturer Organization
DirectoryContextProvider dcp = WTContainerHelper.service.getExchangeContainer().getContextProvider();
WTOrganization targetOrg = null;
LoaderCacheManager lcm = LoaderCacheManager.getInstance();
WTOrganization containerOrg =null;
StringBuffer containerPath =null;
try{
// Retrieve the Organization for association to the supplier
logger.debug("Retrieving Organizatin for association with the supplier...");
containerOrg = WCUtils.getWTOrganization(supplierorg);
}
catch(Exception e){
logger.debug("Exceptio caught..."+ e);
}
if(containerOrg == null)
{
targetOrg = OrganizationServicesHelper.manager.getOrganization(supplierorg, dcp);
}
else
{
targetOrg = containerOrg;
}
String windchillDomain = (String) getValue(recordData,"WINDCHILLDOMAIN", false);
logger.debug("WINDCHILL DOMAIN from CSV ..."+windchillDomain);
if(windchillDomain == null){
windchillDomain = WCDOMAIN;
logger.debug("WINDCHILL DOMAIN from dmf.properties ..."+windchillDomain);
}
//if Org exists create the Supplier otherwise create a new organization on the basis of the Supplier.
if (targetOrg==null)
{
//START of - Code added to create new Organization if one doesn't exist
try{
targetOrg = (WTOrganization)WCUtils.createOrganization(supplierorg, supplierorg_id, windchillDomain);
}
catch(WTException e){
//e.printStackTrace();
logger.debug(e.getLocalizedMessage());
throw new WTException("Wrong WINDCHILLDOMAIN/Incorrect format of WINDCHILLDOMAIN is given");
}
//End of - Code added to create new Organization if one doesn't exist
}
containerPath = (new StringBuffer("/wt.inf.container.OrgContainer=")).append(container_organization_name);
WTContainerRef containerRef = WTContainerHelper.service.getByPath(containerPath.toString());
try
{
if(supplierType.equalsIgnoreCase("MANUFACTURER"))
{
supplier = Manufacturer.newManufacturer(targetOrg, containerRef.getReferencedContainer());
}
else if(supplierType.equalsIgnoreCase("VENDOR"))
{
supplier = Vendor.newVendor(targetOrg, containerRef.getReferencedContainer());
}
logger.debug("Setting Lifecycle....");
// START : Modified for setting Lifecycle
Object lifecycleTemplate = null;
try {
lifecycleTemplate = lcm.getLifeCycle(recordData);
} catch (Exception e) {
logger.debug("Error in getting Lifecycle Template..." + e);
}
if (lifecycleTemplate != null) {
recordData.put("LifeCycleTemplate", lifecycleTemplate);
} else {
throw new WTException("Can't find Lifecycle \""
+ recordData.get("LIFECYCLE")
+ "\" in the container \""
+ container + "\" ("
+ recordData.get("CONTAINERTYPE") + ").");
}
Object obj = recordData.get("LifeCycleTemplate");
// END : Modified for setting Lifecycle
if (obj != null && obj instanceof LifeCycleTemplate) {
LifeCycleTemplate lctemplate = (LifeCycleTemplate) obj;
LifeCycleHelper.setLifeCycle(supplier, lctemplate);
} else {
logger.debug("LifeCycleTemplate is " + obj);
}
// End : Modified for setting Lifecycle
if(state != null)
{
logger.debug("setting Life Cycle State....");
supplier = (Supplier)LifeCycleServerHelper.setState(supplier, State.toState(state));
}
if(type != null)
{ //for bug 36910
if((supplierType.equalsIgnoreCase("MANUFACTURER") && type.contains("Manufacturer") )||((supplierType.equalsIgnoreCase("VENDOR") && type.contains("Vendor") ))){
supplier = (Supplier) setType(recordData, supplier);
}
else{
throw new WTException("SupplierType \""+ supplierType + "\" and type \""+ type + "\" does not match");
}
}
supplier = (Supplier)PersistenceHelper.manager.save(((wt.fc.Persistable) (supplier)));
}
catch(wt.pom.UniquenessException ue)
{
ue.getMessage();
ue.printStackTrace();
throw new WTException(ue.getLocalizedMessage());
}
catch(wt.util.WTPropertyVetoException wpe)
{
LoadServerHelper.printMessage("\ncreateWTProduct() caught an exception: " + wpe.getLocalizedMessage());
wpe.printStackTrace();
}
catch (Exception e) {
LoadServerHelper.printMessage("\nsetWTPartAttributes: "
+ e.getLocalizedMessage());
e.printStackTrace();
throw new WTException(e.getLocalizedMessage());
}
return (WTObject)supplier;
}
Note in the above snippet we have a few helper methods to get an organization if one exists and also to create an organization if one doesn't exist.
public static WTOrganization createOrganization( String supplierorg, String supplierorg_id,String windchillDomain )
throws WTException {
DirectoryContextProvider dcp = WTContainerHelper.service.getExchangeContainer().getContextProvider();
WTOrganization targetOrg = WTOrganization.newWTOrganization (supplierorg,dcp);
WTOrganizationIdentifier uniqueId = WTOrganizationIdentifier.newWTOrganizationIdentifier();
AdminDomainRef domainRef = null;
try{
if(windchillDomain != null){
domainRef = LoadServerHelper.getTargetDomain(windchillDomain, null, null);
}
}catch(Exception e){
throw new WTException("Wrong WINDCHILLDOMAIN/Incorrect format of WINDCHILLDOMAIN is given");
}
if(domainRef != null){
targetOrg.setDomainRef(domainRef);
}
try {
uniqueId.setCodingSystem(targetOrg+"_CodingSystem");
uniqueId.setUniqueIdentifier(targetOrg+"_UniqueOrgId");
uniqueId.setUniqueIdentifier(supplierorg_id);
targetOrg.setOrganizationIdentifier(uniqueId);
targetOrg = (WTOrganization)OrganizationServicesHelper.manager.createPrincipal(targetOrg);
targetOrg=(WTOrganization)PersistenceHelper.manager.save(targetOrg);
}
catch (WTPropertyVetoException e)
{
e.printStackTrace();
}
return targetOrg;
}
public static WTOrganization getWTOrganization(String name)
throws WTException {
WTOrganization organization = null;
String prefix = "Organization|";
StringBuffer organizationKey = (new StringBuffer(prefix)).append(name);
organization = (WTOrganization) LoadServerHelper
.getCacheValue(organizationKey.toString());
if (organization == null) {
QuerySpec qs = new QuerySpec(wt.org.WTOrganization.class);
qs.appendWhere(new SearchCondition(wt.org.WTOrganization.class,
"name", "=", name));
QueryResult qr = PersistenceHelper.manager.find(qs);
if (qr.hasMoreElements()) {
while (qr.hasMoreElements()) {
organization = (WTOrganization) qr.nextElement();
organizationKey = (new StringBuffer(prefix))
.append(organization.getName());
LoadServerHelper.setCacheValue(organizationKey.toString(),
organization);
}
organizationKey = (new StringBuffer(prefix)).append(name);
organization = (WTOrganization) LoadServerHelper
.getCacheValue(organizationKey.toString());
}
}
return organization;
}
Note: In Windchill 11.2+ customers use OData-based REST APIs, currently there is no support provided by PTC on creating a new supplier, we can leverage the above APIs to create the backend logic for a REST action that can take POST JSON payload and develop a implementation logic that can leverage above APIs.