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

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

Introduction

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

Prototype

public static String trimToNull(final String str) 

Source Link

Document

Removes control characters (char <= 32) from both ends of this String returning null if the String is empty ("") after the trim or if it is null .

Usage

From source file:org.gbif.ipt.model.Resource.java

/**
 * Construct author name for citation. Name must have a last name and at least one first name to be included. If
 * both the first and last name are left blank on purpose, the organisation name can be used as an alternative.
 *
 * @param creator creator//from   ww  w  . j  a  v  a 2 s  .c o  m
 *
 * @return author name
 */
@VisibleForTesting
protected String getAuthorName(Agent creator) {
    StringBuilder sb = new StringBuilder();
    String lastName = StringUtils.trimToNull(creator.getLastName());
    String firstNames = StringUtils.trimToNull(creator.getFirstName());
    String organisation = StringUtils.trimToNull(creator.getOrganisation());
    if (lastName != null && firstNames != null) {
        sb.append(lastName);
        sb.append(" ");
        // add first initial of each first name, capitalized
        String[] names = firstNames.split("\\s+");
        for (int i = 0; i < names.length; i++) {
            sb.append(StringUtils.upperCase(String.valueOf(names[i].charAt(0))));
            if (i < names.length - 1) {
                sb.append(" ");
            }
        }
    } else if (lastName == null && firstNames == null && organisation != null) {
        sb.append(organisation);
    }
    return sb.toString();
}

From source file:org.gbif.ipt.service.manage.impl.ResourceManagerImplTest.java

@Test
public void testSaveVersionCount() throws ParserConfigurationException, SAXException, IOException {
    // create instance of manager
    ResourceManagerImpl resourceManager = getResourceManagerImpl();
    // prepare resource, with published record count and version 3
    resource.setEmlVersion(3);/* www .java2  s. c  o m*/
    resource.setRecordsPublished(4000);

    // assure count file exists unchanged
    File countFile = mockedDataDir.resourceCountFile(resource.getShortname(), resource.getEmlVersion());
    String countAsString = StringUtils.trimToNull(org.apache.commons.io.FileUtils.readFileToString(countFile));
    assertEquals("1234", countAsString);

    // save count file, persisting count 4000
    resourceManager.saveVersionCount(resource);

    // assure count file reflects new count
    countAsString = StringUtils.trimToNull(org.apache.commons.io.FileUtils.readFileToString(countFile));
    assertEquals("4000", countAsString);
}

From source file:org.gbif.ipt.service.manage.impl.SourceManagerImpl.java

private String analyze(SqlSource ss) {
    String problem = null;/*from  w  ww .j  av a 2s .c om*/
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        con = getDbConnection(ss);
        // test sql
        if (StringUtils.trimToNull(ss.getSql()) != null) {
            stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            stmt.setFetchSize(FETCH_SIZE);
            rs = stmt.executeQuery(ss.getSqlLimited(FETCH_SIZE));
            // get number of columns
            ResultSetMetaData meta = rs.getMetaData();
            ss.setColumns(meta.getColumnCount());
            ss.setReadable(true);
        }
    } catch (SQLException e) {
        log.warn("Cant read sql source " + ss, e);
        problem = e.getMessage();
        ss.setReadable(false);
    } finally {
        // close result set, statement, and connection in that order
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                log.error("ResultSet could not be closed: " + e.getMessage(), e);
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                log.error("Statement could not be closed: " + e.getMessage(), e);
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                log.error("Connection could not be closed: " + e.getMessage(), e);
            }
        }
    }
    return problem;
}

From source file:org.gbif.ipt.validation.BaseValidator.java

/**
 * Checks if string has a length in a certain range.
 *
 * @param x         incoming string//from w  w w.  j  a va  2  s  .  com
 * @param minLength min length inclusive
 * @param maxLength max length inclusive
 *
 * @return true if string
 */
protected boolean existsInRange(String x, int minLength, int maxLength) {
    x = StringUtils.trimToNull(x);
    return x != null && x.length() >= minLength && x.length() <= maxLength;
}

From source file:org.gbif.ipt.validation.ExtensionMappingValidator.java

private boolean isValidDataType(DataType dt, PropertyMapping pm, List<String[]> peek) {
    // shortcut for strings and nulls
    if (dt == null || dt == DataType.string) {
        return true;
    }//from   w  w  w. jav a 2  s  .co  m

    // prepare set of strings to test
    Set<String> testData = new HashSet<String>();
    testData.add(pm.getDefaultValue());
    if (pm.getIndex() != null) {
        for (String[] row : peek) {
            if (row.length >= pm.getIndex() && pm.getIndex() >= 0) {
                testData.add(row[pm.getIndex()]);
            }
        }
    }
    for (String val : testData) {
        val = StringUtils.trimToNull(val);
        if (val == null) {
            continue;
        }
        try {
            if (DataType.bool == dt) {
                if (val.equals("1")) {
                    continue;
                }
            } else if (DataType.date == dt) {
                Date d = DateUtils.parse(val, DateUtils.ISO_DATE_FORMAT);
                if (d == null) {
                    return false;
                }
            } else if (DataType.decimal == dt) {
                Float.parseFloat(val);
            } else if (DataType.integer == dt) {
                Integer.parseInt(val);
            } else if (DataType.uri == dt) {
                new URI(val);
            }
        } catch (NumberFormatException e) {
            return false;
        } catch (URISyntaxException e) {
            return false;
        }
    }
    return true;
}

From source file:org.gbif.ipt.validation.ExtensionMappingValidator.java

public ValidationStatus validate(ExtensionMapping mapping, Resource resource, List<String[]> peek) {
    ValidationStatus v = new ValidationStatus();
    // check required fields
    Extension ext = mapping.getExtension();
    if (ext != null) {
        for (ExtensionProperty p : ext.getProperties()) {
            if (p.isRequired() && !mapping.isMapped(p)) {
                v.addMissingRequiredField(p);
            }/*from   www .ja va2  s  .  c o m*/
        }

        // check data types for default mappings
        for (PropertyMapping pm : mapping.getFields()) {
            ExtensionProperty extProperty = mapping.getExtension().getProperty(pm.getTerm());
            DataType type = extProperty != null ? extProperty.getType() : null;
            if (type != null && DataType.string != type) {
                // non string data type. check
                if (!isValidDataType(type, pm, peek)) {
                    v.addWrongDataTypeField(pm.getTerm());
                }
            }
        }

        // check id mapping
        if (mapping.getIdColumn() == null) {
            // no id, ok if this is a core
            if (!ext.isCore()) {
                v.setIdProblem("validation.mapping.coreid.missing");
            }
        } else if (mapping.getIdColumn().equals(ExtensionMapping.IDGEN_LINE_NUMBER)) {
            // pure integers are not allowed as line numbers plus integers will result in duplicates
            if (StringUtils.isNumericSpace(StringUtils.trimToNull(mapping.getIdSuffix()))) {
                v.setIdProblem("validation.mapping.coreid.linenumber.integer");
            }
            // if its core make sure all other mappings to the same extensions dont use linenumber with the same suffix or
            if (ext.isCore()) {
                Set<ExtensionMapping> maps = new HashSet<ExtensionMapping>(
                        resource.getMappings(ext.getRowType()));
                maps.remove(mapping);
                if (!maps.isEmpty()) {
                    // we more mappings to the same extension, compare their id policy
                    for (ExtensionMapping m : maps) {
                        if (m.getIdColumn() != null
                                && m.getIdColumn().equals(ExtensionMapping.IDGEN_LINE_NUMBER)) {
                            // do we have different suffices?
                            if (StringUtils.trimToEmpty(mapping.getIdSuffix()).equals(m.getIdSuffix())) {
                                v.setIdProblem("validation.mapping.coreid.linenumber.samesufix");
                            }
                        }
                    }
                }
            } else {
                // linenumbers for extensions only make sense when there is a core with the same suffix
                boolean found = false;
                for (ExtensionMapping m : resource.getCoreMappings()) {
                    if (m.getIdColumn() != null && m.getIdColumn().equals(ExtensionMapping.IDGEN_LINE_NUMBER)) {
                        // do they have the same suffix?
                        if (StringUtils.trimToEmpty(mapping.getIdSuffix())
                                .equals(StringUtils.trimToEmpty(m.getIdSuffix()))) {
                            found = true;
                            break;
                        }
                    }
                }
                if (!found) {
                    v.setIdProblem("validation.mapping.coreid.linenumber.nocoresuffix");
                }
            }
        } else if (mapping.getIdColumn().equals(ExtensionMapping.IDGEN_UUID)) {
            // not allowed for extensions
            if (ext.isCore()) {
                // if there are any extensions existing the cant link to the new UUIDs
                for (ExtensionMapping m : resource.getMappings()) {
                    if (!m.isCore()) {
                        v.setIdProblem("validation.mapping.coreid.uuid.extensions.exist");
                    }
                }
            } else {
                v.setIdProblem("validation.mapping.coreid.uuid.extension");
            }
        } // else {
          // TODO: anything to check for regular columns?
          // }
    }
    return v;
}

From source file:org.gbif.ipt.validation.OrganisationSupport.java

/**
 * Validate the fields entered for a new or edited Organisation. If not valid, an explanatory error message is
 * added to the action.//from   w w w . j ava 2s. co  m
 *
 * @param action       action
 * @param organisation organisation
 */
public boolean validate(BaseAction action, Organisation organisation) {
    boolean valid = true;
    if (organisation.getKey() == null || organisation.getKey().toString().length() < 1) {
        valid = false;
        action.addFieldError("organisation.key", action.getText("validation.organisation.key.required"));
    }

    if (StringUtils.trimToNull(organisation.getPassword()) == null) {
        valid = false;
        action.addFieldError("organisation.password",
                action.getText("validation.organisation.password.required"));
    }

    // validate if the key+password combination validates to true
    if (organisation.getKey() != null && organisation.getPassword() != null) {
        if (organisation.getKey().toString().length() > 0 && organisation.getPassword().length() > 0) {
            if (!registryManager.validateOrganisation(organisation.getKey().toString(),
                    organisation.getPassword())) {
                valid = false;
                action.addFieldError("organisation.password",
                        action.getText("validation.organisation.password.invalid"));
            }
        }
    }

    DOIRegistrationAgency agency = organisation.getDoiRegistrationAgency();
    String agencyUsername = StringUtils.trimToNull(organisation.getAgencyAccountUsername());
    String agencyPassword = StringUtils.trimToNull(organisation.getAgencyAccountPassword());
    String prefix = StringUtils.trimToNull(organisation.getDoiPrefix());
    boolean isAgencyAccountPrimary = organisation.isAgencyAccountPrimary();

    // validate that if any DOI registration agency account fields were entered, that they are all present
    if (agency != null || agencyUsername != null || agencyPassword != null || prefix != null
            || isAgencyAccountPrimary) {

        // ensure archival mode is turned ON, otherwise ensure activation of agency account fails
        if (isAgencyAccountPrimary) {
            if (!cfg.isArchivalMode()) {
                valid = false;
                action.addFieldError("organisation.agencyAccountPrimary",
                        action.getText("admin.organisation.doiAccount.activated.failed"));
            }
        }

        if (agency == null) {
            valid = false;
            action.addFieldError("organisation.doiRegistrationAgency",
                    action.getText("validation.organisation.doiRegistrationAgency.required"));
        }

        if (agencyUsername == null) {
            valid = false;
            action.addFieldError("organisation.agencyAccountUsername",
                    action.getText("validation.organisation.agencyAccountUsername.required"));
        }

        if (agencyPassword == null) {
            valid = false;
            action.addFieldError("organisation.agencyAccountPassword",
                    action.getText("validation.organisation.agencyAccountPassword.required"));
        }

        if (prefix == null) {
            valid = false;
            action.addFieldError("organisation.doiPrefix",
                    action.getText("validation.organisation.doiPrefix.required"));
        } else if (!prefix.startsWith("10.")) {
            valid = false;
            action.addFieldError("organisation.doiPrefix",
                    action.getText("validation.organisation.doiPrefix.invalid"));
        } else if (!prefix.contains("/") && agency != null && agency.equals(DOIRegistrationAgency.EZID)) {
            valid = false;
            action.addFieldError("organisation.doiPrefix",
                    action.getText("validation.organisation.doiPrefix.ezid.invalid"));
        } else {
            // running IPT in development, the test DOI prefix is expected, but not mandatory - show warning otherwise
            if (cfg.getRegistryType() == AppConfig.REGISTRY_TYPE.DEVELOPMENT
                    && !Constants.TEST_DOI_PREFIX.equalsIgnoreCase(prefix)
                    && !Constants.EZID_TEST_DOI_SHOULDER.equalsIgnoreCase(prefix)) {
                action.addActionWarning(action.getText("validation.organisation.doiPrefix.invalid.testMode"));
            }
            // running IPT in production, the test DOI prefix cannot be used
            else if (cfg.getRegistryType() == AppConfig.REGISTRY_TYPE.PRODUCTION
                    && (Constants.TEST_DOI_PREFIX.equalsIgnoreCase(prefix)
                            || Constants.EZID_TEST_DOI_SHOULDER.equalsIgnoreCase(prefix))) {
                valid = false;
                action.addFieldError("organisation.doiPrefix",
                        action.getText("validation.organisation.doiPrefix.invalid.productionMode"));
            }
        }

        // validate if the account configuration is correct, e.g. by reserving a test DOI
        if (valid) {
            DoiService service;

            // before configuring EZID service: clear EZID session cookie otherwise connection reuses existing login
            if (agency.equals(DOIRegistrationAgency.EZID) && !client.getCookieStore().getCookies().isEmpty()) {
                client.getCookieStore().clear();
            }

            ServiceConfig cfg = new ServiceConfig(agencyUsername, agencyPassword);
            service = (agency.equals(DOIRegistrationAgency.DATACITE)) ? new DataCiteService(client, cfg)
                    : new EzidService(client, cfg);

            try {
                DOI doi = DOIUtils.mintDOI(agency, prefix);
                DataCiteMetadata metadata = getTestDataCiteMetadata(doi);
                service.reserve(doi, metadata);
                // clean up
                service.delete(doi);
            } catch (DoiException e) {
                valid = false;
                String msg = action.getText("validation.organisation.agencyAccount.cantAuthenticate");
                LOG.error(msg, e);
                action.addActionError(msg);
            } finally {
                // in case fields were trimmed, re-save agency account values
                organisation.setAgencyAccountUsername(agencyUsername);
                organisation.setAgencyAccountPassword(agencyPassword);
                organisation.setDoiPrefix(prefix);
            }
        } else {
            LOG.debug(
                    "Not all DOI Registration agency fields were entered correctly - bypassing DOI Registration Agency validation");
        }
    }
    return valid;
}

From source file:org.gbif.nub.lookup.HigherTaxaLookup.java

/**
 * @return non empty uppercased string with normalized whitespace and all non latin letters replaced. Or null
 *//*from   w w  w.  ja v a2s  .  c o m*/
@VisibleForTesting
protected static String norm(String x) {
    Pattern REMOVE_NON_LETTERS = Pattern.compile("[\\W\\d]+");
    x = Strings.nullToEmpty(x);
    x = REMOVE_NON_LETTERS.matcher(x).replaceAll(" ");
    x = StringUtils.normalizeSpace(x).toUpperCase();
    return StringUtils.trimToNull(x);
}

From source file:org.gbif.occurrence.parsing.xml.HigherTaxonParser.java

/**
 * Given a raw taxon rank and name, finds the matching Linnean rank (if any) and builds a Taxon
 * object with the rank and name./* ww  w .  ja v a 2  s  .com*/
 */
public Taxon parseTaxon(String rawTaxonRank, String taxonName) {
    Taxon taxon = null;
    String processedTaxonRank = StringUtils.trimToNull(rawTaxonRank);
    if (processedTaxonRank != null && !StringUtils.equals(processedTaxonRank, "null")) {
        processedTaxonRank = processedTaxonRank.replaceAll(" ", "").toUpperCase();
        String rawRank = taxonRankMapping.getProperty(processedTaxonRank);
        if (rawRank == null) {
            LOG.info("Could not process taxon ranking of [{}], skipping.", processedTaxonRank);
        } else {
            int rank = Integer.valueOf(rawRank.trim());
            LOG.debug("ProcessedTaxonRank [{}] gives numeric rank [{}]", processedTaxonRank, rank);
            switch (rank) {
            case 1000:
                taxon = new Taxon(TaxonRankEnum.KINGDOM, taxonName);
                break;
            case 2000:
                taxon = new Taxon(TaxonRankEnum.PHYLUM, taxonName);
                break;
            case 3000:
                taxon = new Taxon(TaxonRankEnum.CLASS, taxonName);
                break;
            case 4000:
                taxon = new Taxon(TaxonRankEnum.ORDER, taxonName);
                break;
            case 5000:
                taxon = new Taxon(TaxonRankEnum.FAMILY, taxonName);
                break;
            }
        }
    }

    return taxon;
}

From source file:org.gbif.portal.action.BaseFacetedSearchAction.java

private void initSelectedFacetCounts() {
    if (!searchRequest.getParameters().isEmpty()) {
        // convert request filters into facets instances
        for (P facet : searchRequest.getParameters().keySet()) {
            selectedFacetCounts.put(facet, Lists.<FacetInstance>newArrayList());
            for (String filterValue : searchRequest.getParameters().get(facet)) {
                if (StringUtils.trimToNull(filterValue) != null) {
                    FacetInstance facetInstance = findFacetInstanceByName(facet, filterValue);
                    if (facetInstance == null) {
                        facetInstance = new FacetInstance(filterValue);
                        facetInstance.setCount(null);
                        // also add them to the list of all counts so we will lookup titles
                        if (!facetCounts.containsKey(facet)) {
                            facetCounts.put(facet, Lists.<FacetInstance>newArrayList());
                        }//from  ww w . j  av  a2  s .c  om
                        facetCounts.get(facet).add(facetInstance);
                    }
                    selectedFacetCounts.get(facet).add(facetInstance);
                }
            }
        }
    }
}