}
private ViewDTO createViewDTO(View view) {
assert view instanceof Persistable;
ViewDTO viewDTO = new ViewDTO();
DefaultResourceSetCollector persistanceManager = new DefaultResourceSetCollector();
// TODO use view content label instead of window title
// viewDTO.setTitle(window.getWindowTitle()); We will eventually store
// the title
viewDTO.setContentType(view.getContentType());
Persistable persistable = view;
viewDTO.setViewState(persistable.save(persistanceManager));
// Resource set DTOs
// 1. resolved unmodified sets --> changes list size
List<ResourceSet> resourceSets = new ArrayList<ResourceSet>(
persistanceManager.getResourceSets());
for (ResourceSet resourceSet : resourceSets) {
if (resourceSet instanceof UnmodifiableResourceSet) {
persistanceManager
.storeResourceSet(((UnmodifiableResourceSet) resourceSet)
.getDelegate());
}
}
// 2. store sets
ResourceSet resourceCollector = new DefaultResourceSet();
ResourceSetDTO[] resourceSetDTOs = new ResourceSetDTO[persistanceManager
.getResourceSets().size()];
for (int i = 0; i < persistanceManager.getResourceSets().size(); i++) {
ResourceSetDTO dto = new ResourceSetDTO();
ResourceSet resourceSet = persistanceManager.getResourceSets().get(
i);
if (resourceSet.hasLabel()) {
dto.setLabel(resourceSet.getLabel());
}
dto.setId(i);
if (resourceSet instanceof UnmodifiableResourceSet) {
ResourceSet sourceSet = ((UnmodifiableResourceSet) resourceSet)
.getDelegate();
dto.setDelegateSetId(persistanceManager
.storeResourceSet(sourceSet));
} else {
List<String> resourceIds = new ArrayList<String>();
for (Resource resource : resourceSet) {
resourceCollector.add(resource);
resourceIds.add(resource.getUri());
}
dto.setResourceIds(resourceIds);
}
resourceSetDTOs[i] = dto;
}
viewDTO.setResourceSets(resourceSetDTOs);
Resource[] resources = new Resource[resourceCollector.size()];
int count = 0;
for (Resource resource : resourceCollector) {
resources[count++] = resource;
}
viewDTO.setResources(resources);
viewDTO.setTitle(view.getLabel());
return viewDTO;
}