package oracle.demo.pts.fusionservices;

import com.oracle.xmlns.adf.svc.types.DataObjectResult;
import com.oracle.xmlns.adf.svc.types.FindControl;

import com.oracle.xmlns.apps.crmcommon.salesparties.accountservice.Account;


import com.sun.xml.ws.developer.WSBindingProvider;

import java.net.MalformedURLException;
import java.net.URL;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

import javax.xml.namespace.QName;

import oracle.demo.pts.fusionproxy.account.AccountService;
import oracle.demo.pts.fusionproxy.account.AccountService_Service;
import oracle.demo.pts.fusionservices.utils.FusionHelper;
import oracle.demo.pts.fusionservices.utils.FusionService;

public class FusionAccountService extends FusionService {
    private AccountService_Service accountService_Service;
    private AccountService          accountService;
    
    public void initialiseConnection(String pJWTToken, String pUsername,
                                     String pPassword,
                                     String pFusionEndpointURL, int pFetchSize,
                                     int pFetchStart) {
        // This method initialises the connection to the FusionCRM WS
        // Currently does it every time, but one could/should be able to cache if needed
        System.setProperty("javax.xml.transform.TransformerFactory",
                "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");


        URL wsdlLocationURL = null;
        try {
            wsdlLocationURL = new URL(pFusionEndpointURL + "?wsdl");
        } catch (MalformedURLException e) {
            e.printStackTrace();
            logger.log(Level.ALL, e.getLocalizedMessage());
        }
        if (accountService_Service == null)
            accountService_Service =
                    new AccountService_Service(wsdlLocationURL,
                        new QName("http://xmlns.oracle.com/apps/crmCommon/salesParties/accountService/",
                        "AccountService"));
        accountService = accountService_Service.getAccountServiceSoapHttpPort(this.getSecurityFeature(pFusionEndpointURL,
                                                                                                             pJWTToken));
        // Call Helper Function
        this.initFusionEndpoint(pJWTToken, pUsername, pPassword,
                                (WSBindingProvider)accountService,
                                pFusionEndpointURL, pFetchSize, pFetchStart);
    }
    
    
    public List<Account> find(String pSOAPMessageString,
                                       String pAttributes, String pQuery,
                                       String pConjunction) throws Exception {
       
        checkIfInitialised();

        fc =
    FusionHelper.createCustomFindCriteria(fc, pSOAPMessageString, pAttributes,
                                       pQuery, pConjunction, getFetchSize(),
                                       getFetchStart());       
        DataObjectResult result= null;
        try {
            // Call Fusion findOpportunity
            result =
                    accountService.findAccount(fc, new FindControl());

        } catch (com.sun.xml.ws.client.ClientTransportAccessException ctae) {
            throw ctae;
        } catch (Exception e) {
            logMessage("Generic exception @ findMkLead");
            e.printStackTrace();
            throw e;
        }
        
        List<Account> returnValues = new ArrayList<Account>();
                        
        if (result!=null && result.getValue()!=null) {
            // Construct a new <List>Contacts> from result
            for (Object o : result.getValue()) {
                Account a = (Account)o;
                returnValues.add(a);
            }
        
        }
        return returnValues;
    }
    public AccountService getService() {
        return this.accountService;
    }
    
    
    
}
