@Override public void visitResource(Resource resource) { InMemoryResourceRepository.this.resources.put(resource.id(),resource); } @Override
/** * {@inheritDoc} */ @Override public void add(Resource resource) { checkNotNull(resource,"Resource cannot be null"); lock.writeLock().lock(); try { checkState(find(resource.id())==null,"A resource with identifier '%s' already exists",resource.id()); resource.accept( new ResourceVisitor() { @Override public void visitResource(Resource resource) { InMemoryResourceRepository.this.resources.put(resource.id(),resource); } @Override public void visitContainer(Container resource) { InMemoryResourceRepository.this.containers.put(resource.id(),resource); } } ); } finally { lock.writeLock().unlock(); } }
@Override public void visitResource(Resource resource) { for(DelegatedAttachmentSnapshot deleted:PersistentResourceState.this.deletedAttachments.values()) { Attachment attachment = resource.findAttachment(deleted.resource().resourceId()); resource.detach(attachment); } for(DelegatedAttachmentSnapshot attachment:PersistentResourceState.this.newAttachments.values()) { DelegatedResourceSnapshot attachedResource = attachment.resource(); Resource attach = resource.attach(attachment.id(), attachedResource.resourceId()); attachedResource.setDelegate(attach); } }
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; }
@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()); } }
@Test public void test() { Resource resource = TestHelper. createResource( TestHelper.name("name"), "template"); ConstraintReport report= resource. addConstraintReport( TestHelper.constraints(), new Date(), TestHelper.httpRequest()); ConstraintReportTransformer sut= ConstraintReportTransformer.create(resource,report); DataSet result = sut.transform( TestHelper. createEndpoint( "path", resource, new Date(), EntityTag.createStrong("mytag"))); assertThat(result,notNullValue()); assertThat(result.individualOfId(TestHelper.managedIndividualId(resource.id())),notNullValue()); assertThat(result.individualOfId(TestHelper.name("s0")),notNullValue()); assertThat(result.individualOfId(TestHelper.name("s1")),notNullValue()); }
private String generatePathForAttachment(Resource child, Resource parent) throws EndpointNotFoundException { Attachment attachment=parent.findAttachment(child.id()); if(attachment==null) { return null; } Endpoint endpoint=getResourceEndpoint(parent.id()); ResourceTemplate parentTemplate=this.templateManagementService.templateOfId(parent.id().templateId()); AttachedTemplate attachedTemplate = parentTemplate.attachedTemplate(attachment.id()); return PathBuilder. create(). addSegment(endpoint.path()). addSegment(attachedTemplate.path()). addSegment(attachment.version()>0?attachment.version():null). build(); }
static AttachmentSnapshotCollection createFromResource(Resource resource, DelegatedWriteSession session) { AttachmentSnapshotCollection repository=new AttachmentSnapshotCollection(); for(Attachment attachment:resource.attachments()) { DelegatedResourceSnapshot snapshot = session.resolveResource(attachment.resourceId()); checkState(snapshot!=null,"Could not resolve attached resource of attachment %s of resource %s",attachment,resource.id()); DelegatedAttachmentSnapshot newAttachment=new DelegatedAttachmentSnapshot(attachment.id(),snapshot); repository.safeAttach(newAttachment); } return repository; }
private void registerConstraintReport(Resource resource, InvalidContentException error) { ConstraintReport report= resource. addConstraintReport( error.getConstraints(), new Date(), currentRequest()); this.constraintReportRepository.add(report); LOGGER.debug("Constraint validation failed. Registered constraint report {}",report.id()); error.setConstraintsId(report.id().failureId()); }
@Override void saveChanges(final DelegatedResourceSnapshot ctx) { delegate(ctx).accept(new ResourceSaver()); this.deletedAttachments.clear(); this.newAttachments.clear(); this.deletedMembers.clear(); this.newMembers.clear(); }
@Override public void visitResource(Resource resource) { for(DelegatedAttachmentSnapshot attachment:TransientResourceState.this.attachments(ctx)) { DelegatedResourceSnapshot attachedResource = attachment.resource(); Resource attach = resource.attach(attachment.id(), attachedResource.resourceId()); attachedResource.setDelegate(attach); } }
@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()); } }
static MemberCollection createFromResource(Resource resource, final DelegatedWriteSession session) { final MemberCollection memberRepository = new MemberCollection(); resource.accept( new ResourceVisitor() { @Override public void visitResource(Resource resource) { // Nothing to do } @Override public void visitContainer(Container resource) { for(Member member:resource.members()) { DelegatedResourceSnapshot memberResourceSnapshot = session.resolveResource(member.memberId()); checkState(memberResourceSnapshot!=null,"Could not resolve member %s of container %s",member,resource); memberRepository.registerMember(memberResourceSnapshot); } } } ); return memberRepository; }
@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); } }
@Override public String toString() { return MoreObjects. toStringHelper(getClass()). add("child.id()", this.child.id()). toString(); }
@Override void saveChanges(final DelegatedResourceSnapshot ctx) { delegate(ctx).accept(new ResourceSaver(ctx)); }
DelegatedResourceSnapshot newPersistent(Resource resource, final ResourceTemplate template) { final AtomicReference<DelegatedResourceSnapshot> result=new AtomicReference<DelegatedResourceSnapshot>(); resource.accept( new ResourceVisitor() { @Override public void visitResource(Resource resource) { DelegatedResourceSnapshot snapshot=new DelegatedResourceSnapshot(resource.id(),template.handlerClass()); snapshot.setParentState(ParentState.parentOf(resource)); snapshot.setPersistencyState(PersistencyState.newPersistentReferenceState(resource.id(),template)); result.set(snapshot); } @Override public void visitContainer(Container resource) { DelegatedContainerSnapshot snapshot=new DelegatedContainerSnapshot(resource.id(),handlerClass(template,ContainerHandler.class)); snapshot.setParentState(ParentState.parentOf(resource)); snapshot.setPersistencyState(PersistencyState.newPersistentReferenceState(resource.id(),template)); result.set(snapshot); } } ); return result.get(); }
private PersistentResourceState(Resource delegate, ResourceTemplate template, AttachmentSnapshotCollection attachmentRepository, MemberCollection memberRepository) { super(delegate.id(),template,attachmentRepository,memberRepository); setDelegate(delegate); this.newAttachments=new LinkedHashMap<String,DelegatedAttachmentSnapshot>(); this.deletedAttachments=new LinkedHashMap<String,DelegatedAttachmentSnapshot>(); this.newMembers=new LinkedHashMap<ResourceId, DelegatedResourceSnapshot>(); this.deletedMembers=new LinkedHashMap<ResourceId, DelegatedResourceSnapshot>(); }
@Override public Endpoint createEndpoint(String path, Resource resource, Date created, EntityTag entityTag) { checkNotNull(resource,"Resource cannot be null"); return InMemoryEndpoint.create(path, resource.id(), created, entityTag); }