Java tutorial
/* * Este programa es software libre; usted puede redistribuirlo y/o modificarlo bajo los trminos * de la licencia "GNU General Public License" publicada por la Fundacin "Free Software Foundation". * Este programa se distribuye con la esperanza de que pueda ser til, pero SIN NINGUNA GARANTIA; * vea la licencia "GNU General Public License" para obtener mas informacin. */ package ips1ap101.lib.core.jsf; import com.sun.data.provider.RowKey; import com.sun.webui.jsf.component.Breadcrumbs; import com.sun.webui.jsf.component.Calendar; import com.sun.webui.jsf.component.DropDown; import com.sun.webui.jsf.component.Field; import com.sun.webui.jsf.component.Form; import com.sun.webui.jsf.component.Hyperlink; import com.sun.webui.jsf.component.Page; import com.sun.webui.jsf.component.Selector; import com.sun.webui.jsf.component.StaticText; import com.sun.webui.jsf.component.TabSet; import com.sun.webui.jsf.component.TextArea; import com.sun.webui.jsf.component.TextField; import com.sun.webui.jsf.component.Tree; import com.sun.webui.jsf.component.TreeNode; import com.sun.webui.jsf.component.Upload; import com.sun.webui.jsf.component.WebuiInput; import com.sun.webui.jsf.model.Option; import com.sun.webui.jsf.model.UploadedFile; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.security.Principal; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.MissingResourceException; import java.util.ResourceBundle; import javax.el.MethodExpression; import javax.faces.application.FacesMessage; import javax.faces.component.UIComponent; import javax.faces.component.UIViewRoot; import javax.faces.component.html.HtmlPanelGrid; import javax.faces.context.FacesContext; import javax.faces.validator.ValidatorException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.servlet.http.Part; import ips1ap101.lib.base.app.constant.URX; import ips1ap101.lib.base.bundle.BundleWebui; import ips1ap101.lib.base.constant.CBM; import ips1ap101.lib.base.constant.CPP; import ips1ap101.lib.base.interfaces.RecursoCodificable; import ips1ap101.lib.base.interfaces.RecursoEnumerable; import ips1ap101.lib.base.interfaces.RecursoIdentificable; import ips1ap101.lib.base.interfaces.RecursoNombrable; import ips1ap101.lib.base.list.ListOption; import ips1ap101.lib.base.list.ListOptions; import ips1ap101.lib.base.util.BitUtils; import ips1ap101.lib.base.util.LongUtils; import ips1ap101.lib.base.util.StrUtils; import ips1ap101.lib.core.app.Bitacora; import ips1ap101.lib.core.app.ListaParametros; import ips1ap101.lib.core.app.TLC; import ips1ap101.lib.core.constant.Global; import ips1ap101.lib.core.control.Auditor; import ips1ap101.lib.core.db.util.DB; import ips1ap101.lib.core.db.xdp.RecursoCachedRowSetDataProvider; import ips1ap101.lib.core.enumeration.EnumColumnaEtiqueta; import ips1ap101.lib.core.enumeration.EnumOpcionAbrirVentana; import ips1ap101.lib.core.enumeration.EnumOpcionUpload; import ips1ap101.lib.core.jsf.component.CampoArchivo; import ips1ap101.lib.core.jsf.component.CampoArchivoMultiPart; import ips1ap101.lib.core.jsf.component.ComponenteAlternativo; import ips1ap101.lib.core.jsf.component.ComponenteComplementario; import ips1ap101.lib.core.jsf.component.Etiqueta; import ips1ap101.lib.core.jsf.component.TextoEstaticoAlternativo; import ips1ap101.lib.core.util.JS; import ips1ap101.lib.core.util.STP; import ips1ap101.lib.core.util.Utils; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; /** * @author Jorge Campins */ public class JSF { private static final String REDIRECT = "redirect.html?url=http://localhost:8080/"; private static final String WAR = "double-u--a--ar"; private static final String FACES = "/faces/"; // <editor-fold defaultstate="collapsed" desc="metodos para el manejo de archivos adjuntos "> public static void upload(Upload upload) throws Exception { upload(upload, null); } public static void upload(Upload upload, String carpeta) throws Exception { upload(upload, carpeta, EnumOpcionUpload.FILA_Y_ARCHIVO); } public static void upload(Upload upload, String carpeta, EnumOpcionUpload opcion) throws Exception { Bitacora.trace(JSF.class, "upload", upload, carpeta, opcion); if (upload == null) { return; } CampoArchivo campoArchivo = upload instanceof CampoArchivo ? (CampoArchivo) upload : null; if (campoArchivo != null) { campoArchivo.setClientFileName(null); campoArchivo.setServerFileName(null); } UploadedFile uploadedFile = upload.getUploadedFile(); if (uploadedFile == null) { return; } /**/ Bitacora.trace(JSF.class, "upload", "name=" + uploadedFile.getOriginalName()); Bitacora.trace(JSF.class, "upload", "path=" + uploadedFile.getClientFilePath()); Bitacora.trace(JSF.class, "upload", "type=" + uploadedFile.getContentType()); Bitacora.trace(JSF.class, "upload", "size=" + uploadedFile.getSize()); /**/ if (uploadedFile.getSize() == 0) { return; } /**/ String sep = System.getProperties().getProperty("file.separator"); String validChars = StrUtils.VALID_CHARS; String filepath = null; if (STP.esIdentificadorArchivoValido(carpeta)) { filepath = carpeta.replace(".", sep); } long id = LongUtils.getNewId(); // String filename = STP.getRandomString(); String filename = id + ""; String pathname = Utils.getAttachedFilesDir(filepath) + filename; String ext = Utils.getExtensionArchivo(uploadedFile.getOriginalName()); if (StringUtils.isNotBlank(ext)) { String str = ext.toLowerCase(); if (StringUtils.containsOnly(str, validChars)) { filename += "." + str; pathname += "." + str; } } // if (!EnumOpcionUpload.FILA.equals(opcion)) { // File file = new File(pathname); // uploadedFile.write(file); // } int bytesRead; int bufferSize = 8192; byte[] bytes = null; byte[] buffer = new byte[bufferSize]; try (InputStream input = uploadedFile.getInputStream(); OutputStream output = new FileOutputStream(pathname)) { while ((bytesRead = input.read(buffer)) != -1) { if (!EnumOpcionUpload.FILA.equals(opcion)) { output.write(buffer, 0, bytesRead); } if (!EnumOpcionUpload.ARCHIVO.equals(opcion)) { if (bytesRead < bufferSize) { bytes = ArrayUtils.addAll(bytes, ArrayUtils.subarray(buffer, 0, bytesRead)); } else { bytes = ArrayUtils.addAll(bytes, buffer); } } } } /**/ String clientFilePath = uploadedFile.getClientFilePath(); String originalName = uploadedFile.getOriginalName(); String clientFileName = clientFilePath == null ? originalName : clientFilePath + originalName; String serverFileName = Utils.getWebServerRelativePath(pathname); if (!EnumOpcionUpload.ARCHIVO.equals(opcion)) { String contentType = uploadedFile.getContentType(); long size = uploadedFile.getSize(); // byte[] bytes = uploadedFile.getBytes(); insert(id, clientFileName, serverFileName, contentType, size, bytes); } if (campoArchivo != null) { campoArchivo.setClientFileName(clientFileName); campoArchivo.setServerFileName(serverFileName); } } public static CampoArchivoMultiPart upload(Part part) throws Exception { return upload(part, null); } public static CampoArchivoMultiPart upload(Part part, String carpeta) throws Exception { return upload(part, carpeta, EnumOpcionUpload.FILA_Y_ARCHIVO); } public static CampoArchivoMultiPart upload(Part part, String carpeta, EnumOpcionUpload opcion) throws Exception { Bitacora.trace(JSF.class, "upload", part, carpeta, opcion); if (part == null) { return null; } if (part.getSize() == 0) { return null; } String originalName = getPartFileName(part); if (StringUtils.isBlank(originalName)) { return null; } CampoArchivoMultiPart campoArchivo = new CampoArchivoMultiPart(); campoArchivo.setPart(part); campoArchivo.setClientFileName(null); campoArchivo.setServerFileName(null); /**/ Bitacora.trace(JSF.class, "upload", "name=" + part.getName()); Bitacora.trace(JSF.class, "upload", "type=" + part.getContentType()); Bitacora.trace(JSF.class, "upload", "size=" + part.getSize()); /**/ String sep = System.getProperties().getProperty("file.separator"); String validChars = StrUtils.VALID_CHARS; String filepath = null; if (STP.esIdentificadorArchivoValido(carpeta)) { filepath = carpeta.replace(".", sep); } long id = LongUtils.getNewId(); // String filename = STP.getRandomString(); String filename = id + ""; String pathname = Utils.getAttachedFilesDir(filepath) + filename; String ext = Utils.getExtensionArchivo(originalName); if (StringUtils.isNotBlank(ext)) { String str = ext.toLowerCase(); if (StringUtils.containsOnly(str, validChars)) { filename += "." + str; pathname += "." + str; } } /**/ // part.write(pathname); /**/ // OutputStream outputStream = new FileOutputStream(pathname); // outputStream.write(IOUtils.readFully(part.getInputStream(), -1, false)); /**/ int bytesRead; int bufferSize = 8192; byte[] bytes = null; byte[] buffer = new byte[bufferSize]; try (InputStream input = part.getInputStream(); OutputStream output = new FileOutputStream(pathname)) { while ((bytesRead = input.read(buffer)) != -1) { if (!EnumOpcionUpload.FILA.equals(opcion)) { output.write(buffer, 0, bytesRead); } if (!EnumOpcionUpload.ARCHIVO.equals(opcion)) { if (bytesRead < bufferSize) { bytes = ArrayUtils.addAll(bytes, ArrayUtils.subarray(buffer, 0, bytesRead)); } else { bytes = ArrayUtils.addAll(bytes, buffer); } } } } /**/ String clientFilePath = null; String clientFileName = clientFilePath == null ? originalName : clientFilePath + originalName; String serverFileName = Utils.getWebServerRelativePath(pathname); if (!EnumOpcionUpload.ARCHIVO.equals(opcion)) { String contentType = part.getContentType(); long size = part.getSize(); insert(id, clientFileName, serverFileName, contentType, size, bytes); } campoArchivo.setClientFileName(clientFileName); campoArchivo.setServerFileName(serverFileName); return campoArchivo; } private static long INPUT_FILE_MIN_SIZE = 1; public static void validateInputFile(Part file, String label, long maxSize) { validateInputFile(file, label, maxSize, null); } public static void validateInputFile(Part file, String label, long maxSize, String[] validTypes) { long size; String type; String summary; FacesMessage message; List<FacesMessage> messages = new ArrayList<>(); size = file.getSize(); type = file.getContentType(); if (size < INPUT_FILE_MIN_SIZE) { summary = Bitacora.getTextoMensaje(CBM.UPLOAD_ROW_EXCEPTION_1, label, INPUT_FILE_MIN_SIZE); message = new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, null); messages.add(message); } if (size > maxSize) { summary = Bitacora.getTextoMensaje(CBM.UPLOAD_ROW_EXCEPTION_2, label, maxSize); message = new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, null); messages.add(message); } if (validTypes != null && validTypes.length > 0) { if (StringUtils.startsWithAny(type, validTypes)) { } else { summary = Bitacora.getTextoMensaje(CBM.UPLOAD_ROW_EXCEPTION_3, label, type); message = new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, null); messages.add(message); } } if (messages.isEmpty()) { } else { throw new ValidatorException(messages); } } public static boolean isValidPart(Object value) { if (value instanceof Part) { Part part = (Part) value; return part.getSize() > 0 && StringUtils.isNotBlank(getPartFileName(part)); } return false; } private static String getPartFileName(Part part) { for (String cd : part.getHeader("content-disposition").split(";")) { if (cd.trim().startsWith("filename")) { String filename = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", ""); return filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\\') + 1); // MSIE fix. } } return null; } private static void insert(long id, String clientFileName, String serverFileName, String contentType, long size, byte[] bytes) throws Exception { Bitacora.trace(Auditor.class, "insert", clientFileName, serverFileName, contentType, size); Object[] args; if (bytes != null && bytes.length > 0) { if (TLC.getAgenteSql().connected()) { if (TLC.getAgenteSql().isStoredProcedure(DB.ARCHIVO_ADJUNTO_INSERT)) { args = new Object[9]; args[0] = id; args[1] = clientFileName; args[2] = serverFileName; args[3] = TLC.getUsuarioActual().getIdUsuario(); args[4] = TLC.getUsuarioActual().getCodigoUsuario(); args[5] = TLC.getUsuarioActual().getNombreUsuario(); args[6] = contentType; args[7] = size; args[8] = bytes; TLC.getAgenteSql().executeProcedure(DB.ARCHIVO_ADJUNTO_INSERT, args); } } } } // </editor-fold> // <editor-fold defaultstate="collapsed" desc="metodos para establecer propiedades de componentes visuales "> public static Form getForm(UIComponent uic) { UIComponent parent = uic.getParent(); while (parent != null && parent != uic && !(parent instanceof Form)) { parent = parent.getParent(); } return parent instanceof Form ? (Form) parent : null; } public static Page getPage(UIComponent uic) { UIComponent parent = uic.getParent(); while (parent != null && parent != uic && !(parent instanceof Page)) { parent = parent.getParent(); } return parent instanceof Page ? (Page) parent : null; } public static WebuiInput getWebuiInput(UIComponent uic) { return getWebuiInput(uic, getForm(uic)); } public static WebuiInput getWebuiInput(UIComponent uic, UIComponent parent) { if (parent != null) { WebuiInput webuiInput; List<UIComponent> children = parent.getChildren(); for (UIComponent child : children) { if (child instanceof WebuiInput) { if (child.getId() == null || uic.getId().equals(child.getId())) { } else if (uic instanceof Etiqueta && child.getId().equals(((Etiqueta) uic).getFor(true))) { //Bitacora.trace(uic.getClass().getName() + "$" + uic.getId() + ".webuiInput=" + child.getId()); return (WebuiInput) child; } else if (child instanceof TabSet) { webuiInput = getWebuiInput(uic, child); if (webuiInput != null) { return webuiInput; } } else if (uic.getId().startsWith(child.getId())) { //Bitacora.trace(uic.getClass().getName() + "$" + uic.getId() + ".webuiInput=" + child.getId()); return (WebuiInput) child; } } else { webuiInput = getWebuiInput(uic, child); if (webuiInput != null) { return webuiInput; } } } } return null; } public static TextoEstaticoAlternativo getTextoEstaticoAlternativo(UIComponent uic) { return getTextoEstaticoAlternativo(uic, getForm(uic)); } public static TextoEstaticoAlternativo getTextoEstaticoAlternativo(UIComponent uic, UIComponent parent) { if (parent != null) { TextoEstaticoAlternativo tea; List<UIComponent> children = parent.getChildren(); for (UIComponent child : children) { if (child instanceof TextoEstaticoAlternativo) { if (child.getId() == null || uic.getId().equals(child.getId())) { } else if (child.getId().startsWith(uic.getId())) { //Bitacora.trace(uic.getClass().getName() + "$" + uic.getId() + ".TextoEstaticoAlternativo=" + child.getId()); return (TextoEstaticoAlternativo) child; } } else { tea = getTextoEstaticoAlternativo(uic, child); if (tea != null) { return tea; } } } } return null; } public static ComponenteAlternativo getComponenteAlternativo(UIComponent uic) { return getComponenteAlternativo(uic, getForm(uic)); } public static ComponenteAlternativo getComponenteAlternativo(UIComponent uic, UIComponent parent) { if (parent != null) { ComponenteAlternativo ca; List<UIComponent> children = parent.getChildren(); for (UIComponent child : children) { if (child instanceof ComponenteAlternativo) { if (child.getId() == null || uic.getId().equals(child.getId())) { } else if (child.getId().startsWith(uic.getId())) { //Bitacora.trace(uic.getClass().getName() + "$" + uic.getId() + ".ComponenteAlternativo=" + child.getId()); return (ComponenteAlternativo) child; } } else { ca = getComponenteAlternativo(uic, child); if (ca != null) { return ca; } } } } return null; } public static ComponenteComplementario getComponenteComplementario(UIComponent uic) { return getComponenteComplementario(uic, getForm(uic)); } public static ComponenteComplementario getComponenteComplementario(UIComponent uic, UIComponent parent) { if (parent != null) { ComponenteComplementario cc; List<UIComponent> children = parent.getChildren(); for (UIComponent child : children) { if (child instanceof ComponenteComplementario) { if (child.getId() == null || uic.getId().equals(child.getId())) { } else if (child.getId().startsWith(uic.getId())) { //Bitacora.trace(uic.getClass().getName() + "$" + uic.getId() + ".ComponenteComplementario=" + child.getId()); return (ComponenteComplementario) child; } } else { cc = getComponenteComplementario(uic, child); if (cc != null) { return cc; } } } } return null; } public static String getWebuiString(UIComponent uic, String key) { // Bitacora.trace(JSF.class, "getWebuiString-1", uic, key); // FacesContext fc = FacesContext.getCurrentInstance(); // ResourceBundle rb = fc.getApplication().getResourceBundle(fc, "bundle"); String x = getPageBeanSimpleName(); String y = StringUtils.trimToEmpty(uic.getId()); String z = StringUtils.isBlank(key) ? "" : "_" + key; String k = x + y + z; if (StringUtils.isNotBlank(k)) { // String v = getWebuiString(rb, k); String v = BundleWebui.getTrimmedToNullString(k); // <editor-fold defaultstate="collapsed"> // if (v == null) { // k = y + z; // v = getWebuiString(rb, k); // } // </editor-fold> return v; } return null; } private static String getPageBeanSimpleName() { FacesContext fc = FacesContext.getCurrentInstance(); UIViewRoot uivr = fc.getViewRoot(); String value = uivr.getViewId(); if (StringUtils.isNotBlank(value)) { value = value.trim(); int i = value.lastIndexOf('/') + 1; int j = value.indexOf(JS.JSP_SUFFIX); if (i > 0 && j > i) { return value.substring(i, j) + '_'; } } return ""; } public static String getWebuiString(String key) { return getWebuiString(key, null); } public static String getWebuiString(String key, String altkey) { return getWebuiString(key, altkey, null); } public static String getWebuiString(String key, String altkey, String styles) { // Bitacora.trace(JSF.class, "getWebuiString-2", key, altkey, styles); String k = StringUtils.trimToEmpty(key); if (StringUtils.isNotBlank(k)) { // String v = getWebuiString(BundleWebui.getResourceBundle(), k); String v = BundleWebui.getTrimmedToNullString(k); // <editor-fold defaultstate="collapsed"> // if (v == null) { // String ak = StringUtils.trimToEmpty(altkey); // int l = ak.length() - 1; // if (l > 0) { // ak = ak.substring(0, l).trim(); // ak = STP.getHumplessCase(ak); // String w = ak.startsWith("id_") ? "id" : ak.startsWith("numero_") ? "numero" : ""; // l = w.length(); // if (l > 0) { // String s = StringUtils.trimToEmpty(styles); // String c = "pdq-estilo-id-2"; // if (s.contains(c)) { // ak = "nombre" + ak.substring(l); ////------------------------v = getWebuiString(BundleParametros.getResourceBundle(), ak); // } // } ////----------------v = getWebuiString(BundleParametros.getResourceBundle(), ak); // } // } // </editor-fold> return fix(v); } return null; } private static String getWebuiString(ResourceBundle resourceBundle, String key) { // Bitacora.trace(JSF.class, "getWebuiString-3", key); try { return resourceBundle.getString(key); } catch (MissingResourceException e) { return null; } } private static String fix(String s) { if (StringUtils.isBlank(s)) { return s; } s = " " + s + " "; s = s.replace(" Ano ", " Ao "); s = s.replace(" ano ", " ao "); s = s.replace(" Anos ", " Aos "); s = s.replace(" anos ", " aos "); return s.trim(); } private static final String BLANK_LABEL = " "; public static void fixBlankLabels(UIComponent uic) { if (uic != null) { Field field; Selector selector; List<UIComponent> children = uic.getChildren(); for (UIComponent child : children) { if (child instanceof Field) { field = (Field) child; if (field.isDisabled() || field.isReadOnly() || StringUtils.isNotBlank(field.getLabel())) { } else { field.setLabel(BLANK_LABEL); } } else if (child instanceof Selector) { selector = (Selector) child; if (selector.isDisabled() || StringUtils.isNotBlank(selector.getLabel())) { } else { selector.setLabel(BLANK_LABEL); } } else { fixBlankLabels(child); } } } } public static String getFirstClientIdWithMessages() { FacesContext facesContext = FacesContext.getCurrentInstance(); Iterator<String> messageClientIds = facesContext.getClientIdsWithMessages(); String clientId; while (messageClientIds.hasNext()) { clientId = messageClientIds.next(); if (clientId != null) { return clientId; } } return null; } public static void fixFacesMessages() { FacesContext facesContext = FacesContext.getCurrentInstance(); UIViewRoot uivr = facesContext.getViewRoot(); Iterator<String> messageClientIds = facesContext.getClientIdsWithMessages(); String clientId; UIComponent uic; Iterator<FacesMessage> messages; FacesMessage message; String detail; String summary; String detalle; String resumen; while (messageClientIds.hasNext()) { clientId = messageClientIds.next(); if (clientId != null) { uic = uivr == null ? null : uivr.findComponent(clientId); messages = facesContext.getMessages(clientId); while (messages.hasNext()) { message = messages.next(); // Get the message detail detail = message.getDetail(); summary = message.getSummary(); // translate //Bitacora.trace("detalle(" + clientId + ")" + detail); //Bitacora.trace("resumen(" + clientId + ")" + summary); detalle = getMensaje(uic, detail); resumen = getMensaje(uic, summary); // Set your own message message.setDetail(detalle); message.setSummary(resumen); } } } } private static String getMensaje(UIComponent uic, String message) { FacesContext facesContext = FacesContext.getCurrentInstance(); String mensaje = StringUtils.trimToEmpty(message); /* * */ if (uic != null) { mensaje = Utils.trimPrefix(mensaje, uic.getClientId(facesContext)); mensaje = Utils.trimPrefix(mensaje, uic.getId()); } mensaje = Utils.trimPrefix(mensaje, ": "); mensaje = Utils.trimPrefix(mensaje, "Validation Error: "); mensaje = Utils.trimPrefix(mensaje, "Error de Validacin: "); /* * */ mensaje = Utils.trimSuffix(mensaje, "Example:"); mensaje = Utils.trimSuffix(mensaje, "Ejemplo:"); /* * */ mensaje = mensaje.replace("is not a number", "debe ser un nmero"); mensaje = mensaje.replace("must be a number consisting of one or more digits", "debe ser un nmero"); mensaje = mensaje.replace("must be a number between", "debe ser un nmero entre"); mensaje = mensaje.replace("Specified attribute is not between the expected values of", "El valor debe estar comprendido entre"); mensaje = mensaje.replace(" and ", " y "); /* * */ mensaje = mensaje.replace("Valor es necesario", "El valor es necesario"); /* * */ String sujeto = uic instanceof TextArea ? "La longitud del valor" : "El valor"; mensaje = mensaje.replace("Valor es ms grande de valor de mximo permitido:", sujeto + " debe ser menor o igual que"); mensaje = mensaje.replace("Valor is menos de valor de mnimo permitido:", sujeto + " debe ser mayor o igual que"); /* * */ return mensaje; } // </editor-fold> // <editor-fold defaultstate="collapsed" desc="metodos para establecer propiedades de componentes visuales "> public static void set(HtmlPanelGrid[] htmlPanelGrids, boolean rendered) { for (HtmlPanelGrid htmlPanelGrid : htmlPanelGrids) { set(htmlPanelGrid, rendered); } } public static void set(HtmlPanelGrid htmlPanelGrid, boolean rendered) { htmlPanelGrid.setRendered(rendered); } public static void set(TextField textField, boolean readOnly, boolean rendered) { if (textField != null) { textField.setReadOnly(readOnly); textField.setRendered(rendered); } } public static void set(Calendar calendar, StaticText staticText, boolean readOnly, boolean rendered) { setRendered(calendar, staticText, readOnly, rendered); setText(calendar, staticText, readOnly, rendered); } public static void set(DropDown dropDown, StaticText staticText, boolean readOnly, boolean rendered) { setRendered(dropDown, staticText, readOnly, rendered); setText(dropDown, staticText, readOnly, rendered); } private static void setRendered(WebuiInput webuiInput, StaticText staticText, boolean readOnly, boolean rendered) { boolean editable = !readOnly; if (webuiInput != null) { webuiInput.setRendered(rendered && editable); } if (staticText != null) { staticText.setRendered(rendered && readOnly); } } private static void setText(WebuiInput webuiInput, StaticText staticText, boolean readOnly, boolean rendered) { if (staticText != null) { String text = null; if (rendered && readOnly && webuiInput != null) { if (webuiInput instanceof Calendar) { Calendar calendar = (Calendar) webuiInput; java.util.Date selected = calendar.getSelectedDate(); text = selected == null ? "selected == null" : STP.getString(selected); } if (webuiInput instanceof DropDown) { DropDown dropDown = (DropDown) webuiInput; Option selected = getOpcion(dropDown); text = selected == null ? "selected == null" : dropDown.getItems() == null ? "items == null" : selected.getLabel(); } } staticText.setText(text); } } // </editor-fold> // <editor-fold defaultstate="collapsed" desc="metodos para el manejo de ventanas "> public static String getCloseWindowJavaScript() { return JS.getCloseWindowJavaScript(); } public static String getOpenFileWindowJavaScript(String file) { return JS.getOpenFileWindowJavaScript(getFileURL(file)); } public static String getOpenURIWindowJavaScript(String uri) { return JS.getOpenFileWindowJavaScript(encodeURI(uri)); } public static String getOpenURLWindowJavaScript(String url) { return JS.getOpenFileWindowJavaScript(encodeURL(url)); } public static String getOpenURXWindowJavaScript(String urx) { return JS.getOpenFileWindowJavaScript(encodeURX(urx)); } public static String getOpenHelpWindowJavaScript(String pagina) { // FacesContext facesContext = FacesContext.getCurrentInstance(); // if (facesContext == null) { // return null; // } // String url = facesContext.getExternalContext().getRequestContextPath() + URX.AYUDA; // String rpi = facesContext.getExternalContext().getRequestPathInfo(); String url = URX.AYUDA; String rpl = null; // String htm; if (StringUtils.isBlank(pagina)) { // if (rpi != null) { // int j = rpi.lastIndexOf(JS.JSP_SUFFIX); // if (j > 0) { // rpl = "?jsp=" + rpi.substring(rpi.lastIndexOf("/", j) + 1, j); // } // } } else { rpl = "?jsp=" + pagina; // htm = BundlePaginas.getCodigoGrupoAplicacion(pagina); // if (StringUtils.isNotBlank(htm)) { //// rpl += "&htm=" + STP.getCamelCase('_' + htm); // rpl += "&htm=" + htm; // } } return JS.getOpenWindowJavaScript(url, rpl, Global.HELP_WINDOW_NAME); } public static String getOpenSearchWindowJavaScript(String urx, long domain, EnumOpcionAbrirVentana option, String field) { return getOpenSearchWindowJavaScript(urx, domain, option, field, null); } public static String getOpenSearchWindowJavaScript(String urx, long domain, EnumOpcionAbrirVentana option, String field, String button) { return getOpenSearchWindowJavaScript(urx, domain, option, field, button, null); } public static String getOpenSearchWindowJavaScript(String urx, long domain, EnumOpcionAbrirVentana option, String field, String button, ListaParametros rpl) { String url = getJSPURL(urx); String window = JS.getTargetWindowName(url); return JS.getOpenSearchWindowJavaScript(url, domain, option, field, button, rpl, window); } public static String getOpenSearchWindowJavaScript(String urx, long domain, String column, String field) { return getOpenSearchWindowJavaScript(urx, domain, column, field, null); } public static String getOpenSearchWindowJavaScript(String urx, long domain, String column, String field, String button) { return getOpenSearchWindowJavaScript(urx, domain, column, field, button, null); } public static String getOpenSearchWindowJavaScript(String urx, long domain, String column, String field, String button, ListaParametros rpl) { String url = getJSPURL(urx); String window = JS.getTargetWindowName(url); return JS.getOpenSearchWindowJavaScript(url, domain, column, field, button, rpl, window); } public static String getOpenDetailWindowJavaScript(String urx, String rpl) { String url = getJSPURL(urx); String window = JS.getTargetWindowName(url); return JS.getOpenWindowJavaScript(url, rpl, window); } public static String getOpenWindowJavaScript(String urx, String rpl) { return JS.getOpenWindowJavaScript(getJSPURL(urx), rpl); } public static String getFileURL(String relativePath) { if (StringUtils.isBlank(relativePath)) { return null; } String url = getCurrentServer(); if (url == null) { return null; } String slash = Utils.slash(relativePath); String start = slash.startsWith("/") ? "" : "/"; url += start + slash; return url; } public static String getFileExtension(String path) { String ext = Utils.getExtensionArchivo(path); if (StringUtils.isNotBlank(ext)) { String pathname = Utils.getServerAbsolutePath(path); File file = new File(pathname); if (file.isFile()) { return ext.toUpperCase(); } } return null; } public static String getRequestParameter(String key) { if (StringUtils.isBlank(key)) { return null; } // key = StringUtils.trimToEmpty(key); FacesContext facesContext = FacesContext.getCurrentInstance(); if (facesContext == null) { return null; } return ((HttpServletRequest) facesContext.getExternalContext().getRequest()).getParameter(key); } public static String getRequestURL() { FacesContext facesContext = FacesContext.getCurrentInstance(); if (facesContext == null) { return null; } HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest(); if (request == null) { return null; } return request.getRequestURL() == null ? null : StringUtils.trimToNull(request.getRequestURL().toString()); } public static String encodeURI(String uri) { if (StringUtils.isBlank(uri)) { return null; } String url = getCurrentServer(); if (url == null) { return null; } uri = StringUtils.trimToEmpty(uri); if (!uri.startsWith("/")) { url += "/"; } url += uri; return url; } public static String encodeURL(String url) { url = StringUtils.trimToEmpty(url); if (StringUtils.isBlank(url)) { return null; } else if (url.startsWith(JS.FILE_PREFIX)) { } else if (url.startsWith(JS.HTTP_PREFIX)) { } else if (url.startsWith(JS.HTTPS_PREFIX)) { } else { // url = JS.HTTP_PREFIX + url; return null; } // if(!url.endsWith("/")) url += "/"; return url; } public static String encodeURX(String urx) { if (StringUtils.isBlank(urx)) { return null; } urx = StringUtils.trimToEmpty(urx); return urx.startsWith("/") ? encodeURI(urx) : encodeURL(urx); } public static void redirect(String url) throws IOException { redirect(url, true); } public static void redirect(String url, boolean encode) throws IOException { Bitacora.trace(JSF.class, "redirect", "url=" + url, "encode=" + encode); if (StringUtils.isBlank(url)) { return; } FacesContext facesContext = FacesContext.getCurrentInstance(); if (facesContext == null) { return; } HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse(); url = StringUtils.trimToEmpty(url); url = encode ? response.encodeRedirectURL(url) : url; Bitacora.trace(url); try { response.sendRedirect(url); facesContext.responseComplete(); } catch (Exception ex) { // TODO: manejar el java.lang.IllegalStateException? Bitacora.logTrace(ex); } } public static void stamp(Object objeto, String metodo, String contexto, boolean request) { if (objeto != null) { String str = "<" + StringUtils.trimToEmpty(metodo) + StringUtils.trimToEmpty(contexto) + ">"; if (request) { String url = getRequestURL(); if (StringUtils.isNotBlank(url)) { str += " " + StringUtils.trimToEmpty(url); String reset = getRequestParameter(Global.PARAMETRO_CONDICION_SESION); if (StringUtils.isNotBlank(reset)) { str += "?" + Global.PARAMETRO_CONDICION_SESION + "=" + StringUtils.trimToEmpty(reset); } } } Bitacora.stamp(objeto, str); } } private static String getCurrentServer() { String url = getRequestURL(); int j = jota(url); return j > 0 ? url.substring(0, j) : null; } private static String getJSPURL(String urx) { if (StringUtils.isBlank(urx)) { return null; } urx = StringUtils.trimToEmpty(urx); String server = getJSPServer(urx); if (server == null) { return null; } String uri = getJSPURI(urx); if (uri == null) { return null; } return server + uri; } private static String getJSPServer(String urx) { if (StringUtils.isBlank(urx)) { return null; } urx = StringUtils.trimToEmpty(urx); if (urx.endsWith(JS.JSP_SUFFIX)) { urx = urx.startsWith("/") ? getRequestURL() : urx; int j = jota(urx); if (j > 0) { return urx.substring(0, j); } } return null; } private static String getJSPURI(String urx) { if (StringUtils.isBlank(urx)) { return null; } urx = StringUtils.trimToEmpty(urx); if (urx.endsWith(JS.JSP_SUFFIX)) { if (urx.startsWith("/")) { return urx; } int j = jota(urx); if (j > 0) { return urx.substring(j); } } return null; } private static int jota(String url) { if (StringUtils.isBlank(url)) { return -1; } url = StringUtils.trimToEmpty(url); if (url.startsWith(JS.HTTP_PREFIX) || url.startsWith(JS.HTTPS_PREFIX)) { int i = url.startsWith(JS.HTTP_PREFIX) ? JS.HTTP_PREFIX.length() : JS.HTTPS_PREFIX.length(); int j = url.indexOf("/", i); if (j > i) { return j; } } return -1; } // </editor-fold> // <editor-fold defaultstate="collapsed" desc="metodos para el manejo de cookies "> private static String getCookie(String key) { return getCookie(key, false); } private static String getCookie(String key, boolean qualified) { return qualified ? getCookie(key, 1) : getCookie(key, 2); } private static String getCookie(String key, int option) { Bitacora.trace(JSF.class, "getCookie", "key=" + key, "option=" + option); /* * 1 = busca con clave privada (cualificada) * 2 = busca con clave publica (no cualificada) * 3 = busca primero con clave privada y luego con clave publica * 4 = busca primero con clave publica y luego con clave privada */ FacesContext facesContext = FacesContext.getCurrentInstance(); String qualifiedKey = key + getRequestQualifier(); HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest(); Cookie[] cookie = request.getCookies(); String[] clave = new String[2]; switch (option) { case 1: clave[0] = qualifiedKey; clave[1] = null; break; case 2: clave[0] = key; clave[1] = null; break; case 3: clave[0] = qualifiedKey; clave[1] = key; break; case 4: clave[0] = key; clave[1] = qualifiedKey; break; default: return null; } for (int i = 0; i < cookie.length; i++) { for (int j = 0; j < clave.length; j++) { if ((clave[j] != null && cookie[i].getName().equals(clave[j]))) { return cookie[i].getValue(); } } } return null; } private static String putCookie(String key, String value) { return putCookie(key, value, false); } private static String putCookie(String key, String value, boolean qualified) { return putCookie(key, value, qualified, -1); } private static String putCookie(String key, String value, boolean qualified, int expiry) { Bitacora.trace(JSF.class, "putCookie", "key=" + key, "value=" + value, "qualified=" + qualified, "expiry=" + expiry); FacesContext facesContext = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse(); String qualifiedKey = key + getRequestQualifier(); String name = qualified ? qualifiedKey : key; Cookie cookie = new Cookie(name, value); /* * maximum age of the cookie in seconds; if negative, the cookie is not stored; if zero, deletes the cookie. */ cookie.setMaxAge(expiry); response.addCookie(cookie); return name; } private static String deleteCookie(String key) { Bitacora.trace(JSF.class, "deleteCookie", "key=" + key); return getCookie(key) == null ? null : putCookie(key, "", false, 0); } private static String getRequestQualifier() { FacesContext facesContext = FacesContext.getCurrentInstance(); HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest(); String qualifier = request.getContextPath(); // request.getRequestURI().replace("/faces", StringUtils.EMPTY); return qualifier; } private static String getRequestValue(String key, int cookies) { String value = getRequestParameter(key); if (value == null && cookies != 0) { value = getCookie(key, cookies); } return value; } // </editor-fold> // <editor-fold defaultstate="collapsed" desc="metodos para el manejo de sesin "> public static HttpServletRequest getRequest() { FacesContext fc = FacesContext.getCurrentInstance(); return fc != null && fc.getExternalContext() != null && fc.getExternalContext().getRequest() instanceof HttpServletRequest ? (HttpServletRequest) fc.getExternalContext().getRequest() : null; } public static HttpSession getSession() { HttpServletRequest request = getRequest(); return request == null ? null : request.getSession(false); } public static Principal getPrincipal() { HttpServletRequest request = getRequest(); return request == null ? null : request.getUserPrincipal(); } // </editor-fold> // <editor-fold defaultstate="collapsed" desc="metodos para el manejo de listas desplegables (DropDown) "> public static Option getOpcion(DropDown list) { Option option = null; if (list != null) { option = getOpcion(list.getSelected(), list.getItems()); } return option; } public static Option getOpcion(Object value, Object items) { Option option = null; if (value != null && items != null) { if (items instanceof List) { Iterator iterator = ((List) items).iterator(); Object optionObject; Object valueOption; while (iterator.hasNext() && option == null) { optionObject = iterator.next(); if (optionObject != null && optionObject instanceof Option) { valueOption = ((Option) optionObject).getValue(); if (valueOption != null && value.equals(valueOption)) { option = (Option) optionObject; } } } } else { if (items instanceof Option[]) { Option[] options = ((Option[]) items); Object valueOption; for (int i = 0; i < options.length && option == null; i++) { valueOption = options[i].getValue(); if (valueOption != null && value.equals(valueOption)) { option = options[i]; } } } } } return option; } public static ArrayList getListaOpciones(RecursoCachedRowSetDataProvider dp, boolean anulable, String columna, boolean ordenado) { return getListaOpciones(dp, anulable, columna, EnumColumnaEtiqueta.AMBAS_COLUMNAS, ordenado); } public static ArrayList getListaOpciones(RecursoCachedRowSetDataProvider dp, boolean anulable, EnumColumnaEtiqueta columna, boolean ordenado) { return getListaOpciones(dp, anulable, null, columna, ordenado); } private static ArrayList getListaOpciones(RecursoCachedRowSetDataProvider dp, boolean anulable, String colname, EnumColumnaEtiqueta coltype, boolean ordenado) { Object value; String label; String description; ListOptions list = new ListOptions(anulable, ordenado); if (dp.cursorFirst()) { do { if (dp.isRecursoEnumerable()) { value = dp.getRecursoEnumerableDataProvider().getNumeroRecurso(); } else if (dp.isRecursoIdentificable()) { value = dp.getRecursoIdentificableDataProvider().getIdentificacionRecurso(); } else { value = null; } if (value != null) { if (StringUtils.isBlank(colname)) { label = getRowLabel(dp, dp.getCursorRow(), coltype, PREFIJO_STRING_ID_FILA); } else { label = getRowLabel(dp, dp.getCursorRow(), colname, PREFIJO_STRING_ID_FILA); } if (dp.isRecursoNombrable()) { description = dp.getRecursoNombrableDataProvider().getNombreRecurso(); } else { description = null; } list.add(value, label, description); } } while (dp.cursorNext()); } return getListaOpciones(list); } public static ArrayList getListaOpciones(Collection c) { return getListaOpciones(c, true); } public static ArrayList getListaOpciones(Collection c, boolean anulable) { return getListaOpciones(c, anulable, EnumColumnaEtiqueta.AMBAS_COLUMNAS); } public static ArrayList getListaOpciones(Collection c, boolean anulable, EnumColumnaEtiqueta coltype) { return getListaOpciones(c, anulable, coltype, true); } public static ArrayList getListaOpciones(Collection c, boolean anulable, EnumColumnaEtiqueta coltype, boolean ordenado) { Object value; String label; String description; ListOptions list = new ListOptions(anulable, ordenado); for (Object o : c) { if (o instanceof RecursoEnumerable) { value = ((RecursoEnumerable) o).getNumeroRecurso(); } else if (o instanceof RecursoIdentificable) { value = ((RecursoIdentificable) o).getIdentificacionRecurso(); } else { value = null; } if (value != null) { label = getRowLabel(o, coltype, PREFIJO_STRING_ID_FILA); if (o instanceof RecursoNombrable) { description = ((RecursoNombrable) o).getNombreRecurso(); } else { description = null; } list.add(value, label, description); } } return getListaOpciones(list); } public static ArrayList getListaOpciones(ListOption[] options) { return getListaOpciones(options, true); } public static ArrayList getListaOpciones(ListOption[] options, boolean anulable) { return getListaOpciones(options, anulable, true); } public static ArrayList getListaOpciones(ListOption[] options, boolean anulable, boolean ordenado) { ListOptions list = new ListOptions(anulable, ordenado); list.add(options); return getListaOpciones(list); } public static ArrayList getListaOpciones(ListOptions list) { ArrayList<Option> options = new ArrayList(); for (ListOption option : list.getList()) { options.add(new Option(option.getOptionValue(), option.getOptionLabel(), option.getOptionDescription(), option.isOptionDisabled())); } return options; } // </editor-fold> // <editor-fold defaultstate="collapsed" desc="metodos para el manejo de arboles de nodos (Tree/TreeNode)"> public static Tree getArbolNodos(RecursoCachedRowSetDataProvider dp, String columna, String url, MethodExpression mex) { return getArbolNodos(dp, columna, EnumColumnaEtiqueta.AMBAS_COLUMNAS, url, mex, null, false); } public static Tree getArbolNodos(RecursoCachedRowSetDataProvider dp, String columna, String url, MethodExpression mex, Long id) { return getArbolNodos(dp, columna, EnumColumnaEtiqueta.AMBAS_COLUMNAS, url, mex, id, false); } public static Tree getArbolNodos(RecursoCachedRowSetDataProvider dp, String columna, String url, MethodExpression mex, Long id, boolean base) { return getArbolNodos(dp, columna, EnumColumnaEtiqueta.AMBAS_COLUMNAS, url, mex, id, base); } public static Tree getArbolNodos(RecursoCachedRowSetDataProvider dp, EnumColumnaEtiqueta columna, String url, MethodExpression mex) { return getArbolNodos(dp, null, columna, url, mex, null, false); } public static Tree getArbolNodos(RecursoCachedRowSetDataProvider dp, EnumColumnaEtiqueta columna, String url, MethodExpression mex, Long id) { return getArbolNodos(dp, null, columna, url, mex, id, false); } public static Tree getArbolNodos(RecursoCachedRowSetDataProvider dp, EnumColumnaEtiqueta columna, String url, MethodExpression mex, Long id, boolean base) { return getArbolNodos(dp, null, columna, url, mex, id, base); } public static Tree getArbolNodos(RecursoCachedRowSetDataProvider dp, String colname, EnumColumnaEtiqueta coltype, String url, MethodExpression mex, Long id, boolean base) { return getArbolNodos(dp, colname, coltype, url, mex, id, base, false); } public static Tree getArbolNodos(RecursoCachedRowSetDataProvider dp, String colname, EnumColumnaEtiqueta coltype, String url, MethodExpression mex, Long id, boolean base, boolean activeOnly) { Tree arbol = getNewTree(dp); if (dp.isRecursoJerarquizable()) { if (dp.cursorFirst()) { int limite = dp.getLimiteFilasFuncionSelect(); if (limite > 0) { int filas = dp.getRowCount(); if (filas == limite) { Bitacora.trace(JSF.class, "getArbolNodos", "limite=" + limite); return arbol; } } if (id == null) { do { if (dp.getRecursoJerarquizableDataProvider().getRecursoSuperior() == null) { addTreeNode(arbol, dp, colname, coltype, url, mex, dp.getCursorRow(), activeOnly); } } while (dp.cursorNext()); } else { RowKey rowKey = dp.findFirst( dp.getRecursoJerarquizableDataProvider().getColumnaIdentificacionRecurso(), id); if (dp.isRowAvailable(rowKey)) { if (base) { addTreeNode(arbol, dp, colname, coltype, url, mex, rowKey, activeOnly); } else { arbol.setId(getTreeNodeId(dp, rowKey)); arbol.setText(getTreeNodeText(dp, rowKey, colname)); do { if (dp.getRecursoJerarquizableDataProvider().getRecursoSuperior() != null) { if (dp.getRecursoJerarquizableDataProvider().getRecursoSuperior().equals(id)) { addTreeNode(arbol, dp, colname, coltype, url, mex, dp.getCursorRow(), activeOnly); } } } while (dp.cursorNext()); } } } } } return arbol; } private static Tree getNewTree(RecursoCachedRowSetDataProvider dp) { Tree arbol = new Tree(); arbol.setId(dp.getCodigoDominioRecurso().replace('.', '_')); arbol.setText(dp.getNombreDominioRecurso()); return arbol; } private static TreeNode addTreeNode(TreeNode parent, RecursoCachedRowSetDataProvider dp, String colname, EnumColumnaEtiqueta coltype, String url, MethodExpression mex, RowKey rowKey, boolean activeOnly) { if (activeOnly && dp.isRecursoDesactivable() && BitUtils.valueOf(dp.getRecursoDesactivableDataProvider().getEsRecursoInactivo(rowKey))) { return null; } String clave = dp.getRecursoJerarquizableDataProvider().getColumnaRecursoSuperior(); Long id = dp.getRecursoJerarquizableDataProvider().getIdentificacionRecurso(rowKey); String tnid = getTreeNodeId(dp, rowKey); String text; String toolTip; if (dp.isRecursoDescriptible()) { toolTip = dp.getRecursoDescriptibleDataProvider().getDescripcionRecurso(rowKey); } else { toolTip = getTreeNodeText(dp, rowKey, EnumColumnaEtiqueta.AMBAS_COLUMNAS); } String urlx; String target = null; if (StringUtils.isBlank(colname)) { text = getTreeNodeText(dp, rowKey, coltype); } else { text = getTreeNodeText(dp, rowKey, colname); } if (dp.isRecursoLocalizable()) { urlx = dp.getRecursoLocalizableDataProvider().getUrl(rowKey); // <editor-fold> // if (urlx != null) { // target = Global.DEFAULT_WINDOW_NAME; // target = getWindowName(); // target = "_blank"; /* renders the response in a new, unnamed window */ // target = "_self"; /* renders the response in the current frame */ // target = "_parent"; /* renders the response in the immediate frameset parent */ // target = "_top"; /* renders the response in the full, unframed window */ // } // </editor-fold> if (urlx != null) { String war = StringUtils.substringBetween(urlx, REDIRECT, FACES); if (StringUtils.isNotBlank(war) && !war.equals(URX.WAR)) { urlx = urlx.replaceAll(war, URX.WAR); } urlx = urlx.replaceAll(WAR, URX.WAR); String sfn = StringUtils.substringBetween(urlx.replace("?sfn=", "&sfn=").concat("&"), "&sfn=", "&"); if (sfn != null && sfn.matches("[0-9\\-]*")) { Long funcion; boolean funcionAutorizada = false; String[] strings = StringUtils.split(sfn, '-'); for (String string : strings) { funcion = Long.valueOf(string); funcionAutorizada = TLC.getControlador().esFuncionAutorizada(funcion); if (funcionAutorizada) { break; } } if (funcionAutorizada) { } else { return null; } } } } else { urlx = addRequestPostBackParameters(url, id); } TreeNode child = addTreeNode(parent, tnid, text, toolTip, urlx, mex, target); if (dp.esFilaEscogida(rowKey)) { expand(child); } if (StringUtils.isBlank(clave)) { /* * para que no falle con ListaRecursoCachedRowSetDataProvider */ return child; } RowKey[] children = dp.findAll(clave, id); if (children != null && children.length > 0) { for (int i = 0; i < children.length; i++) { addTreeNode(child, dp, colname, coltype, url, mex, children[i], activeOnly); } } return child; } private static TreeNode addTreeNode(TreeNode parent, String id, String text, String toolTip, String url, MethodExpression mex, String target) { TreeNode child = new TreeNode(); child.setId(id); child.setText(text); child.setToolTip(toolTip); if (url != null) { child.setUrl(url); } if (mex != null) { child.setActionExpression(mex); } if (target != null) { child.setTarget(target); } List children = parent.getChildren(); children.add(child); return child; } private static String getTreeNodeId(RecursoCachedRowSetDataProvider dp, RowKey rowKey) { return getRowIdStr(dp, rowKey, PREFIJO_STRING_ID_NODO); } private static String getTreeNodeId(Long id) { return PREFIJO_STRING_ID_NODO + id; } private static String getTreeNodeText(RecursoCachedRowSetDataProvider dp, RowKey rowKey, String columna) { return getRowLabel(dp, rowKey, columna, PREFIJO_STRING_ID_NODO); } private static String getTreeNodeText(RecursoCachedRowSetDataProvider dp, RowKey rowKey, EnumColumnaEtiqueta columna) { return getRowLabel(dp, rowKey, columna, PREFIJO_STRING_ID_NODO); } public static void addNodosArbol(TreeNode source, TreeNode target) { // No se puede usar el metodo addAll de la clase List; tampoco un loop con el Iterator o el ListIterator; // usando cualquiera de estos tres, el metodo add dentro del loop hace que se salten objetos de la lista, // dando como resultado una copia incompleta (uno si y uno no); por eso se necesita esta funcion. List sourceChildren = source.getChildren(); List targetChildren = target.getChildren(); Object child; Object[] sourceChildrenArray = sourceChildren.toArray(); for (int i = 0; i < sourceChildrenArray.length; i++) { child = sourceChildrenArray[i]; if (child != null && child instanceof TreeNode) { targetChildren.add((TreeNode) child); } } } public static TreeNode getNodo(Tree tree, Long id) { return getTreeNodeById(tree, getTreeNodeId(id)); } public static TreeNode getNodo(Tree tree, String id) { return getTreeNodeById(tree, id); } public static TreeNode getNodoSeleccionado(Tree tree) { // String id = tree.getCookieSelectedTreeNode(); String id = tree.getSelected(); return getTreeNodeById(tree, id); } // <editor-fold> // public static TreeNode getNodoSeleccionado(Tree tree, Form form) { // String id = tree.getCookieSelectedTreeNode(); // return getTreeNodeById(form, id); // } // </editor-fold> /* * */ public static TreeNode setNodoSeleccionado(Tree tree, Long id) { String treeKey = "form1:tree1"; /* * expand node */ /* * glassfish-3.1: El nombre de Cookie form1:tree1-expand es una palabra reservada */ // String cookie1 = treeKey + "-expand"; String cookie1 = "form1-tree1-expand"; // /* * highlight node */ /* * glassfish-3.1: El nombre de Cookie form1:tree1-hi es una palabra reservada */ // String cookie2 = treeKey + "-hi"; String cookie2 = "form1-tree1-hi"; // TreeNode nodo = getNodo(tree, id); if (nodo == null) { deleteCookie(cookie1); deleteCookie(cookie2); } else { putCookie(cookie1, treeKey + ":" + getTreeNodeConcatId(nodo)); putCookie(cookie2, treeKey + ":" + getTreeNodeConcatId(nodo)); } return nodo; } private static TreeNode getTreeNodeById(TreeNode parent, String id) { if (StringUtils.isBlank(id)) { return null; } String nodeId = id.substring(id.lastIndexOf(":") + 1); Object child; TreeNode child1; TreeNode child2; List children = parent.getChildren(); Iterator iterator = children.iterator(); while (iterator.hasNext()) { child = iterator.next(); if (child != null && child instanceof TreeNode) { child1 = (TreeNode) child; if (child1.getId().equals(nodeId)) { return child1; } else { child2 = getTreeNodeById(child1, id); if (child2 != null) { return child2; } } } } return null; } // <editor-fold> // private static TreeNode getTreeNodeById(Form form, String id) { // if (StringUtils.isBlank(id)) { // return null; // } // int i = id.lastIndexOf(":"); // if (i < 0) { // return null; // } // UIComponent component = form.findComponent(id); // if (component != null && component instanceof TreeNode) { // return (TreeNode) component; // } // return null; // } // </editor-fold> /* * */ private static Tree getTreeNodeTree(TreeNode node) { if (node instanceof Tree) { return (Tree) node; } UIComponent component = node.getParent(); if (component != null && component instanceof TreeNode) { return getTreeNodeTree((TreeNode) component); } return null; } private static void expand(TreeNode child) { child.setExpanded(true); if (child.getParent() instanceof TreeNode) { expand((TreeNode) child.getParent()); } } private static String getTreeNodeConcatId(TreeNode child) { if (child.getParent() instanceof Tree) { return child.getId(); } if (child.getParent() instanceof TreeNode) { return getTreeNodeConcatId((TreeNode) child.getParent()) + ":" + child.getId(); } return child.getId(); } // </editor-fold> // <editor-fold defaultstate="collapsed" desc="metodos para el manejo de migas de pan (Breadcrumbs/Hyperlink) "> public static Hyperlink addHipervinculoInicio(Breadcrumbs breadcrumbs) { String text = BundleWebui.getString("hyperlink.inicio.text"); String toolTip = BundleWebui.getString("hyperlink.inicio.toolTip"); String url = encodeURX(URX.INICIO); String target = null; return addHyperlink(breadcrumbs, text, toolTip, url, target); } public static Hyperlink addHipervinculo(Breadcrumbs parent, String text, String toolTip, String url, boolean scratch) { if (scratch && parent.getChildCount() > 0) { parent.getChildren().clear(); } if (parent.getChildCount() == 0) { addHipervinculoInicio(parent); } if (StringUtils.isBlank(text) || StringUtils.isBlank(url)) { return null; } Hyperlink child = getHyperlinkByUrl(parent, url); if (child == null) { return addHyperlink(parent, text, toolTip, url, null); } child.setText(text); Breadcrumbs breadcrumbs = new Breadcrumbs(); addHipervinculos(parent, breadcrumbs, url); addHipervinculos(breadcrumbs, parent); return getHyperlinkByUrl(parent, url); } public static Hyperlink updateHipervinculo(Breadcrumbs parent, String text, String toolTip, String url) { if (parent.getChildCount() == 0) { return null; } if (StringUtils.isBlank(text) || StringUtils.isBlank(url)) { return null; } Hyperlink child = getHyperlinkByUrl(parent, url); if (child == null) { return null; } child.setText(text); child.setToolTip(toolTip); child.setUrl(addRequestPostBackParameters(url)); return child; } public static void removeHipervinculo(Breadcrumbs parent, String url) { if (StringUtils.isBlank(url)) { return; } Hyperlink child = getHyperlinkByUrl(parent, url); if (child != null) { parent.getChildren().remove(child); } } private static Hyperlink addHyperlink(Breadcrumbs parent, String text, String toolTip, String url, String target) { Hyperlink child = new Hyperlink(); child.setId(parent.getId() + "Hyperlink" + (parent.getChildCount() + 1)); child.setText(text); child.setToolTip(toolTip); if (url != null) { child.setUrl(url); } if (target != null) { child.setTarget(target); } List children = parent.getChildren(); children.add(child); return child; } private static Hyperlink getHyperlinkByUrl(Breadcrumbs parent, String url) { if (StringUtils.isBlank(url)) { return null; } Hyperlink child; List children = parent.getChildren(); Iterator iterator = children.iterator(); while (iterator.hasNext()) { child = (Hyperlink) iterator.next(); if (child != null) { if (StringUtils.substringBefore(url, "?") .equals(StringUtils.substringBefore(child.getUrl(), "?"))) { return child; } } } return null; } public static void addHipervinculos(Breadcrumbs source, Breadcrumbs target) { /* * copia completa */ addHipervinculos(source, target, null); } private static void addHipervinculos(Breadcrumbs source, Breadcrumbs target, String url) { // Esto esta peor que el TreeNode, falla aun con el Array // Hay que clonar el hyperlink para poder copiarlo /* * copia solo hasta el hyperlink con el url especoficado */ boolean up2url = !StringUtils.isBlank(url); List sourceChildren = source.getChildren(); List targetChildren = target.getChildren(); Hyperlink child; Object[] sourceChildrenArray = sourceChildren.toArray(); targetChildren.clear(); for (int i = 0; i < sourceChildrenArray.length; i++) { child = (Hyperlink) sourceChildrenArray[i]; if (child != null) { Hyperlink clone = new Hyperlink(); clone.setId(child.getId()); clone.setText(child.getText()); clone.setToolTip(child.getToolTip()); clone.setUrl(child.getUrl()); clone.setTarget(child.getTarget()); targetChildren.add(clone); if (up2url && StringUtils.substringBefore(url, "?") .equals(StringUtils.substringBefore(clone.getUrl(), "?"))) { return; } } } } // </editor-fold> // <editor-fold defaultstate="collapsed" desc="constantes publicas y metodos privados "> private static final String PREFIJO_STRING_ID_FILA = Global.PREFIJO_STRING_ID_RECURSO; private static final String PREFIJO_STRING_ID_NODO = "N"; private static String getRowIdStr(RecursoCachedRowSetDataProvider dp, RowKey rowKey, String prefijo) { String p = StringUtils.trimToEmpty(prefijo); Object id; if (dp.isRecursoEnumerable()) { id = dp.getRecursoEnumerableDataProvider().getNumeroRecurso(rowKey); } else if (dp.isRecursoIdentificable()) { id = dp.getRecursoIdentificableDataProvider().getIdentificacionRecurso(rowKey); } else { id = null; } return id == null ? null : p + id; } private static String getRowLabel(RecursoCachedRowSetDataProvider dp, RowKey rowKey, String columna, String prefijo) { String p = ""; // StringUtils.trimToEmpty(prefijo); String id = getRowIdStr(dp, rowKey, p); String label = null; Object value = dp.getValue(columna, rowKey); if (value instanceof String) { label = (String) value; } return StringUtils.isBlank(label) ? id : label; } private static String getRowLabel(RecursoCachedRowSetDataProvider dp, RowKey rowKey, EnumColumnaEtiqueta columna, String prefijo) { String p = ""; // StringUtils.trimToEmpty(prefijo); String id = getRowIdStr(dp, rowKey, p); String code = dp.isRecursoCodificable() ? dp.getRecursoCodificableDataProvider().getCodigoRecurso(rowKey) : null; String name = dp.isRecursoNombrable() ? dp.getRecursoNombrableDataProvider().getNombreRecurso(rowKey) : null; return getRowLabel(columna, id, code, name); } private static String getRowLabel(Object o, EnumColumnaEtiqueta columna, String prefijo) { String p = ""; // StringUtils.trimToEmpty(prefijo); String id; if (o instanceof RecursoEnumerable) { id = p + ((RecursoEnumerable) o).getNumeroRecurso(); } else if (o instanceof RecursoIdentificable) { id = p + ((RecursoIdentificable) o).getIdentificacionRecurso(); } else { id = null; } String code = o instanceof RecursoCodificable ? ((RecursoCodificable) o).getCodigoRecurso() : null; String name = o instanceof RecursoNombrable ? ((RecursoNombrable) o).getNombreRecurso() : null; return getRowLabel(columna, id, code, name); } private static String getRowLabel(EnumColumnaEtiqueta columna, String id, String code, String name) { String codigo, nombre, ambas, id_y_codigo, id_y_nombre; if (StringUtils.isBlank(code) && StringUtils.isBlank(name)) { codigo = id; nombre = id; ambas = id; id_y_codigo = id; id_y_nombre = id; } else if (StringUtils.isBlank(code)) { codigo = name; nombre = name; ambas = name; id_y_codigo = id + " (" + name + ") "; id_y_nombre = id + " (" + name + ") "; } else if (StringUtils.isBlank(name)) { codigo = code; nombre = code; ambas = code; id_y_codigo = id + " (" + code + ") "; id_y_nombre = id + " (" + code + ") "; } else { codigo = code; nombre = name; ambas = code + " (" + name + ") "; id_y_codigo = id + " (" + code + ") "; id_y_nombre = id + " (" + name + ") "; } switch (columna) { case COLUMNA_CODIGO: return codigo; case COLUMNA_NOMBRE: return nombre; case AMBAS_COLUMNAS: return ambas; case ID_Y_CODIGO: return id_y_codigo; case ID_Y_NOMBRE: return id_y_nombre; default: return codigo; } } private static String addRequestPostBackParameters(String url) { if (StringUtils.isBlank(url)) { return null; } String param = ""; String state = getRequestParameter(Global.PARAMETRO_RETRUQUE); if (StringUtils.isNotBlank(state)) { param += StringUtils.indexOf(url, "?") < 0 ? "?" : "&"; param += Global.PARAMETRO_RETRUCAR + "=" + true; // param += "&" + Global.PARAMETRO_RETRUQUE + "=" + state; } return url + param; } private static String addRequestPostBackParameters(String url, Long id) { if (StringUtils.isBlank(url)) { return null; } String param = ""; if (id != null) { param += StringUtils.indexOf(url, "?") < 0 ? "?" : "&"; param += Global.PARAMETRO_RETRUCAR + "=" + true; param += "&" + CPP.ID_RECURSO + "=" + id + Global.PREFIJO_ENLACE_URL_HTTP; } return url + param; } // </editor-fold> }