Example usage for org.apache.commons.lang3 StringUtils substringBefore

List of usage examples for org.apache.commons.lang3 StringUtils substringBefore

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils substringBefore.

Prototype

public static String substringBefore(final String str, final String separator) 

Source Link

Document

Gets the substring before the first occurrence of a separator.

Usage

From source file:org.guess.core.orm.hibernate.HibernateDao.java

/**
 * /*w w  w.  ja v  a  2s.  co  m*/
 */
public Page<T> findPage(final PageRequest pageRequest, Criterion[] criterions,
        final List<PropertyFilter> filters) {
    Page<T> page = new Page<T>(pageRequest);
    Criteria c = createCriteria(criterions);
    Map<String, String> alias = Maps.newHashMap();
    for (PropertyFilter filter : filters) {
        if (!filter.hasMultiProperties()) {
            if (filter.getPropertyName().indexOf(".") != -1) {
                String objName = StringUtils.substringBefore(filter.getPropertyName(), ".");
                alias.put(objName, objName);
            }
        } else {
            for (String param : filter.getPropertyNames()) {
                if (param.indexOf(".") != -1) {
                    String objName = StringUtils.substringBefore(param, ".");
                    alias.put(objName, objName);
                }
            }
        }
    }

    for (String key : alias.keySet()) {
        c.createAlias(key, alias.get(key));
    }

    if (pageRequest.isCountTotal()) {
        long totalCount = countCriteriaResult(c);
        page.setTotalItems(totalCount);
    }

    setPageRequestToCriteria(c, pageRequest);

    List result = c.list();
    page.setResult(result);
    return page;
}

From source file:org.guess.core.orm.PropertyFilter.java

/**
 * @param filterName ,???. //from ww  w  . j a  v  a2 s. co  m
 *                   eg. LIKES_NAME_OR_LOGIN_NAME
 * @param value .
 */
public PropertyFilter(final String filterName, final String value) {

    String firstPart = StringUtils.substringBefore(filterName, "_");
    String matchTypeCode = StringUtils.substring(firstPart, 0, firstPart.length() - 1);
    String propertyTypeCode = StringUtils.substring(firstPart, firstPart.length() - 1, firstPart.length());

    try {
        matchType = Enum.valueOf(MatchType.class, matchTypeCode);
    } catch (RuntimeException e) {
        throw new IllegalArgumentException(
                "filter??" + filterName + ",.", e);
    }

    try {
        propertyClass = Enum.valueOf(PropertyType.class, propertyTypeCode).getValue();
    } catch (RuntimeException e) {
        throw new IllegalArgumentException(
                "filter??" + filterName + ",.", e);
    }

    String propertyNameStr = StringUtils.substringAfter(filterName, "_");
    AssertUtils.isTrue(StringUtils.isNotBlank(propertyNameStr),
            "filter??" + filterName + ",??.");
    propertyNames = StringUtils.splitByWholeSeparator(propertyNameStr, PropertyFilter.OR_SEPARATOR);

    this.matchValue = ConvertUtils.convertStringToObject(value, propertyClass);
}

From source file:org.gvnix.addon.jpa.addon.entitylistener.JpaOrmEntityListenerMetadata.java

/**
 * @param metadataIdentificationString/* ww  w .  ja va 2 s .co  m*/
 * @return listener class
 */
public static final JavaType getJavaType(String metadataIdentificationString) {
    return PhysicalTypeIdentifierNamingUtils.getJavaType(PROVIDES_TYPE_STRING,
            StringUtils.substringBefore(metadataIdentificationString, METADATA_SOURCE_DELIMITER));
}

From source file:org.gvnix.addon.jpa.addon.entitylistener.JpaOrmEntityListenerMetadata.java

/**
 * @param metadataIdentificationString/* www .  j  ava 2  s  .c o  m*/
 * @return listener logical path
 */
public static final LogicalPath getPath(String metadataIdentificationString) {
    return PhysicalTypeIdentifierNamingUtils.getPath(PROVIDES_TYPE_STRING,
            StringUtils.substringBefore(metadataIdentificationString, METADATA_SOURCE_DELIMITER));
}

From source file:org.gvnix.addon.jpa.addon.entitylistener.JpaOrmEntityListenerMetadata.java

/**
 * @param metadataIdentificationString/*ww  w  . j a  v a2 s .  c  om*/
 * @return if metadata is valid
 */
public static boolean isValid(String metadataIdentificationString) {
    return PhysicalTypeIdentifierNamingUtils.isValid(PROVIDES_TYPE_STRING,
            StringUtils.substringBefore(metadataIdentificationString, METADATA_SOURCE_DELIMITER))
            && StringUtils.contains(metadataIdentificationString, METADATA_SOURCE_DELIMITER)
            && StringUtils.isNotBlank(
                    StringUtils.substringAfter(metadataIdentificationString, METADATA_SOURCE_DELIMITER));
}

From source file:org.gvnix.addon.jpa.addon.entitylistener.JpaOrmEntityListenerMetadata.java

/**
 * @param metadataIdentificationString//from  www .  ja  v a  2s .  c  o  m
 * @return gets base metadata id of this metadata (whitout source
 *         information)
 */
public static final String getBaseId(String metadataIdentificationString) {
    return StringUtils.substringBefore(metadataIdentificationString, METADATA_SOURCE_DELIMITER);
}

From source file:org.gvnix.web.datatables.util.DatatablesUtils.java

/**
 * Get Date formatter by field name/* w w w.ja  v  a  2s .c o m*/
 * <p/>
 * If no pattern found, try standard Roo key
 * {@code uncapitalize( ENTITY ) + "_" + lower_case( FIELD ) + "_date_format"}
 * 
 * @param datePatterns Contains field name and related data pattern
 * @param entityClass Entity class to which the field belong to
 * @param fieldName Field to search pattern
 * @return
 */
private static SimpleDateFormat getDateFormatter(Map<String, Object> datePatterns,
        Map<String, SimpleDateFormat> dateFormatters, Class<?> entityClass, String fieldName) {

    SimpleDateFormat result = null;
    String lowerCaseFieldName = fieldName.toLowerCase();
    result = dateFormatters.get(lowerCaseFieldName);
    if (result != null) {
        return result;
    } else if (dateFormatters.containsKey(lowerCaseFieldName)) {
        return null;
    }

    // Get pattern by field name
    String pattern = (String) datePatterns.get(lowerCaseFieldName);
    if (StringUtils.isEmpty(pattern)) {
        // Try to get the name of entity class (without javassit suffix)
        String baseClass = StringUtils.substringBefore(entityClass.getSimpleName(), "$");// );"_$");
        // try to get pattern by Roo key
        String rooKey = StringUtils.uncapitalize(baseClass).concat("_").concat(lowerCaseFieldName)
                .concat("_date_format");

        pattern = (String) datePatterns.get(rooKey);
    }
    if (!StringUtils.isEmpty(pattern)) {
        result = new SimpleDateFormat(pattern);
    }
    dateFormatters.put(lowerCaseFieldName, result);
    return result;
}

From source file:org.gvnix.web.datatables.util.impl.DatatablesUtilsBeanImpl.java

/**
 * Get Date formatter by field name/*from www  .jav  a2 s .c  om*/
 * <p/>
 * If no pattern found, try standard Roo key
 * {@code uncapitalize( ENTITY ) + "_" + lower_case( FIELD ) + "_date_format"}
 * 
 * @param datePatterns Contains field name and related data pattern
 * @param entityClass Entity class to which the field belong to
 * @param fieldName Field to search pattern
 * @return
 */
private SimpleDateFormat getDateFormatter(Map<String, Object> datePatterns,
        Map<String, SimpleDateFormat> dateFormatters, Class<?> entityClass, String fieldName) {

    SimpleDateFormat result = null;
    String lowerCaseFieldName = fieldName.toLowerCase();
    result = dateFormatters.get(lowerCaseFieldName);
    if (result != null) {
        return result;
    } else if (dateFormatters.containsKey(lowerCaseFieldName)) {
        return null;
    }

    // Get pattern by field name
    String pattern = (String) datePatterns.get(lowerCaseFieldName);
    if (StringUtils.isEmpty(pattern)) {
        // Try to get the name of entity class (without javassit suffix)
        String baseClass = StringUtils.substringBefore(entityClass.getSimpleName(), "$");// );"_$");
        // try to get pattern by Roo key
        String rooKey = StringUtils.uncapitalize(baseClass).concat("_").concat(lowerCaseFieldName)
                .concat("_date_format");

        pattern = (String) datePatterns.get(rooKey);
    }
    if (!StringUtils.isEmpty(pattern)) {
        result = new SimpleDateFormat(pattern);
    }
    dateFormatters.put(lowerCaseFieldName, result);
    return result;
}

From source file:org.gvnix.web.json.BindingResultSerializer.java

/**
 * Iterate over list items errors and load it on allErrorsMessages map.
 * <p/>/*from ww w . ja v  a2  s. co m*/
 * Delegates on {@link #loadObjectError(FieldError, String, Map)}
 * 
 * @param fieldErrors
 * @param allErrorsMessages
 */
@SuppressWarnings("unchecked")
private void loadListErrors(List<FieldError> fieldErrors, Map<String, Object> allErrorsMessages) {

    // Get prefix to unwrapping list:
    // "list[0].employedSince"
    String fieldNamePath = fieldErrors.get(0).getField();
    // "list"
    String prefix = StringUtils.substringBefore(fieldNamePath, "[");

    String index;
    Map<String, Object> currentErrors;

    // Iterate over errors
    for (FieldError error : fieldErrors) {
        // get property path without list prefix
        // "[0].employedSince"
        fieldNamePath = StringUtils.substringAfter(error.getField(), prefix);

        // Get item's index:
        // "[0].employedSince"
        index = StringUtils.substringBefore(StringUtils.substringAfter(fieldNamePath, "["), "]");

        // Remove index definition from field path
        // "employedSince"
        fieldNamePath = StringUtils.substringAfter(fieldNamePath, ".");

        // Check if this item already has errors registered
        currentErrors = (Map<String, Object>) allErrorsMessages.get(index);
        if (currentErrors == null) {
            // No errors registered: create map to contain this error
            currentErrors = new HashMap<String, Object>();
            allErrorsMessages.put(index, currentErrors);
        }

        // Load error on item's map
        loadObjectError(error, fieldNamePath, currentErrors);
    }
}

From source file:org.gvnix.web.json.BindingResultSerializer.java

/**
 * Loads an object field error in errors map.
 * <p/>//from  ww w  . j  a v  a2 s  .  c  o  m
 * This method identifies if referred object property is an array, an object
 * or a simple property to decide how to store the error message.
 * 
 * @param error
 * @param fieldNamePath
 * @param objectErrors
 */
@SuppressWarnings("unchecked")
private void loadObjectError(FieldError error, String fieldNamePath, Map<String, Object> objectErrors) {

    String propertyName;
    boolean isObject = false;
    boolean isList = false;

    // Get this property name and if is a object property
    if (StringUtils.contains(fieldNamePath, ".")) {
        isObject = true;
        propertyName = StringUtils.substringBefore(fieldNamePath, ".");
    } else {
        isObject = false;
        propertyName = fieldNamePath;
    }

    // Check if property is an array or a list
    isList = StringUtils.contains(propertyName, "[");

    // Process a list item property
    if (isList) {
        // Get property name
        String listPropertyName = StringUtils.substringBefore(propertyName, "[");

        // Get referred item index
        String index = StringUtils.substringBefore(StringUtils.substringAfter(propertyName, "["), "]");

        // Get item path
        String itemPath = StringUtils.substringAfter(fieldNamePath, ".");

        // Get container of list property errors
        Map<String, Object> listErrors = (Map<String, Object>) objectErrors.get(listPropertyName);

        if (listErrors == null) {
            // property has no errors yet: create a container for it
            listErrors = new HashMap<String, Object>();
            objectErrors.put(listPropertyName, listErrors);
        }

        // Get current item errors
        Map<String, Object> itemErrors = (Map<String, Object>) listErrors.get(index);

        if (itemErrors == null) {
            // item has no errors yet: create a container for it
            itemErrors = new HashMap<String, Object>();
            listErrors.put(index, itemErrors);
        }

        // Load error in item property path
        loadObjectError(error, itemPath, itemErrors);

    } else if (isObject) {
        // It's not a list but it has properties in it value

        // Get current property errors
        Map<String, Object> propertyErrors = (Map<String, Object>) objectErrors.get(propertyName);

        if (propertyErrors == null) {
            // item has no errors yet: create a container for it
            propertyErrors = new HashMap<String, Object>();
            objectErrors.put(propertyName, propertyErrors);
        }

        // Get error sub path
        String subFieldPath = StringUtils.substringAfter(fieldNamePath, ".");

        // Load error in container
        loadObjectError(error, subFieldPath, propertyErrors);

    } else {
        // standard property with no children value

        // Store error message in container
        objectErrors.put(propertyName, error.getDefaultMessage());
    }
}