Example usage for com.liferay.portal.kernel.exception LocaleException TYPE_CONTENT

List of usage examples for com.liferay.portal.kernel.exception LocaleException TYPE_CONTENT

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.exception LocaleException TYPE_CONTENT.

Prototype

int TYPE_CONTENT

To view the source code for com.liferay.portal.kernel.exception LocaleException TYPE_CONTENT.

Click Source Link

Usage

From source file:com.liferay.dynamic.data.mapping.service.impl.DDMStructureLocalServiceImpl.java

License:Open Source License

protected void validate(Map<Locale, String> nameMap, Locale contentDefaultLocale) throws PortalException {

    String name = nameMap.get(contentDefaultLocale);

    if (Validator.isNull(name)) {
        throw new StructureNameException("Name is null for locale " + contentDefaultLocale.getDisplayName());
    }/*from www . j  av  a2 s  . c om*/

    if (!LanguageUtil.isAvailableLocale(contentDefaultLocale)) {
        Long companyId = CompanyThreadLocal.getCompanyId();

        LocaleException le = new LocaleException(LocaleException.TYPE_CONTENT,
                StringBundler.concat("The locale ", String.valueOf(contentDefaultLocale),
                        " is not available in company ", String.valueOf(companyId)));

        le.setSourceAvailableLocales(Collections.singleton(contentDefaultLocale));
        le.setTargetAvailableLocales(LanguageUtil.getAvailableLocales());

        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;
        }//from   w w w. ja v a  2  s  .  c  o m

        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);
    }
}

From source file:com.liferay.journal.util.impl.JournalUtil.java

License:Open Source License

public static String prepareLocalizedContentForImport(String content, Locale defaultImportLocale)
        throws LocaleException {

    try {//from  w w  w . jav a 2s . c  o  m
        Document oldDocument = SAXReaderUtil.read(content);

        Document newDocument = SAXReaderUtil.read(content);

        Element newRootElement = newDocument.getRootElement();

        Attribute availableLocalesAttribute = newRootElement.attribute("available-locales");

        if (availableLocalesAttribute == null) {
            newRootElement = newRootElement.addAttribute("available-locales", StringPool.BLANK);

            availableLocalesAttribute = newRootElement.attribute("available-locales");
        }

        String defaultImportLanguageId = LocaleUtil.toLanguageId(defaultImportLocale);

        if (!StringUtil.contains(availableLocalesAttribute.getValue(), defaultImportLanguageId)) {

            if (Validator.isNull(availableLocalesAttribute.getValue())) {
                availableLocalesAttribute.setValue(defaultImportLanguageId);
            } else {
                availableLocalesAttribute.setValue(
                        availableLocalesAttribute.getValue() + StringPool.COMMA + defaultImportLanguageId);
            }

            _mergeArticleContentUpdate(oldDocument, newRootElement,
                    LocaleUtil.toLanguageId(defaultImportLocale));

            content = XMLUtil.formatXML(newDocument);
        }

        Attribute defaultLocaleAttribute = newRootElement.attribute("default-locale");

        if (defaultLocaleAttribute == null) {
            newRootElement = newRootElement.addAttribute("default-locale", StringPool.BLANK);

            defaultLocaleAttribute = newRootElement.attribute("default-locale");
        }

        Locale defaultContentLocale = LocaleUtil.fromLanguageId(defaultLocaleAttribute.getValue());

        if (!LocaleUtil.equals(defaultContentLocale, defaultImportLocale)) {
            defaultLocaleAttribute.setValue(defaultImportLanguageId);

            content = XMLUtil.formatXML(newDocument);
        }
    } catch (Exception e) {
        throw new LocaleException(LocaleException.TYPE_CONTENT,
                "The locale " + defaultImportLocale + " is not available");
    }

    return content;
}