package oracle.demo.pts.fusionservices;

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

import com.oracle.xmlns.apps.crmcommon.activities.activitymanagementservice.Activity;

import oracle.demo.pts.fusionservices.utils.FusionService;
import com.sun.xml.ws.developer.WSBindingProvider;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.logging.Level;
import javax.annotation.Resource;
import javax.xml.namespace.QName;
import oracle.demo.pts.fusionproxy.activity.ActivityService;
import oracle.demo.pts.fusionproxy.activity.ActivityService_Service;
import oracle.demo.pts.fusionservices.utils.FusionHelper;


public class FusionActivityService extends FusionService {


    // @WebServiceRef
    @Resource(name = "ActivityService_Service")
    
    ActivityService_Service activityService_Service;
    ActivityService activityService;
  

    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 (activityService_Service == null)
            activityService_Service =
                    new ActivityService_Service(wsdlLocationURL,
                        new QName("http://xmlns.oracle.com/apps/crmCommon/activities/activityManagementService/",
                        "ActivityService"));
        activityService = activityService_Service.  getActivityServiceSoapHttpPort(this.getSecurityFeature(pFusionEndpointURL,
                                                                                                             pJWTToken));
        // Call Helper Function
        this.initFusionEndpoint(pJWTToken, pUsername, pPassword,
                                (WSBindingProvider)activityService,
                                pFusionEndpointURL, pFetchSize, pFetchStart);
    }


    
    public List<Activity> find(String pSOAPMessageString,String pAttributes,String pQuery,String pConjunction) throws Exception {
        checkIfInitialised();
       
        
        fc=FusionHelper.createCustomFindCriteria(fc, pSOAPMessageString, pAttributes, pQuery, pConjunction,getFetchSize(),getFetchStart());

        List<Activity> ops = null;
        try {
            // Call Fusion findOpportunity
            ops = activityService.findActivity(fc, new FindControl());
            
        } catch (com.sun.xml.ws.client.ClientTransportAccessException ctae) {
            throw ctae;
        } catch (Exception e) {
            logMessage("Generic exception @ findActivity");
            e.printStackTrace();
            throw e;
        }
        logMessage("Number of Activities found " + ops.size());
        return ops;
    }
    
    
    

    public ActivityService getService() {
        return activityService;
    }
}
