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