Commit d474006c LN

BadRequestException修改:增加国际化

1 个父辈 7cda0b7a
正在显示 24 个修改的文件 包含 178 行增加96 行删除
......@@ -30,8 +30,27 @@ public class BadRequestException extends RuntimeException{
private Integer status = BAD_REQUEST.value();
public BadRequestException(String msg){
super(msg);
private String msgKey="";
private String[] msgParam;
private String defaultMsg="";
// public BadRequestException( String defMsg){
// super(defMsg);
// this.msgKey=defMsg;
// this.msgParam=null;
// this.defaultMsg=defMsg;
// }
public BadRequestException(String msgKey, String defMsg){
super(defMsg);
this.msgKey=msgKey;
this.msgParam=null;
this.defaultMsg=defMsg;
}
public BadRequestException(String msgKey, String defMsg,String[] msgParam){
super(defMsg);
this.msgKey=msgKey;
this.msgParam=msgParam;
this.defaultMsg=defMsg;
}
public BadRequestException(HttpStatus status, String msg){
......
......@@ -5,8 +5,10 @@ import com.neotel.smfcore.common.exception.EntityExistException;
import com.neotel.smfcore.common.exception.EntityNotFoundException;
import com.neotel.smfcore.common.exception.ValidateException;
import com.neotel.smfcore.common.utils.ThrowableUtil;
import com.neotel.smfcore.core.language.util.MessageUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.BadCredentialsException;
......@@ -16,6 +18,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.support.HttpRequestHandlerServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.Locale;
import java.util.Objects;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
......@@ -24,6 +27,8 @@ import static org.springframework.http.HttpStatus.NOT_FOUND;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
@Autowired
private MessageUtils messageUtils;
/**
* 处理所有不可知的异常
*/
......@@ -53,7 +58,8 @@ public class GlobalExceptionHandler {
String language= servlet.getLocale().getLanguage();
// 打印堆栈信息
log.error(ThrowableUtil.getStackTrace(e));
return buildResponseEntity(ApiError.error(e.getStatus(),e.getMessage()+servlet.toString()));
String targetMsg=messageUtils.getText(e.getMsgKey(),e.getMsgParam(),new Locale(language) ,e.getDefaultMsg());
return buildResponseEntity(ApiError.error(e.getStatus(),targetMsg));
}
/**
......
......@@ -20,6 +20,7 @@ import cn.hutool.core.util.IdUtil;
import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil;
import com.neotel.smfcore.common.exception.BadRequestException;
import com.neotel.smfcore.core.language.util.MessageUtils;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.slf4j.Logger;
......@@ -254,7 +255,7 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
// 1M
int len = 1024 * 1024;
if (size > (maxSize * len)) {
throw new BadRequestException("文件超出规定大小");
throw new BadRequestException("smfcode.fileToLong", "文件超出规定大小");
}
}
......
......@@ -89,7 +89,7 @@ public class BarcodeController {
public ResponseEntity<Object> update(@Validated @RequestBody BarcodeDto resources) {
Barcode barcode=barcodeMapper.toEntity(resources);
if (barcode.getId() == null) {
throw new BadRequestException("修改条码:ID不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"ID"});
}
barcodeManager.saveBarcode(barcode);
return new ResponseEntity<>(HttpStatus.OK);
......@@ -102,7 +102,7 @@ public class BarcodeController {
List<Barcode> barcodes = new ArrayList<Barcode>();
for (String id : ids) {
if (id == null) {
throw new BadRequestException("删除条码:ID不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"ID"} );
}
}
barcodeManager.deleteBarcodes(ids);
......@@ -123,13 +123,13 @@ public class BarcodeController {
@PreAuthorize("@el.check('barcode:setting')")
public ResponseEntity<Object> updateSetting(@Validated @RequestBody BarcodeSettingDto settingDto) {
if(settingDto.getCodeRuleList().isEmpty()){
throw new BadRequestException("条码规则不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"codeRuleList"} );
}
if(settingDto.getPageHeight()==null){
throw new BadRequestException("纸张高度不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"pageHeight"} );
}
if(settingDto.getPageWidth()==null){
throw new BadRequestException("纸张宽度不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"pageWidth"} );
}
Settings settings=dataCache.getSettings();
settings.setCodeRuleList(settingDto.getCodeRuleList());
......@@ -146,7 +146,7 @@ public class BarcodeController {
String image = "csv";
String fileType = FileUtil.getExtensionName(uploadFile.getOriginalFilename());
if(fileType != null && !image.contains(fileType)){
throw new BadRequestException("文件格式错误!, 仅支持 " + image +" 格式");
throw new BadRequestException("smfcode.feleFormatError","文件格式错误!, 仅支持{0}格式",new String[]{image});
}
File folder = new File(properties.getPath(),"pos");
File file = FileUtil.upload(uploadFile, folder.getAbsolutePath());
......
......@@ -70,7 +70,7 @@ public class ComponentController {
public ResponseEntity<Object> update(@Validated @RequestBody ComponentDto resources) {
Component component=componentMapper.toEntity(resources);
if (component.getId() == null) {
throw new BadRequestException("修改条码:ID不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"ID"} );
}
componentManager.saveComponent(component);
return new ResponseEntity<>(HttpStatus.OK);
......@@ -83,7 +83,7 @@ public class ComponentController {
List<Component> Components = new ArrayList<Component>();
for (String id : ids) {
if (id == null) {
throw new BadRequestException("删除元器件:ID不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"ID"} );
}
}
componentManager.deleteComponents(ids);
......
......@@ -109,7 +109,7 @@ public class BarcodeManagerImpl implements IBarcodeManager {
//readID不能重复
Barcode data= barcodeDao.findOne(query);
if(data!=null){
throw new BadRequestException("条码["+resources.getBarcode()+"]已存在");
throw new BadRequestException("smfcode.codeExist", "条码["+resources.getBarcode()+"]已存在");
}
if(resources.getProvider()==null){
......@@ -177,22 +177,26 @@ public class BarcodeManagerImpl implements IBarcodeManager {
protected void validateSave(Barcode barcode) throws BadRequestException {
if (barcode.getBarcode()==null) {
throw new ValidateException("barcode.error.empty");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"barcode"} );
// throw new ValidateException("barcode.error.empty");
}
if (barcode.getPartNumber()==null) {
throw new BadRequestException("["+barcode.getBarcode()+"]barcode.error.partNumber.empty");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"partNumber"} );
// throw new BadRequestException("["+barcode.getBarcode()+"]barcode.error.partNumber.empty");
}
if (barcode.getBatch() == null) {
throw new BadRequestException("批次不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"batch"} );
// throw new BadRequestException("批次不能为空");
}
if (barcode.getAmount() < 0) {
throw new BadRequestException("["+barcode.getBarcode()+"]barcode.error.amount.negative" );
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"batch"} );
// throw new BadRequestException("["+barcode.getBarcode()+"]barcode.error.amount.negative" );
}
if (!validateUnique(barcode)) {
throw new BadRequestException("["+barcode.getBarcode()+"]barcode.error.unique" );
// throw new BadRequestException("["+barcode.getBarcode()+"]barcode.error.unique" );
}
if (!validateComponent(barcode)) {
throw new BadRequestException("["+barcode.getBarcode()+"]barcode.error.component.notExist" );
// throw new BadRequestException("["+barcode.getBarcode()+"]barcode.error.component.notExist" );
}
}
protected boolean validateUnique(Barcode barcode) {
......
......@@ -124,7 +124,8 @@ public class ComponentManagerImpl implements IComponentManager {
public Component saveComponent(Component resources) {
if(resources.getPartNumber()==null){
throw new BadRequestException("料号不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"partNumber"} );
// throw new BadRequestException("料号不能为空");
}
if(resources.getProvider()==null){
resources.setProvider("");
......@@ -150,7 +151,8 @@ public class ComponentManagerImpl implements IComponentManager {
}
Component com = componentDao.findOne(new Query(c));
if (com != null) {
throw new BadRequestException("料号["+resources.getPartNumber()+"]已存在");
throw new BadRequestException("smfcode.valueAlreadyExist","{0}[{1}]已存在",new String[]{"partNumber",resources.getPartNumber()});
// throw new BadRequestException("料号["+resources.getPartNumber()+"]已存在");
}
resources= componentDao.save(resources);
......
......@@ -20,7 +20,7 @@ public class LocaleConfig {
/**
* 默认解析器 其中locale表示默认语言
*/
@Bean
// @Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(Locale.CHINESE);
......
......@@ -4,6 +4,7 @@ import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import java.text.MessageFormat;
import java.util.Locale;
/**
......@@ -25,10 +26,12 @@ public class MessageUtils {
public String getText(String msgKey, String[] params, Locale locale, String defaultMsg) {
try{
if (params != null) {
return messageSource.getMessage(msgKey, params, locale);
if (params == null) {
return defaultMsg;
// return messageSource.getMessage(msgKey, params, locale);
} else {
return messageSource.getMessage(msgKey, new String[]{}, locale);
return MessageFormat.format(defaultMsg,params);
// return messageSource.getMessage(msgKey, new String[]{}, locale);
}
}catch (Exception ex){
if(defaultMsg != null){
......
......@@ -99,14 +99,15 @@ public class OrderController {
String image = "csv";
String fileType = FileUtil.getExtensionName(orderFile.getOriginalFilename());
if (fileType != null && !image.contains(fileType)) {
throw new BadRequestException("文件格式错误!, 仅支持 " + image + " 格式");
throw new BadRequestException("smfcode.feleFormatError","文件格式错误!, 仅支持{0}格式",new String[]{image});
}
File folder = new File(properties.getPath(), "pos");
File localFile = FileUtil.upload(orderFile, folder.getAbsolutePath());
List<LiteOrderItem> liteOrderItems = handleOrderCsv(localFile.getAbsolutePath());
if (liteOrderItems == null || liteOrderItems.size() <= 0) {
throw new BadRequestException("文件解析失败");
throw new BadRequestException("smfcode.fileError","文件解析失败");
}
LiteOrder liteOrder = new LiteOrder(localFile.getName(), liteOrderItems);
......@@ -120,7 +121,9 @@ public class OrderController {
// TaskService.liteOrderMap.put(liteOrder.getOrderNo(), liteOrder);
} else {
log.info("数据库中已存在工单号为[" + liteOrder.getOrderNo() + "],忽略文件:" + localFile.getAbsolutePath());
throw new BadRequestException("工单号[" + liteOrder.getOrderNo() + "]已存在");
throw new BadRequestException("smfcode.valueAlreadyExist","{0}[{1}]已存在",new String[]{"orderNo",liteOrder.getOrderNo()});
// throw new BadRequestException("工单号[" + liteOrder.getOrderNo() + "]已存在");
}
return ResultBean.newOkResult("工单上传成功");
......@@ -132,11 +135,12 @@ public class OrderController {
public ResultBean delete(@RequestBody Map<String,String> mapValues) {
String orderNo = mapValues.get("orderNo");
if (orderNo == null) {
throw new BadRequestException("工单号不能为空");
// throw new BadRequestException("工单号不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"orderNo"} );
}
LiteOrder liteOrder = liteOrderManager.findByOrderNo(orderNo);
if (liteOrder == null) {
throw new BadRequestException("未找到工单:" + orderNo);
throw new BadRequestException("smfcode.valueNotFind","未找到{0}[{1}]" ,new String[]{"orderNo", orderNo});
}
// ResultBean resultBean = liteOrderCache.checkOutOrder(liteOrder);
String result = liteOrderCache.checkOutLiteOrder(orderNo, false);
......
......@@ -101,7 +101,7 @@ public class StorageController {
public ResponseEntity<Object> update(@Validated @RequestBody StorageDto resources) {
Storage Storage=storageMapper.toEntity(resources);
if (Storage.getId() == null) {
throw new BadRequestException("修改条码:ID不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"ID"} );
}
saveStorage(Storage);
return new ResponseEntity<>(HttpStatus.OK);
......@@ -116,7 +116,7 @@ public class StorageController {
for (String id : ids) {
if (id == null) {
throw new BadRequestException("删除料仓:ID不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"ID"} );
}
}
deleteStorages(ids);
......@@ -138,7 +138,8 @@ public class StorageController {
if (pos.getBarcode() == null || pos.getBarcode().equals("")) {
} else {
throw new BadRequestException("料仓[" + storage.getName() + "]的库位[" + pos.getPosName() + "]有料[" + pos.getBarcode() + "],删除失败");
throw new BadRequestException("smfcode.posIsused","料仓[{0}]的库位[{1}}]有料[{2}],删除失败",new String[]{ storage.getName() ,pos.getPosName(),pos.getBarcode().getBarcode()});
// throw new BadRequestException("料仓[" + storage.getName() + "]的库位[" + pos.getPosName() + "]有料[" + pos.getBarcode() + "],删除失败");
}
}
}
......@@ -152,13 +153,17 @@ public class StorageController {
}
private Storage saveStorage(Storage storage) {
if(storage.getName()==null){
throw new BadRequestException("料仓名称不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"name"} );
// throw new BadRequestException("料仓名称不能为空");
} if(storage.getCid()==null){
throw new BadRequestException("料仓编号不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"cid"} );
// throw new BadRequestException("料仓编号不能为空");
} if(storage.getType()==null){
throw new BadRequestException("料仓类型不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"type"} );
// throw new BadRequestException("料仓类型不能为空");
} if(storage.getCompatibleType()==null){
throw new BadRequestException("料仓兼容性不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"compatibleType"} );
// throw new BadRequestException("料仓兼容性不能为空");
}
boolean isNew=(storage.getId()==null);
......@@ -169,10 +174,12 @@ public class StorageController {
if(isNew || (!storage.getId().equals(s.getId()))) {
if(s.getCid().equals(storage.getCid())){
throw new BadRequestException("料仓cid["+storage.getCid()+"]已存在");
throw new BadRequestException("smfcode.valueAlreadyExist","{0}[{1}]已存在",new String[]{"cid",storage.getCid()});
// throw new BadRequestException("料仓cid["+storage.getCid()+"]已存在");
}
else if(s.getName().equals(storage.getName())){
throw new BadRequestException("料仓名称["+storage.getCid()+"]已存在");
throw new BadRequestException("smfcode.valueAlreadyExist","{0}[{1}]已存在",new String[]{"name",storage.getName()});
// throw new BadRequestException("料仓名称["+storage.getCid()+"]已存在");
}
}
}
......@@ -197,7 +204,7 @@ public class StorageController {
String image = "csv";
String fileType = FileUtil.getExtensionName(uploadFile.getOriginalFilename());
if(fileType != null && !image.contains(fileType)){
throw new BadRequestException("文件格式错误!, 仅支持 " + image +" 格式");
throw new BadRequestException("smfcode.feleFormatError","文件格式错误!, 仅支持{0}格式",new String[]{image});
}
File folder = new File(properties.getPath(),"pos");
File file = FileUtil.upload(uploadFile, folder.getAbsolutePath());
......
......@@ -78,7 +78,8 @@ public class StoragePosController {
log.info("开始清理料仓["+cid+"] 所有位置");
Storage storage=dataCache.getStorage(cid);
if(storage==null){
throw new BadRequestException("未找到料仓:"+cid);
throw new BadRequestException("smfcode.valueNotFind","未找到{0}[{1}]" ,new String[]{"cid", cid});
// throw new BadRequestException("未找到料仓:"+cid);
}
storagePosManager.removePosByStorageId(storage.getId());
log.info("清理料仓["+cid+"] 所有位置完成");
......@@ -98,11 +99,12 @@ public class StoragePosController {
@PreAuthorize("@el.check('storage:edit')")
public ResponseEntity<Object> enabledPos(@RequestBody StoragePosEnabledDto enabledDto) {
if(enabledDto.getId()==null){
throw new BadRequestException("库位ID为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"ID"} );
}
StoragePos pos=storagePosDao.findOneById(enabledDto.getId());
if(pos==null){
throw new BadRequestException("未找到库位 ");
throw new BadRequestException("smfcode.valueNotFind","未找到{0}[{1}]" ,new String[]{"PosId", enabledDto.getId()});
// throw new BadRequestException("未找到库位 ");
}
pos.setEnabled(enabledDto.isEnabled());
storagePosDao.save(pos);
......@@ -117,14 +119,15 @@ public class StoragePosController {
@PreAuthorize("@el.check('storage:edit')")
public ResponseEntity<Object> update(@Validated @RequestBody StoragePosSaveDto saveDto) {
if(saveDto.getId()==null){
throw new BadRequestException("库位ID为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"ID"} );
}
if(saveDto.getPosName()==null){
throw new BadRequestException("库位ID为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"ID"} );
}
StoragePos pos=storagePosDao.findOneById(saveDto.getId());
if(pos==null){
throw new BadRequestException("未找到库位 ");
throw new BadRequestException("smfcode.valueNotFind","未找到{0}[{1}]" ,new String[]{"PosId", saveDto.getId()});
// throw new BadRequestException("未找到库位 ");
}
pos.setPosName(saveDto.getPosName());
pos.setPriority(saveDto.getPriority());
......@@ -150,7 +153,7 @@ public class StoragePosController {
for (String id : ids) {
if (id == null) {
throw new BadRequestException("删除库位:ID不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"ID"} );
}
}
storagePosManager.deletePoss(ids);
......@@ -201,26 +204,34 @@ public class StoragePosController {
@PutMapping("/checkout")
public ResponseEntity<Object> checkout(@Validated @RequestBody CheckOutDto checkOutDto) {
if (checkOutDto.getPids() == null) {
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"ID"} );
}
if(checkOutDto.getSingleOut()==null){
checkOutDto.setSingleOut(true+"");
}
String isSingleOutStr = checkOutDto.getSingleOut();
boolean isSingleOut = Boolean.valueOf(isSingleOutStr);
String pids = checkOutDto.getPids();
for (String pid : pids.split(";")) {
// String pids = checkOutDto.getPids();
for (String pid : checkOutDto.getPids()) {
StoragePos pos = storagePosManager.get(pid);
if (pos == null) {
throw new BadRequestException("位置[" + pid + "]不存在");
throw new BadRequestException("smfcode.valueNotExist","{0}[{1}]不存在",new String[]{"pid",pid});
// throw new BadRequestException("位置[" + pid + "]不存在");
}
Storage storage = dataCache.getStorageById(pos.getStorageId());
if (storage == null) {
throw new BadRequestException("料仓[" + pos.getStorageId() + "]不存在");
throw new BadRequestException("smfcode.valueNotExist","{0}[{1}]不存在",new String[]{"storageId",pos.getStorageId()});
// throw new BadRequestException("料仓[" + pos.getStorageId() + "]不存在");
}
log.info("出库料仓【" + storage.getName() + "_" + storage.getCid() + "】位置仓位【" + pos.getPosName() + "】");
String outResult = taskService.checkout(storage, pos, isSingleOut);
if (!Strings.isNullOrEmpty(outResult)) {
throw new BadRequestException(outResult);
throw new BadRequestException("smfcode.error", outResult);
}
}
return new ResponseEntity<>(HttpStatus.OK);
}
}
......@@ -5,8 +5,8 @@ import lombok.Data;
@Data
public class CheckOutDto {
@ApiModelProperty("库位号,多个用;分隔")
private String pids;
@ApiModelProperty("库位ID号 ")
private String[] pids;
@ApiModelProperty("是否是单盘出库")
private String singleOut;
}
......@@ -266,7 +266,8 @@ public class StoragePosManagerImpl implements IStoragePosManager {
if(pos.getBarcode()==null||pos.getBarcode().equals("")) {
}else{
throw new BadRequestException("库位[" + pos.getPosName() + "]中有料[" + pos.getBarcode() + "],不能删除");
throw new BadRequestException("smfcode.thePosIsused"," 库位[{0}]有料[{1}],不能删除",new String[]{ pos.getPosName(),pos.getBarcode().getBarcode()});
// throw new BadRequestException("库位[" + pos.getPosName() + "]中有料[" + pos.getBarcode() + "],不能删除");
}
delPosName+="["+pos.getId()+"_"+pos.getPosName()+"]";
}
......
......@@ -48,16 +48,21 @@ public class SettingsController {
public ResponseEntity<Object> update(@Validated @RequestBody SettingsDto settingsDto) {
if(settingsDto.getMaxHumidity()<=settingsDto.getMinHumidity()){
throw new BadRequestException("温度范围数据错误");
throw new BadRequestException("smfcode.humidityValueError","温度范围数据错误" );
// throw new BadRequestException("温度范围数据错误");
}
if(settingsDto.getMaxHumidityShow()<=settingsDto.getMinHumidityShow()){
throw new BadRequestException("温度显示范围数据错误");
throw new BadRequestException("smfcode.humidityShowValueError","温度显示范围数据错误" );
// throw new BadRequestException("温度显示范围数据错误");
}
if(settingsDto.getMaxTemperature()<=settingsDto.getMinTemperature()){
throw new BadRequestException("湿度范围数据错误");
throw new BadRequestException("smfcode.temperatureValueError","湿度范围数据错误" );
// throw new BadRequestException("湿度范围数据错误");
}
if(settingsDto.getMaxTemperatureShow()<=settingsDto.getMinTemperatureShow()){
throw new BadRequestException("湿度显示范围数据错误");
throw new BadRequestException("smfcode.temperatureShowValueError","湿度显示范围数据错误" );
// throw new BadRequestException("湿度显示范围数据错误");
}
if(settingsDto.getInNotifyApi()==null){
......
......@@ -96,7 +96,7 @@ public class GroupController {
public ResponseEntity<Object> update(@RequestBody GroupDto groupDto) {
Group resources=groupMapper.toEntity(groupDto);
if (resources.getId() == null) {
throw new BadRequestException("新增分组: 组ID不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"ID"} );
}
groupManager.saveGroup(resources);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
......@@ -109,20 +109,21 @@ public class GroupController {
Set<Group> menuSet = new HashSet<>();
for (String id : ids) {
if (id == null) {
throw new BadRequestException("删除: 组ID不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"ID"} );
}
Group group = groupManager.get(id);
//查找组下是否有设备
Query query = new Query(Criteria.where("groupId").is(id));
List<Storage> storages = storageManager.findByQuery(query);
if (storages != null && storages.size() >= 1) {
throw new BadRequestException("删除失败,组[" + group.getGroupName() + "]已和设备关联");
throw new BadRequestException("smfcode.groupWithStorage","组[{0}]已和设备关联",new String[]{group.getGroupName()});
}
Query userQuery = new Query(Criteria.where("groups").all(id));
List<User> users = userManager.findByQuery(userQuery);
if (users != null && users.size() >= 1) {
throw new BadRequestException("删除失败,组[" + group.getGroupName() + "]已和用户关联");
throw new BadRequestException("smfcode.groupWithUser","组[{0}]已和用户关联",new String[]{group.getGroupName()});
// throw new BadRequestException("删除失败,组[" + group.getGroupName() + "]已和用户关联");
}
groupManager.delete(group);
}
......
......@@ -156,7 +156,7 @@ public class MenuController {
public ResponseEntity<Object> update(@Validated @RequestBody MenuDto menuDto) {
Menu resources=menuMapper.toEntity(menuDto);
if (resources.getId() == null) {
throw new BadRequestException(" 修改菜单 :ID不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"ID"} );
}
menuManager.saveMenu(resources);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
......@@ -168,7 +168,7 @@ public class MenuController {
@PreAuthorize("@el.check('menu:del')")
public ResponseEntity<Object> delete(@RequestBody Set<String> ids) {
if(!SecurityUtils.getCurrentUsername().equals(Constants.SUPER_USERNAME)){
throw new BadRequestException("没有删除菜单的权限");
throw new BadRequestException("smfcode.noAccessUpdate","没有删除菜单的权限");
}
Set<Menu> menuSet = new HashSet<>();
for (String id : ids) {
......
......@@ -104,7 +104,7 @@ public class RoleController {
Role resources=roleMapper.toEntity(roleDto);
if (resources.getId() == null) {
throw new BadRequestException("修改角色:ID不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"ID"} );
}
roleManager.saveRole(resources);
return new ResponseEntity<>(HttpStatus.OK);
......@@ -116,7 +116,7 @@ public class RoleController {
public ResponseEntity<Object> updateMenu(@RequestBody RoleDto roleDto) {
Role resources=roleMapper.toEntity(roleDto);
if (resources.getId() == null) {
throw new BadRequestException("修改角色菜单:ID不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"ID"} );
}
Role role = roleManager.get(resources.getId());
roleManager.updateMenu(resources);
......@@ -131,13 +131,13 @@ public class RoleController {
List<Role> roles = new ArrayList<Role>();
for (String id : ids) {
if (id == null) {
throw new BadRequestException("删除角色:ID不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"ID"} );
}
Role role = roleManager.get(id);
// 验证是否被用户关联
List<User> users = userManager.findByRoleId(role.getId());
if (users != null && users.size() > 0) {
throw new BadRequestException("删除失败:角色[" + role.getName() + "]有[" + users.size() + "]个用户关联改角色");
throw new BadRequestException("smfcode.roleCannotDel","角色[{0}]有[{1}]个用户关联改角色,不能删除",new String[]{role.getName(),users.size()+"" });
}
roles.add(role);
}
......
......@@ -105,13 +105,16 @@ public class UserController {
public ResponseEntity<Object> create(@Validated @RequestBody UserDto userDto) {
User resources=userMapper.toEntity(userDto);
if (resources.getId() != null) {
throw new BadRequestException("新增用户:ID已存在");
throw new BadRequestException("smfcode.valueAlreadyExist","{0}[{1}]已存在",new String[]{"ID",resources.getId()});
// throw new BadRequestException("新增用户:ID已存在");
}
if(resources.getUsername()==null){
throw new BadRequestException("新增用户:用户名不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"username"} );
// throw new BadRequestException("新增用户:用户名不能为空");
}
if(resources.getRoleId()==null){
throw new BadRequestException("新增用户:角色ID不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"roleId"} );
// throw new BadRequestException("新增用户:角色ID不能为空");
}
if(resources.getEnabled()==null){
resources.setEnabled(false);
......@@ -137,17 +140,21 @@ public class UserController {
public ResponseEntity<Object> update(@Validated @RequestBody UserDto userDto) {
User resources=userMapper.toEntity(userDto);
if (resources.getId() == null) {
throw new BadRequestException("修改用户:ID不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"ID"} );
// throw new BadRequestException("修改用户:ID不能为空");
}
if(resources.getUsername()==null){
throw new BadRequestException("修改用户:用户名不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"username"} );
// throw new BadRequestException("修改用户:用户名不能为空");
}
if(resources.getRoleId()==null){
throw new BadRequestException("修改用户:角色ID不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"roleId"} );
// throw new BadRequestException("修改用户:角色ID不能为空");
}
if (resources.getUsername().equals(Constants.SUPER_USERNAME)) {
if (!resources.getId().equals(SecurityUtils.getCurrentUserId())) {
throw new BadRequestException("无权限修改此用户信息");
throw new BadRequestException("smfcode.hasNoAccess","无权限修改此用户信息" );
// throw new BadRequestException("无权限修改此用户信息");
}
}
userManager.update(resources);
......@@ -161,7 +168,8 @@ public class UserController {
public ResponseEntity<Object> center(@Validated @RequestBody UserDto userDto) {
User resources=userMapper.toEntity(userDto);
if (!resources.getId().equals(SecurityUtils.getCurrentUserId())) {
throw new BadRequestException("不能修改他人资料");
throw new BadRequestException("smfcode.hasNoAccess","无权限修改此用户信息" );
// throw new BadRequestException("不能修改他人资料");
}
userManager.updateCenter(resources);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
......@@ -176,13 +184,14 @@ public class UserController {
//不能删除自己
String currId = SecurityUtils.getCurrentUserId();
if (currId == id) {
throw new BadRequestException("删除用户:不能删除自己");
throw new BadRequestException("smfcode.canotDelSelf","删除用户:不能删除自己");
}
User user = userManager.get(id);
//admin的用户不让删除
if ( user.getUsername().equals(Constants.SUPER_USERNAME)) {
throw new BadRequestException("删除用户:此用户不能删除");
throw new BadRequestException("smfcode.canotDelUser","删除用户:此用户不能删除");
// throw new BadRequestException("删除用户:此用户不能删除");
}
}
userManager.deleteUsers(ids);
......@@ -195,10 +204,10 @@ public class UserController {
User user = userManager.get(SecurityUtils.getCurrentUserId());
if (!passwordEncoder.matches(passVo.getOldPass(), user.getPassword())) {
throw new BadRequestException("修改失败,旧密码错误");
throw new BadRequestException("smfcode.oldPwdError","修改失败,旧密码错误");
}
if (passwordEncoder.matches(passVo.getNewPass(), user.getPassword())) {
throw new BadRequestException("新密码不能与旧密码相同");
throw new BadRequestException("smfcode.newPwdError","新密码不能与旧密码相同");
}
userManager.updatePass(user.getUsername(), passwordEncoder.encode(passVo.getNewPass()));
return new ResponseEntity<>(HttpStatus.OK);
......@@ -212,7 +221,7 @@ public class UserController {
String password = RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, user.getPassword());
User myUser = userManager.findByUserName(SecurityUtils.getCurrentUsername());
if (!passwordEncoder.matches(password, myUser.getPassword())) {
throw new BadRequestException("修改邮箱失败,密码错误");
throw new BadRequestException("smfcode.pwdError","修改邮箱失败,密码错误");
}
// verificationCodeService.validated(CodeEnum.EMAIL_RESET_EMAIL_CODE.getKey() + user.getEmail(), code);
userManager.updateEmail(myUser.getUsername(), myUser.getEmail());
......@@ -224,7 +233,8 @@ public class UserController {
public ResponseEntity<Object> updateGroup(@RequestBody UserDto userDto) {
User resources=userMapper.toEntity(userDto);
if (resources.getId() == null) {
throw new BadRequestException("修改用户组:ID不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"ID"} );
// throw new BadRequestException("修改用户组:ID不能为空");
}
User user = userManager.get(resources.getId());
userManager.updateGroups(resources);
......@@ -237,7 +247,7 @@ public class UserController {
String image = "csv";
String fileType = FileUtil.getExtensionName(uploadFile.getOriginalFilename());
if(fileType != null && !image.contains(fileType)){
throw new BadRequestException("文件格式错误!, 仅支持 " + image +" 格式");
throw new BadRequestException("smfcode.feleFormatError","文件格式错误!, 仅支持{0}格式",new String[]{image});
}
File folder = new File(properties.getPath(),"user");
File file = FileUtil.upload(uploadFile, folder.getAbsolutePath());
......
......@@ -82,10 +82,10 @@ public class UserDetailsServiceImpl implements UserDetailsService {
throw new UsernameNotFoundException("");
} else {
if(user.getEnabled()==null){
throw new BadRequestException("账号未激活");
throw new BadRequestException("smfcode.notActivated","账号未激活");
}
if (!user.getEnabled()) {
throw new BadRequestException("账号未激活!");
throw new BadRequestException("smfcode.notActivated","账号未激活");
}
if(user.getUsername().equals(Constants.SUPER_USERNAME)){
......@@ -93,7 +93,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
//判断激活码是否正确
String code = Md5Utls.getMd5(user.getId(), user.getCreateDate());
if (!code.equals(user.getCheckCode())) {
throw new BadRequestException("账号未激活!");
throw new BadRequestException("smfcode.notActivated","账号未激活");
}
}
List<Long> dataScopes = new ArrayList<>();
......
......@@ -43,7 +43,8 @@ public class GroupManagerImpl implements IGroupManager {
logName="修改分组:";
}
if (resources.getGroupName() == null) {
throw new BadRequestException(logName+"组名称不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"groupName"} );
// throw new BadRequestException(logName+"组名称不能为空");
}
Query que = new Query(c);
List<Group> groups = findByQuery(que);
......
......@@ -229,10 +229,12 @@ public class MenuManagerImpl implements IMenuManager {
if(menu.getType()==null){
throw new BadRequestException("菜单类型不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"Type"} );
// throw new BadRequestException("菜单类型不能为空");
}
if(menu.getTitle()==null){
throw new BadRequestException( "菜单标题不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"Title"} );
// throw new BadRequestException( "菜单标题不能为空");
}
if(menu.getChildren()==null){
menu.setChildren(new ArrayList<Menu>());
......@@ -265,7 +267,8 @@ public class MenuManagerImpl implements IMenuManager {
//菜单path不能重复
Menu pathMenu=menuDao.findOne(quer);
if(pathMenu!=null){
throw new BadRequestException("路径["+pathMenu+"]已存在");
throw new BadRequestException("smfcode.valueAlreadyExist","{0}[{1}]已存在",new String[]{"path",pathMenu.getPath()});
// throw new BadRequestException("路径["+pathMenu+"]已存在");
}
return menuDao.save(menu);
......
......@@ -81,7 +81,8 @@ public class RoleManagerImpl implements IRoleManager {
@Override
public Role saveRole(Role role) throws ValidateException {
if(role.getName()==null){
throw new BadRequestException("角色名称不能为空");
throw new BadRequestException("smfcode.valueCanotNull","{0}不能为空",new String[]{"name"} );
// throw new BadRequestException("角色名称不能为空");
}
if (role.getMenus() == null) {
role.setMenus(new HashSet<String>());
......@@ -109,11 +110,13 @@ public class RoleManagerImpl implements IRoleManager {
Role role=this.get(resources.getId());
if(role==null){
throw new BadRequestException("请要修改的选择角色");
throw new BadRequestException("smfcode.notSelRole","请要修改的选择角色");
// throw new BadRequestException("请要修改的选择角色");
}
if(role.getName().equals("admin")){
if(!SecurityUtils.getCurrentUsername().equals(Constants.SUPER_USERNAME)){
throw new BadRequestException("只有超级管理员才能修改此角色的菜单");
throw new BadRequestException("smfcode.noaccess","无权限修改此角色的菜单");
// throw new BadRequestException("只有超级管理员才能修改此角色的菜单");
}
}
role.setMenus(resources.getMenus());
......
......@@ -202,7 +202,8 @@ public class UserManagerImpl implements IUserManager {
public String GetUserCheckCode(User user){
if(user.getId()==null||user.getCreateDate()==null){
throw new BadRequestException("用户信息不完整");
throw new BadRequestException("smfcode.userInfoError","用户信息不完整");
// throw new BadRequestException("用户信息不完整");
}
return "";
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!