Advertisement
Guest User

Untitled

a guest
Nov 20th, 2016
1,676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.81 KB | None | 0 0
  1. package ejercicio2;
  2.  
  3. import java.io.File;
  4. import java.io.PrintWriter;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.util.ArrayList;
  11. import java.util.Scanner;
  12.  
  13. import org.apache.commons.httpclient.HttpClient;
  14. import org.apache.commons.httpclient.methods.PostMethod;
  15.  
  16. public class Main {
  17.     public static void main(String[] args) {
  18.  
  19.         Scanner teclado = new Scanner(System.in);
  20.  
  21.         ArrayList<Lead> leads = new ArrayList<Lead>();
  22.        
  23.         // Configurar parámetros de conexión
  24.         Connection conexion = null;
  25.         String user = "Ejemplo"; // Usuario para la conexión a la base de datos
  26.         String password = "Ejemplo"; // Contraseña para la conexión a la base de
  27.                                         // datos
  28.         String database = "Zoho"; // Base de datos a la que conectarse
  29.         String cadena; // String auxiliar para la introducción de los datos
  30.  
  31.         System.out.print("Introduzca el nombre de usuario (vacío para el valor determinado \"" + user + "\"): ");
  32.         cadena = teclado.nextLine();
  33.         if (!cadena.equals(""))
  34.             user = cadena;
  35.  
  36.         System.out.print("Introduzca la contraseña (vacío para el valor determinado \"" + password + "\"): ");
  37.         cadena = teclado.nextLine();
  38.         if (!cadena.equals(""))
  39.             password = cadena;
  40.  
  41.         System.out.print(
  42.                 "Introduzca el nombre de la base de datos (vacío para el valor determinado \"" + database + "\"): ");
  43.         cadena = teclado.nextLine();
  44.         if (!cadena.equals(""))
  45.             database = cadena;
  46.         String url = "jdbc:mysql://localhost:3306/" + database
  47.                 + "?useSSL=false&serverTimezone=Europe/Madrid&noAccessToProcedureBodies=true";
  48.  
  49.         PreparedStatement pstm = null;
  50.         ResultSet result = null;
  51.  
  52.         String LeadOwner = null, Salutation = null, FirstName = null, Title = null, LastName = null, Company = null, LeadSource = null, Industry = null, AnnualRevenue = null, Phone = null,
  53.                 Mobile = null, Fax = null, Email = null, SecondaryEmail = null, SkypeId = null, Website = null, LeadStatus = null, Rating = null, EmailOptOut = null, Street = null, City = null,
  54.                 State = null, ZipCode = null, Country = null, Description = null;
  55.         int IdLead = 0, NoEmployees;
  56.  
  57.         try {
  58.             // Codigo para cargar el Driver de la Base de Datos
  59.             Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
  60.             // Establecemos la conexión
  61.             conexion = DriverManager.getConnection(url, user, password);
  62.             System.err.println("Conexión establecida");
  63.  
  64.             // sentecia en SQL podría ser
  65.             // SELECT Empleados.nombre, fechaalta, salario, comision,
  66.             // departamentos.depnombre, localidad
  67.             // FROM empleados JOIN Departamentos USING (CodDepartamento)
  68.             // WHERE CodDepartamento = 10 AND Puesto = Comercial And Salario >
  69.             // 1500;
  70.  
  71.             // creamos la sentencia SQL
  72.             String sql = "SELECT IdLead, LeadOwner, Salutation, FirstName, Title, LastName, Company, LeadSource, Industry, AnnualRevenue, Phone, Mobile, Fax, Email, SecondaryEmail, SkypeId, Website, LeadStatus, Rating, NoEmployees, EmailOptOut, Street, City, State, ZipCode, Country, Description FROM Leads;";
  73.  
  74.             // construimos el PreparedStatement
  75.             pstm = conexion.prepareStatement(sql);
  76.  
  77.             // creamos el resultado de la consulta
  78.             result = pstm.executeQuery();
  79.  
  80.             // Recorremos el resultado de la consulta visualizando los registros
  81.             while (result.next()) {
  82.                 Lead lead = new Lead();
  83.                
  84.                 lead.setIdLead(result.getInt(1));
  85.                 lead.setLeadOwner(result.getString(2));
  86.                 lead.setSalutation(result.getString(3));
  87.                 lead.setFirstName(result.getString(4));
  88.                 lead.setTitle(result.getString(5));
  89.                 lead.setLastName(result.getString(6));
  90.                 lead.setCompany(result.getString(7));
  91.                 lead.setLeadSource(result.getString(8));
  92.                 lead.setIndustry(result.getString(9));
  93.                 lead.setAnnualRevenue(result.getString(10));
  94.                 lead.setPhone(result.getString(11));
  95.                 lead.setMobile(result.getString(12));
  96.                 lead.setFax(result.getString(13));
  97.                 lead.setEmail(result.getString(14));
  98.                 lead.setSecondaryEmail(result.getString(15));
  99.                 lead.setSkypeId(result.getString(16));
  100.                 lead.setWebsite(result.getString(17));
  101.                 lead.setLeadStatus(result.getString(18));
  102.                 lead.setRating(result.getString(19));
  103.                 lead.setEmailOptOut(result.getString(20));
  104.                 lead.setStreet(result.getString(21));
  105.                 lead.setCity(result.getString(22));
  106.                 lead.setState(result.getString(23));
  107.                 lead.setZipCode(result.getString(24));
  108.                 lead.setCountry(result.getString(25));
  109.                 lead.setDescription(result.getString(26));
  110.                
  111.                 leads.add(lead);
  112.             } // fin while
  113.  
  114.             conexion.close();
  115.         } catch (ClassNotFoundException cnf) {
  116.             cnf.printStackTrace();
  117.         } catch (SQLException sqle) {
  118.             sqle.printStackTrace();
  119.         } catch (Exception e) {
  120.             e.printStackTrace();
  121.         }
  122.        
  123.         try {
  124.             String authtoken = "da40358769a9abad922d69234c2e0583";
  125.             // String targetURL =
  126.             // "https://crm.zoho.com/crm/private/xml/Leads/getRecords";
  127.  
  128.             /*
  129.              * String targetURL =
  130.              * "https://crm.zoho.com/crm/private/xml/Contacts/updateRecords?scope=crmapi"+
  131.              * "&newFormat=1"+ "&id=1056501000000067035"+ "&xmlData=";
  132.              */
  133.             String targetURL = "https://crm.zoho.com/crm/private/xml/Leads/insertRecords?";
  134.            
  135.             String xml = "<Contacts>";
  136.            
  137.             for(int i = 1; i<leads.size(); i++){
  138.                 xml.concat("<row no='"+i+"'>");
  139.             }
  140.             String xml = "<Contacts><row no='1'><FL val='CONTACTID'>2190741000000115009</FL><FL val='SMOWNERID'>2190741000000107005</FL><FL val='Contact Owner'><![CDATA[Alvaro Fernandez Neira]]></FL><FL val='First Name'><![CDATA[Juan]]></FL><FL val='Last Name'><![CDATA[Garrido Perez]]></FL><FL val='Email'><![CDATA[juan@gmail.com]]></FL><FL val='Phone'><![CDATA[986474849]]></FL><FL val='SMCREATORID'>2190741000000107005</FL><FL val='Created By'><![CDATA[Alvaro Fernandez Neira]]></FL><FL val='MODIFIEDBY'>2190741000000107005</FL><FL val='Modified By'><![CDATA[Alvaro Fernandez Neira]]></FL><FL val='Created Time'><![CDATA[2016-11-08 17:43:55]]></FL><FL val='Modified Time'><![CDATA[2016-11-08 17:43:55]]></FL><FL val='Email Opt Out'><![CDATA[false]]></FL><FL val='Last Activity Time'><![CDATA[2016-11-08 17:43:55]]></FL></row><row no='2'><FL val='CONTACTID'>2190741000000110001</FL><FL val='SMOWNERID'>2190741000000107005</FL><FL val='Contact Owner'><![CDATA[Alvaro Fernandez Neira]]></FL><FL val='First Name'><![CDATA[Manuel]]></FL><FL val='Last Name'><![CDATA[Gomex]]></FL><FL val='SMCREATORID'>2190741000000107005</FL><FL val='Created By'><![CDATA[Alvaro Fernandez Neira]]></FL><FL val='MODIFIEDBY'>2190741000000107005</FL><FL val='Modified By'><![CDATA[Alvaro Fernandez Neira]]></FL><FL val='Created Time'><![CDATA[2016-11-04 10:02:07]]></FL><FL val='Modified Time'><![CDATA[2016-11-04 10:02:07]]></FL><FL val='Email Opt Out'><![CDATA[false]]></FL><FL val='Last Activity Time'><![CDATA[2016-11-04 10:02:07]]></FL></row></Contacts>";
  141.            
  142.            
  143.             String xml = "<SalesOrders>" + "<row no='1'>" + "<FL val='Subject'>orden de venta desde api</FL>"
  144.                     + "<FL val='Due Date'>2009-03-10</FL>" + "<FL val='Sub Total'>48000.0</FL>"
  145.                     + "<FL val='Tax'>0.0</FL>" + "<FL val='Adjustment'>0.0</FL>" + "<FL val='Grand Total'>48000.0</FL>"
  146.                     + "<FL val='Billing Street'>test</FL>" + "<FL val='Shipping Street'>test</FL>"
  147.                     + "<FL val='Billing City'>test</FL>" + "<FL val='Shipping City'>test</FL>"
  148.                     + "<FL val='Billing State'>test</FL>" + "<FL val='Shipping State'>test</FL>"
  149.                     + "<FL val='Billing Code'>223</FL>" + "<FL val='Shipping Code'>223</FL>"
  150.                     + "<FL val='Billing Country'>test</FL>" + "<FL val='Shipping Country'>test</FL>"
  151.                     + "<FL val='Product Details'>" + "<product no='1'>"
  152.                     + "<FL val='Product Id'>1056501000000083075</FL>" + "<FL val='Unit Price'>10.0</FL>"
  153.                     + "<FL val='Quantity'>1.0</FL>" + "<FL val='Total'>123.0</FL>" + "<FL val='Discount'>1.23</FL>"
  154.                     + "<FL val='Total After Discount'>121.77</FL>" + "<FL val='List Price'>123.0</FL>"
  155.                     + "<FL val='Net Total'>121.77</FL>" + "</product>" + "</FL>"
  156.                     + "<FL val='Terms and Conditions'>Test by Zoho</FL>" + "<FL val='Description'>Test By Zoho</FL>"
  157.                     + "</row>" + "</SalesOrders>";
  158.  
  159.             String paramname = "content";
  160.             PostMethod post = new PostMethod(targetURL);
  161.             post.setParameter("authtoken", authtoken);
  162.             post.setParameter("scope", "crmapi");
  163.  
  164.             post.setParameter("newFormat", "1");
  165.             // post.setParameter("id","1056501000000067035");
  166.             post.setParameter("xmlData", xml);
  167.             // post.setParameter("scope","crmapi");
  168.             HttpClient httpclient = new HttpClient();
  169.             PrintWriter myout = null;
  170.  
  171.             /*-------------------------------------- Execute the http request--------------------------------*/
  172.             try {
  173.                 long t1 = System.currentTimeMillis();
  174.                 int resultZoho = httpclient.executeMethod(post);
  175.                 System.out.println("HTTP Response status code: " + resultZoho);
  176.                 System.out.println(">> Time taken " + (System.currentTimeMillis() - t1));
  177.                 /*-------------------------------------- Execute the http request--------------------------------*/
  178.  
  179.                 /*
  180.                  * ---------------------------writing the response to a
  181.                  * file--------------------
  182.                  */
  183.                 myout = new PrintWriter(new File("response.xml"));
  184.                 myout.print(post.getResponseBodyAsString());
  185.                 /*
  186.                  * ---------------------------writing the response to a
  187.                  * file--------------------
  188.                  */
  189.  
  190.                 /*-----------------------Get response as a string ----------------*/
  191.                 String postResp = post.getResponseBodyAsString();
  192.                 System.out.println("postResp=======>" + postResp);
  193.                 /*
  194.                  * ---------------------Get response as a string
  195.                  * ----------------------------
  196.                  */
  197.                 if (postResp.equals("Invalid Ticket Id")) {
  198.                     // generate new auth token and call the API
  199.                 }
  200.             } catch (Exception e) {
  201.                 e.printStackTrace();
  202.             } finally {
  203.                 myout.close();
  204.                 post.releaseConnection();
  205.             }
  206.         } catch (Exception e) {
  207.             e.printStackTrace();
  208.         }
  209.        
  210.         teclado.close();
  211.     } // fin
  212.         // main
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement