Example usage for com.liferay.portal.kernel.language LanguageUtil isAvailableLocale

List of usage examples for com.liferay.portal.kernel.language LanguageUtil isAvailableLocale

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.language LanguageUtil isAvailableLocale.

Prototype

public static boolean isAvailableLocale(long groupId, String languageId) 

Source Link

Usage

From source file:com.liferay.exportimport.controller.LayoutImportController.java

License:Open Source License

protected void validateFile(long companyId, long groupId, Map<String, String[]> parameterMap,
        ZipReader zipReader) throws Exception {

    // XML/*  w  ww  .  j a va  2  s.  c  om*/

    String xml = zipReader.getEntryAsString("/manifest.xml");

    if (xml == null) {
        throw new LARFileException(LARFileException.TYPE_MISSING_MANIFEST);
    }

    Element rootElement = null;

    try {
        Document document = SAXReaderUtil.read(xml);

        rootElement = document.getRootElement();
    } catch (Exception e) {
        throw new LARFileException(LARFileException.TYPE_INVALID_MANIFEST, e);
    }

    // Bundle compatibility

    Element headerElement = rootElement.element("header");

    int importBuildNumber = GetterUtil.getInteger(headerElement.attributeValue("build-number"));

    if (importBuildNumber < ReleaseInfo.RELEASE_7_0_0_BUILD_NUMBER) {
        int buildNumber = ReleaseInfo.getBuildNumber();

        if (buildNumber != importBuildNumber) {
            throw new LayoutImportException(LayoutImportException.TYPE_WRONG_BUILD_NUMBER,
                    new Object[] { importBuildNumber, buildNumber });
        }
    } else {
        BiPredicate<Version, Version> majorVersionBiPredicate = (currentVersion, importVersion) -> Objects
                .equals(currentVersion.getMajor(), importVersion.getMajor());

        BiPredicate<Version, Version> minorVersionBiPredicate = (currentVersion, importVersion) -> {
            int currentMinorVersion = GetterUtil.getInteger(currentVersion.getMinor(), -1);
            int importedMinorVersion = GetterUtil.getInteger(importVersion.getMinor(), -1);

            if (((currentMinorVersion == -1) && (importedMinorVersion == -1))
                    || (currentMinorVersion < importedMinorVersion)) {

                return false;
            }

            return true;
        };

        BiPredicate<Version, Version> manifestVersionBiPredicate = (currentVersion, importVersion) -> {
            BiPredicate<Version, Version> versionBiPredicate = majorVersionBiPredicate
                    .and(minorVersionBiPredicate);

            return versionBiPredicate.test(currentVersion, importVersion);
        };

        String importSchemaVersion = GetterUtil.getString(headerElement.attributeValue("schema-version"),
                "1.0.0");

        if (!manifestVersionBiPredicate.test(
                Version.getInstance(ExportImportConstants.EXPORT_IMPORT_SCHEMA_VERSION),
                Version.getInstance(importSchemaVersion))) {

            throw new LayoutImportException(LayoutImportException.TYPE_WRONG_LAR_SCHEMA_VERSION,
                    new Object[] { importSchemaVersion, ExportImportConstants.EXPORT_IMPORT_SCHEMA_VERSION });
        }
    }

    // Type

    String larType = headerElement.attributeValue("type");

    String[] expectedLARTypes = { "layout-prototype", "layout-set", "layout-set-prototype" };

    Stream<String> stream = Stream.of(expectedLARTypes);

    if (stream.noneMatch(lt -> lt.equals(larType))) {
        throw new LARTypeException(larType, expectedLARTypes);
    }

    Group group = _groupLocalService.fetchGroup(groupId);

    String layoutsImportMode = MapUtil.getString(parameterMap, PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE);

    if (larType.equals("layout-prototype") && !group.isLayoutPrototype()
            && !layoutsImportMode.equals(PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE_CREATED_FROM_PROTOTYPE)) {

        throw new LARTypeException(LARTypeException.TYPE_LAYOUT_PROTOTYPE);
    }

    if (larType.equals("layout-set")) {
        if (group.isLayoutPrototype() || group.isLayoutSetPrototype()) {
            throw new LARTypeException(LARTypeException.TYPE_LAYOUT_SET);
        }

        long sourceCompanyGroupId = GetterUtil.getLong(headerElement.attributeValue("company-group-id"));
        long sourceGroupId = GetterUtil.getLong(headerElement.attributeValue("group-id"));

        boolean companySourceGroup = false;

        if (sourceCompanyGroupId == sourceGroupId) {
            companySourceGroup = true;
        }

        if (group.isCompany() ^ companySourceGroup) {
            throw new LARTypeException(LARTypeException.TYPE_COMPANY_GROUP);
        }
    }

    if (larType.equals("layout-set-prototype") && !group.isLayoutSetPrototype()
            && !layoutsImportMode.equals(PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE_CREATED_FROM_PROTOTYPE)) {

        throw new LARTypeException(LARTypeException.TYPE_LAYOUT_SET_PROTOTYPE);
    }

    // Portlets compatibility

    List<Element> portletElements = fetchPortletElements(rootElement);

    for (Element portletElement : portletElements) {
        String portletId = GetterUtil.getString(portletElement.attributeValue("portlet-id"));

        if (Validator.isNull(portletId)) {
            continue;
        }

        String schemaVersion = GetterUtil.getString(portletElement.attributeValue("schema-version"));

        PortletDataHandler portletDataHandler = _portletDataHandlerProvider.provide(companyId, portletId);

        if (portletDataHandler == null) {
            continue;
        }

        if (!portletDataHandler.validateSchemaVersion(schemaVersion)) {
            throw new LayoutImportException(LayoutImportException.TYPE_WRONG_PORTLET_SCHEMA_VERSION,
                    new Object[] { schemaVersion, portletId, portletDataHandler.getSchemaVersion() });
        }
    }

    // Available locales

    List<Locale> sourceAvailableLocales = Arrays.asList(
            LocaleUtil.fromLanguageIds(StringUtil.split(headerElement.attributeValue("available-locales"))));

    for (Locale sourceAvailableLocale : sourceAvailableLocales) {
        if (!LanguageUtil.isAvailableLocale(groupId, sourceAvailableLocale)) {

            LocaleException le = new LocaleException(LocaleException.TYPE_EXPORT_IMPORT);

            le.setSourceAvailableLocales(sourceAvailableLocales);
            le.setTargetAvailableLocales(LanguageUtil.getAvailableLocales(groupId));

            throw le;
        }
    }

    // Layout prototypes validity

    Element layoutsElement = rootElement.element(Layout.class.getSimpleName());

    validateLayoutPrototypes(companyId, headerElement, layoutsElement);
}

From source file:com.liferay.exportimport.controller.PortletImportController.java

License:Open Source License

protected void validateFile(long companyId, long groupId, String portletId, ZipReader zipReader)
        throws Exception {

    // XML//w  w w  . ja v  a  2s . c o  m

    String xml = zipReader.getEntryAsString("/manifest.xml");

    if (xml == null) {
        throw new LARFileException(LARFileException.TYPE_MISSING_MANIFEST);
    }

    Element rootElement = null;

    try {
        Document document = SAXReaderUtil.read(xml);

        rootElement = document.getRootElement();
    } catch (Exception e) {
        throw new LARFileException(LARFileException.TYPE_INVALID_MANIFEST, e);
    }

    // Build compatibility

    Element headerElement = rootElement.element("header");
    Element portletElement = rootElement.element("portlet");

    int importBuildNumber = GetterUtil.getInteger(headerElement.attributeValue("build-number"));

    if (importBuildNumber < ReleaseInfo.RELEASE_7_0_0_BUILD_NUMBER) {
        int buildNumber = ReleaseInfo.getBuildNumber();

        throw new LayoutImportException(LayoutImportException.TYPE_WRONG_BUILD_NUMBER,
                new Object[] { importBuildNumber, buildNumber });
    } else {
        BiPredicate<Version, Version> majorVersionBiPredicate = (currentVersion, importVersion) -> Objects
                .equals(currentVersion.getMajor(), importVersion.getMajor());

        BiPredicate<Version, Version> minorVersionBiPredicate = (currentVersion, importVersion) -> {
            int currentMinorVersion = GetterUtil.getInteger(currentVersion.getMinor(), -1);
            int importedMinorVersion = GetterUtil.getInteger(importVersion.getMinor(), -1);

            if (((currentMinorVersion == -1) && (importedMinorVersion == -1))
                    || (currentMinorVersion < importedMinorVersion)) {

                return false;
            }

            return true;
        };

        BiPredicate<Version, Version> manifestVersionBiPredicate = (currentVersion, importVersion) -> {
            BiPredicate<Version, Version> versionBiPredicate = majorVersionBiPredicate
                    .and(minorVersionBiPredicate);

            return versionBiPredicate.test(currentVersion, importVersion);
        };

        String importSchemaVersion = GetterUtil.getString(headerElement.attributeValue("schema-version"),
                "1.0.0");

        if (!manifestVersionBiPredicate.test(
                Version.getInstance(ExportImportConstants.EXPORT_IMPORT_SCHEMA_VERSION),
                Version.getInstance(importSchemaVersion))) {

            throw new LayoutImportException(LayoutImportException.TYPE_WRONG_LAR_SCHEMA_VERSION,
                    new Object[] { importSchemaVersion, ExportImportConstants.EXPORT_IMPORT_SCHEMA_VERSION });
        }
    }

    // Type

    String larType = headerElement.attributeValue("type");

    if (!larType.equals("portlet")) {
        throw new LARTypeException(larType, new String[] { "portlet" });
    }

    // Portlet compatibility

    String rootPortletId = headerElement.attributeValue("root-portlet-id");

    String expectedRootPortletId = PortletIdCodec.decodePortletName(portletId);

    if (!expectedRootPortletId.equals(rootPortletId)) {
        throw new PortletIdException(expectedRootPortletId);
    }

    String schemaVersion = GetterUtil.getString(portletElement.attributeValue("schema-version"), "1.0.0");

    PortletDataHandler portletDataHandler = _portletDataHandlerProvider.provide(companyId, portletId);

    if (!portletDataHandler.validateSchemaVersion(schemaVersion)) {
        throw new LayoutImportException(LayoutImportException.TYPE_WRONG_PORTLET_SCHEMA_VERSION,
                new Object[] { schemaVersion, portletId, portletDataHandler.getSchemaVersion() });
    }

    // Available locales

    if (portletDataHandler.isDataLocalized()) {
        List<Locale> sourceAvailableLocales = Arrays.asList(LocaleUtil
                .fromLanguageIds(StringUtil.split(headerElement.attributeValue("available-locales"))));

        for (Locale sourceAvailableLocale : sourceAvailableLocales) {
            if (!LanguageUtil.isAvailableLocale(_portal.getSiteGroupId(groupId), sourceAvailableLocale)) {

                LocaleException le = new LocaleException(LocaleException.TYPE_EXPORT_IMPORT);

                le.setSourceAvailableLocales(sourceAvailableLocales);
                le.setTargetAvailableLocales(LanguageUtil.getAvailableLocales(_portal.getSiteGroupId(groupId)));

                throw le;
            }
        }
    }
}

From source file:com.liferay.journal.service.impl.JournalArticleLocalServiceImpl.java

License:Open Source License

protected void validate(long companyId, long groupId, long classNameId, Map<Locale, String> titleMap,
        String content, String ddmStructureKey, String ddmTemplateKey, Date displayDate, Date expirationDate,
        boolean smallImage, String smallImageURL, File smallImageFile, byte[] smallImageBytes,
        ServiceContext serviceContext) throws PortalException {

    Locale articleDefaultLocale = LocaleUtil.fromLanguageId(LocalizationUtil.getDefaultLanguageId(content));

    if (!ExportImportThreadLocal.isImportInProcess()) {
        if (!LanguageUtil.isAvailableLocale(groupId, articleDefaultLocale)) {

            LocaleException le = new LocaleException(LocaleException.TYPE_CONTENT,
                    "The locale " + articleDefaultLocale + " is not available in site with groupId" + groupId);

            le.setSourceAvailableLocales(Collections.singleton(articleDefaultLocale));
            le.setTargetAvailableLocales(LanguageUtil.getAvailableLocales(groupId));

            throw le;
        }// w ww. j av  a2  s. com

        if ((expirationDate != null) && (expirationDate.before(new Date())
                || ((displayDate != null) && expirationDate.before(displayDate)))) {

            throw new ArticleExpirationDateException("Expiration date " + expirationDate + " is in the past");
        }
    }

    if ((classNameId == JournalArticleConstants.CLASSNAME_ID_DEFAULT)
            && (titleMap.isEmpty() || Validator.isNull(titleMap.get(articleDefaultLocale)))) {

        throw new ArticleTitleException("Title is null");
    }

    validateContent(content);

    DDMStructure ddmStructure = ddmStructureLocalService.getStructure(PortalUtil.getSiteGroupId(groupId),
            classNameLocalService.getClassNameId(JournalArticle.class), ddmStructureKey, true);

    validateDDMStructureFields(ddmStructure, classNameId, content, articleDefaultLocale);

    if (Validator.isNotNull(ddmTemplateKey)) {
        DDMTemplate ddmTemplate = ddmTemplateLocalService.getTemplate(PortalUtil.getSiteGroupId(groupId),
                classNameLocalService.getClassNameId(DDMStructure.class), ddmTemplateKey, true);

        if (ddmTemplate.getClassPK() != ddmStructure.getStructureId()) {
            throw new NoSuchTemplateException("{templateKey=" + ddmTemplateKey + "}");
        }
    } else if (classNameId == JournalArticleConstants.CLASSNAME_ID_DEFAULT) {
        throw new NoSuchTemplateException("DDM template key is null");
    }

    if (!smallImage || Validator.isNotNull(smallImageURL) || (smallImageFile == null)
            || (smallImageBytes == null)) {

        return;
    }

    String smallImageName = smallImageFile.getName();

    boolean validSmallImageExtension = false;

    for (String imageExtension : _journalFileUploadsConfiguration.imageExtensions()) {

        if (StringPool.STAR.equals(imageExtension) || StringUtil.endsWith(smallImageName, imageExtension)) {

            validSmallImageExtension = true;

            break;
        }
    }

    if (!validSmallImageExtension) {
        throw new ArticleSmallImageNameException(smallImageName);
    }

    long smallImageMaxSize = _journalFileUploadsConfiguration.smallImageMaxSize();

    if ((smallImageMaxSize > 0) && (smallImageBytes.length > smallImageMaxSize)) {

        throw new ArticleSmallImageSizeException(smallImageBytes.length + " exceeds " + smallImageMaxSize);
    }
}