|
|
|
@ -420,6 +420,41 @@ public class ResourcesService extends BaseService {
|
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* verify resource by name and type |
|
|
|
|
* @param name |
|
|
|
|
* @param type |
|
|
|
|
* @param loginUser |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public Result verifyResourceName(String name, ResourceType type,User loginUser) { |
|
|
|
|
Result result = new Result(); |
|
|
|
|
putMsg(result, Status.SUCCESS); |
|
|
|
|
Resource resource = resourcesMapper.queryResourceByNameAndType(name, type.ordinal()); |
|
|
|
|
if (resource != null) { |
|
|
|
|
logger.error("resource type:{} name:{} has exist, can't create again.", type, name); |
|
|
|
|
putMsg(result, Status.RESOURCE_EXIST); |
|
|
|
|
} else { |
|
|
|
|
// query tenant
|
|
|
|
|
String tenantCode = tenantMapper.queryById(loginUser.getTenantId()).getTenantCode(); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
String hdfsFilename = getHdfsFileName(type,tenantCode,name); |
|
|
|
|
if(HadoopUtils.getInstance().exists(hdfsFilename)){ |
|
|
|
|
logger.error("resource type:{} name:{} has exist in hdfs {}, can't create again.", type, name,hdfsFilename); |
|
|
|
|
putMsg(result, Status.RESOURCE_FILE_EXIST,hdfsFilename); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
logger.error(e.getMessage(),e); |
|
|
|
|
putMsg(result,Status.HDFS_OPERATION_ERROR); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* verify resource by name and type |
|
|
|
|
* |
|
|
|
@ -480,6 +515,7 @@ public class ResourcesService extends BaseService {
|
|
|
|
|
String hdfsFileName = HadoopUtils.getHdfsFilename(tenantCode, resource.getAlias()); |
|
|
|
|
logger.info("resource hdfs path is {} ", hdfsFileName); |
|
|
|
|
try { |
|
|
|
|
if(HadoopUtils.getInstance().exists(hdfsFileName)){ |
|
|
|
|
List<String> content = HadoopUtils.getInstance().catFile(hdfsFileName, skipLineNum, limit); |
|
|
|
|
|
|
|
|
|
putMsg(result, Status.SUCCESS); |
|
|
|
@ -487,6 +523,11 @@ public class ResourcesService extends BaseService {
|
|
|
|
|
map.put(ALIAS, resource.getAlias()); |
|
|
|
|
map.put(CONTENT, StringUtils.join(content.toArray(), "\n")); |
|
|
|
|
result.setData(map); |
|
|
|
|
}else{ |
|
|
|
|
logger.error("read file {} not exist in hdfs", hdfsFileName); |
|
|
|
|
putMsg(result, Status.RESOURCE_FILE_NOT_EXIST,hdfsFileName); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
logger.error(String.format("Resource %s read failed", hdfsFileName), e); |
|
|
|
|
putMsg(result, Status.HDFS_OPERATION_ERROR); |
|
|
|
@ -530,17 +571,14 @@ public class ResourcesService extends BaseService {
|
|
|
|
|
|
|
|
|
|
String name = fileName.trim() + "." + nameSuffix; |
|
|
|
|
|
|
|
|
|
//check file already exists
|
|
|
|
|
Resource resource = resourcesMapper.queryResourceByNameAndType(name, type.ordinal()); |
|
|
|
|
if (resource != null) { |
|
|
|
|
logger.error("resource {} has exist, can't recreate .", name); |
|
|
|
|
putMsg(result, Status.RESOURCE_EXIST); |
|
|
|
|
result = verifyResourceName(name,type,loginUser); |
|
|
|
|
if (!result.getCode().equals(Status.SUCCESS.getCode())) { |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// save data
|
|
|
|
|
Date now = new Date(); |
|
|
|
|
resource = new Resource(name,name,desc,loginUser.getId(),type,content.getBytes().length,now,now); |
|
|
|
|
Resource resource = new Resource(name,name,desc,loginUser.getId(),type,content.getBytes().length,now,now); |
|
|
|
|
|
|
|
|
|
resourcesMapper.insert(resource); |
|
|
|
|
|
|
|
|
@ -569,6 +607,7 @@ public class ResourcesService extends BaseService {
|
|
|
|
|
* @param resourceId |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
@Transactional(value = "TransactionManager",rollbackFor = Exception.class) |
|
|
|
|
public Result updateResourceContent(int resourceId, String content) { |
|
|
|
|
Result result = new Result(); |
|
|
|
|
|
|
|
|
@ -597,6 +636,10 @@ public class ResourcesService extends BaseService {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
resource.setSize(content.getBytes().length); |
|
|
|
|
resource.setUpdateTime(new Date()); |
|
|
|
|
resourcesMapper.update(resource); |
|
|
|
|
|
|
|
|
|
User user = userMapper.queryDetailsById(resource.getUserId()); |
|
|
|
|
String tenantCode = tenantMapper.queryById(user.getTenantId()).getTenantCode(); |
|
|
|
|
|
|
|
|
@ -643,6 +686,7 @@ public class ResourcesService extends BaseService {
|
|
|
|
|
logger.error("{} is not exist", resourcePath); |
|
|
|
|
result.setCode(Status.HDFS_OPERATION_ERROR.getCode()); |
|
|
|
|
result.setMsg(String.format("%s is not exist", resourcePath)); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
logger.error(e.getMessage(), e); |
|
|
|
@ -809,6 +853,23 @@ public class ResourcesService extends BaseService {
|
|
|
|
|
return hdfsFileName; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* get hdfs file name |
|
|
|
|
* |
|
|
|
|
* @param resourceType |
|
|
|
|
* @param tenantCode |
|
|
|
|
* @param hdfsFileName |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
private String getHdfsFileName(ResourceType resourceType, String tenantCode, String hdfsFileName) { |
|
|
|
|
if (resourceType.equals(ResourceType.FILE)) { |
|
|
|
|
hdfsFileName = HadoopUtils.getHdfsFilename(tenantCode, hdfsFileName); |
|
|
|
|
} else if (resourceType.equals(ResourceType.UDF)) { |
|
|
|
|
hdfsFileName = HadoopUtils.getHdfsUdfFilename(tenantCode, hdfsFileName); |
|
|
|
|
} |
|
|
|
|
return hdfsFileName; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* get authorized resource list |
|
|
|
|
* |
|
|
|
|