1
22
23 package com.liferay.portlet.webform.action;
24
25 import com.liferay.portal.kernel.dao.orm.QueryUtil;
26 import com.liferay.portal.kernel.util.ContentTypes;
27 import com.liferay.portal.kernel.util.StringPool;
28 import com.liferay.portal.kernel.util.Validator;
29 import com.liferay.portal.struts.ActionConstants;
30 import com.liferay.portal.struts.PortletAction;
31 import com.liferay.portal.util.PortalUtil;
32 import com.liferay.portlet.PortletPreferencesFactoryUtil;
33 import com.liferay.portlet.expando.model.ExpandoRow;
34 import com.liferay.portlet.expando.service.ExpandoRowLocalServiceUtil;
35 import com.liferay.portlet.expando.service.ExpandoValueLocalServiceUtil;
36 import com.liferay.portlet.webform.util.WebFormUtil;
37 import com.liferay.util.servlet.ServletResponseUtil;
38
39 import java.io.ByteArrayInputStream;
40 import java.io.InputStream;
41
42 import java.util.ArrayList;
43 import java.util.List;
44
45 import javax.portlet.ActionRequest;
46 import javax.portlet.ActionResponse;
47 import javax.portlet.PortletConfig;
48 import javax.portlet.PortletPreferences;
49
50 import javax.servlet.http.HttpServletResponse;
51
52 import org.apache.struts.action.ActionForm;
53 import org.apache.struts.action.ActionMapping;
54
55
61 public class ExportDataAction extends PortletAction {
62
63 public void processAction(
64 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
65 ActionRequest actionRequest, ActionResponse actionResponse)
66 throws Exception {
67
68 PortletPreferences prefs =
69 PortletPreferencesFactoryUtil.getPortletSetup(actionRequest);
70
71 String databaseTableName = prefs.getValue(
72 "databaseTableName", StringPool.BLANK);
73 String title = prefs.getValue("title", "no-title");
74
75 StringBuilder sb = new StringBuilder();
76
77 List<String> fieldLabels = new ArrayList<String>();
78
79 for (int i = 1; i <= WebFormUtil.MAX_FIELDS; i++) {
80 String fieldLabel = prefs.getValue(
81 "fieldLabel" + i, StringPool.BLANK);
82
83 if (Validator.isNotNull(fieldLabel)) {
84 fieldLabels.add(fieldLabel);
85
86 sb.append("\"");
87 sb.append(fieldLabel.replaceAll("\"", "\\\""));
88 sb.append("\";");
89 }
90 }
91
92 sb.deleteCharAt(sb.length() - 1);
93 sb.append("\n");
94
95 if (Validator.isNotNull(databaseTableName)) {
96 List<ExpandoRow> rows = ExpandoRowLocalServiceUtil.getRows(
97 WebFormUtil.class.getName(), databaseTableName,
98 QueryUtil.ALL_POS, QueryUtil.ALL_POS);
99
100 for (ExpandoRow row : rows) {
101 for (String fieldName : fieldLabels) {
102 String data = ExpandoValueLocalServiceUtil.getData(
103 WebFormUtil.class.getName(), databaseTableName,
104 fieldName, row.getClassPK(), StringPool.BLANK);
105
106 data = data.replaceAll("\"", "\\\"");
107
108 sb.append("\"");
109 sb.append(data);
110 sb.append("\";");
111 }
112
113 sb.deleteCharAt(sb.length() - 1);
114 sb.append("\n");
115 }
116 }
117
118 HttpServletResponse response = PortalUtil.getHttpServletResponse(
119 actionResponse);
120 String fileName = title + ".csv";
121 InputStream is = new ByteArrayInputStream(sb.toString().getBytes());
122 String contentType = ContentTypes.APPLICATION_TEXT;
123
124 ServletResponseUtil.sendFile(response, fileName, is, contentType);
125
126 setForward(actionRequest, ActionConstants.COMMON_NULL);
127 }
128
129 }