@Override public void execute(ResourceRepository sut) { Resource result1 = sut.resourceOfId(resourceId); debug("Retrieving resource {%s}: %s",resourceId,result1); Container result2 = sut.containerOfId(containerId); debug("Retrieving container {%s}: %s",containerId,result2); Resource result3 = sut.resourceById(memberId,Resource.class); debug("Retrieving member resource {%s}: %s",memberId,result3); } }
private void deleteResource(final Resource resource, final Date lastModified) { try { this.resourceRepository.remove(resource); final Endpoint endpoint = this.endpointManagementService. deleteResourceEndpoint( resource, lastModified); if(LOGGER.isTraceEnabled()) { LOGGER.trace("Deleted "+resource); LOGGER.trace("Deleted "+endpoint); } } catch (final EndpointNotFoundException e) { throw new IllegalStateException(e); } }
@Override public void execute(ResourceRepository sut) { Resource result1 = sut.resourceOfId(resourceId); debug("Retrieving resource {%s}: %s",resourceId,result1); Container result2 = sut.containerOfId(containerId); debug("Retrieving container {%s}: %s",containerId,result2); Resource result3 = sut.resourceById(memberId,Resource.class); debug("Retrieving member resource {%s}: %s",memberId,result3); sut.remove(result2); debug("Deleted resource {%s}",result2.id()); Resource result4 = sut.resourceById(attachmentId,Resource.class); debug("Retrieving attached resource {%s}: %s",attachmentId,result4); Attachment attachment = result1.findAttachment(result4.id()); debug("Retrieving attachment {%s}: %s",result4.id(),attachment); result1.detach(attachment); debug("Detached resource {%s}",attachment.id()); sut.remove(result4); debug("Deleted resource {%s}",result4.id()); } }
@Override public void execute(ResourceRepository sut) { Resource result1 = sut.resourceOfId(resourceId); debug("Retrieving resource {%s}: %s",resourceId,result1); Resource attachment = result1.attach(PersonHandler.ADDRESS_ID,attachmentId); sut.add(attachment); debug("Created attachment: %s",attachment); Container result2 = sut.containerOfId(containerId); debug("Retrieving container {%s}: %s",containerId,result2); Slug slug1=result2.addSlug("test"); debug("Created slug: %s",slug1); Slug slug2=result2.addSlug("anotherTest"); debug("Created slug: %s",slug2); Resource member = result2.addMember(memberId); sut.add(member); debug("Created member: %s",member); } }
private void publish(RootResource rootResource, Date creationDate) throws ApplicationConfigurationException { ResourceId resourceId = rootResource.resourceId(); String path = rootResource.path(); Resource prevResource = this.resourceRepository.resourceById(resourceId,Resource.class); Endpoint prevEndpoint = this.endpointRepository.endpointOfPath(path); if(prevEndpoint!=null && !prevEndpoint.resourceId().equals(resourceId)) { throw new ApplicationConfigurationException(String.format("Resource %s cannot be published at '%s' as that path is already in use by a resource %s",toString(resourceId),path,toString(prevEndpoint.resourceId()))); } if(prevEndpoint==null) { if(prevResource!=null) { throw new ApplicationConfigurationException(String.format("Resource %s cannot be published at '%s' as it is already published at '%s'",toString(resourceId),path,this.endpointRepository.endpointOfResource(resourceId).path())); } else { Resource resource=this.modelFactory.createResource(rootResource.template(),rootResource.name()); this.resourceRepository.add(resource); Endpoint endpoint=this.modelFactory.createEndpoint(path,resource,creationDate,EntityTag.createStrong(path)); this.endpointRepository.add(endpoint); } } }
Resource resourceOfId(final ResourceId resourceId) { return this.resourceRepository.resourceById(resourceId,Resource.class); }
private Resource findResource(Name<?> name, Class<? extends ResourceHandler> handlerClass) { ResourceTemplate template = this.templateManagementService.templateOfHandler(handlerClass); ResourceId id = ResourceId.createId(name,template); return this.resourceRepository.resourceOfId(id); }
Resource createResource(Endpoint endpoint, DataSet dataSet, String desiredPath) throws ApplicationExecutionException { ResourceId resourceId=endpoint.resourceId(); Container resource = this.resourceRepository.containerOfId(resourceId); if(resource==null) { String errorMessage = applicationFailureMessage(COULD_NOT_FIND_CONTAINER_FOR_ENDPOINT,endpoint); LOGGER.error(errorMessage); throw new ApplicationExecutionException(errorMessage); } try { WriteSessionConfiguration config= DefaultApplicationContextHelper. create(this.engine().templateManagementService()). createConfiguration(resource,dataSet,desiredPath,lastModified()); return this.engine().resourceControllerService().createResource(resource,dataSet,config); } catch (FeatureExecutionException e) { processConstraintValidationFailure(resource, e); String errorMessage = applicationFailureMessage(RESOURCE_CREATION_FAILED,endpoint); throw createException(errorMessage,e); } catch (InvalidIndirectIdentifierException e) { // Just rethrow throw e; } catch (Exception e) { String errorMessage = applicationFailureMessage(RESOURCE_CREATION_FAILED,endpoint); throw createException(errorMessage,e); } }
private Resource loadResource(ResourceId resourceId) { return this.resourceRepository.resourceById(resourceId,Resource.class); }
@Override public void execute(ResourceRepository sut) { Resource result1 = sut.resourceOfId(resourceId); debug("Retrieving resource {%s}: %s",resourceId,result1); Container result2 = sut.containerOfId(containerId); debug("Retrieving container {%s}: %s",containerId,result2); Resource result3 = sut.resourceById(memberId,Resource.class); debug("Retrieving member resource {%s}: %s",memberId,result3); Resource result4 = sut.resourceById(attachmentId,Resource.class); debug("Retrieving attached resource {%s}: %s",attachmentId,result4); ConstraintReport report = result1.addConstraintReport(null,new Date(),httpRequest()); debug("Created report {%s}",report.id()); } }
private void createResource(final Resource resource, final Date lastModified, final String relativePath, final URI indirectId) { try { resource.setIndirectId(indirectId); this.resourceRepository.add(resource); final Endpoint newEndpoint= this.endpointManagementService. createEndpointForResource( resource, relativePath, generateEntityTag(resource), lastModified); if(LOGGER.isTraceEnabled()) { LOGGER.trace("Created "+resource); LOGGER.trace("Created "+newEndpoint); } } catch (final EndpointCreationException e) { throw new IllegalStateException(e); } }
private String calculateResourcePath(Resource resource, String desiredPath) throws EndpointNotFoundException { if(resource.isRoot()) { throw new IllegalStateException("Cannot get path for root resource"); } Resource parent=this.resourceRepository.resourceById(resource.parentId(),Resource.class); if(parent==null) { throw new IllegalStateException("Could not load resource '"+resource.parentId()+"' from the repository"); } String result= parent instanceof Container? generatePathForMember(resource,(Container)parent,desiredPath): null; if(result==null) { result = generatePathForAttachment(resource,parent); if(result==null) { throw new IllegalStateException("Could not determine path for resource '"+resource.id()+"' with parent '"+parent.id()+"'"); } } return result; }
@Test public void testPublishResource$preexisting$noClash() throws ApplicationConfigurationException { this.endpointRepository.add(this.endpoint); this.resourceRepository.add(this.resource); this.sut.publishResource(NAME1,PersonHandler.class,VALID_PATH); this.sut.configureRootResources(); Resource resource1 = verifyResource(NAME1, PersonHandler.class); assertThat(resource1,sameInstance(this.resource)); Endpoint endpoint1 = verifyEndpoint(VALID_PATH, resource1); assertThat(endpoint1,sameInstance(this.endpoint)); }
@Test public void testPublishResource$preexisting$idClash() throws ApplicationConfigurationException { this.endpointRepository.add(this.endpoint); this.resourceRepository.add(this.resource); this.sut.publishResource(NAME1,PersonHandler.class,"anotherPath/"); try { this.sut.configureRootResources(); fail("Should not allow publishing root resources with preexisting id clash"); } catch (ApplicationConfigurationException e) { logFailure(e); } }
@Test public void testPublishResource$preexisting$pathClash() throws ApplicationConfigurationException { this.endpointRepository.add(this.endpoint); this.resourceRepository.add(this.resource); this.sut.publishResource(NAME2,PersonHandler.class,VALID_PATH); try { this.sut.configureRootResources(); fail("Should not allow publishing root resources with preexisting path clash"); } catch (ApplicationConfigurationException e) { logFailure(e); } }
private <T extends Resource> T publishResource(Class<? extends T> clazz, String templateId, Name<?> resourceName, String path) { Transaction transaction = RuntimeDelegate.getInstance().getTransactionManager().currentTransaction(); transaction.begin(); Resource newResource=this.modelFactory.createResource(this.tms.templateOfId(templateId),resourceName); T resource=clazz.cast(newResource); RuntimeDelegate.getInstance().getResourceRepository().add(resource); Endpoint endpoint=this.modelFactory.createEndpoint(path,resource,new Date(),EntityTag.createStrong(path)); RuntimeDelegate.getInstance().getEndpointRepository().add(endpoint); transaction.commit(); return resource; }
@Test public void testPublishResource$preexisting$corrupted() throws ApplicationConfigurationException { Resource createResource = TestingModelFactory.createResource(NAME1, "UnknownTemplate"); this.endpointRepository.add(createEndpoint(VALID_PATH, createResource)); this.resourceRepository.add(createResource); this.sut.publishResource(NAME2,PersonHandler.class,VALID_PATH); try { this.sut.configureRootResources(); fail("Should not allow publishing root resources with preexisting path clash"); } catch (ApplicationConfigurationException e) { logFailure(e); } }
private Resource initialize(final String id, final String path) { final Transaction transaction= RuntimeDelegate. getInstance(). getTransactionManager(). currentTransaction(); transaction.begin(); try { this.uow = UnitOfWork.newCurrent(); final Resource resource= this.modelFactory.createResource( this.templateManagementService.templateOfId("personTemplate"), name(id)); final Endpoint endpoint=this.modelFactory.createEndpoint(path,resource,new Date(),EntityTag.createStrong(path)); RuntimeDelegate.getInstance().getResourceRepository().add(resource); RuntimeDelegate.getInstance().getEndpointRepository().add(endpoint); UnitOfWork.setCurrent(null); transaction.commit(); return resource; } finally { if(transaction.isActive()) { transaction.rollback(); } } }