Package com.lbslocal.api.utils

Source Code of com.lbslocal.api.utils.Common

package com.lbslocal.api.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.lbslocal.api.objects.TokenStatus;
import com.lbslocal.api.objects.TokenValidated;
import com.lbslocal.api.proxy.Proxy;
import com.lbslocal.master.common.ConfigurationManager;

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;

public class Common {

  public static String readFile(String file, String csName) throws IOException {
    Charset cs = Charset.forName(csName);
    return readFile(file, cs);
  }
    public static String GetDateTimeTrafficTiles()
    {
      Format formatter = new SimpleDateFormat("yyyy-MM-dd%20HH:mm:00");
      //2011-03-15%2010:35:00
        String t =formatter.format(new Date());
       
       
//        DateTime.Now.ToString("yyyy-MM-dd") + "%20";
//        t += ((DateTime.Now.Hour < 10) ? "0" : "") + DateTime.Now.Hour.ToString();
//        t += ((DateTime.Now.Minute < 10) ? ":0" : ":") + DateTime.Now.Minute.ToString();
//        t += ":00";

        return t;
    }
  public static String readFile(String file, Charset cs) throws IOException {
    // No real need to close the BufferedReader/InputStreamReader
    // as they're only wrapping the stream

    FileInputStream stream = new FileInputStream(file);
    try {
      Reader reader = new BufferedReader(new InputStreamReader(stream, cs));
      StringBuilder builder = new StringBuilder();
      char[] buffer = new char[8192];
      int read;
      while ((read = reader.read(buffer, 0, buffer.length)) > 0) {
        builder.append(buffer, 0, read);
      }

      return builder.toString();
    } finally {
      // Potential issue here: if this throws an IOException,
      // it will mask any others. Normally I'd use a utility
      // method which would log exceptions and swallow them
      stream.close();
    }
  }

  public static void writeFile(String path, String value) throws IOException {

    File file = new File(path);

    BufferedWriter output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));
    output.write(value);
    output.flush();
    output.close();

  }

  public static void writeFile(String path, byte[] value) throws IOException {
    FileOutputStream fos = new FileOutputStream(path);

    fos.write(value);

    fos.close();
  }

  public static TokenValidated ValidateUser(Proxy proxy) throws Exception {
    ConfigurationManager cm = ConfigurationManager.getInstance();
    TokenValidated token = new TokenValidated(Boolean.parseBoolean(cm.getAppSettings("ATIVO")), cm.getAppSettings("ID"), Integer.parseInt(cm.getAppSettings("CLIENTTYPE")));
    Common.ValidateStatusToken(token);

    return token;
  }

  private static void ValidateStatusToken(TokenValidated token) throws Exception {
    TokenStatus ts = new TokenStatus();
    ts.valid = true;

    if (token.getStatus() == TokenValidated._DOMINIO_INVALIDO) {
      if (token.getId() == 1254 || token.getId() == 151) {
        ts.message = "Domínio inválido!";
        ts.valid = false;
      }
    }

    if (token.getStatus() == TokenValidated._CANCELADO) {
      ts.message = "Login cancelado!";
      ts.valid = false;
    }

    if (token.getStatus() == TokenValidated._TOKEN_INVALIDO) {
      ts.message = "Login inválido!";
      ts.valid = false;
    }

    if (!ts.valid) {
      throw (new Exception(ts.message));
    }
  }

 
  public static String getURLCall(String urlConex) throws MalformedURLException, IOException {

    HttpURLConnection conexHttp = (HttpURLConnection) new URL(urlConex).openConnection();

    StringBuffer retorno = new StringBuffer();
    String inputLine = "";

    BufferedReader in = new BufferedReader(new InputStreamReader(conexHttp.getInputStream()));

    while ((inputLine = in.readLine()) != null)
      retorno.append(inputLine);

    in.close();
    conexHttp.disconnect();
    return retorno.toString();
  }

  public static void gravarLogUnique(int id, int i, int j, Object object, Object object2) {
    // TODO Auto-generated method stub

  }

  @SuppressWarnings({ "rawtypes" })
  public static Object[] getArrayByJSon(String jsonStr, Class[] classTypes) {
    int indexArrayObjects = 0;

    List<Object> arrayObjects = new JSONDeserializer<List<Object>>().deserialize(jsonStr);
    Object[] objects = new Object[arrayObjects.size()];
    JSONSerializer serializer = new JSONSerializer();

    for (int i = 0; i < objects.length; i++) {
      String part = serializer.exclude("*.class").serialize(arrayObjects.get(i));
      if (part.startsWith("[")) {
        Object[] objs = getArrayByJSon(part, new Class[] { classTypes[indexArrayObjects] });
        objects[i] = objs;
      } else
        objects[i] = new JSONDeserializer().use(null, classTypes[indexArrayObjects]).deserialize(part);

      if (arrayObjects.size() == classTypes.length) {
        indexArrayObjects++;
      }
    }

    return objects;
  }

  public static String SerializeJSObject(Object objeto) {
    return new JSONSerializer().exclude("*.class").deepSerialize(objeto);
  }

  public static String CamelCase(String text) {
    if (!text.isEmpty()) {
      text = text.substring(0, 1).toUpperCase() + text.substring(1).toLowerCase();

      text = text.replace(" a", " A");
      text = text.replace(" b", " B");
      text = text.replace(" c", " C");
      text = text.replace(" d", " D");
      text = text.replace(" e", " E");

      text = text.replace(" f", " F");
      text = text.replace(" g", " G");
      text = text.replace(" j", " J");
      text = text.replace(" h", " H");
      text = text.replace(" l", " L");
      text = text.replace(" m", " M");

      text = text.replace(" n", " N");
      text = text.replace(" o", " O");
      text = text.replace(" p", " P");
      text = text.replace(" q", " Q");
      text = text.replace(" i", " I");

      text = text.replace(" r", " R");
      text = text.replace(" s", " S");
      text = text.replace(" t", " T");
      text = text.replace(" u", " U");

      text = text.replace(" v", " V");
      text = text.replace(" x", " X");
      text = text.replace(" y", " Y");
      text = text.replace(" z", " Z");
      text = text.replace(" w", " W");

      text = text.replace("/a", "/A");
      text = text.replace("/b", "/B");
      text = text.replace("/c", "/C");
      text = text.replace("/d", "/D");
      text = text.replace("/e", "/E");

      text = text.replace("/f", "/F");
      text = text.replace("/g", "/G");
      text = text.replace("/j", "/J");
      text = text.replace("/h", "/H");
      text = text.replace("/l", "/L");
      text = text.replace("/m", "/M");

      text = text.replace("/n", "/N");
      text = text.replace("/o", "/O");
      text = text.replace("/p", "/P");
      text = text.replace("/q", "/Q");
      text = text.replace("/i", "/I");

      text = text.replace("/r", "/R");
      text = text.replace("/s", "/S");
      text = text.replace("/t", "/T");
      text = text.replace("/u", "/U");

      text = text.replace("/v", "/V");
      text = text.replace("/x", "/X");
      text = text.replace("/y", "/Y");
      text = text.replace("/z", "/Z");
      text = text.replace("/w", "/W");

      text = text.replace(" DO ", " do ");
      text = text.replace(" DA ", " da ");
      text = text.replace(" DE ", " de ");
      text = text.replace(" DI ", " di ");
      text = text.replace(" DU ", " du ");

      text = text.replace(" DOS ", " dos ");
      text = text.replace(" DAS ", " das ");
      text = text.replace(" DES ", " des ");
      text = text.replace(" DIS ", " dis ");
      text = text.replace(" DUS ", " dus ");

      text = text.replace(" Do ", " do ");
      text = text.replace(" Da ", " da ");
      text = text.replace(" De ", " de ");
      text = text.replace(" Di ", " di ");
      text = text.replace(" Du ", " du ");

      text = text.replace(" Dos ", " dos ");
      text = text.replace(" Das ", " das ");
      text = text.replace(" Des ", " des ");
      text = text.replace(" Dis ", " dis ");
      text = text.replace(" Dus ", " dus ");

      text = text.replace("Br-", "BR-");
      text = text.replace("Ac-", "AC-");
      text = text.replace("Al-", "AL-");
      text = text.replace("Am-", "AM-");
      text = text.replace("Ap-", "AP-");
      text = text.replace("Ba-", "BA-");
      text = text.replace("Ce-", "CE-");
      text = text.replace("Df-", "DF-");
      text = text.replace("Es-", "ES-");
      text = text.replace("Go-", "GO-");
      text = text.replace("Ma-", "MA-");
      text = text.replace("Mg-", "MG-");
      text = text.replace("Ms-", "MS-");
      text = text.replace("Mt-", "MT-");
      text = text.replace("Pa-", "PA-");
      text = text.replace("Pb-", "PB-");
      text = text.replace("Pe-", "PE-");
      text = text.replace("Pi-", "PI-");
      text = text.replace("Pr-", "PR-");
      text = text.replace("Rj-", "RJ-");
      text = text.replace("Rn-", "RN-");
      text = text.replace("Ro-", "RO-");
      text = text.replace("Rr-", "RR-");
      text = text.replace("Rs-", "RS-");
      text = text.replace("Sc-", "SC-");
      text = text.replace("Se-", "SE-");
      text = text.replace("Sp-", "SP-");
      text = text.replace("To-", "TO-");

      text = text.replace("Br ", "BR ");
      text = text.replace("Ac ", "AC ");
      text = text.replace("Al ", "AL ");
      text = text.replace("Am ", "AM ");
      text = text.replace("Ap ", "AP ");
      text = text.replace("Ba ", "BA ");
      text = text.replace("Ce ", "CE ");
      text = text.replace("Df ", "DF ");
      text = text.replace("Es ", "ES ");
      text = text.replace("Go ", "GO ");
      text = text.replace("Ma ", "MA ");
      text = text.replace("Mg ", "MG ");
      text = text.replace("Ms ", "MS ");
      text = text.replace("Mt ", "MT ");
      text = text.replace("Pa ", "PA ");
      text = text.replace("Pb ", "PB ");
      text = text.replace("Pe ", "PE ");
      text = text.replace("Pi ", "PI ");
      text = text.replace("Pr ", "PR ");
      text = text.replace("Rj ", "RJ ");
      text = text.replace("Rn ", "RN ");
      text = text.replace("Ro ", "RO ");
      text = text.replace("Rr ", "RR ");
      text = text.replace("Rs ", "RS ");
      text = text.replace("Sc ", "SC ");
      text = text.replace("Se ", "SE ");
      text = text.replace("Sp ", "SP ");
      text = text.replace("To ", "TO ");

      text = text.replace(" �", " �");
      text = text.replace(" �", " �");
      text = text.replace(" �", " �");
      text = text.replace(" �", " �");
      text = text.replace(" �", " �");

      text = text.replace(" E ", " e ");

      text = text.replace("'", "`");
    }

    if (text.contains("\\"))
      text = text.trim().substring(0, text.trim().indexOf('\\'));

    return text.trim();
  }

  public static String ConvertCountry(String country) {
    if (country.toUpperCase() == "BR")
      country = "Brasil";

    if (country.toUpperCase() == "AR")
      country = "Argentina";

    return country;
  }
}
TOP

Related Classes of com.lbslocal.api.utils.Common

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.