/*
 * Created on Aug 20, 2003
 *
 */

package XmlCommunication;
import java.io.*;
import java.util.*;
//import org.apache.xml.*;
import org.exolab.castor.xml.XMLNaming;
import org.exolab.castor.xml.util.*;
import org.exolab.castor.xml.util.XMLClassDescriptorImpl;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import nz.co.cks.tms.xml.*;
import nz.co.cks.tms.xml.types.*;
import Data.*;
import java.math.*;
/**
 * @author student
 * The class is used for compiling xml packets using the caster generated classes
 * 
 */
public class XmlGenerator {
	private static int counter = 1;

	/**
	 * This method is being used for the compilation of an admin add
	 * xml packet, which adds a new ncb to the TMS.
	 * @param user
	 * @return String
	 */
	/**Admin Add*/
	public  static String adminAddXml(UserData user) {

		try {
			//FileWriter writer = new FileWriter("test.xml");
			StringWriter writer = new StringWriter();
			System.out.println("xmlGenerator adminAddXml Started...");

			/** Initialise **/
			TmsMessage tmsMessage = new TmsMessage();

			MessageAuthBlk mab = new MessageAuthBlk();
			MessageTypeChoice mtc = new MessageTypeChoice();

			MessageAdminBlk madminb = new MessageAdminBlk();
			MessageTypeChoice mtc2 = new MessageTypeChoice(); //not needed?
			AdminAdd aa = new AdminAdd();
			Ncb ncb = new Ncb();

			NcbService ncbservice = new NcbService();

			NcbAddress ncbaddress = new NcbAddress();

			NcbTypeType ncbtypetype = NcbTypeType.valueOf("Person");

			/** set simpleTypes **/

			ncb.setNcbType(ncbtypetype);

			//Adding  new customer
			/** set simpleTypes **/
			ncb.setNcbType(ncbtypetype);
			ncb.setName(user.getFName());
			ncb.setNameLast(user.getLName());

			ncb.setLogin(user.getLoginId()); //generated login.
			ncb.setPassword(user.getPassword());

			Attrib att = new Attrib();
			att.setName("Pinboard");
			att.setValue("Pinboard");
			ncbservice.addAttrib(att);
			ncbservice.setServiceCode("PinboardServiceCode");

			NcbAddressTypeType ncbaddresstypetype =
				NcbAddressTypeType.valueOf("Billing");
			ncbaddress.setSelectedAddressType(ncbaddresstypetype);

			ncbaddress.setEmail(user.getEmailAddress());
			ncbaddress.setPhoneNumber(user.getMobileNumber());
			ncbaddress.setFaxNumber(user.getContactNumber());

			/** Add to parent **/
			aa.setNcb(ncb);
			aa.addNcbService(ncbservice);
			aa.addNcbAddress(ncbaddress);
			madminb.setAdminAdd(aa);
			mtc.setMessageAdminBlk(madminb);
			tmsMessage.setMessageTypeChoice(mtc); //adding choice.

			/** Set attributes **/
			mab.setNcbId(user.getNcbId()); //for adding new customer
			tmsMessage.setMessageAuthBlk(mab); //adding attribute.
			tmsMessage.setMessageId(counter);
			counter++;
			Date date = new Date();
			tmsMessage.setMessageDateTime(date);

			/** Get XML Packet **/
			tmsMessage.marshal(writer);

			//			InputStreamReader reader = new InputStreamReader(new FileInputStream("test.xml"));
			//			int i = reader.read();
			//			String output ="";
			//			while(i != -1){
			//				System.out.write(i);
			//				Integer integ = new Integer(i);
			//				byte b[] = {integ.byteValue()};
			//				String byteString = new String(b);
			//				output += byteString;
			//				i=reader.read();
			//			}
			//			System.out.println("xmlGenerator adminAddXml Finished...");					
			//			return output;
			return writer.toString();
		} catch (Exception e) {
			System.out.println("EXCEPTION! : " + e.toString());
			return null;
		}

	}

	/**Admin Update*/
	//		public static String adminUpdateXml(UserData user){
	//			try{
	//				//FileWriter writer = new FileWriter("test.xml");
	//				StringWriter writer = new StringWriter();
	//				System.out.println("Started...");
	//			
	//				/** Initialise **/
	//				TmsMessage tmsMessage = new TmsMessage();
	//						
	//				MessageAuthBlk mab = new MessageAuthBlk();
	//				MessageTypeChoice mtc = new MessageTypeChoice();
	//			
	//				MessageAdminBlk madminb = new MessageAdminBlk();
	//				MessageTypeChoice mtc2 = new MessageTypeChoice(); //not needed?
	//				AdminUpdate au = new AdminUpdate();
	//				Ncb ncb = new Ncb();
	//			
	//				NcbService ncbservice = new NcbService();
	//				Attrib att = new Attrib();
	//				att.setName("StockTrading");
	//				att.setValue("StockTrading");
	//			
	//				/** set simpleTypes **/
	//				ncb.setNcbId(user.getNcbId());
	//				if(user.getType().equals("Organisation")){
	//					ncb.setName(user.getOrganisation());
	//				}
	//				else{
	//					ncb.setName(user.getFirstName());
	//					ncb.setNameLast(user.getLastName());
	//				}
	//				ncb.setLogin(user.getUserName()); //generate the login.
	//				ncb.setPassword(user.getPassword());
	//						
	//				ncbservice.addAttrib(att);				
	//				ncbservice.setServiceCode("StockTradingCode");
	//				
	//				NcbAddress ncbaddress = new NcbAddress();
	//				NcbAddressTypeType ncbaddresstypetype = NcbAddressTypeType.valueOf("Billing");
	//				ncbaddress.setSelectedAddressType(ncbaddresstypetype);
	//				
	//				ncbaddress.setCity(address.getCity());
	//				ncbaddress.setStreetLine1(address.getStreet());
	//				ncbaddress.setStreetLine2(address.getSuburb());
	//				ncbaddress.setStreetLine3(address.getCountry());
	//				ncbaddress.setEmail(address.getEmail());
	//				ncbaddress.setPhoneNumber(address.getPhone());
	//						
	//				/** Add to parent **/
	//				au.setNcb(ncb);
	//				au.addNcbService(ncbservice);
	//				au.addNcbAddress(ncbaddress);
	//				madminb.setAdminUpdate(au);
	//				mtc.setMessageAdminBlk(madminb);
	//				tmsMessage.setMessageTypeChoice(mtc); //adding choice.
	//			
	//				/** Set attributes **/
	//				mab.setNcbId(user.getNcbId());
	//				tmsMessage.setMessageAuthBlk(mab); //adding attribute.
	//				tmsMessage.setMessageId(counter);
	//				counter++;
	//				System.out.println(Calendar.DATE);
	//				Date date = new Date();
	//				tmsMessage.setMessageDateTime(date);
	//			
	//				/** Get XML Packet **/
	//				tmsMessage.marshal(writer);
	////				System.out.println("finder");		
	////				InputStreamReader reader = new InputStreamReader(new FileInputStream("test.xml"));
	////				int i = reader.read();
	////				String output ="";
	////				while(i != -1){
	////					System.out.write(i);
	////					Integer integ = new Integer(i);
	////					byte b[] = {integ.byteValue()};
	////					String byteString = new String(b);
	////					output += byteString;
	////					i=reader.read();
	////				}
	////				System.out.println();
	////				System.out.println("AdminUpdate Finished...");
	////				return output;
	//				return writer.toString();
	//			}
	//			catch(Exception e){
	//				System.out.println("EXCEPTION! : "+e.toString());
	//				return null;
	//			}
	//		
	//		}
	//		
	//
	//		/**Transaction Auth*/	
	//		public static String transactionAuthXml(UserData user, CreditCard_Data card){
	//			try{
	//				//FileWriter writer = new FileWriter("test.xml");
	//				StringWriter writer = new StringWriter();
	//				System.out.println("Transaction Auth xml Started...");
	//			
	//				/** Initialise **/
	//				TmsMessage tmsMessage = new TmsMessage();
	//						
	//				MessageAuthBlk mab = new MessageAuthBlk();
	//				MessageTypeChoice mtc = new MessageTypeChoice();
	//			
	//				MessageTransactionBlk mtransb = new MessageTransactionBlk();
	//				TransactionAuth ta = new TransactionAuth();
	//				CardInfo ci = new CardInfo();
	//			
	//				/** set simpleTypes **/
	//				ci.setCardName(card.getCardType());
	//				ci.setCardHolderName(card.getCardHolderName());
	//				ci.setCardNumber(card.getCardNumber());
	//				ci.setExpiryDate(card.getExpiryDate());
	//				
	//				BigDecimal bd = new BigDecimal(user.getPreBillingAmount());
	//				ta.setPaymentAmount(bd);
	//				ta.setPaymentCurrencyCode("NZD"); //refer to technical docs..
	//				ta.setNcbId(user.getNcbId());
	//								
	//				/** Add to parent **/
	//				ta.setCardInfo(ci);
	//				mtransb.setTransactionAuth(ta);
	//				mtc.setMessageTransactionBlk(mtransb);
	//				tmsMessage.setMessageTypeChoice(mtc);
	//							
	//				/** Set attributes **/
	//				mab.setNcbId(user.getNcbId());
	//				tmsMessage.setMessageAuthBlk(mab); //adding attribute.
	//				tmsMessage.setMessageId(counter);
	//				counter++;
	//				Date date = new Date();
	//				tmsMessage.setMessageDateTime(date);
	//			
	//				/** Get XML Packet **/
	//				tmsMessage.marshal(writer);
	////				System.out.println("finder");		
	////				InputStreamReader reader = new InputStreamReader(new FileInputStream("test.xml"));
	////				int i = reader.read();
	////				String output = "";
	////				while(i != -1){
	////					System.out.write(i);
	////					Integer integ = new Integer(i);
	////					byte b[] = {integ.byteValue()};
	////					String byteString = new String(b);
	////					output += byteString;
	////					i=reader.read();
	////				}
	////				System.out.println();
	////				System.out.println("Finished...");
	////				return output;
	////				
	//				return writer.toString();
	//				
	//			}
	//			catch(Exception e){
	//				
	//				System.out.println("EXCEPTION! : "+e.toString());
	//				return null;
	//			}		
	//		}
	//		/**Transaction Purchase*/	
	//		public static String transactionPurchaseXml(Stock_Data stock,UserData user,UserAddress_Data address){
	//			try{
	//				//FileWriter writer = new FileWriter("test.xml");
	//				StringWriter writer = new StringWriter();
	//				System.out.println("Started...");
	//				
	//				/** Initialise */
	//				TmsMessage tmsMessage = new TmsMessage();
	//							
	//				MessageAuthBlk mab = new MessageAuthBlk();
	//				MessageTypeChoice mtc = new MessageTypeChoice();
	//				
	//				MessageTransactionBlk mtransb = new MessageTransactionBlk();
	//				TransactionPurchase tp = new TransactionPurchase();
	//								
	//				Ncb ncb = new Ncb();
	//				NcbAddress ncbaddress = new NcbAddress();
	//				NcbAddressTypeType ncbaddresstypetype = NcbAddressTypeType.valueOf("Billing");
	//				PurchaseItem pi = new PurchaseItem();
	//			
	//				/** set simpleTypes */	
	//				pi.setItemCode(stock.getStockCode());	
	//				pi.setItemQuantity(Integer.parseInt(stock.getQuantity()));	
	//				pi.setServiceCode(stock.getServiceCode());
	//				pi.setServiceRequest(stock.getServiceRequest());
	//				pi.setServiceResponse(stock.getServiceResponse());
	//				
	//				ncb.setNcbId(user.getNcbId());
	//				ncb.setName(user.getFirstName());
	//					
	//				/** Add to parent */
	//				tp.setNcb(ncb);				
	//				tp.addPurchaseItem(pi);
	//				tp.setPaymentCurrencyCode("NZD");
	//				
	//				mtransb.setTransactionPurchase(tp);
	//				mtc.setMessageTransactionBlk(mtransb);
	//				tmsMessage.setMessageTypeChoice(mtc);
	//								
	//				/** Set attributes */
	//				mab.setNcbId(user.getNcbId());
	//				
	//				tmsMessage.setMessageAuthBlk(mab); //adding attribute.
	//				tmsMessage.setMessageId(counter);
	//				counter++;
	//				Date date = new Date();
	//				tmsMessage.setMessageDateTime(date);
	//				
	//				/** Get XML Packet */
	//				tmsMessage.marshal(writer);				
	//				System.out.println("finder");		
	////				InputStreamReader reader = new InputStreamReader(new FileInputStream("test.xml"));
	////				int i = reader.read();
	////				String output = "";
	////				while(i != -1){
	////					System.out.write(i);
	////					Integer integ = new Integer(i);
	////					byte b[] = {integ.byteValue()};
	////					String byteString = new String(b);
	////					output += byteString;
	////					i=reader.read();
	////				}
	////				System.out.println();
	////				System.out.println("Finished...");
	////				return output;
	//				return writer.toString();
	//			}
	//			catch(Exception e){
	//				System.out.println("EXCEPTION! : "+e.toString());
	//				return null;
	//			}
	//		}
	//		/**Transaction Settlement*/	
	//		public static String transactionSettlementXml(UserData user,CreditCard_Data card, Stock_Data stock){
	//			try{
	//				//FileWriter writer = new FileWriter("test.xml");
	//				StringWriter writer = new StringWriter();
	//				System.out.println("Started...");
	//				
	//				/** Initialise */
	//				TmsMessage tmsMessage = new TmsMessage();
	//							
	//				MessageAuthBlk mab = new MessageAuthBlk();
	//				MessageTypeChoice mtc = new MessageTypeChoice();
	//				
	//				MessageTransactionBlk mtransb = new MessageTransactionBlk();
	//				TransactionSettlement ts = new TransactionSettlement();
	//								
	//				/** set simpleTypes */
	//				double d = Double.parseDouble(stock.getQuantity()) * stock.getPrice();
	//				String str = String.valueOf(d);
	//				
	//				BigDecimal bd = new BigDecimal(str);
	//				ts.setPaymentAmount(bd);
	//				ts.setPaymentCurrencyCode("NZD"); //refer to technical docs..
	//				ts.setNcbId(user.getNcbId());
	//					
	//					
	//					
	//				/** Add to parent */
	//				mtransb.setTransactionSettlement(ts);
	//				mtc.setMessageTransactionBlk(mtransb);
	//				tmsMessage.setMessageTypeChoice(mtc);
	//								
	//				/** Set attributes */
	//				mab.setNcbId(user.getNcbId());
	//				tmsMessage.setMessageAuthBlk(mab); //adding attribute.
	//				tmsMessage.setMessageId(counter);
	//				counter++;
	//				Date date = new Date();
	//				tmsMessage.setMessageDateTime(date);
	//				
	//				/** Get XML Packet */
	//				tmsMessage.marshal(writer);				
	//				System.out.println("finder");		
	////				InputStreamReader reader = new InputStreamReader(new FileInputStream("test.xml"));
	////				int i = reader.read();
	////				String output = "";
	////				while(i != -1){
	////					System.out.write(i);
	////					Integer integ = new Integer(i);
	////					byte b[] = {integ.byteValue()};
	////					String byteString = new String(b);
	////					output += byteString;
	////					i=reader.read();
	////				}
	////				System.out.println();
	////				System.out.println("Finished...");
	////				return output;
	//				return writer.toString();
	//			}
	//			catch(Exception e){
	//				System.out.println("EXCEPTION! : "+e.toString());
	//				return null;
	//			}			
	//		}
	//		/**Enquiry Ncb*/	
//	public static String enquiryNcbXml(UserData user) {
//		try {
//			//FileWriter writer = new FileWriter("test.xml");
//			StringWriter writer = new StringWriter();
//			System.out.println("Started...");
//
//			/** Initialise */
//			TmsMessage tmsMessage = new TmsMessage();
//
//			MessageAuthBlk mab = new MessageAuthBlk();
//			MessageTypeChoice mtc = new MessageTypeChoice();
//
//			MessageEnquiryBlk menquiryb = new MessageEnquiryBlk();
//			//MessageTypeChoice mtc2 = new MessageTypeChoice(); //not needed?
//			EnquiryNcb en = new EnquiryNcb();
//
//			/** set simpleTypes */
//			en.setNcbId(user.getNcbId());
//
//			/** Add to parent */
//			menquiryb.setEnquiryNcb(en);
//			mtc.setMessageEnquiryBlk(menquiryb);
//			tmsMessage.setMessageTypeChoice(mtc);
//
//			/** Set attributes */
//			mab.setNcbId(user.getNcbId());
//			tmsMessage.setMessageAuthBlk(mab); //adding attribute.
//			tmsMessage.setMessageId(counter);
//			counter++;
//			Date date = new Date();
//			tmsMessage.setMessageDateTime(date);
//
//			/** Get XML Packet */
//			tmsMessage.marshal(writer);
//
//			return writer.toString();
//		} catch (Exception e) {
//			System.out.println("EXCEPTION! : " + e.toString());
//			return null;
//		}
//	}
//	/**Enquiry NcbAddress*/
//	public static String enquiryNcbAddressXml(UserData user) {
//		try {
//			//FileWriter writer = new FileWriter("test.xml");
//			StringWriter writer = new StringWriter();
//			System.out.println("Started...");
//
//			/** Initialise */
//			TmsMessage tmsMessage = new TmsMessage();
//
//			MessageAuthBlk mab = new MessageAuthBlk();
//			MessageTypeChoice mtc = new MessageTypeChoice();
//
//			MessageEnquiryBlk menquiryb = new MessageEnquiryBlk();
//			EnquiryNcbAddress ena = new EnquiryNcbAddress();
//
//			NcbAddress na = new NcbAddress();
//			NcbAddressTypeType ncbaddresstypetype =
//				NcbAddressTypeType.valueOf("Billing");
//
//			/** set simpleTypes */
//			na.setSelectedAddressType(ncbaddresstypetype);
//
//			ena.setNcbId(user.getNcbId());
//
//			/** Add to parent */
//			ena.addNcbAddress(na);
//			menquiryb.setEnquiryNcbAddress(ena);
//			mtc.setMessageEnquiryBlk(menquiryb);
//			tmsMessage.setMessageTypeChoice(mtc);
//
//			/** Set attributes */
//			mab.setNcbId(user.getNcbId());
//			tmsMessage.setMessageAuthBlk(mab); //adding attribute.
//			tmsMessage.setMessageId(counter);
//			counter++;
//			Date date = new Date();
//			tmsMessage.setMessageDateTime(date);
//
//			/** Get XML Packet */
//			tmsMessage.marshal(writer);
//
//			return writer.toString();
//		} catch (Exception e) {
//			System.out.println("EXCEPTION! : " + e.toString());
//			return null;
//		}
//	}

}
