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.util.ArrayList;
40 import java.util.List;
41
42 import javax.portlet.ActionRequest;
43 import javax.portlet.ActionResponse;
44 import javax.portlet.PortletConfig;
45 import javax.portlet.PortletPreferences;
46
47 import javax.servlet.http.HttpServletResponse;
48
49 import org.apache.struts.action.ActionForm;
50 import org.apache.struts.action.ActionMapping;
51
52
57 public class ExportDataAction extends PortletAction {
58
59 public void processAction(
60 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
61 ActionRequest actionRequest, ActionResponse actionResponse)
62 throws Exception {
63
64 PortletPreferences preferences =
65 PortletPreferencesFactoryUtil.getPortletSetup(actionRequest);
66
67 String databaseTableName = preferences.getValue(
68 "databaseTableName", StringPool.BLANK);
69 String title = preferences.getValue("title", "no-title");
70
71 StringBuilder sb = new StringBuilder();
72
73 List<String> fieldLabels = new ArrayList<String>();
74
75 for (int i = 1; true; i++) {
76 String fieldLabel = preferences.getValue(
77 "fieldLabel" + i, StringPool.BLANK);
78
79 if (Validator.isNull(fieldLabel)) {
80 break;
81 }
82
83 fieldLabels.add(fieldLabel);
84
85 sb.append("\"");
86 sb.append(fieldLabel.replaceAll("\"", "\\\""));
87 sb.append("\";");
88 }
89
90 sb.deleteCharAt(sb.length() - 1);
91 sb.append("\n");
92
93 if (Validator.isNotNull(databaseTableName)) {
94 List<ExpandoRow> rows = ExpandoRowLocalServiceUtil.getRows(
95 WebFormUtil.class.getName(), databaseTableName,
96 QueryUtil.ALL_POS, QueryUtil.ALL_POS);
97
98 for (ExpandoRow row : rows) {
99 for (String fieldName : fieldLabels) {
100 String data = ExpandoValueLocalServiceUtil.getData(
101 WebFormUtil.class.getName(), databaseTableName,
102 fieldName, row.getClassPK(), StringPool.BLANK);
103
104 data = data.replaceAll("\"", "\\\"");
105
106 sb.append("\"");
107 sb.append(data);
108 sb.append("\";");
109 }
110
111 sb.deleteCharAt(sb.length() - 1);
112 sb.append("\n");
113 }
114 }
115
116 HttpServletResponse response = PortalUtil.getHttpServletResponse(
117 actionResponse);
118 byte[] bytes = sb.toString().getBytes();
119 String fileName = title + ".csv";
120 String contentType = ContentTypes.APPLICATION_TEXT;
121
122 ServletResponseUtil.sendFile(response, fileName, bytes, contentType);
123
124 setForward(actionRequest, ActionConstants.COMMON_NULL);
125 }
126
127 }