Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ejercicio2;
- import java.io.File;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.Scanner;
- import org.apache.commons.httpclient.HttpClient;
- import org.apache.commons.httpclient.methods.PostMethod;
- public class Main {
- public static void main(String[] args) {
- Scanner teclado = new Scanner(System.in);
- ArrayList<Lead> leads = new ArrayList<Lead>();
- // Configurar parámetros de conexión
- Connection conexion = null;
- String user = "Ejemplo"; // Usuario para la conexión a la base de datos
- String password = "Ejemplo"; // Contraseña para la conexión a la base de
- // datos
- String database = "Zoho"; // Base de datos a la que conectarse
- String cadena; // String auxiliar para la introducción de los datos
- System.out.print("Introduzca el nombre de usuario (vacío para el valor determinado \"" + user + "\"): ");
- cadena = teclado.nextLine();
- if (!cadena.equals(""))
- user = cadena;
- System.out.print("Introduzca la contraseña (vacío para el valor determinado \"" + password + "\"): ");
- cadena = teclado.nextLine();
- if (!cadena.equals(""))
- password = cadena;
- System.out.print(
- "Introduzca el nombre de la base de datos (vacío para el valor determinado \"" + database + "\"): ");
- cadena = teclado.nextLine();
- if (!cadena.equals(""))
- database = cadena;
- String url = "jdbc:mysql://localhost:3306/" + database
- + "?useSSL=false&serverTimezone=Europe/Madrid&noAccessToProcedureBodies=true";
- PreparedStatement pstm = null;
- ResultSet result = null;
- String LeadOwner = null, Salutation = null, FirstName = null, Title = null, LastName = null, Company = null, LeadSource = null, Industry = null, AnnualRevenue = null, Phone = null,
- Mobile = null, Fax = null, Email = null, SecondaryEmail = null, SkypeId = null, Website = null, LeadStatus = null, Rating = null, EmailOptOut = null, Street = null, City = null,
- State = null, ZipCode = null, Country = null, Description = null;
- int IdLead = 0, NoEmployees;
- try {
- // Codigo para cargar el Driver de la Base de Datos
- Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
- // Establecemos la conexión
- conexion = DriverManager.getConnection(url, user, password);
- System.err.println("Conexión establecida");
- // sentecia en SQL podría ser
- // SELECT Empleados.nombre, fechaalta, salario, comision,
- // departamentos.depnombre, localidad
- // FROM empleados JOIN Departamentos USING (CodDepartamento)
- // WHERE CodDepartamento = 10 AND Puesto = Comercial And Salario >
- // 1500;
- // creamos la sentencia SQL
- 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;";
- // construimos el PreparedStatement
- pstm = conexion.prepareStatement(sql);
- // creamos el resultado de la consulta
- result = pstm.executeQuery();
- // Recorremos el resultado de la consulta visualizando los registros
- while (result.next()) {
- Lead lead = new Lead();
- lead.setIdLead(result.getInt(1));
- lead.setLeadOwner(result.getString(2));
- lead.setSalutation(result.getString(3));
- lead.setFirstName(result.getString(4));
- lead.setTitle(result.getString(5));
- lead.setLastName(result.getString(6));
- lead.setCompany(result.getString(7));
- lead.setLeadSource(result.getString(8));
- lead.setIndustry(result.getString(9));
- lead.setAnnualRevenue(result.getString(10));
- lead.setPhone(result.getString(11));
- lead.setMobile(result.getString(12));
- lead.setFax(result.getString(13));
- lead.setEmail(result.getString(14));
- lead.setSecondaryEmail(result.getString(15));
- lead.setSkypeId(result.getString(16));
- lead.setWebsite(result.getString(17));
- lead.setLeadStatus(result.getString(18));
- lead.setRating(result.getString(19));
- lead.setEmailOptOut(result.getString(20));
- lead.setStreet(result.getString(21));
- lead.setCity(result.getString(22));
- lead.setState(result.getString(23));
- lead.setZipCode(result.getString(24));
- lead.setCountry(result.getString(25));
- lead.setDescription(result.getString(26));
- leads.add(lead);
- } // fin while
- conexion.close();
- } catch (ClassNotFoundException cnf) {
- cnf.printStackTrace();
- } catch (SQLException sqle) {
- sqle.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- try {
- String authtoken = "da40358769a9abad922d69234c2e0583";
- // String targetURL =
- // "https://crm.zoho.com/crm/private/xml/Leads/getRecords";
- /*
- * String targetURL =
- * "https://crm.zoho.com/crm/private/xml/Contacts/updateRecords?scope=crmapi"+
- * "&newFormat=1"+ "&id=1056501000000067035"+ "&xmlData=";
- */
- String targetURL = "https://crm.zoho.com/crm/private/xml/Leads/insertRecords?";
- String xml = "<Contacts>";
- for(int i = 1; i<leads.size(); i++){
- xml.concat("<row no='"+i+"'>");
- }
- 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>";
- String xml = "<SalesOrders>" + "<row no='1'>" + "<FL val='Subject'>orden de venta desde api</FL>"
- + "<FL val='Due Date'>2009-03-10</FL>" + "<FL val='Sub Total'>48000.0</FL>"
- + "<FL val='Tax'>0.0</FL>" + "<FL val='Adjustment'>0.0</FL>" + "<FL val='Grand Total'>48000.0</FL>"
- + "<FL val='Billing Street'>test</FL>" + "<FL val='Shipping Street'>test</FL>"
- + "<FL val='Billing City'>test</FL>" + "<FL val='Shipping City'>test</FL>"
- + "<FL val='Billing State'>test</FL>" + "<FL val='Shipping State'>test</FL>"
- + "<FL val='Billing Code'>223</FL>" + "<FL val='Shipping Code'>223</FL>"
- + "<FL val='Billing Country'>test</FL>" + "<FL val='Shipping Country'>test</FL>"
- + "<FL val='Product Details'>" + "<product no='1'>"
- + "<FL val='Product Id'>1056501000000083075</FL>" + "<FL val='Unit Price'>10.0</FL>"
- + "<FL val='Quantity'>1.0</FL>" + "<FL val='Total'>123.0</FL>" + "<FL val='Discount'>1.23</FL>"
- + "<FL val='Total After Discount'>121.77</FL>" + "<FL val='List Price'>123.0</FL>"
- + "<FL val='Net Total'>121.77</FL>" + "</product>" + "</FL>"
- + "<FL val='Terms and Conditions'>Test by Zoho</FL>" + "<FL val='Description'>Test By Zoho</FL>"
- + "</row>" + "</SalesOrders>";
- String paramname = "content";
- PostMethod post = new PostMethod(targetURL);
- post.setParameter("authtoken", authtoken);
- post.setParameter("scope", "crmapi");
- post.setParameter("newFormat", "1");
- // post.setParameter("id","1056501000000067035");
- post.setParameter("xmlData", xml);
- // post.setParameter("scope","crmapi");
- HttpClient httpclient = new HttpClient();
- PrintWriter myout = null;
- /*-------------------------------------- Execute the http request--------------------------------*/
- try {
- long t1 = System.currentTimeMillis();
- int resultZoho = httpclient.executeMethod(post);
- System.out.println("HTTP Response status code: " + resultZoho);
- System.out.println(">> Time taken " + (System.currentTimeMillis() - t1));
- /*-------------------------------------- Execute the http request--------------------------------*/
- /*
- * ---------------------------writing the response to a
- * file--------------------
- */
- myout = new PrintWriter(new File("response.xml"));
- myout.print(post.getResponseBodyAsString());
- /*
- * ---------------------------writing the response to a
- * file--------------------
- */
- /*-----------------------Get response as a string ----------------*/
- String postResp = post.getResponseBodyAsString();
- System.out.println("postResp=======>" + postResp);
- /*
- * ---------------------Get response as a string
- * ----------------------------
- */
- if (postResp.equals("Invalid Ticket Id")) {
- // generate new auth token and call the API
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- myout.close();
- post.releaseConnection();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- teclado.close();
- } // fin
- // main
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement