package com.twc.isa.PackageModule;


import org.apache.log4j.Logger;
import org.omg.CORBA.StringHolder;

import com.twc.isa.AssetModule.Asset;
import com.twc.isa.AssetModule.AssetData;
import com.twc.isa.AssetModule.AssetDataSummary;
import com.twc.isa.AssetModule.AssetWithRelations;
import com.twc.isa.ServerModule.CompletionCode;
import com.twc.isa.ServerModule.DestroyFailed;
import com.twc.isa.ServerModule.DuplicateServant;
import com.twc.isa.ServerModule.NameNotFound;
import com.twc.isa.ServerModule.NoGuiProvisioned;
import com.twc.isa.ServerModule.OutOfService;
import com.twc.isa.ServerModule.ServantBase;
import com.twc.isa.ServerModule.ServantBaseIteratorHolder;
import com.twc.isa.ServerModule.ServantBaseListHolder;
import com.twc.isa.ServerModule.ServantCreateFailed;
import com.twc.isa.ServerModule.ServantFactory_;
import com.twc.isa.ServerModule.ServantNotFound;
import com.twc.isa.ServerModule.Unimplemented;
import com.twc.isa.ServerModule.UnspecifiedException;

/**
 * 
 * @author <a href="mailto:cbrown@tandbergtv.com">Corey Brown</a>
 *
 */
public class PackageFactory_impl extends PackageFactoryPOA {
	
	static final public boolean CREATESERVANT_THROWS_DUPLICATE = false;

	private final Logger log = Logger.getLogger(PackageFactory_impl.class);

	public ServantFactory_ base = null;
	
	//
	// Some unimplemented operations
	//
	
	public Asset
	findAsset(String providerID, String assetID) {
		return null;
	}
	
    public void
    listPartialExportDetails(PackagePartialExportDataListHolder pl) {;}
	
	public PackagePropagationData[]
	getPackagePropagationHistoryDetails(String providerID, String assetID) {
		return null;
	}
	
	public AssetData[]
	listAssetDetails() {return null;}
	
	public void
	listPackageDetails(PackageDataListHolder pdl) {;}
	
	public PackagePropagationData[]
	listPackagePropagationHistory() { return null; }
	
	public AssetWithRelations
	getAssetDetails(String assetName) { return null; }

	public PackageFactory_impl() {;}

	public PackageFactory_impl(org.omg.CORBA.ORB orb, String strName) {

		try {
			base = new ServantFactory_(orb, strName);
		} catch (Exception ex) {
			log.error(null, ex);
		}
	}

	// Implentation for ServantFactoryOperations
	public ServantBase 
	createServant(String name)
		throws ServantCreateFailed, DuplicateServant {

		log.debug("Inside createServant - name = " + name);

		Package_impl package_impl = new Package_impl(base.getORB(), name);
		Package      package_     = package_impl._this(base.getORB());

		if (package_ == null) {
			throw new com.twc.isa.ServerModule.ServantCreateFailed("", CompletionCode.cc_No);
		} else {
			
			try {
				ServantBase foundServant = base.find(name);

				// It must be an existing package if we reach here.
				// older clients might never call find, just call create
				// here, we're treating create as findorcreate
				  
				if (CREATESERVANT_THROWS_DUPLICATE) {
					throw new DuplicateServant("", CompletionCode.cc_No);
				} else {
					return foundServant;
				}

			} catch (com.twc.isa.ServerModule.NameNotFound ignore) {
				//it's a new servant, because it was not found
				base.add(name, package_);
			}
		}
		
		return package_;
	}

	public com.twc.isa.ServerModule.ServantBase 
	find(String name)
	throws UnspecifiedException, NameNotFound {
		log.debug("Attempt to find on name [" + name + "]");
		
		try {
			return base.find(name);
		} catch(Exception e) {
			log.debug("Failed to find servant:" + name, e);
			return null;
		}
	}

	public void removeServant(com.twc.isa.ServerModule.ServantBase servantBase)
	throws ServantNotFound, UnspecifiedException, DestroyFailed, OutOfService {
		base.removeServant(servantBase);
	}

	public Package[] 
	aPackage() {
		return null;
	}

	public void 
	aPackage(Package[] p) {
		return;
	}

	public com.twc.isa.AssetModule.Asset[] 
	anAsset() {
		return null;
	}

	public void 
	anAsset(com.twc.isa.AssetModule.Asset[] value) {;}

	public ServantBase 
	iterate() {
		return null;
	}

	public void 
	provisioningGui(StringHolder strHolder)
	throws NoGuiProvisioned {
		
		String strName = getClass().getName();
		int iIdx = strName.lastIndexOf("_");
		String strTxtFileName = strName.substring(0, iIdx) + "ProvisioningURL.txt";
		strHolder.value = ServantFactory_.getProvisionGUI_URL_Name(strTxtFileName);
	}

	public void 
	statusGui(org.omg.CORBA.StringHolder strHolder)
	throws NoGuiProvisioned {
		
		String strName = getClass().getName();
		int iIdx = strName.lastIndexOf("_");
		String strTxtFileName = strName.substring(0, iIdx) + "StatusURL.txt";
		strHolder.value = ServantFactory_.getProvisionGUI_URL_Name(strTxtFileName);
	}

	public void 
	destroy() {
		base.destroy();
	}

	public String 
	name() {
		return base.name();
	}

	public com.twc.isa.ServerModule.AdministrativeState 
	getAdminState()
	throws com.twc.isa.ServerModule.UnspecifiedException {
		return base.getAdminState();
	}

	public void 
	setAdminState(com.twc.isa.ServerModule.AdministrativeState st)
	throws com.twc.isa.ServerModule.UnspecifiedException {
		base.setAdminState(st);
	}

	public void
	getCreateTime(org.omg.CORBA.LongHolder longHolder)
	throws UnspecifiedException, Unimplemented {
		base.getCreateTime(longHolder);
	}

	public void 
	getLastModifiedTime(org.omg.CORBA.LongHolder longHolder)
	throws UnspecifiedException, Unimplemented {
		base.getLastModifiedTime(longHolder);
	}

	public com.twc.isa.ServerModule.OperationalState 
	getOpState()
		throws com.twc.isa.ServerModule.UnspecifiedException {
		return base.getOpState();
	}

	public void 
	list(int iCount, ServantBaseListHolder sblh, ServantBaseIteratorHolder sbih) {
		base.list(iCount, sblh, sbih);
	}

	public AssetDataSummary[] listAssetDataSummary() throws UnspecifiedException {
		return null;
	}
}
