feat(web): 优化统一的Controller和Service接口
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package com.eshore.gringotts.web.domain.base.controller;
|
||||
|
||||
import com.eshore.gringotts.web.configuration.amis.AmisResponse;
|
||||
import com.eshore.gringotts.web.domain.base.entity.SimpleEntity;
|
||||
import com.eshore.gringotts.web.domain.base.service.SimpleService;
|
||||
import org.eclipse.collections.api.set.ImmutableSet;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
/**
|
||||
* @author lanyuanxiaoyao
|
||||
* @date 2024-11-26
|
||||
*/
|
||||
public abstract class SimpleController<ENTITY extends SimpleEntity, SAVE_ITEM, LIST_ITEM, DETAIL_ITEM> {
|
||||
protected static final String SAVE = "/save";
|
||||
protected static final String LIST = "/list";
|
||||
protected static final String DETAIL = "/detail/{id}";
|
||||
protected static final String REMOVE = "/remove/{id}";
|
||||
|
||||
protected final SimpleService<ENTITY> service;
|
||||
|
||||
public SimpleController(SimpleService<ENTITY> service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@PostMapping(SAVE)
|
||||
public AmisResponse<Long> save(@RequestBody SAVE_ITEM item) throws Exception {
|
||||
return AmisResponse.responseSuccess(service.save(fromSaveItem(item)));
|
||||
}
|
||||
|
||||
@GetMapping(LIST)
|
||||
public AmisResponse<ImmutableSet<LIST_ITEM>> list() {
|
||||
return AmisResponse.responseSuccess(service.list().collect(entity -> {
|
||||
try {
|
||||
return toListItem(entity);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
@GetMapping(DETAIL)
|
||||
public AmisResponse<DETAIL_ITEM> detail(@PathVariable Long id) throws Exception {
|
||||
return AmisResponse.responseSuccess(toDetailItem(service.detail(id)));
|
||||
}
|
||||
|
||||
@GetMapping(REMOVE)
|
||||
public AmisResponse<Object> remove(@PathVariable Long id) {
|
||||
service.remove(id);
|
||||
return AmisResponse.responseSuccess();
|
||||
}
|
||||
|
||||
protected abstract ENTITY fromSaveItem(SAVE_ITEM item) throws Exception;
|
||||
|
||||
protected abstract LIST_ITEM toListItem(ENTITY entity) throws Exception;
|
||||
|
||||
protected abstract DETAIL_ITEM toDetailItem(ENTITY entity) throws Exception;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.eshore.gringotts.web.domain.base.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 创建对象使用的接收类
|
||||
*
|
||||
* @author lanyuanxiaoyao
|
||||
* @date 2024-11-26
|
||||
*/
|
||||
@Data
|
||||
public abstract class SimpleDetailItem<ENTITY> {
|
||||
private Long id;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.eshore.gringotts.web.domain.base.entity;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 创建对象使用的接收类
|
||||
*
|
||||
* @author lanyuanxiaoyao
|
||||
* @date 2024-11-26
|
||||
*/
|
||||
@Data
|
||||
public abstract class SimpleListItem<ENTITY> {
|
||||
private Long id;
|
||||
private String createdUsername;
|
||||
private LocalDateTime createdTime;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.eshore.gringotts.web.domain.base.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 创建对象使用的接收类
|
||||
*
|
||||
* @author lanyuanxiaoyao
|
||||
* @date 2024-11-26
|
||||
*/
|
||||
@Data
|
||||
public abstract class SimpleSaveItem<ENTITY> {
|
||||
private Long id;
|
||||
}
|
||||
@@ -1,25 +1,79 @@
|
||||
package com.eshore.gringotts.web.domain.base.service;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.eshore.gringotts.web.domain.base.entity.SimpleEntity;
|
||||
import com.eshore.gringotts.web.domain.base.repository.SimpleRepository;
|
||||
import org.eclipse.collections.api.factory.Lists;
|
||||
import org.eclipse.collections.api.list.ImmutableList;
|
||||
import com.eshore.gringotts.web.domain.user.entity.User;
|
||||
import com.eshore.gringotts.web.domain.user.service.UserService;
|
||||
import java.util.Optional;
|
||||
import javax.transaction.Transactional;
|
||||
import org.eclipse.collections.api.factory.Sets;
|
||||
import org.eclipse.collections.api.set.ImmutableSet;
|
||||
|
||||
/**
|
||||
* @author lanyuanxiaoyao
|
||||
* @date 2024-11-21
|
||||
*/
|
||||
public abstract class SimpleService<E, ID> {
|
||||
protected abstract SimpleRepository<E, ID> repository();
|
||||
public abstract class SimpleService<ENTITY extends SimpleEntity> {
|
||||
protected final SimpleRepository<ENTITY, Long> repository;
|
||||
protected final UserService userService;
|
||||
|
||||
public ImmutableList<E> list() {
|
||||
return Lists.immutable.ofAll(repository().findAll());
|
||||
public SimpleService(SimpleRepository<ENTITY, Long> repository, UserService userService) {
|
||||
this.repository = repository;
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
public E detail(ID id) {
|
||||
@Transactional(rollbackOn = Throwable.class)
|
||||
public Long save(ENTITY entity) {
|
||||
User user = userService.currentLoginUser();
|
||||
if (ObjectUtil.isNull(entity.getCreatedUser())) {
|
||||
entity.setCreatedUser(user);
|
||||
}
|
||||
entity.setModifiedUser(user);
|
||||
entity = repository.saveOrUpdateByNotNullProperties(entity);
|
||||
return entity.getId();
|
||||
}
|
||||
|
||||
public ImmutableSet<ENTITY> list() {
|
||||
return Sets.immutable.ofAll(repository.findAll());
|
||||
}
|
||||
|
||||
public ImmutableSet<ENTITY> list(ImmutableSet<Long> ids) {
|
||||
return Sets.immutable.ofAll(repository.findAllById(ids.select(ObjectUtil::isNotNull)));
|
||||
}
|
||||
|
||||
public ENTITY detail(Long id) {
|
||||
if (ObjectUtil.isNull(id)) {
|
||||
return null;
|
||||
}
|
||||
return repository().findById(id).orElse(null);
|
||||
return repository.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
public Optional<ENTITY> detailOptional(Long id) {
|
||||
if (ObjectUtil.isNull(id)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return repository.findById(id);
|
||||
}
|
||||
|
||||
public ENTITY detailOrThrow(Long id) {
|
||||
if (ObjectUtil.isNull(id)) {
|
||||
throw new IdNotFoundException(id);
|
||||
}
|
||||
return repository.findById(id).orElseThrow(() -> new IdNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional(rollbackOn = Throwable.class)
|
||||
public void remove(Long id) {
|
||||
if (ObjectUtil.isNotNull(id)) {
|
||||
repository.deleteById(id);
|
||||
}
|
||||
}
|
||||
|
||||
public static final class IdNotFoundException extends RuntimeException {
|
||||
public IdNotFoundException(Long id) {
|
||||
super(StrUtil.format("ID为{}的资源不存在", id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
package com.eshore.gringotts.web.domain.confirmation.controller;
|
||||
|
||||
import com.eshore.gringotts.web.domain.base.controller.SimpleController;
|
||||
import com.eshore.gringotts.web.domain.base.entity.SimpleListItem;
|
||||
import com.eshore.gringotts.web.domain.base.entity.SimpleSaveItem;
|
||||
import com.eshore.gringotts.web.domain.confirmation.entity.Confirmation;
|
||||
import com.eshore.gringotts.web.domain.confirmation.service.ConfirmationService;
|
||||
import com.eshore.gringotts.web.domain.resource.service.DataResourceService;
|
||||
import com.eshore.gringotts.web.domain.upload.entity.DataFile;
|
||||
import com.eshore.gringotts.web.domain.upload.service.DataFileService;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.eclipse.collections.api.factory.Sets;
|
||||
import org.eclipse.collections.api.set.ImmutableSet;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author lanyuanxiaoyao
|
||||
* @date 2024-11-26
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("confirmation")
|
||||
public class ConfirmationController extends SimpleController<Confirmation, ConfirmationController.SaveItem, ConfirmationController.ListItem, ConfirmationController.DetailItem> {
|
||||
private final DataResourceService dataResourceService;
|
||||
private final DataFileService dataFileService;
|
||||
|
||||
public ConfirmationController(ConfirmationService service, DataResourceService dataResourceService, DataFileService dataFileService) {
|
||||
super(service);
|
||||
this.dataResourceService = dataResourceService;
|
||||
this.dataFileService = dataFileService;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Confirmation fromSaveItem(SaveItem item) {
|
||||
Confirmation confirmation = new Confirmation();
|
||||
confirmation.setId(item.getId());
|
||||
confirmation.setDescription(item.getDescription());
|
||||
confirmation.setTarget(dataResourceService.detailOrThrow(item.getTargetId()));
|
||||
confirmation.setEvidences(dataFileService.list(item.getEvidenceIds()).toSet());
|
||||
return confirmation;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ListItem toListItem(Confirmation entity) {
|
||||
ListItem item = new ListItem();
|
||||
item.setId(entity.getId());
|
||||
item.setName(entity.getTarget().getName());
|
||||
item.setDescription(entity.getDescription());
|
||||
item.setCreatedUsername(entity.getCreatedUser().getUsername());
|
||||
item.setCreatedTime(entity.getCreatedTime());
|
||||
return item;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected DetailItem toDetailItem(Confirmation entity) throws Exception {
|
||||
return new DetailItem(entity);
|
||||
}
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public static class SaveItem extends SimpleSaveItem<Confirmation> {
|
||||
private Long targetId;
|
||||
private String description;
|
||||
private ImmutableSet<Long> evidenceIds;
|
||||
}
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public static class ListItem extends SimpleListItem<Confirmation> {
|
||||
private String name;
|
||||
private String description;
|
||||
}
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public static final class DetailItem extends SaveItem {
|
||||
public DetailItem(Confirmation confirmation) {
|
||||
this.setId(confirmation.getId());
|
||||
this.setDescription(confirmation.getDescription());
|
||||
this.setEvidenceIds(Sets.immutable.ofAll(confirmation.getEvidences()).collect(DataFile::getId));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.eshore.gringotts.web.domain.confirmation.entity;
|
||||
|
||||
import com.eshore.gringotts.core.Constants;
|
||||
import com.eshore.gringotts.web.domain.base.entity.SimpleEntity;
|
||||
import com.eshore.gringotts.web.domain.resource.entity.DataResource;
|
||||
import com.eshore.gringotts.web.domain.upload.entity.DataFile;
|
||||
import java.util.Set;
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.ConstraintMode;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.ForeignKey;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.NamedAttributeNode;
|
||||
import javax.persistence.NamedEntityGraph;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import org.hibernate.annotations.DynamicUpdate;
|
||||
|
||||
/**
|
||||
* 确权
|
||||
*
|
||||
* @author lanyuanxiaoyao
|
||||
* @date 2024-11-26
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@Entity
|
||||
@DynamicUpdate
|
||||
@Table(name = Constants.TABLE_PREFIX + "confirmation")
|
||||
@NamedEntityGraph(name = "confirmation.list", attributeNodes = {
|
||||
@NamedAttributeNode(value = "target"),
|
||||
@NamedAttributeNode(value = "createdUser"),
|
||||
})
|
||||
@NamedEntityGraph(name = "confirmation.detail", attributeNodes = {
|
||||
@NamedAttributeNode(value = "target"),
|
||||
@NamedAttributeNode(value = "evidences"),
|
||||
@NamedAttributeNode(value = "createdUser"),
|
||||
@NamedAttributeNode(value = "modifiedUser"),
|
||||
})
|
||||
public class Confirmation extends SimpleEntity {
|
||||
@OneToOne(fetch = FetchType.EAGER)
|
||||
@JoinColumn(nullable = false, foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
|
||||
private DataResource target;
|
||||
private String description;
|
||||
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
@JoinTable(foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT), inverseForeignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
|
||||
private Set<DataFile> evidences;
|
||||
@Column(nullable = false)
|
||||
@Enumerated(EnumType.STRING)
|
||||
private State state = State.CHECKING;
|
||||
|
||||
public enum State {
|
||||
/**
|
||||
* 审查中
|
||||
*/
|
||||
CHECKING,
|
||||
/**
|
||||
* 正常
|
||||
*/
|
||||
NORMAL,
|
||||
/**
|
||||
* 禁用
|
||||
*/
|
||||
DISABLED,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.eshore.gringotts.web.domain.confirmation.repository;
|
||||
|
||||
import com.eshore.gringotts.web.domain.base.repository.SimpleRepository;
|
||||
import com.eshore.gringotts.web.domain.confirmation.entity.Confirmation;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import org.springframework.data.jpa.repository.EntityGraph;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* @author lanyuanxiaoyao
|
||||
* @date 2024-11-26
|
||||
*/
|
||||
@SuppressWarnings("NullableProblems")
|
||||
@Repository
|
||||
public interface ConfirmationRepository extends SimpleRepository<Confirmation, Long> {
|
||||
@Override
|
||||
@EntityGraph(value = "confirmation.list", type = EntityGraph.EntityGraphType.FETCH)
|
||||
List<Confirmation> findAll();
|
||||
|
||||
@Override
|
||||
@EntityGraph(value = "confirmation.detail", type = EntityGraph.EntityGraphType.FETCH)
|
||||
Optional<Confirmation> findById(Long id);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.eshore.gringotts.web.domain.confirmation.service;
|
||||
|
||||
import com.eshore.gringotts.web.domain.base.service.SimpleService;
|
||||
import com.eshore.gringotts.web.domain.confirmation.entity.Confirmation;
|
||||
import com.eshore.gringotts.web.domain.confirmation.repository.ConfirmationRepository;
|
||||
import com.eshore.gringotts.web.domain.user.service.UserService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author lanyuanxiaoyao
|
||||
* @date 2024-11-26
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class ConfirmationService extends SimpleService<Confirmation> {
|
||||
public ConfirmationService(ConfirmationRepository repository, UserService userService) {
|
||||
super(repository, userService);
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,9 @@ package com.eshore.gringotts.web.domain.resource.controller;
|
||||
|
||||
import cn.hutool.core.util.EnumUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.eshore.gringotts.web.configuration.amis.AmisListResponse;
|
||||
import com.eshore.gringotts.web.configuration.amis.AmisResponse;
|
||||
import com.eshore.gringotts.web.domain.base.controller.SimpleController;
|
||||
import com.eshore.gringotts.web.domain.base.entity.SimpleListItem;
|
||||
import com.eshore.gringotts.web.domain.base.entity.SimpleSaveItem;
|
||||
import com.eshore.gringotts.web.domain.resource.entity.DataResource;
|
||||
import com.eshore.gringotts.web.domain.resource.entity.format.CsvResourceFormat;
|
||||
import com.eshore.gringotts.web.domain.resource.entity.format.JsonLineResourceFormat;
|
||||
@@ -26,14 +27,8 @@ import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@@ -46,43 +41,174 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/data_resource")
|
||||
public class DataResourceController {
|
||||
public class DataResourceController extends SimpleController<DataResource, DataResourceController.SaveItem, DataResourceController.ListItem, DataResourceController.DetailItem> {
|
||||
private final ObjectMapper mapper;
|
||||
private final DataResourceService dataResourceService;
|
||||
private final DataFileService dataFileService;
|
||||
|
||||
public DataResourceController(Jackson2ObjectMapperBuilder builder, DataResourceService dataResourceService, DataFileService dataFileService) {
|
||||
this.mapper = builder.build();
|
||||
this.dataResourceService = dataResourceService;
|
||||
public DataResourceController(DataResourceService dataResourceService, DataFileService dataFileService, Jackson2ObjectMapperBuilder builder) {
|
||||
super(dataResourceService);
|
||||
this.dataFileService = dataFileService;
|
||||
this.mapper = builder.build();
|
||||
}
|
||||
|
||||
@PostMapping("/save")
|
||||
public void create(@RequestBody CreateRequest request) throws JsonProcessingException {
|
||||
log.info("Save request: {}", request);
|
||||
ResourceType type = request.generateResourceType(dataFileService);
|
||||
ResourceFormat format = request.generateResourceFormat(mapper);
|
||||
dataResourceService.create(request.id, request.name, request.description, type, format, dataFileService.detail(request.exampleFileId));
|
||||
@Override
|
||||
protected DataResource fromSaveItem(SaveItem item) throws JsonProcessingException {
|
||||
ResourceType type = generateResourceType(item);
|
||||
ResourceFormat format = generateResourceFormat(item);
|
||||
DataResource dataResource = new DataResource();
|
||||
dataResource.setId(item.getId());
|
||||
dataResource.setName(item.getName());
|
||||
dataResource.setDescription(item.getDescription());
|
||||
dataResource.setType(type);
|
||||
dataResource.setFormat(format);
|
||||
dataResource.setExample(dataFileService.detail(item.getExampleFileId()));
|
||||
return dataResource;
|
||||
}
|
||||
|
||||
@PostMapping("/list")
|
||||
public AmisListResponse list() {
|
||||
return AmisResponse.responseListData(dataResourceService.list().collect(DataResourceListItem::new));
|
||||
@Override
|
||||
protected ListItem toListItem(DataResource dataResource) {
|
||||
ListItem item = new ListItem();
|
||||
item.setId(dataResource.getId());
|
||||
item.setName(dataResource.getName());
|
||||
item.setDescription(dataResource.getDescription());
|
||||
item.setType(dataResource.getType().getResourceType().name());
|
||||
item.setFormat(dataResource.getFormat().getFormatType().name());
|
||||
item.setCreatedUser(dataResource.getCreatedUser().getUsername());
|
||||
item.setCreatedTime(dataResource.getCreatedTime());
|
||||
return item;
|
||||
}
|
||||
|
||||
@GetMapping("/detail/{id}")
|
||||
public AmisResponse<DataResourceDetail> detail(@PathVariable Long id) throws JsonProcessingException {
|
||||
return AmisResponse.responseSuccess(new DataResourceDetail(mapper, dataResourceService.detail(id)));
|
||||
@Override
|
||||
protected DetailItem toDetailItem(DataResource dataResource) throws Exception {
|
||||
DetailItem item = new DetailItem();
|
||||
item.setId(dataResource.getId());
|
||||
item.setResourceTypeId(dataResource.getType().getId());
|
||||
item.setResourceFormatId(dataResource.getFormat().getId());
|
||||
item.setName(dataResource.getName());
|
||||
item.setDescription(dataResource.getDescription());
|
||||
item.setResourceType(dataResource.getType().getResourceType());
|
||||
switch (dataResource.getType().getResourceType()) {
|
||||
case API:
|
||||
ApiResourceType apiType = (ApiResourceType) dataResource.getType();
|
||||
item.setApiUrl(apiType.getUrl());
|
||||
item.setApiUsername(apiType.getUsername());
|
||||
item.setApiPassword(apiType.getPassword());
|
||||
break;
|
||||
case FILE:
|
||||
FileResourceType fileType = (FileResourceType) dataResource.getType();
|
||||
item.setFileId(fileType.getFile().getId());
|
||||
item.setFilename(fileType.getFile().getFilename());
|
||||
break;
|
||||
case DATABASE:
|
||||
DatabaseResourceType databaseType = (DatabaseResourceType) dataResource.getType();
|
||||
item.setDatabaseType(databaseType.getDatabaseType().name());
|
||||
item.setDatabaseJdbc(databaseType.getJdbc());
|
||||
item.setDatabaseUsername(databaseType.getUsername());
|
||||
item.setDatabasePassword(databaseType.getPassword());
|
||||
break;
|
||||
case HDFS:
|
||||
HDFSResourceType hdfsType = (HDFSResourceType) dataResource.getType();
|
||||
item.setCoreSiteFileId(hdfsType.getCoreSite().getId());
|
||||
item.setCoreSiteFilename(hdfsType.getCoreSite().getFilename());
|
||||
item.setHdfsSiteFileId(hdfsType.getHdfsSite().getId());
|
||||
item.setHdfsSiteFilename(hdfsType.getHdfsSite().getFilename());
|
||||
break;
|
||||
case FTP:
|
||||
FtpResourceType ftpType = (FtpResourceType) dataResource.getType();
|
||||
item.setFtpUrl(ftpType.getUrl());
|
||||
item.setFtpUsername(ftpType.getUsername());
|
||||
item.setFtpPassword(ftpType.getPassword());
|
||||
item.setFtpPath(ftpType.getPath());
|
||||
item.setFtpRegexFilter(ftpType.getRegexFilter());
|
||||
break;
|
||||
}
|
||||
item.setFormatType(dataResource.getFormat().getFormatType());
|
||||
switch (dataResource.getFormat().getFormatType()) {
|
||||
case NONE:
|
||||
case LINE:
|
||||
break;
|
||||
case JSON:
|
||||
JsonResourceFormat jsonFormat = (JsonResourceFormat) dataResource.getFormat();
|
||||
item.setJsonSchemaText(jsonFormat.getSchema());
|
||||
item.setJsonSchema(mapper.readValue(jsonFormat.getSchema(), Map.class));
|
||||
break;
|
||||
case JSON_LINE:
|
||||
JsonLineResourceFormat jsonLineFormat = (JsonLineResourceFormat) dataResource.getFormat();
|
||||
item.setJsonLineSchemaText(jsonLineFormat.getSchema());
|
||||
item.setJsonLineSchema(mapper.readValue(jsonLineFormat.getSchema(), Map.class));
|
||||
break;
|
||||
case CSV:
|
||||
CsvResourceFormat csvFormat = (CsvResourceFormat) dataResource.getFormat();
|
||||
item.setCsvSchemaText(csvFormat.getSchema());
|
||||
item.setCsvSchema(mapper.readValue(csvFormat.getSchema(), Map.class));
|
||||
break;
|
||||
}
|
||||
if (ObjectUtil.isNotNull(dataResource.getExample())) {
|
||||
item.setExampleFileId(dataResource.getExample().getId());
|
||||
item.setExampleFilename(dataResource.getExample().getFilename());
|
||||
}
|
||||
item.setCreatedUsername(dataResource.getCreatedUser().getUsername());
|
||||
item.setCreatedTime(dataResource.getCreatedTime());
|
||||
item.setModifiedUsername(dataResource.getModifiedUser().getUsername());
|
||||
item.setModifiedTime(dataResource.getModifiedTime());
|
||||
return item;
|
||||
}
|
||||
|
||||
@GetMapping("/remove/{id}")
|
||||
public void remove(@PathVariable Long id) {
|
||||
dataResourceService.remove(id);
|
||||
private ResourceType generateResourceType(SaveItem item) {
|
||||
ResourceType type = null;
|
||||
switch (item.getResourceType()) {
|
||||
case API:
|
||||
type = new ApiResourceType(item.getApiUrl(), item.getApiUsername(), item.getApiPassword());
|
||||
break;
|
||||
case FILE:
|
||||
DataFile dataFile = dataFileService.detail(item.getFileId());
|
||||
type = new FileResourceType(dataFile);
|
||||
break;
|
||||
case DATABASE:
|
||||
type = new DatabaseResourceType(
|
||||
item.getDatabaseJdbc(),
|
||||
item.getDatabaseUsername(),
|
||||
item.getDatabasePassword(),
|
||||
EnumUtil.fromString(DatabaseResourceType.DatabaseType.class, item.getDatabaseType())
|
||||
);
|
||||
break;
|
||||
case HDFS:
|
||||
type = new HDFSResourceType(dataFileService.detail(item.getCoreSiteFileId()), dataFileService.detail(item.getHdfsSiteFileId()));
|
||||
break;
|
||||
case FTP:
|
||||
type = new FtpResourceType(item.getFtpUrl(), item.getFtpUsername(), item.getFtpPassword(), item.getFtpPath(), item.getFtpRegexFilter());
|
||||
break;
|
||||
}
|
||||
type.setId(item.getResourceTypeId());
|
||||
return type;
|
||||
}
|
||||
|
||||
private ResourceFormat generateResourceFormat(SaveItem item) throws JsonProcessingException {
|
||||
ResourceFormat format = null;
|
||||
switch (item.getFormatType()) {
|
||||
case NONE:
|
||||
format = new NoneResourceFormat();
|
||||
break;
|
||||
case LINE:
|
||||
format = new LineResourceFormat();
|
||||
break;
|
||||
case JSON:
|
||||
format = new JsonResourceFormat(mapper.writeValueAsString(item.getJsonSchema()));
|
||||
break;
|
||||
case JSON_LINE:
|
||||
format = new JsonLineResourceFormat(mapper.writeValueAsString(item.getJsonLineSchema()));
|
||||
break;
|
||||
case CSV:
|
||||
format = new CsvResourceFormat(mapper.writeValueAsString(item.getCsvSchema()));
|
||||
break;
|
||||
}
|
||||
format.setId(item.getResourceFormatId());
|
||||
return format;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class CreateRequest {
|
||||
protected Long id;
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public static class SaveItem extends SimpleSaveItem<DataResource> {
|
||||
protected String name;
|
||||
protected String description;
|
||||
protected Long resourceTypeId;
|
||||
@@ -111,62 +237,11 @@ public class DataResourceController {
|
||||
protected Map<?, ?> csvSchema;
|
||||
protected String csvSchemaText;
|
||||
protected Long exampleFileId;
|
||||
|
||||
public ResourceType generateResourceType(DataFileService dataFileService) {
|
||||
ResourceType type = null;
|
||||
switch (resourceType) {
|
||||
case API:
|
||||
type = new ApiResourceType(apiUrl, apiUsername, apiPassword);
|
||||
break;
|
||||
case FILE:
|
||||
DataFile dataFile = dataFileService.detail(fileId);
|
||||
type = new FileResourceType(dataFile);
|
||||
break;
|
||||
case DATABASE:
|
||||
type = new DatabaseResourceType(
|
||||
databaseJdbc,
|
||||
databaseUsername,
|
||||
databasePassword,
|
||||
EnumUtil.fromString(DatabaseResourceType.DatabaseType.class, databaseType)
|
||||
);
|
||||
break;
|
||||
case HDFS:
|
||||
type = new HDFSResourceType(dataFileService.detail(coreSiteFileId), dataFileService.detail(hdfsSiteFileId));
|
||||
break;
|
||||
case FTP:
|
||||
type = new FtpResourceType(ftpUrl, ftpUsername, ftpPassword, ftpPath, ftpRegexFilter);
|
||||
break;
|
||||
}
|
||||
type.setId(resourceTypeId);
|
||||
return type;
|
||||
}
|
||||
|
||||
public ResourceFormat generateResourceFormat(ObjectMapper mapper) throws JsonProcessingException {
|
||||
ResourceFormat format = null;
|
||||
switch (formatType) {
|
||||
case NONE:
|
||||
format = new NoneResourceFormat();
|
||||
break;
|
||||
case LINE:
|
||||
format = new LineResourceFormat();
|
||||
break;
|
||||
case JSON:
|
||||
format = new JsonResourceFormat(mapper.writeValueAsString(jsonSchema));
|
||||
break;
|
||||
case JSON_LINE:
|
||||
format = new JsonLineResourceFormat(mapper.writeValueAsString(jsonLineSchema));
|
||||
break;
|
||||
case CSV:
|
||||
format = new CsvResourceFormat(mapper.writeValueAsString(csvSchema));
|
||||
break;
|
||||
}
|
||||
format.setId(resourceFormatId);
|
||||
return format;
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
public static final class DataResourceListItem {
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public static final class ListItem extends SimpleListItem<DataResource> {
|
||||
private Long id;
|
||||
private String name;
|
||||
private String description;
|
||||
@@ -174,23 +249,11 @@ public class DataResourceController {
|
||||
private String format;
|
||||
private String createdUser;
|
||||
private LocalDateTime createdTime;
|
||||
|
||||
public DataResourceListItem(DataResource dataResource) {
|
||||
this.id = dataResource.getId();
|
||||
this.name = dataResource.getName();
|
||||
this.description = dataResource.getDescription();
|
||||
this.type = dataResource.getType().getResourceType().name();
|
||||
this.format = dataResource.getFormat().getFormatType().name();
|
||||
this.createdUser = dataResource.getCreatedUser().getUsername();
|
||||
this.createdTime = dataResource.getCreatedTime();
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@NoArgsConstructor
|
||||
@ToString(callSuper = true)
|
||||
public static final class DataResourceDetail extends CreateRequest {
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public static final class DetailItem extends SaveItem {
|
||||
private Long id;
|
||||
private Long fileId;
|
||||
private String filename;
|
||||
@@ -201,79 +264,5 @@ public class DataResourceController {
|
||||
private String createdUsername;
|
||||
private LocalDateTime modifiedTime;
|
||||
private String modifiedUsername;
|
||||
|
||||
public DataResourceDetail(ObjectMapper mapper, DataResource dataResource) throws JsonProcessingException {
|
||||
this.id = dataResource.getId();
|
||||
this.resourceTypeId = dataResource.getType().getId();
|
||||
this.resourceFormatId = dataResource.getFormat().getId();
|
||||
this.name = dataResource.getName();
|
||||
this.description = dataResource.getDescription();
|
||||
this.resourceType = dataResource.getType().getResourceType();
|
||||
switch (dataResource.getType().getResourceType()) {
|
||||
case API:
|
||||
ApiResourceType apiType = (ApiResourceType) dataResource.getType();
|
||||
this.apiUrl = apiType.getUrl();
|
||||
this.apiUsername = apiType.getUsername();
|
||||
this.apiPassword = apiType.getPassword();
|
||||
break;
|
||||
case FILE:
|
||||
FileResourceType fileType = (FileResourceType) dataResource.getType();
|
||||
this.fileId = fileType.getFile().getId();
|
||||
this.filename = fileType.getFile().getFilename();
|
||||
break;
|
||||
case DATABASE:
|
||||
DatabaseResourceType databaseType = (DatabaseResourceType) dataResource.getType();
|
||||
this.databaseType = databaseType.getDatabaseType().name();
|
||||
this.databaseJdbc = databaseType.getJdbc();
|
||||
this.databaseUsername = databaseType.getUsername();
|
||||
this.databasePassword = databaseType.getPassword();
|
||||
break;
|
||||
case HDFS:
|
||||
HDFSResourceType hdfsType = (HDFSResourceType) dataResource.getType();
|
||||
this.coreSiteFileId = hdfsType.getCoreSite().getId();
|
||||
this.coreSiteFilename = hdfsType.getCoreSite().getFilename();
|
||||
this.hdfsSiteFileId = hdfsType.getHdfsSite().getId();
|
||||
this.hdfsSiteFilename = hdfsType.getHdfsSite().getFilename();
|
||||
break;
|
||||
case FTP:
|
||||
FtpResourceType ftpType = (FtpResourceType) dataResource.getType();
|
||||
this.ftpUrl = ftpType.getUrl();
|
||||
this.ftpUsername = ftpType.getUsername();
|
||||
this.ftpPassword = ftpType.getPassword();
|
||||
this.ftpPath = ftpType.getPath();
|
||||
this.ftpRegexFilter = ftpType.getRegexFilter();
|
||||
break;
|
||||
}
|
||||
this.formatType = dataResource.getFormat().getFormatType();
|
||||
switch (dataResource.getFormat().getFormatType()) {
|
||||
case NONE:
|
||||
case LINE:
|
||||
break;
|
||||
case JSON:
|
||||
JsonResourceFormat jsonFormat = (JsonResourceFormat) dataResource.getFormat();
|
||||
this.jsonSchemaText = jsonFormat.getSchema();
|
||||
this.jsonSchema = mapper.readValue(jsonFormat.getSchema(), Map.class);
|
||||
break;
|
||||
case JSON_LINE:
|
||||
JsonLineResourceFormat jsonLineFormat = (JsonLineResourceFormat) dataResource.getFormat();
|
||||
this.jsonLineSchemaText = jsonLineFormat.getSchema();
|
||||
this.jsonLineSchema = mapper.readValue(jsonLineFormat.getSchema(), Map.class);
|
||||
break;
|
||||
case CSV:
|
||||
CsvResourceFormat csvFormat = (CsvResourceFormat) dataResource.getFormat();
|
||||
this.csvSchemaText = csvFormat.getSchema();
|
||||
this.csvSchema = mapper.readValue(csvFormat.getSchema(), Map.class);
|
||||
break;
|
||||
}
|
||||
if (ObjectUtil.isNotNull(dataResource.getExample())) {
|
||||
this.exampleFileId = dataResource.getExample().getId();
|
||||
this.exampleFilename = dataResource.getExample().getFilename();
|
||||
}
|
||||
|
||||
this.createdUsername = dataResource.getCreatedUser().getUsername();
|
||||
this.createdTime = dataResource.getCreatedTime();
|
||||
this.modifiedUsername = dataResource.getModifiedUser().getUsername();
|
||||
this.modifiedTime = dataResource.getModifiedTime();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.eshore.gringotts.web.domain.resource.service;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.eshore.gringotts.web.domain.base.repository.SimpleRepository;
|
||||
import com.eshore.gringotts.web.domain.base.service.SimpleService;
|
||||
import com.eshore.gringotts.web.domain.resource.entity.DataResource;
|
||||
import com.eshore.gringotts.web.domain.resource.entity.format.ResourceFormat;
|
||||
@@ -9,10 +7,7 @@ import com.eshore.gringotts.web.domain.resource.entity.type.ResourceType;
|
||||
import com.eshore.gringotts.web.domain.resource.repository.DataResourceRepository;
|
||||
import com.eshore.gringotts.web.domain.resource.repository.ResourceFormatRepository;
|
||||
import com.eshore.gringotts.web.domain.resource.repository.ResourceTypeRepository;
|
||||
import com.eshore.gringotts.web.domain.upload.entity.DataFile;
|
||||
import com.eshore.gringotts.web.domain.user.service.UserService;
|
||||
import com.eshore.gringotts.web.helper.EntityHelper;
|
||||
import javax.transaction.Transactional;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -23,47 +18,22 @@ import org.springframework.stereotype.Service;
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class DataResourceService extends SimpleService<DataResource, Long> {
|
||||
private final DataResourceRepository dataResourceRepository;
|
||||
public class DataResourceService extends SimpleService<DataResource> {
|
||||
private final ResourceTypeRepository resourceTypeRepository;
|
||||
private final ResourceFormatRepository resourceFormatRepository;
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
public DataResourceService(DataResourceRepository dataResourceRepository, ResourceTypeRepository resourceTypeRepository, ResourceFormatRepository resourceFormatRepository, UserService userService) {
|
||||
this.dataResourceRepository = dataResourceRepository;
|
||||
public DataResourceService(DataResourceRepository repository, ResourceTypeRepository resourceTypeRepository, ResourceFormatRepository resourceFormatRepository, UserService userService) {
|
||||
super(repository, userService);
|
||||
this.resourceTypeRepository = resourceTypeRepository;
|
||||
this.resourceFormatRepository = resourceFormatRepository;
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SimpleRepository<DataResource, Long> repository() {
|
||||
return dataResourceRepository;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void create(Long id, String name, String description, ResourceType type, ResourceFormat format, DataFile example) {
|
||||
type = resourceTypeRepository.save(type);
|
||||
format = resourceFormatRepository.save(format);
|
||||
DataResource dataResource = EntityHelper.fillCreatorAndModifier(new DataResource(), userService);
|
||||
dataResource.setId(id);
|
||||
dataResource.setName(name);
|
||||
dataResource.setDescription(description);
|
||||
dataResource.setType(type);
|
||||
dataResource.setFormat(format);
|
||||
dataResource.setExample(example);
|
||||
dataResourceRepository.saveOrUpdateByNotNullProperties(dataResource);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void remove(Long id) {
|
||||
dataResourceRepository.deleteById(id);
|
||||
}
|
||||
|
||||
public static final class DataResourceNotFoundException extends RuntimeException {
|
||||
public DataResourceNotFoundException(Long id) {
|
||||
super(StrUtil.format("ID为{}的数据资源未找到", id));
|
||||
}
|
||||
public Long save(DataResource entity) {
|
||||
ResourceType type = resourceTypeRepository.save(entity.getType());
|
||||
ResourceFormat format = resourceFormatRepository.save(entity.getFormat());
|
||||
entity.setType(type);
|
||||
entity.setFormat(format);
|
||||
return super.save(entity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
package com.eshore.gringotts.web.domain.upload.service;
|
||||
|
||||
import com.eshore.gringotts.web.domain.base.repository.SimpleRepository;
|
||||
import com.eshore.gringotts.web.domain.base.service.SimpleService;
|
||||
import com.eshore.gringotts.web.domain.upload.entity.DataFile;
|
||||
import com.eshore.gringotts.web.domain.upload.repository.DataFileRepository;
|
||||
import com.eshore.gringotts.web.domain.user.entity.User;
|
||||
import com.eshore.gringotts.web.domain.user.service.UserService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
@@ -16,21 +14,11 @@ import org.springframework.stereotype.Service;
|
||||
* @author lanyuanxiaoyao
|
||||
* @date 2024-11-21
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class DataFileService extends SimpleService<DataFile, Long> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(DataFileService.class);
|
||||
|
||||
private final DataFileRepository dataFileRepository;
|
||||
private final UserService userService;
|
||||
|
||||
public DataFileService(DataFileRepository dataFileRepository, UserService userService) {
|
||||
this.dataFileRepository = dataFileRepository;
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SimpleRepository<DataFile, Long> repository() {
|
||||
return dataFileRepository;
|
||||
public class DataFileService extends SimpleService<DataFile> {
|
||||
public DataFileService(DataFileRepository repository, UserService userService) {
|
||||
super(repository, userService);
|
||||
}
|
||||
|
||||
public DataFile detail(String id) {
|
||||
@@ -46,17 +34,17 @@ public class DataFileService extends SimpleService<DataFile, Long> {
|
||||
User loginUser = userService.currentLoginUser();
|
||||
dataFile.setCreatedUser(loginUser);
|
||||
dataFile.setModifiedUser(loginUser);
|
||||
return dataFileRepository.save(dataFile).getId();
|
||||
return repository.save(dataFile).getId();
|
||||
}
|
||||
|
||||
public void updateDataFile(Long id, String path, Long size, String md5) {
|
||||
DataFile dataFile = dataFileRepository.findById(id).orElseThrow(UpdateDataFileFailedException::new);
|
||||
DataFile dataFile = repository.findById(id).orElseThrow(UpdateDataFileFailedException::new);
|
||||
dataFile.setSize(size);
|
||||
dataFile.setMd5(md5);
|
||||
dataFile.setPath(path);
|
||||
User loginUser = userService.currentLoginUser();
|
||||
dataFile.setModifiedUser(loginUser);
|
||||
dataFileRepository.save(dataFile);
|
||||
repository.save(dataFile);
|
||||
}
|
||||
|
||||
public static final class DataFileNotFoundException extends RuntimeException {
|
||||
|
||||
Reference in New Issue
Block a user