Mostrando el tamaño de un fichero en Content Presenter

Este post surge del siguiente Thread del foro de OTN:

Show the file size in Kilobytes

Para mostrar el tamano de un fichero se utiliza comunmente la siguiente EL Expression que nos da el tamaño en Bytes del nodo actual en una plantilla de Content Presenter.

 #{node.propertyMap['VaultFileSize'].value}


Para mostrar en formato de texto dichos Bytes en Kilobytes etc... Tenemos dos maneras distintas:
  1. Usando el JSF Converter que proporciona la librería por defecto de Document Service de Oracle WebCenter.

     
      
     
    
  2. Usar nuestro propio JSF Converter.
    Fragmento del faces-config.xml registrando el Converter
    
        
          Converts the size of a file expressed as a Number to a String with a size unit.
        
        Documents Size Converter
        
          com.merchan.sample.jsf.converter.Size
        
        
          com.merchan.sample.jsf.converter.SizeConverter
        
      
    

    Código del Converter
    package com.merchan.sample.jsf.converter;
    
    import oracle.webcenter.doclib.internal.view.convert.AbstractConverter;
    
    import java.text.DecimalFormat;
    import java.text.NumberFormat;
    
    import java.util.Locale;
    
    import javax.faces.component.UIComponent;
    import javax.faces.context.FacesContext;
    
    import oracle.webcenter.doclib.internal.model.resource.ResourceKey;
    import oracle.webcenter.doclib.internal.view.resource.DoclibUIResource;
    
    /**
     * Size converter to Kilobytes, Megabytes, Gigabytes depending of the size in bytes
     * @author Daniel Merchan
     * @version 1.0
     */
    public final class SizeConverter extends AbstractConverter {
    
        /**
         * Not implemented
         * @param context - Current Faces Context
         * @param component - UI Component to apply the conversion
         * @param value - Value of the UI Component
         * @return Object
         */
        public Object getAsObject(FacesContext context, UIComponent component,
                                  String value) {
            // Not implemented
            return null;
        }
    
        /**
         * Obtain the conversion in KB, MB, GB... depending of the size of the file
         * @param context - Current Faces Context
         * @param component - UI Component to apply the conversion
         * @param value - Value of the UI Component
         * @return String
         */
        public String getAsString(FacesContext context, UIComponent component,
                                  Object value) {
            // Check if the value is a number, if not ignore it
            if ((value == null) || (!(value instanceof Number))) {
                return "";
            }
            double size = ((Number)value).doubleValue();
            return SizeConverter.transformBytes(size, Boolean.TRUE);
        }
        
        /**
         * Transform the bytes into a bigger readable approach
         * @param bytes - File size
         * @param si - Si or Binary Units
         * @return
         */
        private static String transformBytes(double bytes, boolean si) {
            int unit = si ? 1000 : 1024;
            if (bytes < unit) return bytes + " B";
            int exp = (int) (Math.log(bytes) / Math.log(unit));
            String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
            return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
        }
    }
    
Content Presenter Template usada en el ejemplo:



    
      
      
        
        
          
        
      
      
        
        
            
        
      
    


Content Presenter Showing the Sizes

Nota: El JSF Converter de Document Library esta limitado hasta Gigabytes. Si se requiere sizes mas grandes usar uno propio basado en las librerías de Apache o algún algoritmo propio como el del ejemplo.

Comments

Popular posts from this blog

OJET: Inter-Module communication in TypeScript Template

OJET: Build and Deploy in an Application Server

OJET: Select All options using only Checkboxset