Example usage for com.liferay.portal.kernel.util Validator isNumber

List of usage examples for com.liferay.portal.kernel.util Validator isNumber

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util Validator isNumber.

Prototype

public static boolean isNumber(String number) 

Source Link

Document

Returns true if the string is a decimal integer number, meaning it contains nothing but decimal digits.

Usage

From source file:com.evolveum.liferay.usercreatehook.screenname.CustomScreenNameGenerator.java

License:Apache License

public String generate(long companyId, long userId, String emailAddress) throws Exception {

    String screenName = null;//from   w w w .  ja va2 s .c o m

    if (Validator.isNotNull(emailAddress)) {
        // XXX change 1
        // screenName = StringUtil.extractFirst(emailAddress, CharPool.AT).toLowerCase();
        screenName = emailAddress.toLowerCase();

        for (char c : screenName.toCharArray()) {
            // XXX change 2
            // if (!Validator.isChar(c) && !Validator.isDigit(c) && (c != CharPool.DASH) && (c != CharPool.PERIOD))
            // {
            if (!Validator.isChar(c) && !Validator.isDigit(c) && (c != CharPool.DASH)) {
                // XXX change 3
                // screenName = StringUtil.replace(screenName, c, CharPool.PERIOD);
                screenName = StringUtil.replace(screenName, c, CharPool.DASH);
            }
        }

        if (screenName.equals(DefaultScreenNameValidator.CYRUS)
                || screenName.equals(DefaultScreenNameValidator.POSTFIX)) {

            screenName += StringPool.PERIOD + userId;
        }
    } else {
        screenName = String.valueOf(userId);
    }

    if (!_USERS_SCREEN_NAME_ALLOW_NUMERIC && Validator.isNumber(screenName)) {

        screenName = _NON_NUMERICAL_PREFIX + screenName;
    }

    String[] reservedScreenNames = PrefsPropsUtil.getStringArray(companyId,
            PropsKeys.ADMIN_RESERVED_SCREEN_NAMES, StringPool.NEW_LINE, _ADMIN_RESERVED_SCREEN_NAMES);

    for (String reservedScreenName : reservedScreenNames) {
        if (screenName.equalsIgnoreCase(reservedScreenName)) {
            return getUnusedScreenName(companyId, screenName);
        }
    }

    try {
        UserLocalServiceUtil.getUserByScreenName(companyId, screenName);
    } catch (NoSuchUserException nsue) {
        try {
            GroupLocalServiceUtil.getFriendlyURLGroup(companyId, StringPool.SLASH + screenName);
        } catch (NoSuchGroupException nsge) {
            return screenName;
        }
    }

    return getUnusedScreenName(companyId, screenName);
}

From source file:com.liferay.amfregistration.service.impl.ZipSearchServiceImpl.java

License:Open Source License

public boolean validateZip(String zipCode) {
    if (zipCode.length() != 5 || !Validator.isNumber(zipCode)) {
        return false;
    }/*from   w w  w . j  a  v a2 s. c  om*/

    return true;
}

From source file:com.liferay.asset.categories.navigation.web.internal.exportimport.portlet.preferences.processor.AssetCategoriesNavigationPortletPreferencesProcessor.java

License:Open Source License

@Override
protected Long getImportPortletPreferencesNewValue(PortletDataContext portletDataContext, Class<?> clazz,
        long companyGroupId, Map<Long, Long> primaryKeys, String portletPreferencesOldValue) throws Exception {

    if (Validator.isNumber(portletPreferencesOldValue)) {
        long oldPrimaryKey = GetterUtil.getLong(portletPreferencesOldValue);

        return MapUtil.getLong(primaryKeys, oldPrimaryKey, oldPrimaryKey);
    }//from  w  w  w . j  a v  a  2s.  co m

    String className = clazz.getName();

    String[] oldValues = StringUtil.split(portletPreferencesOldValue, StringPool.POUND);

    String uuid = oldValues[0];

    long groupId = portletDataContext.getScopeGroupId();

    if (oldValues.length > 1) {
        Map<Long, Long> groupIds = (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(Group.class);

        groupId = MapUtil.getLong(groupIds, GetterUtil.getLong(oldValues[1]));
    }

    if (className.equals(AssetVocabulary.class.getName())) {
        AssetVocabulary assetVocabulary = _assetVocabularyLocalService
                .fetchAssetVocabularyByUuidAndGroupId(uuid, groupId);

        if (assetVocabulary != null) {
            return assetVocabulary.getVocabularyId();
        }
    }

    return null;
}

From source file:com.liferay.asset.publisher.web.internal.exportimport.portlet.preferences.processor.AssetPublisherExportImportPortletPreferencesProcessor.java

License:Open Source License

@Override
protected Long getImportPortletPreferencesNewValue(PortletDataContext portletDataContext, Class<?> clazz,
        long companyGroupId, Map<Long, Long> primaryKeys, String portletPreferencesOldValue) throws Exception {

    if (Validator.isNumber(portletPreferencesOldValue)) {
        long oldPrimaryKey = GetterUtil.getLong(portletPreferencesOldValue);

        return MapUtil.getLong(primaryKeys, oldPrimaryKey, oldPrimaryKey);
    }//ww w  . j ava  2 s.co  m

    String className = clazz.getName();

    String[] oldValues = StringUtil.split(portletPreferencesOldValue, StringPool.POUND);

    String uuid = oldValues[0];

    long groupId = portletDataContext.getScopeGroupId();

    if (oldValues.length > 1) {
        Map<Long, Long> groupIds = (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(Group.class);

        groupId = MapUtil.getLong(groupIds, GetterUtil.getLong(oldValues[1]), groupId);
    }

    if (className.equals(AssetCategory.class.getName())) {
        AssetCategory assetCategory = _assetCategoryLocalService.fetchAssetCategoryByUuidAndGroupId(uuid,
                groupId);

        if (assetCategory != null) {
            return assetCategory.getCategoryId();
        }
    } else if (className.equals(AssetVocabulary.class.getName())) {
        AssetVocabulary assetVocabulary = _assetVocabularyLocalService
                .fetchAssetVocabularyByUuidAndGroupId(uuid, groupId);

        if (assetVocabulary != null) {
            return assetVocabulary.getVocabularyId();
        }
    } else if (className.equals(DDMStructure.class.getName())) {
        DDMStructure ddmStructure = _ddmStructureLocalService.fetchDDMStructureByUuidAndGroupId(uuid, groupId);

        if (ddmStructure == null) {
            Map<String, String> structureUuids = (Map<String, String>) portletDataContext
                    .getNewPrimaryKeysMap(DDMStructure.class + ".ddmStructureUuid");

            String defaultStructureUuid = MapUtil.getString(structureUuids, uuid, uuid);

            ddmStructure = _ddmStructureLocalService.fetchDDMStructureByUuidAndGroupId(defaultStructureUuid,
                    groupId);
        }

        if (ddmStructure != null) {
            return ddmStructure.getStructureId();
        }
    } else if (className.equals(DLFileEntryType.class.getName())) {
        DLFileEntryType dlFileEntryType = _dlFileEntryTypeLocalService
                .fetchDLFileEntryTypeByUuidAndGroupId(uuid, groupId);

        if (dlFileEntryType == null) {
            Element rootElement = portletDataContext.getImportDataRootElement();

            Element element = portletDataContext.getReferenceElement(rootElement, clazz, companyGroupId, uuid,
                    PortletDataContext.REFERENCE_TYPE_DEPENDENCY);

            if (element != null) {
                String fileEntryTypeKey = element.attributeValue("file-entry-type-key");

                boolean preloaded = GetterUtil.getBoolean(element.attributeValue("preloaded"));

                if (preloaded) {
                    dlFileEntryType = _dlFileEntryTypeLocalService.fetchFileEntryType(companyGroupId,
                            fileEntryTypeKey);
                }
            }
        }

        if (dlFileEntryType != null) {
            return dlFileEntryType.getFileEntryTypeId();
        }
    }

    return null;
}

From source file:com.liferay.asset.publisher.web.internal.exportimport.portlet.preferences.processor.AssetPublisherExportImportPortletPreferencesProcessor.java

License:Open Source License

protected void updateExportOrderByColumnClassPKs(PortletDataContext portletDataContext, Portlet portlet,
        PortletPreferences portletPreferences, String key) throws Exception {

    String oldValue = portletPreferences.getValue(key, null);

    String[] ddmStructureFieldNameParts = StringUtil.split(oldValue, DDMIndexer.DDM_FIELD_SEPARATOR);

    String primaryKey = ddmStructureFieldNameParts[2];

    if (!Validator.isNumber(primaryKey)) {
        return;/*  www  .  j  a v a  2 s .  c o  m*/
    }

    long primaryKeyLong = GetterUtil.getLong(primaryKey);

    String newPreferencesValue = getExportPortletPreferencesValue(portletDataContext, portlet,
            DDMStructure.class.getName(), primaryKeyLong);

    if (Validator.isNull(newPreferencesValue)) {
        if (_log.isWarnEnabled()) {
            _log.warn(StringBundler.concat("Unable to export portlet preferences value for class ",
                    DDMStructure.class.getName(), " with primary key ", String.valueOf(primaryKeyLong)));
        }

        return;
    }

    String newValue = StringUtil.replace(oldValue, primaryKey, newPreferencesValue);

    portletPreferences.setValue(key, newValue);
}

From source file:com.liferay.asset.publisher.web.internal.exportimport.portlet.preferences.processor.AssetPublisherExportImportPortletPreferencesProcessor.java

License:Open Source License

protected void updateImportScopeIds(PortletDataContext portletDataContext,
        PortletPreferences portletPreferences, String key, long companyGroupId, long plid) throws Exception {

    String[] oldValues = portletPreferences.getValues(key, null);

    if (oldValues == null) {
        return;//from   www  .  j  av  a 2 s .  co  m
    }

    StagedModelDataHandler<StagedGroup> stagedModelDataHandler = (StagedModelDataHandler<StagedGroup>) StagedModelDataHandlerRegistryUtil
            .getStagedModelDataHandler(StagedGroup.class.getName());

    Element rootElement = portletDataContext.getImportDataRootElement();

    Element groupIdMappingsElement = rootElement.element("group-id-mappings");

    for (Element groupIdMappingElement : groupIdMappingsElement.elements("group-id-mapping")) {

        stagedModelDataHandler.importMissingReference(portletDataContext, groupIdMappingElement);
    }

    Map<Long, Long> groupIds = (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(Group.class);

    Layout layout = _layoutLocalService.getLayout(plid);

    String companyGroupScopeId = AssetPublisherUtil.SCOPE_ID_GROUP_PREFIX + companyGroupId;

    List<String> newValues = new ArrayList<>(oldValues.length);

    for (String oldValue : oldValues) {
        String newValue = StringUtil.replace(oldValue, "[$COMPANY_GROUP_SCOPE_ID$]", companyGroupScopeId);

        if (Validator.isNumber(oldValue) && groupIds.containsKey(Long.valueOf(oldValue))) {

            Group group = _groupLocalService.fetchGroup(groupIds.get(Long.valueOf(oldValue)));

            if (group != null) {
                newValue = AssetPublisherUtil.getScopeId(group, portletDataContext.getScopeGroupId());
            }
        }

        if (Validator.isNumber(newValue)) {
            if (_log.isInfoEnabled()) {
                _log.info(StringBundler.concat("Ignoring group ", newValue, " because it cannot ",
                        "be converted to scope"));
            }

            continue;
        }

        try {
            if (!assetPublisherWebUtil.isScopeIdSelectable(PermissionThreadLocal.getPermissionChecker(),
                    newValue, companyGroupId, layout, false)) {

                continue;
            }

            newValues.add(newValue);
        } catch (NoSuchGroupException nsge) {
            if (_log.isInfoEnabled()) {
                _log.info(StringBundler.concat("Ignoring scope ", newValue, " because the ",
                        "referenced group was not found"), nsge);
            }
        } catch (NoSuchLayoutException nsle) {
            if (_log.isInfoEnabled()) {
                _log.info(StringBundler.concat("Ignoring scope ", newValue, " because the ",
                        "referenced layout was not found"), nsle);
            }
        } catch (PrincipalException pe) {
            if (_log.isInfoEnabled()) {
                _log.info(StringBundler.concat("Ignoring scope ", newValue, " because the ",
                        "referenced parent group no longer allows sharing ", "content with child sites"), pe);
            }
        }
    }

    portletPreferences.setValues(key, newValues.toArray(new String[newValues.size()]));
}

From source file:com.liferay.blogs.recent.bloggers.web.internal.exportimport.portlet.preferences.processor.RecentBloggersExportImportPortletPreferencesProcessor.java

License:Open Source License

@Override
protected Long getImportPortletPreferencesNewValue(PortletDataContext portletDataContext, Class<?> clazz,
        long companyGroupId, Map<Long, Long> primaryKeys, String portletPreferencesOldValue) throws Exception {

    if (Validator.isNumber(portletPreferencesOldValue)) {
        long oldPrimaryKey = GetterUtil.getLong(portletPreferencesOldValue);

        return MapUtil.getLong(primaryKeys, oldPrimaryKey, oldPrimaryKey);
    }/*  w w  w  .  j av a  2 s.c  o  m*/

    String className = clazz.getName();

    if (className.equals(Organization.class.getName())) {
        Organization organization = OrganizationUtil.fetchByUuid_C_First(portletPreferencesOldValue,
                portletDataContext.getCompanyId(), null);

        if (organization != null) {
            return organization.getOrganizationId();
        }
    }

    return null;
}

From source file:com.liferay.blogs.util.BlogsUtil.java

License:Open Source License

public static String getUrlTitle(long entryId, String title) {
    if (title == null) {
        return String.valueOf(entryId);
    }/*from   w ww  . j ava 2s  .  co m*/

    title = StringUtil.toLowerCase(title.trim());

    if (Validator.isNull(title) || Validator.isNumber(title) || title.equals("rss")) {

        title = String.valueOf(entryId);
    } else {
        title = FriendlyURLNormalizerUtil.normalizeWithPeriodsAndSlashes(title);
    }

    return ModelHintsUtil.trimString(BlogsEntry.class.getName(), "urlTitle", title);
}

From source file:com.liferay.content.targeting.portlet.lar.BaseContentTargetingDisplayPortletDataHandler.java

License:Open Source License

protected void updateExportAssetEntryIds(PortletDataContext portletDataContext, Portlet portlet,
        PortletPreferences portletPreferences, String key, Element rootElement) throws Exception {

    String oldValue = portletPreferences.getValue(key, null);

    if ((oldValue == null) || !Validator.isNumber(oldValue)) {
        return;/*from  w w  w . j av a2  s. com*/
    }

    long assetEntryId = GetterUtil.getLong(oldValue);

    if (assetEntryId == 0) {
        return;
    }

    AssetEntry assetEntry = AssetEntryLocalServiceUtil.fetchAssetEntry(assetEntryId);

    if ((assetEntry == null) || (assetEntry.getGroupId() != portletDataContext.getScopeGroupId())) {

        return;
    }

    portletPreferences.setValue(key + "classUuid", assetEntry.getClassUuid());
    portletPreferences.setValue(key + "groupId", String.valueOf(assetEntry.getGroupId()));

    AssetEntryReferencedStagedModel assetEntryReferencedStagedModel = new AssetEntryReferencedStagedModel(
            assetEntry);

    if (isExportReferencedContent(portletDataContext)) {
        try {
            StagedModelDataHandlerUtil.exportReferenceStagedModel(portletDataContext,
                    portlet.getRootPortletId(), assetEntryReferencedStagedModel);

            return;
        } catch (Exception e) {
            if (_log.isWarnEnabled()) {
                _log.warn("Cannot export referenced content for portlet " + portlet.getPortletName());
            }
        }
    }

    portletDataContext.addReferenceElement(portlet, rootElement, assetEntryReferencedStagedModel,
            AssetEntryReferencedStagedModel.class, PortletDataContext.REFERENCE_TYPE_WEAK, true);

    Element assetEntryReferencedStagedModelElement = portletDataContext
            .getExportDataElement(assetEntryReferencedStagedModel);

    portletDataContext.addClassedModel(assetEntryReferencedStagedModelElement,
            ExportImportPathUtil.getModelPath(assetEntryReferencedStagedModel),
            assetEntryReferencedStagedModel);
}

From source file:com.liferay.content.targeting.portlet.lar.CampaignContentDisplayPortletDataHandler.java

License:Open Source License

@Override
protected void updateExportReferrerIds(PortletDataContext portletDataContext, Portlet portlet,
        PortletPreferences portletPreferences, int index, Element rootElement) throws Exception {

    String key = "campaignId" + index;

    String oldValue = portletPreferences.getValue(key, null);

    if ((oldValue == null) || !Validator.isNumber(oldValue)) {
        return;/*from  w w w.jav  a 2s. co m*/
    }

    long campaignId = GetterUtil.getLong(oldValue);

    Campaign campaign = CampaignLocalServiceUtil.fetchCampaign(campaignId);

    if (campaign == null) {
        if (_log.isWarnEnabled()) {
            _log.warn("Unable to get campaign with id " + campaignId);
        }

        return;
    }

    portletPreferences.setValue(key + "uuid", campaign.getUuid());

    if (portletDataContext.getBooleanParameter(NAMESPACE, "referenced-campaigns")) {

        StagedModelDataHandlerUtil.exportReferenceStagedModel(portletDataContext, portlet.getRootPortletId(),
                campaign);
    } else {
        portletDataContext.addReferenceElement(portlet, rootElement, campaign, Campaign.class,
                PortletDataContext.REFERENCE_TYPE_WEAK, true);

        Element campaignElement = portletDataContext.getExportDataElement(campaign);

        portletDataContext.addClassedModel(campaignElement, ExportImportPathUtil.getModelPath(campaign),
                campaign);
    }
}