//
//  ReqHandler.m
//  currency converter
//
//  Created by mac on 6/13/14.
//  Copyright (c) 2014 TOPS. All rights reserved.
//
//Return Value:Gives array in getList
//Return Value:Gives number formed with string

#import "ReqHandler.h"

@implementation RequestHandler
@synthesize parser,foundString,arrayMain,stringInXml;

-(NSString *)Firequery:(NSString *)query andSOAPAction:(NSString *)SOAPAction
{
    NSURL *url = [[NSURL alloc]initWithString:@"http://jrlservice.somee.com/WebService/WebService/CommonService.asmx"];
    
    NSMutableURLRequest *req = [[NSMutableURLRequest alloc]initWithURL:url];
    
    [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    [req addValue:SOAPAction forHTTPHeaderField:@"SOAPAction"];
    
    [req addValue:@"jrlservice.somee.com" forHTTPHeaderField:@"Host"];
    
    NSString *body;
    
    if ([SOAPAction isEqualToString:@"http://tempuri.org/doDMLOperation"]) {
        
        body = [NSString stringWithFormat:@"<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><doSelectOperation xmlns='http://tempuri.org/'><query>%@</query></doSelectOperation></soap:Body></soap:Envelope>",query];
    }
    else
    {
        body=[NSString stringWithFormat:@"<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body> <doSelectOperation xmlns='http://tempuri.org/'><query>%@</query></doSelectOperation></soap:Body></soap:Envelope>",query];
    }
    
    
    [req addValue:[NSString stringWithFormat:@"%d",body.length]forHTTPHeaderField:@"Content-Length"];
    
    [req setHTTPMethod:@"POST"];
    
    [req setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
    
    NSData *data = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil];
    
    NSString *string = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    
    return string;
}

//=======================================================================
-(NSString *)execQuery:(NSString*)query
{

    NSString *xmlString = [self Firequery:query andSOAPAction:@"http://tempuri.org/doDMLOperation"];
    
    parser = [[NSXMLParser alloc]initWithData:[xmlString dataUsingEncoding:NSUTF8StringEncoding]];
    
    parser.delegate = self;
    
    [parser parse];
    
    return stringInXml;
    
}

-(NSMutableArray *)getList:(NSString*)query
{
    //Check for Internet Connection
    
    NSString *xmlString = [self Firequery:query andSOAPAction:@"http://tempuri.org/doSelectOperation"];
    
    parser = [[NSXMLParser alloc]initWithData:[xmlString dataUsingEncoding:NSUTF8StringEncoding]];
    
    //NSLog(@"xml string is %@",xmlString);
    
    parser.delegate = self;
    
    [parser parse];

    return arrayMain;
}

//=======================================================================
-(void)parserDidStartDocument:(NSXMLParser *)parser
{
    arrayMain = [[NSMutableArray alloc]init];
}

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{

}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    foundString = string;
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    
    if ([elementName isEqualToString:@"doSelectOperationResult"])
    {
        stringInXml = [NSString stringWithString:foundString];
        isdoSelectOperationResult = YES;
    }
    else if ([elementName isEqualToString:@"doDMLOperationResult"])
    {
        stringInXml = [NSString stringWithString:foundString];
        isdoSelectOperationResult = NO;
    }
}

-(void)parserDidEndDocument:(NSXMLParser *)parser
{
    //select * from .....
    if (isdoSelectOperationResult) {
        //check for error
        if ([stringInXml isEqualToString:@"Error"]) {
            NSLog(@"Error in select query");
        }
        else
        {
            arrayMain = [NSJSONSerialization JSONObjectWithData:[stringInXml dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
        }
    }
    
    //update ...
    else
    {
        //check for error in DML Operation
        if ([stringInXml integerValue] < 0) {
            NSLog(@"Error in DML query");
        }
        else
        {
            NSLog(@"Success in DML query");
        }
    }
}
//=======================================================================

@end