maia/.svn/pristine/5f/5f33e7e16933f47c6947f0138a1...

239 lines
9.1 KiB
Plaintext
Executable File
Raw Permalink Blame History

package com.fp.frontend.utility;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import com.fp.frontend.controller.armas.parametros.CentroControlArmasController;
import com.fp.frontend.controller.armas.parametros.TarmCentroControlJurController;
import com.fp.frontend.controller.pcustomer.PersonAddressController;
import com.fp.frontend.controller.pgeneral.gene.CatalogDetailController;
import com.fp.frontend.helper.MessageHelper;
import com.fp.persistence.parmas.param.TarmCentroControl;
import com.fp.persistence.parmas.param.TarmCentroControlJur;
import com.fp.persistence.pcustomer.gene.TcustPersonAddress;
import com.fp.persistence.pgeneral.gene.TgeneCatalogDetail;
import com.fp.persistence.pgeneral.safe.TsafeUserDetail;
@ManagedBean
@ViewScoped
public class Utilidades {
/**
* Metodo para formatear los decimales que se encuentran dentro de un texto
* @param texto
* @return
*/
public static String formatearDecimalesEnTexto(String texto, String formato){//.177
//NOTA:Se decide cambiar y no formatear los calibres como definicion de ultimo momento
/* DecimalFormat fmt = new DecimalFormat(formato);
String patron ="(\\w+)?([0-9]+)?[\\.\\,]([0-9]+)(\\w+)?";
Pattern p = Pattern.compile(patron);
Matcher m = p.matcher(texto);
String textoReemplazo=texto;
while(m.find()) {
textoReemplazo = m.replaceFirst( (m.group(1)==null?"": m.group(1)) + fmt.format(Double.valueOf(m.group(2)!=null?m.group(2):"0" + "." +m.group(3))) + (m.group(4)!=null?m.group(4):""));
}
return textoReemplazo.replace(",", "."); */
return texto;
}
public static Timestamp getFechaExpiracion() {
Timestamp fechaExpiracion = null;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2999);
calendar.set(Calendar.MONTH, 11);
calendar.set(Calendar.DATE, 31);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
fechaExpiracion = new Timestamp(calendar.getTimeInMillis());
return fechaExpiracion;
}
/**
* Metodo para armar el formato de una fecha sin hora
* @param fechaCldr fecha en formato calendar
* @return
*/
public static String armarFormatoFechaSinHora(Calendar fechaCldr){
return (new SimpleDateFormat("dd-MM-yyyy")).format(fechaCldr.getTime());
}
/**
* Metodo para armar el formato de una fecha sin hora
* @param fechaCldr fecha en formato Date
* @return
*/
public static String armarFormatoFechaSinHora(Date fechaCldr){
return (new SimpleDateFormat("dd-MM-yyyy")).format(fechaCldr);
}
/**
* Metodo para transformar el formato de una fecha con hora
* @param fechaCldr
* @return
*/
public static String armarFormatoFechaConHora(Calendar fechaCldr){
return (new SimpleDateFormat("dd-MM-yyyy HH:mm:ss")).format(fechaCldr.getTime());
}
/* Reemplazo un string los caracteres en blanco y reemplazo caracteres especiales
* @param toConvert
* @return la cadena convertida
*/
public static String obtieneNombreLimpio(String toConvert){
return StringUtils.replaceEach(StringUtils.deleteWhitespace(toConvert),
new String[]{"\u00e1","\u00e9","\u00ed","\u00f3","\u00fa","\u00c1","\u00c9","\u00cd","\u00d3","\u00da","\u00F1","\u00d1"},
new String[]{"a","e","i","o","u","A","E","I","O","U","n","N"});
}
/**
* recorta el nombre
* @param nombre
* @return
*/
public static String recorteNombre(String nombre){
String fielname=nombre;
if(fielname.length()>30){
fielname=fielname.substring(0,30)+extencionArchivo(nombre);
}
return fielname;
}
/**
* obtiene la extencion del archivo
* @param nombre
* @return
*/
private static String extencionArchivo(String nombre){
int pos=nombre.lastIndexOf('.');
nombre=nombre.substring(pos);
return nombre;
}
/**
* Método que revisa si una cadena tiene caracteres especiales
* @param nombre
* @return
* @throws Exception
*/
public static String caractersEspeciales(String nombre) throws Exception{
List<TgeneCatalogDetail> caracteresEsp=CatalogDetailController.find("CARACTERESPECIAL");
for(TgeneCatalogDetail caract:caracteresEsp){
if(nombre.contains(caract.getPk().getCatalog())){
throw new Exception(MsgControlArmas.getProperty("msg_error_elnombredelarchivonocaracteresespeciales")+": "+caract.getPk().getCatalog());
}
}
String numero="1234567890";
if(numero.contains(nombre.substring(0, 1))){
throw new Exception(MsgControlArmas.getProperty("msg_error_elnombredelarchivonodebeiniciarconnum"));
}
if(nombre!=null && nombre.trim().contains(" ")){
throw new Exception(MsgControlArmas.getProperty("msg_error_elnombredelarchivonodebecontenerespaciosenblanco"));
}
if(nombre.length()<9){
nombre="DOCUMENTO-"+nombre;
// throw new Exception(MsgControlArmas.getProperty("msg_error_elnombredelarchivonodebesermenoracincoletras"));
}
return nombre;
}
/**
* Funcion que reemplaza acentos y caracteres especiales de una cadena de texto
* @param input
* @return cadena de texto limpia de acentos y caracteres especiales.
*/
public static String reemplazaAcentosCaractEspeciales(String input) {
// Cadena de caracteres original a sustituir.
String original = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>u<EFBFBD><75><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
// Cadena de caracteres ASCII que reemplazar<61>n los originales.
String ascii = "aaaeeeiiiooouuunAAAEEEIIIOOOUUUNcC";
String output = input;
for (int i=0; i<original.length(); i++) {
// Reemplazamos los caracteres especiales.
output = output.replace(original.charAt(i), ascii.charAt(i));
}
output=output.replace("-", " ").replace("*", " ").replace("(", " ").replace(")", " ").replace("_", " ").replace("^", " ").replace("@", " ").replace("/", " ");
output=output.replace(",", "").replace("$", " ").replace("%", " ").replace("<22>", " ").replace("?", " "); //.replace("&", " ")
return output;
}
/**
* M&eacute;todo que revisa si una cadena tiene el signo %
* @param nombre
* @throws Exception
*/
public static void caracterEspecial(String nombre) throws Exception{
if(nombre.contains("%")){
throw new Exception(MsgControlArmas.getProperty("msg_error_ingresenumsolicitudexacto"));
}
}
/**
* Metodo para setear una celda de excell
* @param celda
* @return la celda seteada como String
*/
public static String setearValorCelda(Cell celda){
if(celda==null){
return null;
}
if(celda.getCellType()==Cell.CELL_TYPE_NUMERIC){
Double datoNumerico=celda.getNumericCellValue();
return datoNumerico.toString();
}else if(celda.getCellType()==Cell.CELL_TYPE_STRING){
return celda.getStringCellValue();
}
return null;
}
/**
* M&eacute;todo para obtener el centro de control
* @throws Exception
*/
public static TarmCentroControl obtenerCentroControl() throws Exception {
TarmCentroControl centroControl=null;
TsafeUserDetail tsafeUserDetail = (TsafeUserDetail) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("TSAFEUSERDETAIL");
PersonAddressController personAddressController = new PersonAddressController();
TcustPersonAddress tcustPersonAddres=personAddressController.findPrincipal(tsafeUserDetail.getPk().getPersoncode().toString());
if(tcustPersonAddres!=null && tcustPersonAddres.getProvincecode()!=null){
//cprovincia=tcustPersonAddres.getProvincecode();
TarmCentroControlJur centroControlJur= TarmCentroControlJurController.findxProvincia(tcustPersonAddres.getProvincecode());
if(centroControlJur!=null && centroControlJur.getCcentrocontrol()!=null){
centroControl= CentroControlArmasController.findPorCodigo(centroControlJur.getCcentrocontrol());
}else{
MessageHelper.setMessageError(MsgControlArmas.getProperty("msg_error_laprovinciasinccontrol"));
return null;
}
}else{
MessageHelper.setMessageError(MsgControlArmas.getProperty("msg_error_notienedireccionprincipal"));
return null;
}
return centroControl;
}
}