class RepositoryResourceServiceImpl implements RepositoryResourceService {

	@Override
	public List<RepositoryResource> findParentGroupsByParentPath(Integer repositoryId, String parentPath) {
		List<RepositoryResource> result = new ArrayList<RepositoryResource>();
		
		if(repositoryId == null) {
			return result;
		}
		if(StringUtils.isBlank(parentPath)) {
			return result;
		}
		
		String[] keys = parentPath.trim().split("/");
		Integer parentId = Constant.TREE_ROOT_ID;
		boolean allMatched = true;
		for(int i = 0; i < keys.length; i++) {
			String key = keys[i];
			Optional<RepositoryResource> resourceOption;
			if(i == 0) {
				// 只支持在仓库的根目录创建 Project，不支持创建 Group
				resourceOption = repositoryResourceDao.findByRepositoryIdAndParentIdAndResourceTypeAndKeyIgnoreCase(
						repositoryId, 
						parentId, 
						RepositoryResourceType.PROJECT, 
						key);
			} else {
				resourceOption = repositoryResourceDao.findByRepositoryIdAndParentIdAndResourceTypeAndKeyIgnoreCase(
						repositoryId, 
						parentId, 
						RepositoryResourceType.GROUP, 
						key);
			}
			if(resourceOption.isEmpty()) {
				allMatched = false;
				break;
			}
			result.add(resourceOption.get());
			parentId = resourceOption.get().getId();
		}
		
		if(!allMatched) {
			return new ArrayList<RepositoryResource>();
		}
		
		return result;
	}

}