Example usage for com.liferay.portal.kernel.util StringUtil replaceLast

List of usage examples for com.liferay.portal.kernel.util StringUtil replaceLast

Introduction

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

Prototype

public static String replaceLast(String s, String[] oldSubs, String[] newSubs) 

Source Link

Document

Replaces the last occurrences of the elements of the string array with the corresponding elements of the new string array.

Usage

From source file:com.liferay.document.library.internal.util.DLValidatorImpl.java

License:Open Source License

protected String replaceDLCharLastBlacklist(String title) {
    String previousTitle = null;/*from w w w .  ja va  2 s .  com*/

    while (!title.equals(previousTitle)) {
        previousTitle = title;

        for (String blacklistLastChar : PropsValues.DL_CHAR_LAST_BLACKLIST) {

            if (blacklistLastChar.startsWith(UnicodeFormatter.UNICODE_PREFIX)) {

                blacklistLastChar = UnicodeFormatter.parseString(blacklistLastChar);
            }

            if (title.endsWith(blacklistLastChar)) {
                title = StringUtil.replaceLast(title, blacklistLastChar, StringPool.BLANK);
            }
        }
    }

    return title;
}

From source file:com.liferay.dynamic.data.mapping.internal.util.DDMIndexerImpl.java

License:Open Source License

@Override
public QueryFilter createFieldValueQueryFilter(String ddmStructureFieldName,
        Serializable ddmStructureFieldValue, Locale locale) throws Exception {

    BooleanQuery booleanQuery = new BooleanQueryImpl();

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

    DDMStructure structure = _ddmStructureLocalService
            .getStructure(GetterUtil.getLong(ddmStructureFieldNameParts[2]));

    String fieldName = StringUtil.replaceLast(ddmStructureFieldNameParts[3],
            StringPool.UNDERLINE.concat(LocaleUtil.toLanguageId(locale)), StringPool.BLANK);

    if (structure.hasField(fieldName)) {
        ddmStructureFieldValue = _ddm.getIndexedFieldValue(ddmStructureFieldValue,
                structure.getFieldType(fieldName));
    }/*from w  w w  .jav a  2 s.  co  m*/

    booleanQuery.addRequiredTerm(ddmStructureFieldName,
            StringPool.QUOTE + ddmStructureFieldValue + StringPool.QUOTE);

    return new QueryFilter(booleanQuery);
}

From source file:com.liferay.exportimport.content.processor.base.BaseTextExportImportContentProcessor.java

License:Open Source License

protected String replaceImportLinksToLayouts(PortletDataContext portletDataContext, String content)
        throws Exception {

    List<String> oldLinksToLayout = new ArrayList<>();
    List<String> newLinksToLayout = new ArrayList<>();

    Matcher matcher = importLinksToLayoutPattern.matcher(content);

    Map<Long, Long> layoutPlids = (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(Layout.class);

    String layoutsImportMode = MapUtil.getString(portletDataContext.getParameterMap(),
            PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE,
            PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE_MERGE_BY_LAYOUT_UUID);

    while (matcher.find()) {
        long oldPlid = GetterUtil.getLong(matcher.group(4));

        Long newPlid = MapUtil.getLong(layoutPlids, oldPlid);

        long oldGroupId = GetterUtil.getLong(matcher.group(6));

        long newGroupId = oldGroupId;

        long oldLayoutId = GetterUtil.getLong(matcher.group(1));

        long newLayoutId = oldLayoutId;

        Layout layout = LayoutLocalServiceUtil.fetchLayout(newPlid);

        if (layout != null) {
            newGroupId = layout.getGroupId();
            newLayoutId = layout.getLayoutId();
        } else if (_log.isDebugEnabled()) {
            StringBundler sb = new StringBundler(5);

            sb.append("Unable to get layout with plid ");
            sb.append(oldPlid);/*from  w  w  w . ja  v a 2  s  . c  o  m*/
            sb.append(", using layout ID  ");
            sb.append(newLayoutId);
            sb.append(" instead");

            _log.debug(sb.toString());
        }

        String oldLinkToLayout = matcher.group(0);

        String newLinkToLayout = StringUtil.replaceFirst(oldLinkToLayout,
                new String[] { StringPool.AT + oldPlid, String.valueOf(oldLayoutId) },
                new String[] { StringPool.BLANK, String.valueOf(newLayoutId) });

        if ((layout != null) && layout.isPublicLayout() && layoutsImportMode
                .equals(PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE_CREATED_FROM_PROTOTYPE)) {

            newLinkToLayout = StringUtil.replace(newLinkToLayout, "private-group", "public");
        }

        if ((oldGroupId != 0) && (oldGroupId != newGroupId)) {
            newLinkToLayout = StringUtil.replaceLast(newLinkToLayout, String.valueOf(oldGroupId),
                    String.valueOf(newGroupId));
        }

        oldLinksToLayout.add(oldLinkToLayout);
        newLinksToLayout.add(newLinkToLayout);
    }

    content = StringUtil.replace(content, ArrayUtil.toStringArray(oldLinksToLayout.toArray()),
            ArrayUtil.toStringArray(newLinksToLayout.toArray()));

    return content;
}

From source file:com.liferay.exportimport.internal.content.processor.LinksToLayoutsExportImportContentProcessor.java

License:Open Source License

protected String replaceImportLinksToLayouts(PortletDataContext portletDataContext, String content)
        throws Exception {

    List<String> oldLinksToLayout = new ArrayList<>();
    List<String> newLinksToLayout = new ArrayList<>();

    Matcher matcher = _importLinksToLayoutPattern.matcher(content);

    Map<Long, Long> layoutPlids = (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(Layout.class);

    String layoutsImportMode = MapUtil.getString(portletDataContext.getParameterMap(),
            PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE,
            PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE_MERGE_BY_LAYOUT_UUID);

    while (matcher.find()) {
        long oldPlid = GetterUtil.getLong(matcher.group(4));

        Long newPlid = MapUtil.getLong(layoutPlids, oldPlid);

        long oldGroupId = GetterUtil.getLong(matcher.group(6));

        long newGroupId = oldGroupId;

        long oldLayoutId = GetterUtil.getLong(matcher.group(1));

        long newLayoutId = oldLayoutId;

        Layout layout = _layoutLocalService.fetchLayout(newPlid);

        if (layout != null) {
            newGroupId = layout.getGroupId();
            newLayoutId = layout.getLayoutId();
        } else if (_log.isWarnEnabled()) {
            _log.warn("Unable to get layout with plid " + oldPlid);
        }/* w  w  w .  j av a  2 s  .  c o m*/

        String oldLinkToLayout = matcher.group(0);

        String newLinkToLayout = StringUtil.replaceFirst(oldLinkToLayout,
                new String[] { StringPool.AT + oldPlid, String.valueOf(oldLayoutId) },
                new String[] { StringPool.BLANK, String.valueOf(newLayoutId) });

        if ((layout != null) && layout.isPublicLayout() && layoutsImportMode
                .equals(PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE_CREATED_FROM_PROTOTYPE)) {

            newLinkToLayout = StringUtil.replace(newLinkToLayout, "private-group", "public");
        }

        if ((oldGroupId != 0) && (oldGroupId != newGroupId)) {
            newLinkToLayout = StringUtil.replaceLast(newLinkToLayout, String.valueOf(oldGroupId),
                    String.valueOf(newGroupId));
        }

        oldLinksToLayout.add(oldLinkToLayout);
        newLinksToLayout.add(newLinkToLayout);
    }

    content = StringUtil.replace(content, ArrayUtil.toStringArray(oldLinksToLayout.toArray()),
            ArrayUtil.toStringArray(newLinksToLayout.toArray()));

    return content;
}

From source file:com.liferay.exportimport.resources.importer.internal.util.ResourceImporter.java

License:Open Source License

@Override
protected void addJournalArticles(String ddmStructureKey, String ddmTemplateKey, String dirName, long folderId)
        throws Exception {

    Set<String> resourcePaths = servletContext.getResourcePaths(_getResourcePath(dirName));

    if (resourcePaths == null) {
        return;//from ww  w . ja v a2s.c o  m
    }

    for (String resourcePath : resourcePaths) {
        if (resourcePath.endsWith(StringPool.SLASH)) {
            String folderName = FileUtil.getShortFileName(
                    StringUtil.replaceLast(resourcePath, CharPool.FORWARD_SLASH, StringPool.BLANK));

            JournalFolder journalFolder = journalFolderLocalService.fetchFolder(groupId, folderName);

            if (journalFolder == null) {
                journalFolder = journalFolderLocalService.addFolder(userId, groupId, folderId, folderName,
                        StringPool.BLANK, serviceContext);
            }

            addJournalArticles(ddmStructureKey, ddmTemplateKey, dirName + CharPool.FORWARD_SLASH + folderName,
                    journalFolder.getFolderId());
        } else {
            String name = FileUtil.getShortFileName(resourcePath);

            URL url = servletContext.getResource(resourcePath);

            URLConnection urlConnection = url.openConnection();

            addJournalArticles(ddmStructureKey, ddmTemplateKey, name, folderId, urlConnection.getInputStream());
        }
    }
}

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

License:Open Source License

@Override
public void postProcessContextQuery(BooleanQuery contextQuery, SearchContext searchContext) throws Exception {

    addStatus(contextQuery, searchContext);

    if (searchContext.isIncludeAttachments()) {
        addRelatedClassNames(contextQuery, searchContext);
    }//from   ww  w  .  ja v  a 2s. co m

    contextQuery.addRequiredTerm(Field.HIDDEN, searchContext.isIncludeAttachments());

    addSearchClassTypeIds(contextQuery, searchContext);

    String ddmStructureFieldName = (String) searchContext.getAttribute("ddmStructureFieldName");
    Serializable ddmStructureFieldValue = searchContext.getAttribute("ddmStructureFieldValue");

    if (Validator.isNotNull(ddmStructureFieldName) && Validator.isNotNull(ddmStructureFieldValue)) {

        String[] ddmStructureFieldNameParts = StringUtil.split(ddmStructureFieldName, StringPool.SLASH);

        DDMStructure structure = DDMStructureLocalServiceUtil
                .getStructure(GetterUtil.getLong(ddmStructureFieldNameParts[1]));

        String fieldName = StringUtil.replaceLast(ddmStructureFieldNameParts[2],
                StringPool.UNDERLINE.concat(LocaleUtil.toLanguageId(searchContext.getLocale())),
                StringPool.BLANK);

        try {
            ddmStructureFieldValue = DDMUtil.getIndexedFieldValue(ddmStructureFieldValue,
                    structure.getFieldType(fieldName));
        } catch (StructureFieldException sfe) {
        }

        contextQuery.addRequiredTerm(ddmStructureFieldName,
                StringPool.QUOTE + ddmStructureFieldValue + StringPool.QUOTE);
    }

    String[] mimeTypes = (String[]) searchContext.getAttribute("mimeTypes");

    if (ArrayUtil.isNotEmpty(mimeTypes)) {
        BooleanQuery mimeTypesQuery = BooleanQueryFactoryUtil.create(searchContext);

        for (String mimeType : mimeTypes) {
            mimeTypesQuery.addTerm("mimeType",
                    StringUtil.replace(mimeType, CharPool.FORWARD_SLASH, CharPool.UNDERLINE));
        }

        contextQuery.add(mimeTypesQuery, BooleanClauseOccur.MUST);
    }
}

From source file:com.liferay.portlet.journal.util.JournalArticleIndexer.java

License:Open Source License

@Override
public void postProcessContextQuery(BooleanQuery contextQuery, SearchContext searchContext) throws Exception {

    Long classNameId = (Long) searchContext.getAttribute(Field.CLASS_NAME_ID);

    if ((classNameId != null) && (classNameId.longValue() != 0)) {
        contextQuery.addRequiredTerm("classNameId", classNameId.toString());
    }/*from   w w  w.j av  a 2s . c  om*/

    addStatus(contextQuery, searchContext);

    addSearchClassTypeIds(contextQuery, searchContext);

    String ddmStructureFieldName = (String) searchContext.getAttribute("ddmStructureFieldName");
    Serializable ddmStructureFieldValue = searchContext.getAttribute("ddmStructureFieldValue");

    if (Validator.isNotNull(ddmStructureFieldName) && Validator.isNotNull(ddmStructureFieldValue)) {

        String[] ddmStructureFieldNameParts = StringUtil.split(ddmStructureFieldName, StringPool.SLASH);

        DDMStructure structure = DDMStructureLocalServiceUtil
                .getStructure(GetterUtil.getLong(ddmStructureFieldNameParts[1]));

        String fieldName = StringUtil.replaceLast(ddmStructureFieldNameParts[2],
                StringPool.UNDERLINE.concat(LocaleUtil.toLanguageId(searchContext.getLocale())),
                StringPool.BLANK);

        try {
            ddmStructureFieldValue = DDMUtil.getIndexedFieldValue(ddmStructureFieldValue,
                    structure.getFieldType(fieldName));
        } catch (StructureFieldException sfe) {
        }

        contextQuery.addRequiredTerm(ddmStructureFieldName,
                StringPool.QUOTE + ddmStructureFieldValue + StringPool.QUOTE);
    }

    String articleType = (String) searchContext.getAttribute("articleType");

    if (Validator.isNotNull(articleType)) {
        contextQuery.addRequiredTerm(Field.TYPE, articleType);
    }

    String ddmStructureKey = (String) searchContext.getAttribute("ddmStructureKey");

    if (Validator.isNotNull(ddmStructureKey)) {
        contextQuery.addRequiredTerm("ddmStructureKey", ddmStructureKey);
    }

    String ddmTemplateKey = (String) searchContext.getAttribute("ddmTemplateKey");

    if (Validator.isNotNull(ddmTemplateKey)) {
        contextQuery.addRequiredTerm("ddmTemplateKey", ddmTemplateKey);
    }
}

From source file:com.liferay.tasks.service.persistence.impl.TasksEntryFinderImpl.java

License:Open Source License

public int countByG_U_P_A_S_T_N(long groupId, long userId, int priority, long assigneeUserId, int status,
        long[] assetTagIds, long[] notAssetTagIds) {

    if ((priority <= 0) && (assetTagIds.length == 0) && (notAssetTagIds.length == 0)) {

        if ((userId > 0) && (assigneeUserId <= 0)) {
            return countByG_U_S(groupId, userId, status);
        }/*from w ww  . ja v  a  2  s.c o m*/

        if ((userId <= 0) && (assigneeUserId > 0)) {
            return countByG_A_S(groupId, assigneeUserId, status);
        }
    }

    Session session = null;

    try {
        session = openSession();

        String sql = CustomSQLUtil.get(COUNT_BY_G_U_P_A_S_T_N);

        sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(assetTagIds, notAssetTagIds));
        sql = StringUtil.replace(sql, "[$ASSET_TAG_TAG_IDS$]", getAssetTagTagIds(assetTagIds, notAssetTagIds));

        if (assetTagIds.length > 0) {
            sql = StringUtil.replaceLast(sql, "WHERE", StringPool.BLANK);
        }

        sql = StringUtil.replace(sql, "[$GROUP_ID$]", getGroupId(groupId));
        sql = StringUtil.replace(sql, "[$USER_ID$]", getUserId(userId));
        sql = StringUtil.replace(sql, "[$PRIORITY$]", getPriority(priority));
        sql = StringUtil.replace(sql, "[$ASSIGNEE_USER_ID$]", getAssigneeUserId(assigneeUserId));

        int[] statuses = getStatuses(status);

        sql = StringUtil.replace(sql, "[$STATUS$]", getStatus(statuses));

        sql = StringUtil.replaceLast(sql, "AND", StringPool.BLANK);

        SQLQuery q = session.createSynchronizedSQLQuery(sql);

        q.addScalar(COUNT_COLUMN_NAME, Type.LONG);

        QueryPos qPos = QueryPos.getInstance(q);

        if ((assetTagIds.length > 0) || (notAssetTagIds.length > 0)) {
            qPos.add(PortalUtil.getClassNameId(TasksEntry.class.getName()));
            qPos.add(assetTagIds);
            qPos.add(notAssetTagIds);
        }

        if (groupId > 0) {
            qPos.add(groupId);
        }

        if (userId > 0) {
            qPos.add(userId);
        }

        if (priority > 0) {
            qPos.add(priority);
        }

        if (assigneeUserId > 0) {
            qPos.add(assigneeUserId);
        }

        qPos.add(statuses);

        Iterator<Long> itr = q.iterate();

        if (itr.hasNext()) {
            Long count = itr.next();

            if (count != null) {
                return count.intValue();
            }
        }

        return 0;
    } catch (Exception e) {
        throw new SystemException(e);
    } finally {
        closeSession(session);
    }
}

From source file:com.liferay.tasks.service.persistence.impl.TasksEntryFinderImpl.java

License:Open Source License

public List<TasksEntry> findByG_U_P_A_S_T_N(long groupId, long userId, int priority, long assigneeUserId,
        int status, long[] assetTagIds, long[] notAssetTagIds, int start, int end) {

    if ((priority <= 0) && (assetTagIds.length == 0) && (notAssetTagIds.length == 0)) {

        if ((userId > 0) && (assigneeUserId <= 0)) {
            return findByG_U_S(groupId, userId, status, start, end);
        }//  w  w w.ja v a 2s .  c  o  m

        if ((userId <= 0) && (assigneeUserId > 0)) {
            return findByG_A_S(groupId, assigneeUserId, status, start, end);
        }
    }

    Session session = null;

    try {
        session = openSession();

        String sql = CustomSQLUtil.get(FIND_BY_G_U_P_A_S_T_N);

        sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(assetTagIds, notAssetTagIds));
        sql = StringUtil.replace(sql, "[$ASSET_TAG_TAG_IDS$]", getAssetTagTagIds(assetTagIds, notAssetTagIds));

        if (assetTagIds.length > 0) {
            sql = StringUtil.replaceLast(sql, "WHERE", StringPool.BLANK);
        }

        sql = StringUtil.replace(sql, "[$GROUP_ID$]", getGroupId(groupId));
        sql = StringUtil.replace(sql, "[$USER_ID$]", getUserId(userId));
        sql = StringUtil.replace(sql, "[$PRIORITY$]", getPriority(priority));
        sql = StringUtil.replace(sql, "[$ASSIGNEE_USER_ID$]", getAssigneeUserId(assigneeUserId));

        int[] statuses = getStatuses(status);

        sql = StringUtil.replace(sql, "[$STATUS$]", getStatus(statuses));

        sql = StringUtil.replaceLast(sql, "AND", StringPool.BLANK);

        SQLQuery q = session.createSynchronizedSQLQuery(sql);

        q.addEntity("TMS_TasksEntry", TasksEntryImpl.class);

        QueryPos qPos = QueryPos.getInstance(q);

        if ((assetTagIds.length > 0) || (notAssetTagIds.length > 0)) {
            qPos.add(PortalUtil.getClassNameId(TasksEntry.class.getName()));
            qPos.add(assetTagIds);
            qPos.add(notAssetTagIds);
        }

        if (groupId > 0) {
            qPos.add(groupId);
        }

        if (userId > 0) {
            qPos.add(userId);
        }

        if (priority > 0) {
            qPos.add(priority);
        }

        if (assigneeUserId > 0) {
            qPos.add(assigneeUserId);
        }

        qPos.add(statuses);

        return (List<TasksEntry>) QueryUtil.list(q, getDialect(), start, end);
    } catch (Exception e) {
        throw new SystemException(e);
    } finally {
        closeSession(session);
    }
}

From source file:com.liferay.tools.sourceformatter.BaseSourceProcessor.java

License:Open Source License

protected String sortAttributes(String fileName, String line, int lineCount, boolean allowApostropheDelimeter) {

    String s = line;//  ww w. ja v a  2  s. c om

    int x = s.indexOf(StringPool.SPACE);

    if (x == -1) {
        return line;
    }

    s = s.substring(x + 1);

    String previousAttribute = null;
    String previousAttributeAndValue = null;

    boolean wrongOrder = false;

    for (x = 0;;) {
        x = s.indexOf(StringPool.EQUAL);

        if ((x == -1) || (s.length() <= (x + 1))) {
            return line;
        }

        String attribute = s.substring(0, x);

        if (!isAttributName(attribute)) {
            return line;
        }

        if (Validator.isNotNull(previousAttribute) && (previousAttribute.compareTo(attribute) > 0)) {

            wrongOrder = true;
        }

        s = s.substring(x + 1);

        char delimeter = s.charAt(0);

        if ((delimeter != CharPool.APOSTROPHE) && (delimeter != CharPool.QUOTE)) {

            if (delimeter != CharPool.AMPERSAND) {
                processErrorMessage(fileName, "delimeter: " + fileName + " " + lineCount);
            }

            return line;
        }

        s = s.substring(1);

        String value = null;

        int y = -1;

        while (true) {
            y = s.indexOf(delimeter, y + 1);

            if ((y == -1) || (s.length() <= (y + 1))) {
                return line;
            }

            value = s.substring(0, y);

            if (value.startsWith("<%")) {
                int endJavaCodeSignCount = StringUtil.count(value, "%>");
                int startJavaCodeSignCount = StringUtil.count(value, "<%");

                if (endJavaCodeSignCount == startJavaCodeSignCount) {
                    break;
                }
            } else {
                int greaterThanCount = StringUtil.count(value, StringPool.GREATER_THAN);
                int lessThanCount = StringUtil.count(value, StringPool.LESS_THAN);

                if (greaterThanCount == lessThanCount) {
                    break;
                }
            }
        }

        if (delimeter == CharPool.APOSTROPHE) {
            if (!value.contains(StringPool.QUOTE)) {
                line = StringUtil.replace(line, StringPool.APOSTROPHE + value + StringPool.APOSTROPHE,
                        StringPool.QUOTE + value + StringPool.QUOTE);

                return sortAttributes(fileName, line, lineCount, allowApostropheDelimeter);
            } else if (!allowApostropheDelimeter) {
                String newValue = StringUtil.replace(value, StringPool.QUOTE, "&quot;");

                line = StringUtil.replace(line, StringPool.APOSTROPHE + value + StringPool.APOSTROPHE,
                        StringPool.QUOTE + newValue + StringPool.QUOTE);

                return sortAttributes(fileName, line, lineCount, allowApostropheDelimeter);
            }
        }

        StringBundler sb = new StringBundler(5);

        sb.append(attribute);
        sb.append(StringPool.EQUAL);
        sb.append(delimeter);
        sb.append(value);
        sb.append(delimeter);

        String currentAttributeAndValue = sb.toString();

        if (wrongOrder) {
            if ((StringUtil.count(line, currentAttributeAndValue) == 1)
                    && (StringUtil.count(line, previousAttributeAndValue) == 1)) {

                line = StringUtil.replaceFirst(line, previousAttributeAndValue, currentAttributeAndValue);

                line = StringUtil.replaceLast(line, currentAttributeAndValue, previousAttributeAndValue);

                return sortAttributes(fileName, line, lineCount, allowApostropheDelimeter);
            }

            return line;
        }

        s = s.substring(y + 1);

        if (s.startsWith(StringPool.GREATER_THAN)) {
            x = s.indexOf(StringPool.SPACE);

            if (x == -1) {
                return line;
            }

            s = s.substring(x + 1);

            previousAttribute = null;
            previousAttributeAndValue = null;
        } else {
            s = StringUtil.trimLeading(s);

            previousAttribute = attribute;
            previousAttributeAndValue = currentAttributeAndValue;
        }
    }
}