Example usage for com.liferay.portal.kernel.util FileUtil stripExtension

List of usage examples for com.liferay.portal.kernel.util FileUtil stripExtension

Introduction

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

Prototype

public static String stripExtension(String fileName) 

Source Link

Usage

From source file:com.liferay.portlet.documentlibrary.store.AdvancedFileSystemStore.java

License:Open Source License

@Override
protected File getFileNameDir(long companyId, long repositoryId, String fileName) {

    if (fileName.indexOf(CharPool.SLASH) != -1) {
        return getDirNameDir(companyId, repositoryId, fileName);
    }//from w w w  . j a  v a 2 s  . co m

    String ext = StringPool.PERIOD + FileUtil.getExtension(fileName);

    if (ext.equals(StringPool.PERIOD)) {
        ext += _HOOK_EXTENSION;
    }

    StringBundler sb = new StringBundler();

    String fileNameFragment = FileUtil.stripExtension(fileName);

    if (fileNameFragment.startsWith("DLFE-")) {
        fileNameFragment = fileNameFragment.substring(5);

        sb.append("DLFE" + StringPool.SLASH);
    }

    buildPath(sb, fileNameFragment);

    File repositoryDir = getRepositoryDir(companyId, repositoryId);

    File fileNameDir = new File(
            repositoryDir + StringPool.SLASH + sb.toString() + StringPool.SLASH + fileNameFragment + ext);

    return fileNameDir;
}

From source file:com.liferay.portlet.documentlibrary.store.AdvancedFileSystemStore.java

License:Open Source License

@Override
protected File getFileNameVersionFile(long companyId, long repositoryId, String fileName, String version) {

    String ext = StringPool.PERIOD + FileUtil.getExtension(fileName);

    if (ext.equals(StringPool.PERIOD)) {
        ext += _HOOK_EXTENSION;//from www  .j  av a 2s  .  c  om
    }

    int pos = fileName.lastIndexOf(CharPool.SLASH);

    if (pos == -1) {
        StringBundler sb = new StringBundler();

        String fileNameFragment = FileUtil.stripExtension(fileName);

        if (fileNameFragment.startsWith("DLFE-")) {
            fileNameFragment = fileNameFragment.substring(5);

            sb.append("DLFE" + StringPool.SLASH);
        }

        buildPath(sb, fileNameFragment);

        File repositoryDir = getRepositoryDir(companyId, repositoryId);

        return new File(repositoryDir + StringPool.SLASH + sb.toString() + StringPool.SLASH + fileNameFragment
                + ext + StringPool.SLASH + fileNameFragment + StringPool.UNDERLINE + version + ext);
    } else {
        File fileNameDir = getDirNameDir(companyId, repositoryId, fileName);

        String fileNameFragment = FileUtil.stripExtension(fileName.substring(pos + 1));

        return new File(
                fileNameDir + StringPool.SLASH + fileNameFragment + StringPool.UNDERLINE + version + ext);
    }
}

From source file:com.liferay.portlet.documentlibrary.util.DLImpl.java

License:Open Source License

@Override
public String getFileName(long groupId, long folderId, String tempFileName) {

    String extension = FileUtil.getExtension(tempFileName);

    int pos = tempFileName.lastIndexOf(EditFileEntryAction.TEMP_RANDOM_SUFFIX);

    if (pos != -1) {
        tempFileName = tempFileName.substring(0, pos);

        if (Validator.isNotNull(extension)) {
            tempFileName = tempFileName + StringPool.PERIOD + extension;
        }//ww  w .  j a  v  a 2 s .co m
    }

    while (true) {
        try {
            DLAppLocalServiceUtil.getFileEntry(groupId, folderId, tempFileName);

            StringBundler sb = new StringBundler(5);

            sb.append(FileUtil.stripExtension(tempFileName));
            sb.append(StringPool.DASH);
            sb.append(StringUtil.randomString());

            if (Validator.isNotNull(extension)) {
                sb.append(StringPool.PERIOD);
                sb.append(extension);
            }

            tempFileName = sb.toString();
        } catch (Exception e) {
            break;
        }
    }

    return tempFileName;
}

From source file:com.liferay.resourcesimporter.util.FileSystemImporter.java

License:Open Source License

protected void addApplicationDisplayTemplate(String script, File file, long classNameId)
        throws PortalException {

    String fileName = FileUtil.stripExtension(file.getName());

    String name = getName(fileName);

    DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.fetchTemplate(groupId, classNameId, getKey(fileName));

    if (ddmTemplate != null) {
        if (!developerModeEnabled) {
            if (_log.isInfoEnabled()) {
                _log.info("DDM template with name " + name + " and version " + version + " already exists");
            }// w  ww. j a v  a 2  s .co  m

            return;
        }

        if (!updateModeEnabled) {
            DDMTemplateLocalServiceUtil.deleteTemplate(ddmTemplate);
        }
    }

    try {
        if (!updateModeEnabled || (ddmTemplate == null)) {
            DDMTemplateLocalServiceUtil.addTemplate(userId, groupId, classNameId, 0, getKey(fileName),
                    getMap(name), null, DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY, StringPool.BLANK,
                    getDDMTemplateLanguage(name), script, false, false, StringPool.BLANK, null, serviceContext);
        } else {
            DDMTemplateLocalServiceUtil.updateTemplate(ddmTemplate.getTemplateId(), ddmTemplate.getClassPK(),
                    getMap(name), null, DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY, StringPool.BLANK,
                    getDDMTemplateLanguage(name), script, false, serviceContext);
        }
    } catch (PortalException e) {
        if (_log.isWarnEnabled()) {
            _log.warn("Unable to import application display template " + file.getName(), e);
        }

        throw e;
    }
}

From source file:com.liferay.resourcesimporter.util.FileSystemImporter.java

License:Open Source License

protected void addDDMStructures(String fileName, InputStream inputStream) throws Exception {

    fileName = FileUtil.stripExtension(fileName);

    String name = getName(fileName);

    DDMStructure ddmStructure = DDMStructureLocalServiceUtil.fetchStructure(groupId,
            PortalUtil.getClassNameId(DDLRecordSet.class), getKey(fileName));

    if (ddmStructure != null) {
        if (!developerModeEnabled) {
            if (_log.isInfoEnabled()) {
                _log.info("DDM structure with name " + name + " and version " + version + " already exists");
            }//from   w w w  . j a va  2  s.c  o m

            return;
        }

        if (!updateModeEnabled) {
            DDMStructureLocalServiceUtil.deleteDDMStructure(ddmStructure);
        }
    }

    try {
        if (!updateModeEnabled || (ddmStructure == null)) {
            ddmStructure = DDMStructureLocalServiceUtil.addStructure(userId, groupId,
                    DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID,
                    PortalUtil.getClassNameId(DDLRecordSet.class), getKey(fileName), getMap(name), null,
                    StringUtil.read(inputStream), PropsUtil.get(PropsKeys.DYNAMIC_DATA_LISTS_STORAGE_TYPE),
                    DDMStructureConstants.TYPE_DEFAULT, serviceContext);
        } else {
            ddmStructure = DDMStructureLocalServiceUtil.updateStructure(ddmStructure.getStructureId(),
                    DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID, getMap(name), null,
                    StringUtil.read(inputStream), serviceContext);
        }
    } catch (Exception e) {
        if (_log.isWarnEnabled()) {
            _log.warn("Unable to import DDM structure " + fileName, e);
        }

        throw e;
    }

    addDDLDisplayTemplates(ddmStructure.getStructureKey(), _DDL_STRUCTURE_DISPLAY_TEMPLATE_DIR_NAME, fileName);

    addDDLFormTemplates(ddmStructure.getStructureKey(), _DDL_STRUCTURE_FORM_TEMPLATE_DIR_NAME, fileName);
}

From source file:com.liferay.resourcesimporter.util.FileSystemImporter.java

License:Open Source License

protected void addDDMStructures(String parentDDMStructureKey, String fileName, InputStream inputStream)
        throws Exception {

    fileName = FileUtil.stripExtension(fileName);

    String name = getName(fileName);

    DDMStructure ddmStructure = DDMStructureLocalServiceUtil.fetchStructure(groupId,
            PortalUtil.getClassNameId(JournalArticle.class), getKey(fileName));

    if (ddmStructure != null) {
        if (!developerModeEnabled) {
            if (_log.isInfoEnabled()) {
                _log.info("DDM structure with name " + name + " and version " + version + " already exists");
            }/*from  www. ja  v a2 s  .c o  m*/

            return;
        }

        if (!updateModeEnabled) {
            DDMStructureLocalServiceUtil.deleteDDMStructure(ddmStructure);
        }
    }

    String xsd = StringUtil.read(inputStream);

    if (isJournalStructureXSD(xsd)) {
        xsd = JournalConverterUtil.getDDMXSD(xsd);
    }

    setServiceContext(fileName);

    try {
        if (!updateModeEnabled || (ddmStructure == null)) {
            ddmStructure = DDMStructureLocalServiceUtil.addStructure(userId, groupId, parentDDMStructureKey,
                    PortalUtil.getClassNameId(JournalArticle.class), getKey(fileName), getMap(name), null, xsd,
                    PropsUtil.get(PropsKeys.JOURNAL_ARTICLE_STORAGE_TYPE), DDMStructureConstants.TYPE_DEFAULT,
                    serviceContext);
        } else {
            DDMStructure parentStructure = DDMStructureLocalServiceUtil.fetchStructure(groupId,
                    PortalUtil.getClassNameId(JournalArticle.class), parentDDMStructureKey);

            long parentDDMStructureId = DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID;

            if (parentStructure != null) {
                parentDDMStructureId = parentStructure.getStructureId();
            }

            ddmStructure = DDMStructureLocalServiceUtil.updateStructure(ddmStructure.getStructureId(),
                    parentDDMStructureId, getMap(name), null, xsd, serviceContext);
        }
    } catch (PortalException e) {
        if (_log.isWarnEnabled()) {
            _log.warn("Unable to import DDM structure " + fileName, e);
        }

        throw e;
    }

    _ddmStructures.add(ddmStructure.getStructureKey());

    addDDMTemplates(ddmStructure.getStructureKey(), _JOURNAL_DDM_TEMPLATES_DIR_NAME + fileName);

    if (Validator.isNull(parentDDMStructureKey)) {
        addDDMStructures(ddmStructure.getStructureKey(), _JOURNAL_DDM_STRUCTURES_DIR_NAME + fileName);
    }
}

From source file:com.liferay.resourcesimporter.util.FileSystemImporter.java

License:Open Source License

protected void addDDMTemplate(long templateGroupId, long ddmStructureId, String fileName, String language,
        String script, String type, String mode) throws Exception {

    fileName = FileUtil.getShortFileName(fileName);

    fileName = FileUtil.stripExtension(fileName);

    String name = getName(fileName);

    DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.fetchTemplate(groupId,
            PortalUtil.getClassNameId(DDMStructure.class), getKey(fileName));

    if (ddmTemplate != null) {
        if (!developerModeEnabled) {
            if (_log.isInfoEnabled()) {
                _log.info("DDM template with name " + name + " and version " + version + " already exists");
            }//from  w w w  . j av a  2  s.  c o m

            return;
        }

        if (!updateModeEnabled) {
            DDMTemplateLocalServiceUtil.deleteTemplate(ddmTemplate);
        }
    }

    try {
        if (!updateModeEnabled || (ddmTemplate == null)) {
            DDMTemplateLocalServiceUtil.addTemplate(userId, templateGroupId,
                    PortalUtil.getClassNameId(DDMStructure.class), ddmStructureId, getKey(fileName),
                    getMap(name), null, type, mode, language, script, false, false, StringPool.BLANK, null,
                    serviceContext);
        } else {
            DDMTemplateLocalServiceUtil.updateTemplate(ddmTemplate.getTemplateId(),
                    PortalUtil.getClassNameId(DDMStructure.class), getMap(name), null,
                    DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY, null, language, script, false, false, null,
                    null, serviceContext);
        }
    } catch (PortalException e) {
        if (_log.isWarnEnabled()) {
            _log.warn("Unable to import DDM template " + fileName, e);
        }

        throw e;
    }
}

From source file:com.liferay.resourcesimporter.util.FileSystemImporter.java

License:Open Source License

protected void addDDMTemplates(String ddmStructureKey, String fileName, InputStream inputStream)
        throws Exception {

    fileName = FileUtil.stripExtension(fileName);

    String name = getName(fileName);

    String xsl = StringUtil.read(inputStream);

    setServiceContext(fileName);/* ww w .j a  va 2  s . c  o  m*/

    DDMStructure ddmStructure = DDMStructureLocalServiceUtil.getStructure(groupId,
            PortalUtil.getClassNameId(JournalArticle.class), ddmStructureKey);

    DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.fetchTemplate(groupId,
            PortalUtil.getClassNameId(DDMStructure.class), getKey(fileName));

    if (ddmTemplate != null) {
        if (!developerModeEnabled) {
            if (_log.isInfoEnabled()) {
                _log.info("DDM template with name " + name + " and version " + version + " already exists");
            }

            return;
        }

        if (!updateModeEnabled) {
            DDMTemplateLocalServiceUtil.deleteTemplate(ddmTemplate);
        }
    }

    try {
        if (!updateModeEnabled || (ddmTemplate == null)) {
            ddmTemplate = DDMTemplateLocalServiceUtil.addTemplate(userId, groupId,
                    PortalUtil.getClassNameId(DDMStructure.class), ddmStructure.getStructureId(),
                    getKey(fileName), getMap(name), null, DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY, null,
                    getDDMTemplateLanguage(fileName), replaceFileEntryURL(xsl), false, false, null, null,
                    serviceContext);
        } else {
            ddmTemplate = DDMTemplateLocalServiceUtil.updateTemplate(ddmTemplate.getTemplateId(),
                    PortalUtil.getClassNameId(DDMStructure.class), getMap(name), null,
                    DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY, null, getDDMTemplateLanguage(fileName),
                    replaceFileEntryURL(xsl), false, false, null, null, serviceContext);
        }
    } catch (PortalException e) {
        if (_log.isWarnEnabled()) {
            _log.warn("Unable to import DDM template " + fileName, e);
        }

        throw e;
    }

    addJournalArticles(ddmStructureKey, ddmTemplate.getTemplateKey(), _JOURNAL_ARTICLES_DIR_NAME + fileName);
}

From source file:com.liferay.resourcesimporter.util.FileSystemImporter.java

License:Open Source License

protected void addDLFileEntry(long parentFolderId, String fileName, InputStream inputStream, long length)
        throws Exception {

    String title = FileUtil.stripExtension(fileName);

    setServiceContext(fileName);/*from  w w w.j av a  2s.co m*/

    FileEntry fileEntry = null;

    try {
        try {
            fileEntry = DLAppLocalServiceUtil.addFileEntry(userId, groupId, parentFolderId, fileName,
                    MimeTypesUtil.getContentType(fileName), title, StringPool.BLANK, StringPool.BLANK,
                    inputStream, length, serviceContext);
        } catch (DuplicateFileException dfe) {
            fileEntry = DLAppLocalServiceUtil.getFileEntry(groupId, parentFolderId, title);

            String previousVersion = fileEntry.getVersion();

            fileEntry = DLAppLocalServiceUtil.updateFileEntry(userId, fileEntry.getFileEntryId(), fileName,
                    MimeTypesUtil.getContentType(fileName), title, StringPool.BLANK, StringPool.BLANK, true,
                    inputStream, length, serviceContext);

            DLFileEntryLocalServiceUtil.deleteFileVersion(fileEntry.getUserId(), fileEntry.getFileEntryId(),
                    previousVersion);
        }
    } catch (PortalException e) {
        if (_log.isWarnEnabled()) {
            _log.warn("Unable to import DL file entry " + fileName, e);
        }

        throw e;
    }

    addPrimaryKey(DLFileEntry.class.getName(), fileEntry.getPrimaryKey());

    _fileEntries.put(fileName, fileEntry);
}

From source file:com.liferay.resourcesimporter.util.FileSystemImporter.java

License:Open Source License

protected void addJournalArticles(String ddmStructureKey, String ddmTemplateKey, String fileName,
        InputStream inputStream) throws Exception {

    String title = FileUtil.stripExtension(fileName);

    JSONObject assetJSONObject = _assetJSONObjectMap.get(fileName);

    Map<Locale, String> descriptionMap = null;

    boolean indexable = true;

    if (assetJSONObject != null) {
        String abstractSummary = assetJSONObject.getString("abstractSummary");

        descriptionMap = getMap(abstractSummary);

        indexable = GetterUtil.getBoolean(assetJSONObject.getString("indexable"), true);
    }//from   www. j  a  va  2  s.c  o m

    String content = StringUtil.read(inputStream);

    content = replaceFileEntryURL(content);

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

    boolean smallImage = false;
    String smallImageURL = StringPool.BLANK;

    if (assetJSONObject != null) {
        String smallImageFileName = assetJSONObject.getString("smallImage");

        if (Validator.isNotNull(smallImageFileName)) {
            smallImage = true;

            FileEntry fileEntry = _fileEntries.get(smallImageFileName);

            if (fileEntry != null) {
                smallImageURL = DLUtil.getPreviewURL(fileEntry, fileEntry.getFileVersion(), null,
                        StringPool.BLANK);
            }
        }
    }

    setServiceContext(fileName);

    String journalArticleId = getJournalId(fileName);

    JournalArticle journalArticle = JournalArticleLocalServiceUtil.fetchLatestArticle(groupId, journalArticleId,
            WorkflowConstants.STATUS_ANY);

    try {
        if (journalArticle == null) {
            journalArticle = JournalArticleLocalServiceUtil.addArticle(userId, groupId, 0, 0, 0,
                    journalArticleId, false, JournalArticleConstants.VERSION_DEFAULT,
                    getMap(articleDefaultLocale, title), descriptionMap, content, ddmStructureKey,
                    ddmTemplateKey, StringPool.BLANK, 1, 1, 2010, 0, 0, 0, 0, 0, 0, 0, true, 0, 0, 0, 0, 0,
                    true, indexable, smallImage, smallImageURL, null, new HashMap<String, byte[]>(),
                    StringPool.BLANK, serviceContext);
        } else {
            journalArticle = JournalArticleLocalServiceUtil.updateArticle(userId, groupId, 0, journalArticleId,
                    journalArticle.getVersion(), getMap(articleDefaultLocale, title), descriptionMap, content,
                    ddmStructureKey, ddmTemplateKey, StringPool.BLANK, 1, 1, 2010, 0, 0, 0, 0, 0, 0, 0, true, 0,
                    0, 0, 0, 0, true, indexable, smallImage, smallImageURL, null, new HashMap<String, byte[]>(),
                    StringPool.BLANK, serviceContext);
        }

        JournalArticleLocalServiceUtil.updateStatus(userId, groupId, journalArticle.getArticleId(),
                journalArticle.getVersion(), WorkflowConstants.STATUS_APPROVED, StringPool.BLANK,
                new HashMap<String, Serializable>(), serviceContext);
    } catch (PortalException e) {
        if (_log.isWarnEnabled()) {
            _log.warn("Unable to import journal article " + fileName, e);
        }

        throw e;
    }

    addPrimaryKey(JournalArticle.class.getName(), journalArticle.getPrimaryKey());
}