class RepositoryResourceServiceImpl implements RepositoryResourceService {

	@Override
	public PageModel getPageModel(RepositoryResource page) {
		Integer pageId = page.getId();
		PageModel model = new PageModel();
		model.setPageId(pageId);
		
		Optional<RepositoryResource> projectInfoOption = this.findProjectInfo(page);
		if(projectInfoOption.isEmpty()) {
			return model;
		}
		
		List<AttachedWidget> widgets = getPageWidgets(projectInfoOption.get(), pageId);
		model.setWidgets(widgets);
		
		List<PageDataItem> pageData = getPageData(pageId);
		model.setData(pageData);
		
		List<PageEventHandler> functions;
		// 因为事件处理函数是与部件上的事件绑定的，所以这里加一层判断
		if(widgets.isEmpty()) {
			functions = Collections.emptyList();
		}else {
			// 获取页面中所有事件
			List<AttachedWidgetProperty> events = widgets.stream().flatMap(widget -> {
				return widget.getProperties().stream().filter(prop -> prop.getValueType().equals(WidgetPropertyValueType.FUNCTION.getKey()));
			}).collect(Collectors.toList());
			functions = getPageFunctions(pageId, events, pageData);
		}
		model.setFunctions(functions);
		
		return model;
	}

}