Patrones de programación de umbrales de objetos de negocio de ejemplo

A continuación se muestran unos pocos ejemplos sobre cómo acceder a los archivos de configuración, efectuar la comprobación de condición sin límite y cómo generar una excepción en el caso de que se produzca una infracción.

Acceder a la propiedad de umbral


String compId = "com.ibm.commerce.storelocator";
ComponentConfiguration config = ComponentConfigurationRegistry.instance().getComponentConfiguration(compId);
String mapWidth = config.getValueByConfigGroupingNameAndPropertyName("map", "mapWidth");

En el ejemplo anterior se muestra una comparación simple con el umbral.

Comprobación respecto al umbral


/**
 * Returns whether the threshold in the given context has been exceeded.
 * The default behaviour passes if there is no threshold.  Subclasses should
 * override this to provide additional checking using additional
 * information.
 *
 * @return  True if the threshold was exceeded, false otherwise.
*/
public boolean isThresholdAcceptable() {
    final String METHODNAME = "isThresholdAcceptable";
    ECTrace.entry(ECTraceIdentifiers.COMPONENT_ORDER, CLASSNAME, METHODNAME);

    boolean passed = threshold == null || threshold.compareTo(BigDecimal.ZERO) < 0;

    if (ECTrace.traceEnabled(ECTraceIdentifiers.COMPONENT_ORDER)) {
        ECTrace.exit(ECTraceIdentifiers.COMPONENT_ORDER, CLASSNAME, METHODNAME, Boolean.valueOf(passed));
    }

    return passed;
}
En la siguiente sección se muestra otro ejemplo de cómo trabajar con el tamaño de página predeterminado y el tamaño de página máximo. El comportamiento se muestra en el siguiente pseudocódigo:

// enter a point in the code where we need to paginate

maxItems = getObject.getMaxItems()
if (maxItems == null) {
    maxItems = getDefaultPageSize()
}

maximumPageSize = getMaximumPageSize()
if (maximumPageSize != null && (maxItems == null || maxItems > maximumPageSize)) {
    maxItems = maximumPageSize
}

if (maxItems == null || maxItems < 0) {
    maxItems = 0
}

// use the startNumber and maxItems to choose the items in the page to return
Además, tenga en cuenta los siguientes casos donde los valores no están definidos:
  1. defaultPageSize existe y maxPageSize existe
    1. si defaultPageSize == maxPageSize: el tamaño de página se limitará con maxPageSize tanto si los parámetros de paginación se establecen como si no.
    2. si defaultPageSize <maxPageSize – si no se establece ningún parámetro, se utilizará defaultPageSize, de lo contrario los parámetros estarán limitados por maxPageSize.
    3. si defaultPageSize > maxPageSize – el tamaño de página se limitará con maxPageSize tanto si los parámetros de paginación se establecen como si no.
  2. defaultPageSize existe pero maxPageSize no existe: el tamaño de página quedará limitado por defaultPageSize solo si no se ha establecido ningún parámetro de paginación.
  3. defaultPageSize no existe, pero maxPageSize existe: el tamaño de página se limitará con maxPageSize tanto si los parámetros de paginación se han establecido como si no.
  4. defaultPageSize no existe y maxPageSize no existe: no se impondrá ningún límite en el tamaño de página.

Manejo de excepciones


Object[] params = new Object[] {getThreshold(), getNumAdded(), getNumCurrent()};
throw new ECApplicationException(getUserErrorMessage(), CLASSNAME, "throwDefaultException", params);

Infracción al conectarse


/**
 * Logs a message to the administrator about the threshold being exceeded.
 * This method should be called when it is known that a threshold has been
 * exceeded.
 *
 * @param logger
 *          The logger to use to notify the administrator.  Cannot be null.
 * @param methodName
 *          The name of the method which is calling this.  Cannot be null
 *          or empty.
*/
public void log(Logger logger, String methodName) {
    if (LoggingHelper.isTraceEnabled(logger)) {
    
        Object[] params = new Object[] {getThreshold(), getNumAdded(),
                getNumCurrent (), getUserID(), getStoreID(), getOrderID()};

        logger.logp(Level.WARNING, logger.getName(), methodName,
                getAdminErrorMessage().getMessageKey(), params);
    }
}