1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
56   * <a href="ExportDataAction.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Alberto Montero
59   *
60   */
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 }