|
4 | 4 | import lombok.EqualsAndHashCode; |
5 | 5 | import org.jumpserver.chen.framework.console.action.DataViewAction; |
6 | 6 | import org.jumpserver.chen.framework.console.component.Logger; |
| 7 | +import org.jumpserver.chen.framework.console.dataview.export.DataExport; |
7 | 8 | import org.jumpserver.chen.framework.console.entity.response.SQLResult; |
8 | 9 | import org.jumpserver.chen.framework.console.state.DataViewState; |
9 | 10 | import org.jumpserver.chen.framework.console.state.StateManager; |
10 | | -import org.jumpserver.chen.framework.datasource.entity.resource.Field; |
11 | 11 | import org.jumpserver.chen.framework.datasource.sql.SQLQueryParams; |
12 | 12 | import org.jumpserver.chen.framework.datasource.sql.SQLQueryResult; |
13 | 13 | import org.jumpserver.chen.framework.jms.entity.CommandRecord; |
14 | 14 | import org.jumpserver.chen.framework.session.SessionManager; |
15 | | -import org.jumpserver.chen.framework.utils.CodeUtils; |
16 | 15 | import org.jumpserver.chen.framework.ws.io.PacketIO; |
17 | 16 |
|
18 | | -import java.io.BufferedWriter; |
19 | | -import java.io.IOException; |
20 | | -import java.nio.file.Files; |
21 | | -import java.sql.Clob; |
| 17 | +import java.io.File; |
22 | 18 | import java.sql.SQLException; |
23 | | -import java.text.SimpleDateFormat; |
24 | | -import java.time.LocalDateTime; |
25 | | -import java.time.format.DateTimeFormatter; |
26 | | -import java.util.Date; |
27 | 19 | import java.util.HashMap; |
28 | 20 | import java.util.List; |
29 | 21 | import java.util.Map; |
@@ -74,7 +66,10 @@ public void doAction(DataViewAction action) throws SQLException { |
74 | 66 | this.changeLimit((int) action.getData()); |
75 | 67 | } |
76 | 68 | case DataViewAction.ACTION_EXPORT -> { |
77 | | - this.export((String) action.getData()); |
| 69 | + var data = (Map<String, String>) action.getData(); |
| 70 | + var scope = data.get("scope"); |
| 71 | + var format = data.get("format"); |
| 72 | + this.export(scope, format); |
78 | 73 | } |
79 | 74 | } |
80 | 75 | } |
@@ -134,84 +129,39 @@ private void fullDataViewData(DataViewData viewData, SQLQueryResult result) { |
134 | 129 | } |
135 | 130 |
|
136 | 131 |
|
137 | | - private static void writeString(BufferedWriter writer, Object object) throws IOException { |
138 | | - var str = object.toString(); |
139 | | - |
140 | | - if (str.contains(",")) { |
141 | | - str = "\"" + str + "\""; |
142 | | - } |
143 | | - writer.write(str); |
144 | | - } |
145 | | - |
146 | | - private void writeCSVData(BufferedWriter writer, DataViewData viewData) throws IOException, SQLException { |
147 | | - |
148 | | - for (Field field : viewData.getFields()) { |
149 | | - writeString(writer, field.getName()); |
150 | | - writer.write(","); |
151 | | - } |
152 | | - for (Map<String, Object> row : viewData.getData()) { |
153 | | - for (Field field : viewData.getFields()) { |
154 | | - var obj = row.get(field.getName()); |
155 | | - if (obj == null) { |
156 | | - writer.write("NULL"); |
157 | | - writer.write(","); |
158 | | - } else if (obj instanceof Clob clob) { |
159 | | - writer.write(CodeUtils.escapeCsvValue(clob.getSubString(1, (int) clob.length()))); |
160 | | - writer.write(","); |
161 | | - } else if (obj instanceof Date) { |
162 | | - SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
163 | | - writeString(writer, fmt.format(obj)); |
164 | | - } else { |
165 | | - writeString(writer, row.get(field.getName())); |
166 | | - writer.write(","); |
167 | | - } |
168 | | - } |
169 | | - writer.newLine(); |
170 | | - } |
171 | | - |
172 | | - writer.newLine(); |
173 | | - } |
174 | | - |
175 | | - public void export(String scope) throws SQLException { |
| 132 | + public void export(String scope, String format) throws SQLException { |
176 | 133 | var session = SessionManager.getCurrentSession(); |
177 | 134 |
|
178 | | - |
179 | | - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss"); |
180 | | - String timestamp = LocalDateTime.now().format(formatter); |
181 | | - var f = session.createFile(String.format("data_%s.csv", timestamp)); |
182 | | - |
183 | 135 | CommandRecord command = new CommandRecord(String.format("Export data: %s", this.title)); |
184 | 136 |
|
185 | 137 | try { |
186 | 138 | if (!SessionManager.getCurrentSession().canDownload()) { |
187 | | - session.getController().sendFile(f.getName()); |
188 | 139 | return; |
189 | 140 | } |
190 | | - var writer = Files.newBufferedWriter(f.toPath()); |
191 | | - |
192 | | - if (scope.equals("current")) { |
193 | | - this.writeCSVData(writer, this.data); |
194 | | - command.setOutput(String.format("%d rows exported", this.data.getData().size())); |
195 | | - } |
196 | | - |
197 | | - if (scope.equals("all")) { |
198 | | - SQLQueryParams queryParams = new SQLQueryParams(); |
199 | | - queryParams.setLimit(-1); |
200 | | - var result = this.loadDataInterface.loadData(queryParams); |
201 | | - var viewData = new DataViewData(); |
202 | | - this.fullDataViewData(viewData, result); |
203 | | - command.setOutput(String.format("%d rows exported", result.getData().size())); |
| 141 | + File f = null; |
| 142 | + switch (scope) { |
| 143 | + case "current": |
| 144 | + f = DataExport.export(format, this.data); |
| 145 | + command.setOutput(String.format("%d rows exported", this.data.getData().size())); |
| 146 | + break; |
| 147 | + case "all": |
| 148 | + SQLQueryParams queryParams = new SQLQueryParams(); |
| 149 | + queryParams.setLimit(-1); |
| 150 | + var result = this.loadDataInterface.loadData(queryParams); |
| 151 | + var viewData = new DataViewData(); |
| 152 | + this.fullDataViewData(viewData, result); |
| 153 | + f = DataExport.export(format, viewData); |
| 154 | + command.setOutput(String.format("%d rows exported", result.getData().size())); |
| 155 | + break; |
204 | 156 | } |
205 | | - writer.flush(); |
206 | | - writer.close(); |
207 | 157 |
|
208 | 158 | this.consoleLogger.success(command.getOutput()); |
209 | 159 | session.recordCommand(command); |
| 160 | + session.getController().sendFile(f.getName()); |
210 | 161 |
|
211 | | - } catch (IOException e) { |
| 162 | + } catch (Exception e) { |
212 | 163 | throw new RuntimeException(e); |
213 | 164 | } |
214 | | - session.getController().sendFile(f.getName()); |
215 | 165 | } |
216 | 166 |
|
217 | 167 |
|
|
0 commit comments