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

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

Introduction

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

Prototype

public static String randomId() 

Source Link

Document

Returns a randomized string of four lower case, alphabetic characters.

Usage

From source file:com.liferay.asset.publisher.upgrade.v1_0_0.test.UpgradePortletPreferencesTest.java

License:Open Source License

protected String getPortletId() {
    StringBundler sb = new StringBundler(3);

    sb.append(AssetPublisherPortletKeys.ASSET_PUBLISHER);
    sb.append("_INSTANCE_");
    sb.append(StringUtil.randomId());

    return sb.toString();
}

From source file:com.liferay.document.library.service.test.DLFileEntryTypeServiceTest.java

License:Open Source License

@Test
public void testUpdateFileEntryTypeWithEmptyDDMForm() throws Exception {
    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group,
            TestPropsValues.getUserId());

    DDMStructure ddmStructure = DDMStructureTestUtil.addStructure(_group.getGroupId(),
            DLFileEntryMetadata.class.getName());

    DDMForm ddmForm = new DDMForm();

    ddmForm.addDDMFormField(new DDMFormField("text", "Text"));
    ddmForm.setAvailableLocales(Collections.singleton(LocaleUtil.getDefault()));
    ddmForm.setDefaultLocale(LocaleUtil.getDefault());

    serviceContext.setAttribute("ddmForm", DDMBeanTranslatorUtil.translate(ddmForm));

    DLFileEntryType dlFileEntryType = DLFileEntryTypeServiceUtil.addFileEntryType(_group.getGroupId(),
            StringUtil.randomString(), StringUtil.randomString(), new long[] { ddmStructure.getStructureId() },
            serviceContext);/*  ww  w. j a  v  a  2 s.  c  o m*/

    serviceContext.setAttribute("ddmForm", DDMBeanTranslatorUtil.translate(new DDMForm()));

    long[] ddmStructureIds = _getDDMStructureIds(dlFileEntryType);

    DLFileEntryTypeServiceUtil.updateFileEntryType(dlFileEntryType.getFileEntryTypeId(),
            StringUtil.randomString(), StringUtil.randomId(), ddmStructureIds, serviceContext);

    dlFileEntryType = DLFileEntryTypeServiceUtil.getFileEntryType(dlFileEntryType.getFileEntryTypeId());

    List<com.liferay.dynamic.data.mapping.kernel.DDMStructure> ddmStructures = dlFileEntryType
            .getDDMStructures();

    Assert.assertEquals(ddmStructures.toString(), 1, ddmStructures.size());
}

From source file:com.liferay.dynamic.data.mapping.form.renderer.internal.DDMFormTemplateContextFactoryImpl.java

License:Open Source License

protected Map<String, Object> doCreate(DDMForm ddmForm, DDMFormLayout ddmFormLayout,
        DDMFormRenderingContext ddmFormRenderingContext) throws PortalException {

    Map<String, Object> templateContext = new HashMap<>();

    String containerId = ddmFormRenderingContext.getContainerId();

    if (Validator.isNull(containerId)) {
        containerId = StringUtil.randomId();
    }/*from www.  jav  a  2 s  .co m*/

    templateContext.put("containerId", containerId);

    templateContext.put("dataProviderSettings",
            _ddmFormTemplateContextFactoryHelper.getDataProviderSettings(ddmForm));

    setDDMFormFieldsEvaluableProperty(ddmForm);

    templateContext.put("evaluatorURL", getDDMFormContextProviderServletURL());

    List<DDMFormFieldType> ddmFormFieldTypes = _ddmFormFieldTypeServicesTracker.getDDMFormFieldTypes();

    templateContext.put("fieldTypes", _ddmFormFieldTypesJSONSerializer.serialize(ddmFormFieldTypes));

    templateContext.put("groupId", ddmFormRenderingContext.getGroupId());

    List<Object> pages = getPages(ddmForm, ddmFormLayout, ddmFormRenderingContext);

    templateContext.put("pages", pages);

    templateContext.put("portletNamespace", ddmFormRenderingContext.getPortletNamespace());
    templateContext.put("readOnly", ddmFormRenderingContext.isReadOnly());

    Locale locale = ddmFormRenderingContext.getLocale();

    if (locale == null) {
        locale = LocaleThreadLocal.getSiteDefaultLocale();
    }

    ResourceBundle resourceBundle = getResourceBundle(locale);

    SoyHTMLContextValue soyHTMLContextValue = new SoyHTMLContextValue(
            getRequiredFieldsWarningMessageHTML(resourceBundle));

    templateContext.put("requiredFieldsWarningMessageHTML", soyHTMLContextValue.getValue());

    templateContext.put("rules", toObjectList(ddmForm.getDDMFormRules()));
    templateContext.put("showRequiredFieldsWarning", ddmFormRenderingContext.isShowRequiredFieldsWarning());

    boolean showSubmitButton = ddmFormRenderingContext.isShowSubmitButton();

    if (ddmFormRenderingContext.isReadOnly()) {
        showSubmitButton = false;
    }

    templateContext.put("showSubmitButton", showSubmitButton);
    templateContext.put("strings", getLanguageStringsMap(resourceBundle));

    String submitLabel = GetterUtil.getString(ddmFormRenderingContext.getSubmitLabel(),
            LanguageUtil.get(locale, "submit"));

    templateContext.put("submitLabel", submitLabel);

    templateContext.put("templateNamespace", getTemplateNamespace(ddmFormLayout));

    return templateContext;
}

From source file:com.liferay.dynamic.data.mapping.internal.render.DDMFormFieldFreeMarkerRenderer.java

License:Open Source License

protected String getDDMFormFieldOptionHTML(HttpServletRequest request, HttpServletResponse response,
        DDMFormField ddmFormField, String mode, boolean readOnly, Locale locale,
        Map<String, Object> freeMarkerContext) throws Exception {

    StringBundler sb = new StringBundler();

    DDMFormFieldOptions ddmFormFieldOptions = ddmFormField.getDDMFormFieldOptions();

    for (String value : ddmFormFieldOptions.getOptionsValues()) {
        Map<String, Object> fieldStructure = new HashMap<>();

        fieldStructure.put("children", StringPool.BLANK);
        fieldStructure.put("fieldNamespace", StringUtil.randomId());

        LocalizedValue label = ddmFormFieldOptions.getOptionLabels(value);

        fieldStructure.put("label", label.getString(locale));

        fieldStructure.put("name", StringUtil.randomId());
        fieldStructure.put("value", value);

        freeMarkerContext.put("fieldStructure", fieldStructure);

        sb.append(processFTL(request, response, ddmFormField.getFieldNamespace(), "option", mode, readOnly,
                freeMarkerContext));/*from w  w  w.  ja va2s .  c o m*/
    }

    return sb.toString();
}

From source file:com.liferay.dynamic.data.mapping.internal.render.DDMFormFieldFreeMarkerRenderer.java

License:Open Source License

protected String getFieldHTML(HttpServletRequest request, HttpServletResponse response,
        DDMFormField ddmFormField, Fields fields, DDMFormField parentDDMFormField, String portletNamespace,
        String namespace, String mode, boolean readOnly, boolean showEmptyFieldLabel, Locale locale)
        throws Exception {

    Map<String, Object> freeMarkerContext = getFreeMarkerContext(request, response, portletNamespace, namespace,
            ddmFormField, parentDDMFormField, showEmptyFieldLabel, locale);

    if (fields != null) {
        freeMarkerContext.put("fields", fields);
    }/*  w  w  w. ja  v a  2 s  .c om*/

    Map<String, Object> fieldStructure = (Map<String, Object>) freeMarkerContext.get("fieldStructure");

    int fieldRepetition = 1;
    int offset = 0;

    DDMFieldsCounter ddmFieldsCounter = getFieldsCounter(request, response, fields, portletNamespace,
            namespace);

    String name = ddmFormField.getName();

    String fieldDisplayValue = getFieldsDisplayValue(request, response, fields);

    String[] fieldsDisplayValues = getFieldsDisplayValues(fieldDisplayValue);

    boolean fieldDisplayable = ArrayUtil.contains(fieldsDisplayValues, name);

    if (fieldDisplayable) {
        Map<String, Object> parentFieldStructure = (Map<String, Object>) freeMarkerContext
                .get("parentFieldStructure");

        String parentFieldName = (String) parentFieldStructure.get("name");

        offset = getFieldOffset(fieldsDisplayValues, name, ddmFieldsCounter.get(name));

        if (offset == fieldsDisplayValues.length) {
            return StringPool.BLANK;
        }

        fieldRepetition = countFieldRepetition(fieldsDisplayValues, parentFieldName, offset);
    }

    StringBundler sb = new StringBundler(fieldRepetition);

    while (fieldRepetition > 0) {
        offset = getFieldOffset(fieldsDisplayValues, name, ddmFieldsCounter.get(name));

        String fieldNamespace = StringUtil.randomId();

        if (fieldDisplayable) {
            fieldNamespace = getFieldNamespace(fieldDisplayValue, ddmFieldsCounter, offset);
        }

        fieldStructure.put("fieldNamespace", fieldNamespace);
        fieldStructure.put("valueIndex", ddmFieldsCounter.get(name));

        if (fieldDisplayable) {
            ddmFieldsCounter.incrementKey(name);
        }

        StringBundler childrenHTMLSB = new StringBundler(2);

        childrenHTMLSB.append(getHTML(request, response, ddmFormField.getNestedDDMFormFields(), fields,
                ddmFormField, portletNamespace, namespace, mode, readOnly, showEmptyFieldLabel, locale));

        if (Objects.equals(ddmFormField.getType(), "select")
                || Objects.equals(ddmFormField.getType(), "radio")) {

            Map<String, Object> optionFreeMarkerContext = new HashMap<>(freeMarkerContext);

            optionFreeMarkerContext.put("parentFieldStructure", fieldStructure);

            childrenHTMLSB.append(getDDMFormFieldOptionHTML(request, response, ddmFormField, mode, readOnly,
                    locale, optionFreeMarkerContext));
        }

        fieldStructure.put("children", childrenHTMLSB.toString());

        sb.append(processFTL(request, response, ddmFormField.getFieldNamespace(), ddmFormField.getType(), mode,
                readOnly, freeMarkerContext));

        fieldRepetition--;
    }

    return sb.toString();
}

From source file:com.liferay.flags.taglib.servlet.taglib.soy.FlagsTag.java

License:Open Source License

@Override
public int doStartTag() {
    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    String randomNamespace = StringUtil.randomId() + StringPool.UNDERLINE;

    try {/*from w w w  .  jav  a  2  s.  c  o  m*/
        Map<String, Object> context = getContext();

        boolean enabled = GetterUtil.getBoolean(context.get("enabled"), true);

        Company company = themeDisplay.getCompany();

        putValue("companyName", company.getName());

        putValue("flagsEnabled", _isFlagsEnabled(themeDisplay));

        putValue("formData", _getDataJSONObject(context));

        putValue("id", randomNamespace + "id");

        putValue("enabled", enabled);

        boolean label = GetterUtil.getBoolean(context.get("label"), true);

        putValue("label", label);

        String message = GetterUtil.getString(context.get("message"), LanguageUtil.get(request, "flag"));

        putValue("message", message);

        putValue("pathTermsOfUse", themeDisplay.getPathMain() + "/portal/terms_of_use");

        putValue("pathThemeImages", themeDisplay.getPathThemeImages());

        putValue("portletNamespace", PortalUtil.getPortletNamespace(PortletKeys.FLAGS));

        boolean signedIn = themeDisplay.isSignedIn();

        putValue("signedIn", signedIn);

        if (signedIn) {
            User user = themeDisplay.getUser();

            putValue("reporterEmailAddress", user.getEmailAddress());
        }

        String title = message;

        if (!enabled) {
            title = LanguageUtil.get(request,
                    "flags-are-disabled-because-this-entry-is-in-the-recycle-" + "bin");
        }

        putValue("title", title);

        putValue("uri", _getURI());

        putValue("reasons", _getReasons(themeDisplay.getCompanyId()));
    } catch (Exception e) {
        _log.error(e, e);
    }

    setTemplateNamespace("Flags.render");

    return super.doStartTag();
}

From source file:com.liferay.frontend.taglib.servlet.taglib.ManagementBarSidenavTogglerButtonTag.java

License:Open Source License

@Override
protected void setAttributes(HttpServletRequest request) {
    setAttributeNamespace(_ATTRIBUTE_NAMESPACE);

    if (Validator.isNull(getId())) {
        setId(StringUtil.randomId());
    }/*from   w  w  w.  j  a  v  a2s.co m*/

    setNamespacedAttribute(request, "href", _href);
    setNamespacedAttribute(request, "position", _position);
    setNamespacedAttribute(request, "type", _type);
    setNamespacedAttribute(request, "typeMobile", _typeMobile);
    setNamespacedAttribute(request, "width", _width);

    super.setAttributes(request);
}

From source file:com.liferay.frontend.taglib.soy.servlet.taglib.TemplateRendererTag.java

License:Open Source License

public String getComponentId() {
    if (Validator.isNull(_componentId)) {
        _componentId = StringUtil.randomId();
    }

    return _componentId;
}

From source file:com.liferay.journal.transformer.JournalTransformer.java

License:Open Source License

protected String doTransform(ThemeDisplay themeDisplay, Map<String, Object> contextObjects,
        Map<String, String> tokens, String viewMode, String languageId, Document document,
        PortletRequestModel portletRequestModel, String script, String langType, boolean propagateException)
        throws Exception {

    // Setup listeners

    if (_log.isDebugEnabled()) {
        _log.debug("Language " + languageId);
    }//from w  ww . j a v a2s . c  o  m

    if (Validator.isNull(viewMode)) {
        viewMode = Constants.VIEW;
    }

    if (_logTokens.isDebugEnabled()) {
        String tokensString = PropertiesUtil.list(tokens);

        _logTokens.debug(tokensString);
    }

    if (_logTransformBefore.isDebugEnabled()) {
        _logTransformBefore.debug(document);
    }

    List<TransformerListener> transformerListeners = JournalTransformerListenerRegistryUtil
            .getTransformerListeners();

    for (TransformerListener transformerListener : transformerListeners) {

        // Modify XML

        if (_logXmlBeforeListener.isDebugEnabled()) {
            _logXmlBeforeListener.debug(document);
        }

        if (transformerListener != null) {
            document = transformerListener.onXml(document, languageId, tokens);

            if (_logXmlAfterListener.isDebugEnabled()) {
                _logXmlAfterListener.debug(document);
            }
        }

        // Modify script

        if (_logScriptBeforeListener.isDebugEnabled()) {
            _logScriptBeforeListener.debug(script);
        }

        if (transformerListener != null) {
            script = transformerListener.onScript(script, document, languageId, tokens);

            if (_logScriptAfterListener.isDebugEnabled()) {
                _logScriptAfterListener.debug(script);
            }
        }
    }

    // Transform

    String output = null;

    if (Validator.isNull(langType)) {
        output = LocalizationUtil.getLocalization(document.asXML(), languageId);
    } else {
        long companyId = 0;
        long companyGroupId = 0;
        long articleGroupId = 0;
        long classNameId = 0;

        if (tokens != null) {
            companyId = GetterUtil.getLong(tokens.get("company_id"));
            companyGroupId = GetterUtil.getLong(tokens.get("company_group_id"));
            articleGroupId = GetterUtil.getLong(tokens.get("article_group_id"));
            classNameId = GetterUtil.getLong(tokens.get(TemplateConstants.CLASS_NAME_ID));
        }

        long scopeGroupId = 0;
        long siteGroupId = 0;

        if (themeDisplay != null) {
            companyId = themeDisplay.getCompanyId();
            companyGroupId = themeDisplay.getCompanyGroupId();
            scopeGroupId = themeDisplay.getScopeGroupId();
            siteGroupId = themeDisplay.getSiteGroupId();
        }

        String templateId = tokens.get("template_id");

        templateId = getTemplateId(templateId, companyId, companyGroupId, articleGroupId);

        Template template = getTemplate(templateId, tokens, languageId, document, script, langType);

        if (contextObjects != null) {
            template.putAll(contextObjects);
        }

        UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();

        try {
            if (document != null) {
                Element rootElement = document.getRootElement();

                List<TemplateNode> templateNodes = getTemplateNodes(themeDisplay, rootElement,
                        Long.valueOf(tokens.get("ddm_structure_id")));

                if (templateNodes != null) {
                    for (TemplateNode templateNode : templateNodes) {
                        template.put(templateNode.getName(), templateNode);
                    }
                }

                if (portletRequestModel != null) {
                    template.put("request", portletRequestModel.toMap());

                    if (langType.equals(TemplateConstants.LANG_TYPE_XSL)) {
                        Document requestDocument = SAXReaderUtil.read(portletRequestModel.toXML());

                        Element requestElement = requestDocument.getRootElement();

                        template.put("xmlRequest", requestElement.asXML());
                    }
                } else {
                    Element requestElement = rootElement.element("request");

                    template.put("request", insertRequestVariables(requestElement));

                    if (langType.equals(TemplateConstants.LANG_TYPE_XSL)) {
                        template.put("xmlRequest", requestElement.asXML());
                    }
                }
            }

            template.put("articleGroupId", articleGroupId);
            template.put("company", getCompany(themeDisplay, companyId));
            template.put("companyId", companyId);
            template.put("device", getDevice(themeDisplay));

            String templatesPath = getTemplatesPath(companyId, articleGroupId, classNameId);

            Locale locale = LocaleUtil.fromLanguageId(languageId);

            template.put("locale", locale);

            template.put("permissionChecker", PermissionThreadLocal.getPermissionChecker());
            template.put("randomNamespace", StringUtil.randomId() + StringPool.UNDERLINE);
            template.put("scopeGroupId", scopeGroupId);
            template.put("siteGroupId", siteGroupId);
            template.put("templatesPath", templatesPath);
            template.put("viewMode", viewMode);

            if (themeDisplay != null) {
                TemplateManager templateManager = TemplateManagerUtil.getTemplateManager(langType);

                HttpServletRequest request = themeDisplay.getRequest();

                templateManager.addTaglibSupport(template, request, themeDisplay.getResponse());
                templateManager.addTaglibTheme(template, "taglibLiferay", request,
                        new PipingServletResponse(themeDisplay.getResponse(), unsyncStringWriter));
            }

            // Deprecated variables

            template.put("groupId", articleGroupId);
            template.put("journalTemplatesPath", templatesPath);

            mergeTemplate(template, unsyncStringWriter, propagateException);
        } catch (Exception e) {
            if (e instanceof DocumentException) {
                throw new TransformException("Unable to read XML document", e);
            } else if (e instanceof IOException) {
                throw new TransformException("Error reading template", e);
            } else if (e instanceof TransformException) {
                throw (TransformException) e;
            } else {
                throw new TransformException("Unhandled exception", e);
            }
        }

        output = unsyncStringWriter.toString();
    }

    // Postprocess output

    for (TransformerListener transformerListener : transformerListeners) {

        // Modify output

        if (_logOutputBeforeListener.isDebugEnabled()) {
            _logOutputBeforeListener.debug(output);
        }

        output = transformerListener.onOutput(output, languageId, tokens);

        if (_logOutputAfterListener.isDebugEnabled()) {
            _logOutputAfterListener.debug(output);
        }
    }

    if (_logTransfromAfter.isDebugEnabled()) {
        _logTransfromAfter.debug(output);
    }

    return output;
}

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

License:Open Source License

protected void updateJournalXSDDynamicElement(Element element) {
    Locale defaultLocale = LocaleUtil.getSiteDefault();

    String name = element.attributeValue("name");
    String type = element.attributeValue("type");

    Element metadataElement = element.element("meta-data");

    if (metadataElement == null) {
        metadataElement = element.addElement("meta-data");
    }//from w w  w.j  a  v  a 2  s  .c om

    if (type.equals("multi-list")) {
        element.addAttribute("multiple", "true");
    } else if (type.equals("selection_break")) {
        Element parentElement = element.getParent();

        parentElement.remove(element);

        return;
    } else {
        Element parentElement = element.getParent();

        String parentType = parentElement.attributeValue("type");

        if ((parentType != null) && parentType.equals("select")) {
            metadataElement.addAttribute("locale", defaultLocale.toString());

            addMetadataEntry(metadataElement, "label", name);

            element.addAttribute("name", "option" + StringUtil.randomId());
            element.addAttribute("type", "option");
            element.addAttribute("value", name);

            return;
        }
    }

    String indexType = StringPool.BLANK;

    Attribute indexTypeAttribute = element.attribute("index-type");

    if (indexTypeAttribute != null) {
        indexType = indexTypeAttribute.getValue();

        element.remove(indexTypeAttribute);
    }

    element.remove(element.attribute("type"));

    String dataType = _ddmDataTypes.get(type);

    if (dataType == null) {
        dataType = "string";
    }

    element.addAttribute("dataType", dataType);
    element.addAttribute("indexType", indexType);

    String required = "false";

    Element requiredElement = fetchMetadataEntry(metadataElement, "name", "required");

    if (requiredElement != null) {
        required = requiredElement.getText();
    }

    element.addAttribute("required", required);

    element.addAttribute("showLabel", "true");

    String newType = _journalTypesToDDMTypes.get(type);

    if (newType == null) {
        newType = type;
    }

    element.addAttribute("type", newType);

    if (newType.startsWith("ddm")) {
        element.addAttribute("fieldNamespace", "ddm");
    }

    metadataElement.addAttribute("locale", defaultLocale.toString());

    List<Element> entryElements = metadataElement.elements();

    if (entryElements.isEmpty()) {
        addMetadataEntry(metadataElement, "label", name);
    } else {
        for (Element entryElement : entryElements) {
            String oldEntryName = entryElement.attributeValue("name");

            String newEntryName = _ddmMetadataAttributes.get(oldEntryName);

            if (newEntryName == null) {
                metadataElement.remove(entryElement);
            } else {
                entryElement.addAttribute("name", newEntryName);
            }
        }
    }

    if (newType.equals("ddm-date") || newType.equals("ddm-decimal") || newType.equals("ddm-integer")
            || newType.equals("ddm-link-to-page") || newType.equals("ddm-number")
            || newType.equals("ddm-text-html") || newType.equals("text") || newType.equals("textarea")) {

        element.addAttribute("width", "25");
    } else if (newType.equals("wcm-image")) {
        element.addAttribute("fieldNamespace", "wcm");
        element.addAttribute("readOnly", "false");
    }

    List<Element> dynamicElementElements = element.elements("dynamic-element");

    for (Element dynamicElementElement : dynamicElementElements) {
        updateJournalXSDDynamicElement(dynamicElementElement);
    }
}