Example usage for org.apache.commons.collections CollectionUtils find

List of usage examples for org.apache.commons.collections CollectionUtils find

Introduction

In this page you can find the example usage for org.apache.commons.collections CollectionUtils find.

Prototype

public static Object find(Collection collection, Predicate predicate) 

Source Link

Document

Finds the first element in the given collection which matches the given predicate.

Usage

From source file:org.tonguetied.datatransfer.importing.ExcelImporter.java

/**
 * Perform the actual import of data into persistent storage.
 * //from  w  w  w .j  a va2  s.  co m
 * @param keywords the map of {@link Keyword}s to import.
 */
private void doImport(Map<String, Keyword> keywords) {
    Keyword keyword;
    for (Map.Entry<String, Keyword> entry : keywords.entrySet()) {
        keyword = getKeywordService().getKeyword(entry.getKey());
        if (keyword == null) {
            keyword = entry.getValue();
        } else {
            // find translation by business key
            Predicate predicate;
            Translation translation;
            for (Translation refTranslation : entry.getValue().getTranslations()) {
                predicate = new TranslationPredicate(refTranslation.getBundle(), refTranslation.getCountry(),
                        refTranslation.getLanguage());
                translation = (Translation) CollectionUtils.find(keyword.getTranslations(), predicate);
                if (translation == null) {
                    keyword.addTranslation(refTranslation);
                } else {
                    translation.setValue(refTranslation.getValue());
                }
            }
        }

        transferRepository.saveOrUpdate(keyword);
    }

    if (logger.isInfoEnabled())
        logger.info("processed " + keywords.size() + " translations");
}

From source file:org.tonguetied.datatransfer.importing.ResourceHandler.java

@Override
public void startElement(String uri, String localName, String name, Attributes attributes) {
    // reset the character array
    chars.setLength(0);//from  w  w w.  j  a v a  2  s. com
    if (ELEMENT_RESHEADER.equalsIgnoreCase(name)) {
        parent = name;
    } else if (ELEMENT_DATA.equalsIgnoreCase(name)) {
        final String keywordStr = attributes.getValue("name");
        keyword = keywordService.getKeyword(keywordStr);
        if (keyword == null) {
            keyword = new Keyword();
            keyword.setKeyword(keywordStr);
            translation = new Translation(bundle, country, language, null, state);
            keyword.addTranslation(translation);
        } else {
            translation = (Translation) CollectionUtils.find(keyword.getTranslations(), predicate);
        }
        parent = name;
    }
}

From source file:org.tonguetied.keywordmanagement.Keyword.java

/**
 * Remove a {@link Translation} from this Keyword's list of 
 * <code>translations</code>. The translation to be removed is matched by
 * the <code>id</code> of the {@link Translation}. If no match is found,
 * such as before the translation has been persisted, then this method 
 * attempts to remove the translation according to its id in the 
 * translation sets natural order./*  w w w  .  ja va  2 s .  com*/
 * 
 * @param translationId the id of the {@link Translation}, or the index of
 * the collection, if no id exists
 */
public void removeTranslation(final Long translationId) {
    if (translationId == null)
        throw new IllegalArgumentException("translationId cannot be null");

    Translation translation = (Translation) CollectionUtils.find(translations,
            new TranslationIdPredicate(translationId));
    if (translation == null) {
        Translation[] array = new Translation[this.translations.size()];
        this.translations.toArray(array);
        translation = array[translationId.intValue()];
    }

    remove(translation);
}

From source file:org.tonguetied.keywordmanagement.KeywordServiceStub.java

public Bundle getBundleByName(final String name) {
    final Predicate bundleCodeFilter = new BundleFilter(name);
    Bundle bundle = (Bundle) CollectionUtils.find(bundles.values(), bundleCodeFilter);
    return bundle;
}

From source file:org.tonguetied.keywordmanagement.KeywordServiceStub.java

public Bundle getBundleByResourceName(final String resourceName) {
    final Predicate bundleCodeFilter = new ResourceBundleFilter(resourceName);
    Bundle bundle = (Bundle) CollectionUtils.find(bundles.values(), bundleCodeFilter);
    return bundle;
}

From source file:org.tonguetied.keywordmanagement.KeywordServiceStub.java

public Bundle getDefaultBundle() {
    final Predicate bundleCodeFilter = new DefaultBundleFilter();
    Bundle bundle = (Bundle) CollectionUtils.find(bundles.values(), bundleCodeFilter);
    return bundle;
}

From source file:org.tonguetied.keywordmanagement.KeywordServiceStub.java

public Country getCountry(final CountryCode code) {
    final Predicate countryCodeFilter = new CountryCodeFilter(code);
    Country country = (Country) CollectionUtils.find(countries.values(), countryCodeFilter);
    return country;
}

From source file:org.tonguetied.keywordmanagement.KeywordServiceStub.java

public Keyword getKeyword(final String keywordString) {
    final Predicate keywordFilter = new KeywordStringFilter(keywordString);
    Keyword result = (Keyword) CollectionUtils.find(keywords.values(), keywordFilter);
    return result;
}

From source file:org.tonguetied.keywordmanagement.KeywordServiceStub.java

public Language getLanguage(final LanguageCode code) {
    final Predicate languageCodeFilter = new LanguageCodeFilter(code);
    Language language = (Language) CollectionUtils.find(languages.values(), languageCodeFilter);
    return language;
}

From source file:org.tonguetied.web.KeywordController.java

/**
 * Merge the filtered out {@link Translation}'s back into the filtered 
 * translation set of the keyword.//w  w  w. j  av  a2s . com
 * 
 * @param keyword the current keyword
 * @param errors
 */
private void mergeFilteredTranslations(final Keyword keyword, BindException errors) {
    final Keyword existingKeyword = keywordService.getKeyword(keyword.getId());
    TranslationIdPredicate predicate;
    Translation existingTranslation;
    for (Translation translation : existingKeyword.getTranslations()) {
        predicate = new TranslationIdPredicate(translation.getId());
        existingTranslation = (Translation) CollectionUtils.find(keyword.getTranslations(), predicate);
        if (existingTranslation == null) {
            keyword.addTranslation(translation);
        }
    }
    KeywordValidator validator = (KeywordValidator) getValidator();
    validator.validateDuplicates(keyword.getTranslations(), errors);
}