1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.webform.action;
16  
17  import com.liferay.portal.kernel.dao.orm.QueryUtil;
18  import com.liferay.portal.kernel.util.ContentTypes;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portal.kernel.util.Validator;
21  import com.liferay.portal.struts.ActionConstants;
22  import com.liferay.portal.struts.PortletAction;
23  import com.liferay.portal.util.PortalUtil;
24  import com.liferay.portlet.PortletPreferencesFactoryUtil;
25  import com.liferay.portlet.expando.model.ExpandoRow;
26  import com.liferay.portlet.expando.service.ExpandoRowLocalServiceUtil;
27  import com.liferay.portlet.expando.service.ExpandoValueLocalServiceUtil;
28  import com.liferay.portlet.webform.util.WebFormUtil;
29  import com.liferay.util.servlet.ServletResponseUtil;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  import javax.portlet.ActionRequest;
35  import javax.portlet.ActionResponse;
36  import javax.portlet.PortletConfig;
37  import javax.portlet.PortletPreferences;
38  
39  import javax.servlet.http.HttpServletResponse;
40  
41  import org.apache.struts.action.ActionForm;
42  import org.apache.struts.action.ActionMapping;
43  
44  /**
45   * <a href="ExportDataAction.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Alberto Montero
48   */
49  public class ExportDataAction extends PortletAction {
50  
51      public void processAction(
52              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
53              ActionRequest actionRequest, ActionResponse actionResponse)
54          throws Exception {
55  
56          PortletPreferences preferences =
57              PortletPreferencesFactoryUtil.getPortletSetup(actionRequest);
58  
59          String databaseTableName = preferences.getValue(
60              "databaseTableName", StringPool.BLANK);
61          String title = preferences.getValue("title", "no-title");
62  
63          StringBuilder sb = new StringBuilder();
64  
65          List<String> fieldLabels = new ArrayList<String>();
66  
67          for (int i = 1; true; i++) {
68              String fieldLabel = preferences.getValue(
69                  "fieldLabel" + i, StringPool.BLANK);
70  
71              if (Validator.isNull(fieldLabel)) {
72                  break;
73              }
74  
75              fieldLabels.add(fieldLabel);
76  
77              sb.append("\"");
78              sb.append(fieldLabel.replaceAll("\"", "\\\""));
79              sb.append("\";");
80          }
81  
82          sb.deleteCharAt(sb.length() - 1);
83          sb.append("\n");
84  
85          if (Validator.isNotNull(databaseTableName)) {
86              List<ExpandoRow> rows = ExpandoRowLocalServiceUtil.getRows(
87                  WebFormUtil.class.getName(), databaseTableName,
88                  QueryUtil.ALL_POS, QueryUtil.ALL_POS);
89  
90              for (ExpandoRow row : rows) {
91                  for (String fieldName : fieldLabels) {
92                      String data = ExpandoValueLocalServiceUtil.getData(
93                          WebFormUtil.class.getName(), databaseTableName,
94                          fieldName, row.getClassPK(), StringPool.BLANK);
95  
96                      data = data.replaceAll("\"", "\\\"");
97  
98                      sb.append("\"");
99                      sb.append(data);
100                     sb.append("\";");
101                 }
102 
103                 sb.deleteCharAt(sb.length() - 1);
104                 sb.append("\n");
105             }
106         }
107 
108         HttpServletResponse response = PortalUtil.getHttpServletResponse(
109             actionResponse);
110         byte[] bytes = sb.toString().getBytes();
111         String fileName = title + ".csv";
112         String contentType = ContentTypes.APPLICATION_TEXT;
113 
114         ServletResponseUtil.sendFile(response, fileName, bytes, contentType);
115 
116         setForward(actionRequest, ActionConstants.COMMON_NULL);
117     }
118 
119 }