Example usage for com.liferay.portal.kernel.util ContentTypes APPLICATION_TEXT

List of usage examples for com.liferay.portal.kernel.util ContentTypes APPLICATION_TEXT

Introduction

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

Prototype

String APPLICATION_TEXT

To view the source code for com.liferay.portal.kernel.util ContentTypes APPLICATION_TEXT.

Click Source Link

Usage

From source file:com.fmdp.webform.portlet.WebFormPortlet.java

License:Open Source License

protected void exportData(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) resourceRequest.getAttribute(WebKeys.THEME_DISPLAY);

    String portletId = PortalUtil.getPortletId(resourceRequest);

    PortletPermissionUtil.check(themeDisplay.getPermissionChecker(), themeDisplay.getPlid(), portletId,
            ActionKeys.CONFIGURATION);//from   w w w  .ja  va  2  s  . c  o  m

    PortletPreferences preferences = PortletPreferencesFactoryUtil.getPortletSetup(resourceRequest);

    String databaseTableName = preferences.getValue("databaseTableName", StringPool.BLANK);
    String title = preferences.getValue("title", "no-title");

    StringBundler sb = new StringBundler();

    List<String> fieldLabels = new ArrayList<String>();

    for (int i = 1; true; i++) {
        String fieldLabel = preferences.getValue("fieldLabel" + i, StringPool.BLANK);

        String localizedfieldLabel = LocalizationUtil.getPreferencesValue(preferences, "fieldLabel" + i,
                themeDisplay.getLanguageId());

        if (Validator.isNull(fieldLabel)) {
            break;
        }

        fieldLabels.add(fieldLabel);

        sb.append(getCSVFormattedValue(localizedfieldLabel));
        sb.append(PortletPropsValues.CSV_SEPARATOR);

    }
    fieldLabels.add("email-from");
    sb.append(getCSVFormattedValue("email-from"));
    sb.append(PortletPropsValues.CSV_SEPARATOR);

    fieldLabels.add("email-sent-on");
    sb.append(getCSVFormattedValue("email-sent-on"));
    sb.append(PortletPropsValues.CSV_SEPARATOR);

    sb.setIndex(sb.index() - 1);

    sb.append(CharPool.NEW_LINE);

    if (Validator.isNotNull(databaseTableName)) {
        List<ExpandoRow> rows = ExpandoRowLocalServiceUtil.getRows(themeDisplay.getCompanyId(),
                WebFormUtil.class.getName(), databaseTableName, QueryUtil.ALL_POS, QueryUtil.ALL_POS);

        for (ExpandoRow row : rows) {

            for (String fieldName : fieldLabels) {

                String data = ExpandoValueLocalServiceUtil.getData(themeDisplay.getCompanyId(),
                        WebFormUtil.class.getName(), databaseTableName, fieldName, row.getClassPK(),
                        StringPool.BLANK);

                sb.append(getCSVFormattedValue(data));
                sb.append(PortletPropsValues.CSV_SEPARATOR);
            }

            sb.setIndex(sb.index() - 1);

            sb.append(CharPool.NEW_LINE);
        }
    }

    String fileName = title + ".csv";
    byte[] bytes = sb.toString().getBytes();
    String contentType = ContentTypes.APPLICATION_TEXT;

    PortletResponseUtil.sendFile(resourceRequest, resourceResponse, fileName, bytes, contentType);
}

From source file:com.liferay.webform.portlet.WebFormPortlet.java

License:Open Source License

protected void exportData(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) resourceRequest.getAttribute(WebKeys.THEME_DISPLAY);

    String portletId = PortalUtil.getPortletId(resourceRequest);

    PortletPermissionUtil.check(themeDisplay.getPermissionChecker(), themeDisplay.getPlid(), portletId,
            ActionKeys.CONFIGURATION);/*  ww  w  .ja  v a  2 s  . c  om*/

    PortletPreferences preferences = PortletPreferencesFactoryUtil.getPortletSetup(resourceRequest);

    String databaseTableName = preferences.getValue("databaseTableName", StringPool.BLANK);
    String title = preferences.getValue("title", "no-title");

    StringBundler sb = new StringBundler();

    List<String> fieldLabels = new ArrayList<String>();

    for (int i = 1; true; i++) {
        String fieldLabel = preferences.getValue("fieldLabel" + i, StringPool.BLANK);

        String localizedfieldLabel = LocalizationUtil.getPreferencesValue(preferences, "fieldLabel" + i,
                themeDisplay.getLanguageId());

        if (Validator.isNull(fieldLabel)) {
            break;
        }

        fieldLabels.add(fieldLabel);

        sb.append(getCSVFormattedValue(localizedfieldLabel));
        sb.append(PortletPropsValues.CSV_SEPARATOR);
    }

    sb.setIndex(sb.index() - 1);

    sb.append(CharPool.NEW_LINE);

    if (Validator.isNotNull(databaseTableName)) {
        List<ExpandoRow> rows = ExpandoRowLocalServiceUtil.getRows(themeDisplay.getCompanyId(),
                WebFormUtil.class.getName(), databaseTableName, QueryUtil.ALL_POS, QueryUtil.ALL_POS);

        for (ExpandoRow row : rows) {
            for (String fieldName : fieldLabels) {
                String data = ExpandoValueLocalServiceUtil.getData(themeDisplay.getCompanyId(),
                        WebFormUtil.class.getName(), databaseTableName, fieldName, row.getClassPK(),
                        StringPool.BLANK);

                sb.append(getCSVFormattedValue(data));
                sb.append(PortletPropsValues.CSV_SEPARATOR);
            }

            sb.setIndex(sb.index() - 1);

            sb.append(CharPool.NEW_LINE);
        }
    }

    String fileName = title + ".csv";
    byte[] bytes = sb.toString().getBytes();
    String contentType = ContentTypes.APPLICATION_TEXT;

    PortletResponseUtil.sendFile(resourceRequest, resourceResponse, fileName, bytes, contentType);
}

From source file:org.gfbio.ContactFormToHelpdeskPortlet.java

License:Open Source License

protected void exportData(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) resourceRequest.getAttribute(WebKeys.THEME_DISPLAY);

    String portletId = PortalUtil.getPortletId(resourceRequest);

    PortletPermissionUtil.check(themeDisplay.getPermissionChecker(), themeDisplay.getPlid(), portletId,
            ActionKeys.CONFIGURATION);/*from w w  w  .java  2s.  c  o  m*/

    PortletPreferences preferences = PortletPreferencesFactoryUtil.getPortletSetup(resourceRequest);

    String databaseTableName = preferences.getValue("databaseTableName", StringPool.BLANK);
    String title = preferences.getValue("title", "no-title");

    StringBuilder sb = new StringBuilder();

    List<String> fieldLabels = new ArrayList<String>();

    for (int i = 1; true; i++) {
        String fieldLabel = preferences.getValue("fieldLabel" + i, StringPool.BLANK);

        String localizedfieldLabel = LocalizationUtil.getPreferencesValue(preferences, "fieldLabel" + i,
                themeDisplay.getLanguageId());

        if (Validator.isNull(fieldLabel)) {
            break;
        }

        fieldLabels.add(fieldLabel);

        sb.append("\"");
        sb.append(localizedfieldLabel.replaceAll("\"", "\\\""));
        sb.append("\";");
    }

    sb.deleteCharAt(sb.length() - 1);
    sb.append("\n");

    if (Validator.isNotNull(databaseTableName)) {
        List<ExpandoRow> rows = ExpandoRowLocalServiceUtil.getRows(themeDisplay.getCompanyId(),
                WebFormUtil.class.getName(), databaseTableName, QueryUtil.ALL_POS, QueryUtil.ALL_POS);

        for (ExpandoRow row : rows) {
            for (String fieldName : fieldLabels) {
                String data = ExpandoValueLocalServiceUtil.getData(themeDisplay.getCompanyId(),
                        WebFormUtil.class.getName(), databaseTableName, fieldName, row.getClassPK(),
                        StringPool.BLANK);

                data = data.replaceAll("\"", "\\\"");

                sb.append("\"");
                sb.append(data);
                sb.append("\";");
            }

            sb.deleteCharAt(sb.length() - 1);
            sb.append("\n");
        }
    }

    String fileName = title + ".csv";
    byte[] bytes = sb.toString().getBytes();
    String contentType = ContentTypes.APPLICATION_TEXT;

    PortletResponseUtil.sendFile(resourceRequest, resourceResponse, fileName, bytes, contentType);
}