Skip to content

Commit

Permalink
Merge pull request edp963#33 from edp963/dev-0.3
Browse files Browse the repository at this point in the history
merge from edp963/davinci dev-0.3
  • Loading branch information
xxxllluuu authored Aug 21, 2020
2 parents eb74031 + 433435d commit 8d2bf6c
Show file tree
Hide file tree
Showing 51 changed files with 1,829 additions and 987 deletions.
2 changes: 1 addition & 1 deletion server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@
<scope>runtime</scope>
</dependency>

<!--oracle -->
<!--oracle -->
<!--<dependency> -->
<!--<groupId>com.oracle</groupId> -->
<!--<artifactId>ojdbc6</artifactId> -->
Expand Down
6 changes: 6 additions & 0 deletions server/src/main/java/edp/core/annotation/AuthShare.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

package edp.core.annotation;

import edp.davinci.service.share.ShareOperation;
import edp.davinci.service.share.ShareType;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand All @@ -31,4 +34,7 @@
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface AuthShare {
ShareType type();

ShareOperation operation() default ShareOperation.READ;
}
36 changes: 28 additions & 8 deletions server/src/main/java/edp/davinci/common/utils/ScriptUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import edp.core.utils.CollectionUtils;
import edp.davinci.core.common.Constants;
import edp.davinci.core.model.ExcelHeader;
import edp.davinci.dto.viewDto.Param;
Expand All @@ -31,34 +33,39 @@
import org.graalvm.polyglot.Value;

import java.util.List;
import java.util.Map;
import java.util.Objects;

public class ScriptUtils {

private final static String LANGUAGE = "js";
private final static String Func_FieldsHeader = "getFieldsHeader";
private final static String Func_DashboardItemExecuteParam = "getDashboardItemExecuteParam";
private static final String LANGUAGE = "js";
private static final String FUNC_FIELDS_HEADER = "getFieldsHeader";
private static final String FUNC_DASHBOARD_ITEM_EXECUTE_PARAM = "getDashboardItemExecuteParam";
private static final String FUNC_FORMATTED_DATA_ROWS = "getFormattedDataRows";

private static ClassLoader classLoader = ScriptUtils.class.getClassLoader();
private static final ClassLoader classLoader = ScriptUtils.class.getClassLoader();

private enum ScriptEnum {
INSTANCE;


private Value tableFormatJs;
private Value executeParamFormatJs;
private Value formatDataRowsJs;

ScriptEnum() {
try {
tableFormatJs = createScriptEngine(Constants.TABLE_FORMAT_JS, Func_FieldsHeader);
executeParamFormatJs = createScriptEngine(Constants.EXECUTE_PARAM_FORMAT_JS, Func_DashboardItemExecuteParam);
tableFormatJs = createScriptEngine(Constants.TABLE_FORMAT_JS, FUNC_FIELDS_HEADER);
formatDataRowsJs = createScriptEngine(Constants.TABLE_FORMAT_JS, FUNC_FORMATTED_DATA_ROWS);
executeParamFormatJs = createScriptEngine(Constants.EXECUTE_PARAM_FORMAT_JS, FUNC_DASHBOARD_ITEM_EXECUTE_PARAM);
} catch (Exception e) {
e.printStackTrace();
}
}

private static Value createScriptEngine(String sourcePath, String member) throws Exception {
Context context = Context.create(LANGUAGE);
Source source = Source.newBuilder(LANGUAGE, classLoader.getResource(sourcePath)).build();
Source source = Source.newBuilder(LANGUAGE, Objects.requireNonNull(classLoader.getResource(sourcePath))).build();
context.eval(source);
Value function = context.getBindings(LANGUAGE).getMember(member);
return function.canExecute() ? function : null;
Expand All @@ -78,8 +85,21 @@ public static synchronized ViewExecuteParam getViewExecuteParam(String dashboard
public static synchronized List<ExcelHeader> formatHeader(String json, List<Param> params) {

Value js = ScriptEnum.INSTANCE.tableFormatJs;
Value result = js.execute(json, JSON.toJSONString(params));
Value result = js.execute(json, JSON.toJSONString(params));
List<ExcelHeader> excelHeaders = JSONArray.parseArray(result.toString(), ExcelHeader.class);
return excelHeaders;
}


public static synchronized List<Map<String, Object>> formatCellValue(String json, List<Map<String, Object>> params) {

Value js = ScriptEnum.INSTANCE.formatDataRowsJs;
Value result = js.execute(json, JSON.toJSONString(params));
List<Map> maps = JSONArray.parseArray(result.toString(), Map.class);
List<Map<String, Object>> formattedValues = Lists.newArrayList();
if (!CollectionUtils.isEmpty(maps)) {
maps.forEach(v -> formattedValues.add((Map<String, Object>) v));
}
return formattedValues;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
import edp.davinci.core.common.Constants;
import edp.davinci.core.common.ResultMap;
import edp.davinci.dto.dashboardDto.*;
import edp.davinci.dto.shareDto.ShareEntity;
import edp.davinci.model.Dashboard;
import edp.davinci.model.DashboardPortal;
import edp.davinci.model.MemDashboardWidget;
import edp.davinci.model.User;
import edp.davinci.service.DashboardPortalService;
import edp.davinci.service.DashboardService;
import edp.davinci.service.share.ShareResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
Expand Down Expand Up @@ -469,15 +471,15 @@ public ResponseEntity deleteMemDashboardWidget(@PathVariable Long relationId,
* 分享dashboard
*
* @param dashboardId
* @param username
* @param shareEntity
* @param user
* @param request
* @return
*/
@ApiOperation(value = "share dashboard")
@GetMapping("/dashboards/{dashboardId}/share")
@ApiOperation(value = "share dashboard", consumes = MediaType.APPLICATION_JSON_VALUE)
@PostMapping(value = "/dashboards/{dashboardId}/share", consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity shareDashboard(@PathVariable Long dashboardId,
@RequestParam(required = false) String username,
@RequestBody ShareEntity shareEntity,
@ApiIgnore @CurrentUser User user,
HttpServletRequest request) {

Expand All @@ -486,8 +488,15 @@ public ResponseEntity shareDashboard(@PathVariable Long dashboardId,
return ResponseEntity.status(resultMap.getCode()).body(resultMap);
}

String shareToken = dashboardService.shareDashboard(dashboardId, username, user);
return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request).payload(shareToken));
try {
shareEntity.valid();
} catch (IllegalArgumentException e) {
ResultMap resultMap = new ResultMap(tokenUtils).failAndRefreshToken(request).message(e.getMessage());
return ResponseEntity.status(resultMap.getCode()).body(resultMap);
}

ShareResult shareResult = dashboardService.shareDashboard(dashboardId, user, shareEntity);
return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request).payload(shareResult));
}

}
21 changes: 15 additions & 6 deletions server/src/main/java/edp/davinci/controller/DisplayController.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
import edp.davinci.core.common.Constants;
import edp.davinci.core.common.ResultMap;
import edp.davinci.dto.displayDto.*;
import edp.davinci.dto.shareDto.ShareEntity;
import edp.davinci.model.Display;
import edp.davinci.model.DisplaySlide;
import edp.davinci.model.MemDisplaySlideWidget;
import edp.davinci.model.User;
import edp.davinci.service.DisplayService;
import edp.davinci.service.DisplaySlideService;
import edp.davinci.service.share.ShareResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
Expand Down Expand Up @@ -601,24 +603,31 @@ public ResponseEntity uploadSlideSubWidgetBGImage(@PathVariable Long relationId,
* 共享display
*
* @param id
* @param username
* @param shareEntity
* @param user
* @param request
* @return
*/
@ApiOperation(value = "share display")
@GetMapping("/{id}/share")
@ApiOperation(value = "share display", consumes = MediaType.APPLICATION_JSON_VALUE)
@PostMapping(value = "/{id}/share", consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity shareDisplay(@PathVariable Long id,
@RequestParam(required = false) String username,
@RequestBody ShareEntity shareEntity,
@ApiIgnore @CurrentUser User user,
HttpServletRequest request) {
if (invalidId(id)) {
ResultMap resultMap = new ResultMap(tokenUtils).failAndRefreshToken(request).message("Invalid id");
return ResponseEntity.status(resultMap.getCode()).body(resultMap);
}

String shareToken = displayService.shareDisplay(id, user, username);
return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request).payload(shareToken));
try {
shareEntity.valid();
} catch (IllegalArgumentException e) {
ResultMap resultMap = new ResultMap(tokenUtils).failAndRefreshToken(request).message(e.getMessage());
return ResponseEntity.status(resultMap.getCode()).body(resultMap);
}

ShareResult shareResult = displayService.shareDisplay(id, user, shareEntity);
return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request).payload(shareResult));
}


Expand Down
79 changes: 31 additions & 48 deletions server/src/main/java/edp/davinci/controller/DownloadController.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package edp.davinci.controller;

import com.alibaba.druid.util.StringUtils;
import edp.core.annotation.AuthIgnore;
import edp.core.annotation.AuthShare;
import edp.core.annotation.CurrentUser;
Expand All @@ -35,6 +34,8 @@
import edp.davinci.model.User;
import edp.davinci.service.DownloadService;
import edp.davinci.service.ShareDownloadService;
import edp.davinci.service.share.ShareOperation;
import edp.davinci.service.share.ShareType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
Expand Down Expand Up @@ -124,65 +125,30 @@ public ResponseEntity submitDownloadTask(@PathVariable String type,


@ApiOperation(value = "submit share download")
@PostMapping(value = "/share/submit/{type}/{uuid}/{dataToken:.*}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@AuthShare
public ResponseEntity submitShareDownloadTask(@PathVariable(name = "type") String type,
@PostMapping(value = "/share/submit/{type}/{uuid}/{token:.*}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@AuthShare(type = ShareType.DATA, operation = ShareOperation.DOWNLOAD)
public ResponseEntity submitShareDownloadTask(@PathVariable(name = "token") String token,
@RequestParam(required = false) String password,
@PathVariable(name = "uuid") String uuid,
@PathVariable(name = "dataToken") String dataToken,
@Valid @RequestBody(required = false) DownloadViewExecuteParam[] params,
@ApiIgnore @CurrentUser User user,
HttpServletRequest request) {


if (StringUtils.isEmpty(dataToken)) {
ResultMap resultMap = new ResultMap().fail().message("Invalid share token");
return ResponseEntity.status(resultMap.getCode()).body(resultMap);
}
@PathVariable(name = "type") String type,
@Valid @RequestBody(required = false) DownloadViewExecuteParam[] params) {

List<DownloadViewExecuteParam> downloadViewExecuteParams = Arrays.asList(params);
boolean rst = shareDownloadService.submit(DownloadType.getDownloadType(type), uuid, dataToken, user, downloadViewExecuteParams);
boolean rst = shareDownloadService.submit(DownloadType.getDownloadType(type), uuid, downloadViewExecuteParams);

return ResponseEntity.ok(rst ? new ResultMap().success() : new ResultMap().fail());
}


@ApiOperation(value = "get share download record page")
@GetMapping(value = "/share/page/{uuid}/{token:.*}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@AuthShare
public ResponseEntity getShareDownloadRecordPage(@PathVariable(name = "uuid") String uuid,
@PathVariable(name = "token") String token,
@ApiIgnore @CurrentUser User user,
HttpServletRequest request) {
if (StringUtils.isEmpty(token)) {
ResultMap resultMap = new ResultMap().fail().message("Invalid share token");
return ResponseEntity.status(resultMap.getCode()).body(resultMap);
}

List<ShareDownloadRecord> records = shareDownloadService.queryDownloadRecordPage(uuid, token, user);

if (null == user) {
return ResponseEntity.ok(new ResultMap(tokenUtils).payloads(records));
} else {
return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request).payloads(records));
}
}


@ApiOperation(value = "get download record file")
@GetMapping(value = "/share/record/file/{id}/{uuid}/{token:.*}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@AuthShare
public ResponseEntity getShareDownloadRecordFile(@PathVariable(name = "id") String id,
@AuthShare(type = ShareType.FILE, operation = ShareOperation.DOWNLOAD)
public ResponseEntity getShareDownloadRecordFile(@PathVariable(name = "token") String token,
@RequestParam(required = false) String password,
@PathVariable(name = "uuid") String uuid,
@PathVariable(name = "token") String token,
@ApiIgnore @CurrentUser User user,
@PathVariable(name = "id") String id,
HttpServletRequest request,
HttpServletResponse response) {
if (StringUtils.isEmpty(token)) {
ResultMap resultMap = new ResultMap().fail().message("Invalid share token");
return ResponseEntity.status(resultMap.getCode()).body(resultMap);
}

ShareDownloadRecord record = shareDownloadService.downloadById(id, uuid, token, user);
ShareDownloadRecord record = shareDownloadService.downloadById(id, uuid);
FileInputStream is = null;
try {
encodeFileName(request, response, record.getName() + FileTypeEnum.XLSX.getFormat());
Expand All @@ -196,6 +162,23 @@ public ResponseEntity getShareDownloadRecordFile(@PathVariable(name = "id") Stri
return null;
}

@ApiOperation(value = "get share download record page")
@GetMapping(value = "/share/page/{uuid}/{token:.*}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@AuthShare(type = ShareType.RECORD, operation = ShareOperation.DOWNLOAD)
public ResponseEntity getShareDownloadRecordPage(@PathVariable(name = "token") String token,
@RequestParam(required = false) String password,
@PathVariable(name = "uuid") String uuid,
@ApiIgnore @CurrentUser User user,
HttpServletRequest request) {
List<ShareDownloadRecord> records = shareDownloadService.queryDownloadRecordPage(uuid);

if (null == user || user.getId() == null) {
return ResponseEntity.ok(new ResultMap(tokenUtils).payloads(records));
} else {
return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request).payloads(records));
}
}


private void encodeFileName(HttpServletRequest request, HttpServletResponse response, String filename) throws UnsupportedEncodingException {
response.setHeader("Content-Type", "application/force-download");
Expand Down
Loading

0 comments on commit 8d2bf6c

Please sign in to comment.