1   /**
2    * Copyright (c) 2000-2009 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.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  /**
53   * <a href="ExportDataAction.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Alberto Montero
56   *
57   */
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 }