Skip to content

Commit

Permalink
1. Add upload and download excel file
Browse files Browse the repository at this point in the history
2. View execute sql support paging query
3. Add system schedule for clear temporary files
  • Loading branch information
RichardShan committed Jan 11, 2019
1 parent e9c7af9 commit 0cdbdec
Show file tree
Hide file tree
Showing 25 changed files with 668 additions and 300 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@ tempFiles/
userfiles/
.vscode
*yarn.lock
bin/upgrade.go
bin/upgrade
bin/upgrade.go
2 changes: 2 additions & 0 deletions server/src/main/java/edp/DavinciServerApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableAutoConfiguration(exclude = {org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.class})
@EnableScheduling
public class DavinciServerApplication {

public static void main(String[] args) {
Expand Down
9 changes: 9 additions & 0 deletions server/src/main/java/edp/core/consts/Consts.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package edp.core.consts;

import java.io.File;

public class Consts {

/**
Expand Down Expand Up @@ -132,4 +134,11 @@ public class Consts {
*/
public static final String REG_SQL_ANNOTATE = "(?ms)('(?:''|[^'])*')|--.*?$|/\\*[^+]*?\\*/";


public static final String DIR_DOWNLOAD = File.separator + "download" + File.separator;

public static final String DIR_EMAIL = File.separator + "email" + File.separator;

public static final String DIR_TEMPL = File.separator + "tempFiles" + File.separator;

}
10 changes: 10 additions & 0 deletions server/src/main/java/edp/core/utils/DateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ public static String getNowDateYYYYMMDD() {
return formatter.format(currentData());
}

public static String getTheDayBeforNowDateYYYYMMDD() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");

Calendar calendar = Calendar.getInstance();
calendar.setTime(currentData());
calendar.add(Calendar.DAY_OF_MONTH, -1);

return formatter.format(calendar.getTime());
}

public static String getNowDateFormatCustom(String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format);

Expand Down
34 changes: 30 additions & 4 deletions server/src/main/java/edp/core/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.alibaba.druid.util.StringUtils;
import edp.core.consts.Consts;
import edp.davinci.core.enums.FileTypeEnum;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
Expand Down Expand Up @@ -68,10 +69,23 @@ public boolean isImage(File file) {
* @param file
* @return
*/
public boolean isCsv(MultipartFile file) {
return file.getOriginalFilename().toLowerCase().endsWith(".csv");
public static boolean isCsv(MultipartFile file) {
return file.getOriginalFilename().toLowerCase().endsWith(FileTypeEnum.CSV.getFormat());
}


/**
* 校验MultipartFile 是否csv文件
*
* @param file
* @return
*/
public static boolean isExcel(MultipartFile file) {
return file.getOriginalFilename().toLowerCase().endsWith(FileTypeEnum.XLSX.getFormat())
|| file.getOriginalFilename().toLowerCase().endsWith(FileTypeEnum.XLS.getFormat());
}


/**
* 上传文件
*
Expand Down Expand Up @@ -114,7 +128,7 @@ public void download(String filePath, HttpServletResponse response) {
File file = null;
if (!filePath.startsWith(fileBasePath)) {
file = new File(fileBasePath + filePath);
}else {
} else {
file = new File(filePath);
}
if (file.exists()) {
Expand Down Expand Up @@ -172,13 +186,14 @@ public boolean remove(String filePath) {

/**
* 删除文件夹及其下文件
*
* @param dir
* @return
*/
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
Expand All @@ -188,8 +203,19 @@ public static boolean deleteDir(File dir) {
return dir.delete();
}

/**
* 格式化文件目录
*
* @param filePath
* @return
*/
public String formatFilePath(String filePath) {
return filePath.replace(fileBasePath, "").replaceAll(File.separator + "{2,}", File.separator);
}

/**
* 压缩文件到zip
*
* @param files
* @param targetFile
*/
Expand Down
1 change: 1 addition & 0 deletions server/src/main/java/edp/core/utils/SqlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ public List<TableInfo> getTableList() throws SourceException {
tables.close();
}
} catch (Exception e) {
e.printStackTrace();
throw new SourceException(e.getMessage() + ", jdbcUrl=" + this.jdbcUrl);
} finally {
releaseConnection(connection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public ResponseEntity testSource(@Valid @RequestBody SourceTest sourceTest,
* 生成csv对应的表结构
*
* @param id
* @param csvmeta
* @param uploadMeta
* @param bindingResult
* @param user
* @param request
Expand All @@ -228,7 +228,7 @@ public ResponseEntity testSource(@Valid @RequestBody SourceTest sourceTest,
@ApiOperation(value = "create csv meta", consumes = MediaType.APPLICATION_JSON_VALUE)
@PostMapping(value = "{id}/csvmeta", consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity createCsvmeta(@PathVariable Long id,
@Valid @RequestBody Csvmeta csvmeta,
@Valid @RequestBody UploadMeta uploadMeta,
@ApiIgnore BindingResult bindingResult,
@ApiIgnore @CurrentUser User user,
HttpServletRequest request) {
Expand All @@ -244,7 +244,7 @@ public ResponseEntity createCsvmeta(@PathVariable Long id,
}

try {
ResultMap resultMap = sourceService.validCsvmeta(id, csvmeta, user, request);
ResultMap resultMap = sourceService.validCsvmeta(id, uploadMeta, user, request);
return ResponseEntity.status(resultMap.getCode()).body(resultMap);
} catch (Exception e) {
e.printStackTrace();
Expand All @@ -257,17 +257,18 @@ public ResponseEntity createCsvmeta(@PathVariable Long id,
* 上csv文件
*
* @param id
* @param csvUpload
* @param sourceDataUpload
* @param bindingResult
* @param file
* @param user
* @param request
* @return
*/
@ApiOperation(value = "upload csv file")
@PostMapping("{id}/uploadcsv")
@PostMapping("{id}/upload{type}")
public ResponseEntity uploadCsv(@PathVariable Long id,
@Valid @ModelAttribute(value = "csvUpload") CsvUpload csvUpload,
@PathVariable String type,
@Valid @ModelAttribute(value = "sourceDataUpload") SourceDataUpload sourceDataUpload,
@ApiIgnore BindingResult bindingResult,
@RequestParam("file") MultipartFile file,
@ApiIgnore @CurrentUser User user,
Expand All @@ -284,12 +285,12 @@ public ResponseEntity uploadCsv(@PathVariable Long id,
}

if (file.isEmpty() || StringUtils.isEmpty(file.getOriginalFilename())) {
ResultMap resultMap = new ResultMap(tokenUtils).failAndRefreshToken(request).message("csv file can not be empty");
ResultMap resultMap = new ResultMap(tokenUtils).failAndRefreshToken(request).message("upload file can not be empty");
return ResponseEntity.status(resultMap.getCode()).body(resultMap);
}

try {
ResultMap resultMap = sourceService.uploadCsv(id, csvUpload, file, user, request);
ResultMap resultMap = sourceService.dataUpload(id, sourceDataUpload, file, user, type, request);
return ResponseEntity.status(resultMap.getCode()).body(resultMap);
} catch (Exception e) {
e.printStackTrace();
Expand Down
19 changes: 10 additions & 9 deletions server/src/main/java/edp/davinci/controller/WidgetController.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,21 @@ public ResponseEntity deleteWidget(@PathVariable Long id,


/**
* 分享widget
* 下载widget
*
* @param id
* @param user
* @param request
* @return
*/
@ApiOperation(value = "download widget")
@PostMapping("/{id}/csv")
public ResponseEntity getWidgetCsv(@PathVariable Long id,
@Valid @RequestBody ViewExecuteParam executeParam,
@ApiIgnore BindingResult bindingResult,
@ApiIgnore @CurrentUser User user,
HttpServletRequest request) {
@ApiOperation(value = "download widget csv")
@PostMapping("/{id}/{type}")
public ResponseEntity downloadWidget(@PathVariable("id") Long id,
@PathVariable("type") String type,
@Valid @RequestBody ViewExecuteParam executeParam,
@ApiIgnore BindingResult bindingResult,
@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);
Expand All @@ -236,7 +237,7 @@ public ResponseEntity getWidgetCsv(@PathVariable Long id,
}

try {
ResultMap resultMap = widgetService.generationCsv(id, executeParam, user, request);
ResultMap resultMap = widgetService.generationFile(id, executeParam, user, type, request);
return ResponseEntity.status(resultMap.getCode()).body(resultMap);
} catch (Exception e) {
e.printStackTrace();
Expand Down
42 changes: 42 additions & 0 deletions server/src/main/java/edp/davinci/core/enums/FileTypeEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* <<
* Davinci
* ==
* Copyright (C) 2016 - 2018 EDP
* ==
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* >>
*
*/

package edp.davinci.core.enums;

public enum FileTypeEnum {
XLSX("excel", ".xlsx"),
XLS("excel", ".xls"),
CSV("csv", ".csv");

private String type;
private String format;

public String getType() {
return type;
}

public String getFormat() {
return format;
}

FileTypeEnum(String type, String format) {
this.type = type;
this.format = format;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

package edp.davinci.core.enums;

public enum CsvmetaModeEnum {
public enum UploadModeEnum {

NEW((short) 0),
REPLACE((short) 1),
Expand All @@ -28,7 +28,7 @@ public enum CsvmetaModeEnum {

private short mode;

CsvmetaModeEnum(short mode) {
UploadModeEnum(short mode) {
this.mode = mode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.Set;

@Data
public class CsvEntity {
public class DataUploadEntity {

private Set<QueryColumn> headers;

Expand Down
Loading

0 comments on commit 0cdbdec

Please sign in to comment.