Skip to content

Commit 8d8e0d6

Browse files
feat: download image and pdf
1 parent 79456ce commit 8d8e0d6

File tree

15 files changed

+368
-144
lines changed

15 files changed

+368
-144
lines changed

core/src/main/java/datart/core/base/consts/AttachmentType.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,23 @@
22

33
public enum AttachmentType {
44

5-
EXCEL,
5+
EXCEL(".xlsx"),
66

7-
IMAGE
7+
IMAGE(".png"),
88

9+
PDF(".pdf");
10+
11+
private String suffix;
12+
13+
AttachmentType(String suffix) {
14+
this.suffix = suffix;
15+
}
16+
17+
public void setSuffix(String suffix) {
18+
this.suffix = suffix;
19+
}
20+
21+
public String getSuffix() {
22+
return suffix;
23+
}
924
}

core/src/main/java/datart/core/common/Application.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public static <T> T getBean(Class<T> t) {
5151
return context.getBean(t);
5252
}
5353

54+
public static <T> T getBean(String beanName, Class<T> t) {
55+
return context.getBean(beanName, t);
56+
}
57+
5458
public static String getProperty(String key) {
5559
return context.getEnvironment().getProperty(key);
5660
}

core/src/main/java/datart/core/common/FileUtils.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ public static void mkdirParentIfNotExist(String path) {
5555
}
5656

5757
public static String withBasePath(String path) {
58-
return concatPath(Application.getFileBasePath(), path);
58+
String fileBasePath = Application.getFileBasePath();
59+
if (path.startsWith(fileBasePath)) {
60+
return path;
61+
}
62+
return concatPath(fileBasePath, path);
5963
}
6064

6165
public static void delete(String path) {

security/src/main/java/datart/security/base/ResourceType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public enum ResourceType {
66
VIEW,
77

88
DATACHART,
9+
WIDGET,
910
DASHBOARD,
1011
FOLDER,
1112
STORYBOARD,

server/src/main/java/datart/server/base/params/DownloadCreateParam.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package datart.server.base.params;
2020

21+
import datart.core.base.consts.AttachmentType;
2122
import lombok.Data;
2223
import lombok.EqualsAndHashCode;
2324

@@ -31,5 +32,9 @@ public class DownloadCreateParam extends BaseCreateParam {
3132

3233
private List<ViewExecuteParam> downloadParams;
3334

35+
private AttachmentType downloadType;
36+
37+
private int imageWidth;
38+
3439
}
3540

server/src/main/java/datart/server/base/params/ViewExecuteParam.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import datart.core.base.PageInfo;
2222
import datart.core.data.provider.sql.*;
23+
import datart.security.base.ResourceType;
2324
import lombok.Data;
2425
import org.springframework.util.CollectionUtils;
2526

@@ -34,7 +35,7 @@ public class ViewExecuteParam {
3435

3536
private String vizName;
3637

37-
private String vizType;
38+
private ResourceType vizType;
3839

3940
private String viewId;
4041

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package datart.server.common;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.DeserializationFeature;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import datart.core.base.exception.Exceptions;
7+
import datart.core.common.JavascriptUtils;
8+
import datart.server.base.params.DownloadCreateParam;
9+
10+
import javax.script.Invocable;
11+
import javax.script.ScriptException;
12+
13+
public class JsParserUtils {
14+
15+
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
16+
17+
private static Invocable parser;
18+
19+
static {
20+
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
21+
}
22+
23+
public static DownloadCreateParam parseExecuteParam(String type, String json) throws ScriptException, NoSuchMethodException, JsonProcessingException {
24+
Invocable parser = getParser();
25+
if (parser == null) {
26+
Exceptions.msg("param parser load error");
27+
}
28+
Object result = parser.invokeFunction("getQueryData", type, json);
29+
return OBJECT_MAPPER.readValue(result.toString(), DownloadCreateParam.class);
30+
}
31+
32+
private static synchronized Invocable getParser() {
33+
if (parser == null) {
34+
try {
35+
parser = JavascriptUtils.load("javascript/parser.js");
36+
} catch (Exception e) {
37+
Exceptions.e(e);
38+
}
39+
}
40+
return parser;
41+
}
42+
}

server/src/main/java/datart/server/job/ScheduleJob.java

Lines changed: 31 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import com.fasterxml.jackson.databind.DeserializationFeature;
55
import com.fasterxml.jackson.databind.ObjectMapper;
6-
import datart.core.base.PageInfo;
76
import datart.core.base.consts.AttachmentType;
87
import datart.core.base.consts.FileOwner;
9-
import datart.core.base.exception.Exceptions;
10-
import datart.core.common.*;
11-
import datart.core.data.provider.Dataframe;
8+
import datart.core.common.Application;
9+
import datart.core.common.FileUtils;
10+
import datart.core.common.UUIDGenerator;
1211
import datart.core.entity.Folder;
1312
import datart.core.entity.Schedule;
1413
import datart.core.entity.ScheduleLog;
1514
import datart.core.entity.User;
16-
import datart.core.entity.poi.POISettings;
1715
import datart.core.mappers.ext.ScheduleLogMapperExt;
1816
import datart.core.mappers.ext.ScheduleMapperExt;
1917
import datart.core.mappers.ext.UserMapperExt;
@@ -24,24 +22,18 @@
2422
import datart.server.base.dto.DatachartDetail;
2523
import datart.server.base.dto.ScheduleJobConfig;
2624
import datart.server.base.params.DownloadCreateParam;
27-
import datart.server.base.params.ShareCreateParam;
28-
import datart.server.base.params.ShareToken;
2925
import datart.server.base.params.ViewExecuteParam;
30-
import datart.server.common.PoiConvertUtils;
31-
import datart.server.service.DataProviderService;
26+
import datart.server.common.JsParserUtils;
27+
import datart.server.service.AttachmentService;
3228
import datart.server.service.FolderService;
3329
import datart.server.service.ShareService;
3430
import datart.server.service.VizService;
3531
import lombok.extern.slf4j.Slf4j;
3632
import org.apache.commons.lang3.StringUtils;
37-
import org.apache.commons.lang3.time.DateUtils;
38-
import org.apache.poi.ss.usermodel.Workbook;
3933
import org.quartz.Job;
4034
import org.quartz.JobExecutionContext;
4135
import org.springframework.util.CollectionUtils;
4236

43-
import javax.script.Invocable;
44-
import javax.script.ScriptException;
4537
import java.io.Closeable;
4638
import java.io.File;
4739
import java.io.IOException;
@@ -56,8 +48,6 @@ public abstract class ScheduleJob implements Job, Closeable {
5648

5749
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
5850

59-
public Invocable parser;
60-
6151
static {
6252
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
6353
}
@@ -128,29 +118,49 @@ public void doGetData() throws Exception {
128118
return;
129119
}
130120

121+
String path = FileUtils.concatPath(FileOwner.SCHEDULE.getPath(), schedule.getId());
122+
131123
FolderService folderService = Application.getBean(FolderService.class);
132124

133125
for (ScheduleJobConfig.VizContent vizContent : config.getVizContents()) {
134126
Folder folder = folderService.retrieve(vizContent.getVizId());
135127
DownloadCreateParam downloadCreateParam;
136128
if (ResourceType.DATACHART.name().equals(folder.getRelType())) {
137129
DatachartDetail datachart = vizService.getDatachart(folder.getRelId());
138-
downloadCreateParam = parseExecuteParam("chart", OBJECT_MAPPER.writeValueAsString(datachart));
130+
downloadCreateParam = JsParserUtils.parseExecuteParam("chart", OBJECT_MAPPER.writeValueAsString(datachart));
139131
} else {
140132
DashboardDetail dashboard = vizService.getDashboard(folder.getRelId());
141-
downloadCreateParam = parseExecuteParam("board", OBJECT_MAPPER.writeValueAsString(dashboard));
133+
downloadCreateParam = JsParserUtils.parseExecuteParam("board", OBJECT_MAPPER.writeValueAsString(dashboard));
142134
}
143-
if (config.getAttachments().contains(AttachmentType.EXCEL)) {
144-
downloadExcel(downloadCreateParam);
135+
136+
if (config.getImageWidth() != null) {
137+
downloadCreateParam.setImageWidth(config.getImageWidth());
145138
}
146139

147-
if (config.getAttachments().contains(AttachmentType.IMAGE)) {
148-
downloadImage(ResourceType.valueOf(folder.getRelType()), folder.getRelId(), config.getImageWidth());
140+
for (AttachmentType type : config.getAttachments()) {
141+
setVizId(downloadCreateParam, folder, type);
142+
String beanName = type.name().toLowerCase() + "AttachmentService";
143+
AttachmentService attachmentService = Application.getBean(beanName, AttachmentService.class);
144+
attachments.add(attachmentService.getFile(downloadCreateParam, path, downloadCreateParam.getFileName()));
149145
}
150146
}
151147

152148
}
153149

150+
private void setVizId(DownloadCreateParam downloadCreateParam, Folder folder, AttachmentType attachmentType) {
151+
if (ResourceType.DATACHART.name().equals(folder.getRelType())
152+
&& !CollectionUtils.isEmpty(downloadCreateParam.getDownloadParams())
153+
&& downloadCreateParam.getDownloadParams().size()==1) {
154+
ViewExecuteParam viewExecuteParam = downloadCreateParam.getDownloadParams().get(0);
155+
if (attachmentType.equals(AttachmentType.EXCEL)) {
156+
viewExecuteParam.setVizId(folder.getRelId());
157+
viewExecuteParam.setVizType(ResourceType.DATACHART);
158+
} else {
159+
viewExecuteParam.setVizId(folder.getId());
160+
}
161+
}
162+
}
163+
154164
public abstract void doSend() throws Exception;
155165

156166
private ScheduleJobConfig parseConfig(Schedule schedule) throws JsonProcessingException {
@@ -171,64 +181,6 @@ private void insertLog(Date start, Date end, String scheduleId, int status, Stri
171181
scheduleLogMapper.insert(scheduleLog);
172182
}
173183

174-
private void downloadExcel(DownloadCreateParam downloadParams) throws Exception {
175-
DataProviderService dataProviderService = Application.getBean(DataProviderService.class);
176-
Workbook workbook = POIUtils.createEmpty();
177-
for (int i = 0; i < downloadParams.getDownloadParams().size(); i++) {
178-
ViewExecuteParam viewExecuteParam = downloadParams.getDownloadParams().get(i);
179-
viewExecuteParam.setPageInfo(PageInfo.builder().pageNo(1)
180-
.pageSize(Integer.MAX_VALUE).build());
181-
Dataframe dataframe = dataProviderService.execute(downloadParams.getDownloadParams().get(i));
182-
String chartConfigStr = vizService.getChartConfigByVizId(viewExecuteParam.getVizId(), viewExecuteParam.getVizType());
183-
POISettings poiSettings = PoiConvertUtils.covertToPoiSetting(chartConfigStr, dataframe);
184-
String sheetName = StringUtils.isNotBlank(viewExecuteParam.getVizName()) ? viewExecuteParam.getVizName() : "Sheet"+i;
185-
POIUtils.withSheet(workbook, sheetName, dataframe, poiSettings);
186-
}
187-
File tempFile = File.createTempFile(UUIDGenerator.generate(), ".xlsx");
188-
POIUtils.save(workbook, tempFile.getPath(), true);
189-
attachments.add(tempFile);
190-
}
191-
192-
private void downloadImage(ResourceType vizType, String vizId, int imageWidth) throws Exception {
193-
194-
ShareCreateParam shareCreateParam = new ShareCreateParam();
195-
shareCreateParam.setVizId(vizId);
196-
shareCreateParam.setVizType(vizType);
197-
shareCreateParam.setExpiryDate(DateUtils.addHours(new Date(), 1));
198-
ShareToken share = shareService.createShare(schedule.getCreateBy(), shareCreateParam);
199-
200-
String url = Application.getWebRootURL() + "/share?eager=true&token=";
201-
202-
log.info("image url {} ", url);
203-
204-
String path = FileUtils.concatPath(Application.getFileBasePath(), FileOwner.SCHEDULE.getPath(), schedule.getId());
205-
206-
File file = WebUtils.screenShot2File(url, path, imageWidth);
207-
208-
attachments.add(file);
209-
210-
}
211-
212-
private DownloadCreateParam parseExecuteParam(String type, String json) throws ScriptException, NoSuchMethodException, JsonProcessingException {
213-
Invocable parser = getParser();
214-
if (parser == null) {
215-
Exceptions.msg("param parser load error");
216-
}
217-
Object result = parser.invokeFunction("getQueryData", type, json);
218-
return OBJECT_MAPPER.readValue(result.toString(), DownloadCreateParam.class);
219-
}
220-
221-
private synchronized Invocable getParser() {
222-
if (parser == null) {
223-
try {
224-
parser = JavascriptUtils.load("javascript/parser.js");
225-
} catch (Exception e) {
226-
Exceptions.e(e);
227-
}
228-
}
229-
return parser;
230-
}
231-
232184
@Override
233185
public void close() throws IOException {
234186
try {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package datart.server.service;
2+
3+
import datart.core.base.consts.AttachmentType;
4+
import datart.core.base.consts.Const;
5+
import datart.core.common.FileUtils;
6+
import datart.server.base.params.DownloadCreateParam;
7+
import org.apache.commons.lang3.RandomStringUtils;
8+
import org.apache.commons.lang3.time.DateFormatUtils;
9+
10+
import java.io.File;
11+
import java.util.Calendar;
12+
13+
public interface AttachmentService {
14+
15+
File getFile(DownloadCreateParam downloadCreateParam, String path, String fileName) throws Exception;
16+
17+
default String generateFileName(String path, String fileName, AttachmentType attachmentType) {
18+
path = FileUtils.withBasePath(path);
19+
String timeStr = DateFormatUtils.format(Calendar.getInstance(), Const.FILE_SUFFIX_DATE_FORMAT);
20+
String randomStr = RandomStringUtils.randomNumeric(3);
21+
fileName = fileName + "_" + timeStr + "_" + randomStr + attachmentType.getSuffix();
22+
return FileUtils.concatPath(path, fileName);
23+
}
24+
25+
}

server/src/main/java/datart/server/service/VizService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public interface VizService {
7676

7777
boolean unpublish(ResourceType resourceType, String vizId);
7878

79-
String getChartConfigByVizId(String vizId, String vizType);
79+
String getChartConfigByVizId(ResourceType resourceType, String vizId);
8080

8181
ResourceTransferModel exportViz(ResourceType vizType, boolean onlyViz, String... vizIds) throws IOException;
8282

0 commit comments

Comments
 (0)