Skip to content

Commit

Permalink
Full support of per-request overlays in updating operations #639
Browse files Browse the repository at this point in the history
* introducing RequestSchema for handling overlays within a single request
  • Loading branch information
andrus committed Jun 30, 2023
1 parent 183fa30 commit b0b5fd3
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.agrest.cayenne.processor.delete.stage;

import io.agrest.cayenne.persister.ICayennePersister;
import io.agrest.meta.AgSchema;
import io.agrest.processor.ProcessingContext;
import io.agrest.processor.ProcessorOutcome;
import io.agrest.runtime.processor.delete.DeleteContext;
Expand All @@ -26,8 +25,7 @@ public static ObjectContext cayenneContext(ProcessingContext<?> context) {

private final ICayennePersister persister;

public CayenneDeleteStartStage(@Inject AgSchema schema, @Inject ICayennePersister persister) {
super(schema);
public CayenneDeleteStartStage(@Inject ICayennePersister persister) {
this.persister = persister;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public <T> UnrelateBuilder<T> unrelate(Class<T> type) {
* @since 5.0
*/
public <T> DeleteBuilder<T> delete(Class<T> type) {
DeleteContext<T> context = new DeleteContext<>(type, injector);
DeleteContext<T> context = new DeleteContext<>(type, new RequestSchema(schema), injector);
return new DefaultDeleteBuilder<>(context, deleteProcessorFactory);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public DeleteBuilder<T> parent(Class<?> parentType, Map<String, Object> parentId

@Override
public DeleteBuilder<T> entityOverlay(AgEntityOverlay<T> overlay) {
context.addEntityOverlay(overlay);
context.getSchema().addOverlay(overlay);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.agrest.runtime.processor.delete;

import io.agrest.id.AgObjectId;
import io.agrest.runtime.EntityParent;
import io.agrest.meta.AgEntity;
import io.agrest.meta.AgEntityOverlay;
import io.agrest.processor.BaseProcessingContext;
import io.agrest.runtime.EntityParent;
import io.agrest.runtime.meta.RequestSchema;
import io.agrest.runtime.processor.update.ChangeOperation;
import org.apache.cayenne.di.Injector;

Expand All @@ -18,14 +18,15 @@
*/
public class DeleteContext<T> extends BaseProcessingContext<T> {

private final RequestSchema schema;
protected AgEntity<T> agEntity;
protected Collection<AgObjectId> ids;
protected EntityParent<?> parent;
protected AgEntityOverlay<T> entityOverlay;
private List<ChangeOperation<T>> deleteOperations;

public DeleteContext(Class<T> type, Injector injector) {
public DeleteContext(Class<T> type, RequestSchema schema, Injector injector) {
super(type, injector);
this.schema = schema;
this.deleteOperations = Collections.emptyList();
}

Expand Down Expand Up @@ -66,21 +67,6 @@ public AgEntity<T> getAgEntity() {
return agEntity;
}

/**
* @since 4.8
*/
public void addEntityOverlay(AgEntityOverlay<T> overlay) {
AgEntityOverlay<T> base = this.entityOverlay != null ? this.entityOverlay : new AgEntityOverlay<>(getType());
this.entityOverlay = base.merge(overlay);
}

/**
* @since 4.8
*/
public AgEntityOverlay<T> getEntityOverlay() {
return entityOverlay;
}

/**
* @since 4.8
*/
Expand All @@ -94,4 +80,11 @@ public List<ChangeOperation<T>> getDeleteOperations() {
public void setDeleteOperations(List<ChangeOperation<T>> deleteOperations) {
this.deleteOperations = deleteOperations;
}

/**
* @since 5.0
*/
public RequestSchema getSchema() {
return schema;
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
package io.agrest.runtime.processor.delete.stage;

import io.agrest.meta.AgSchema;
import io.agrest.meta.AgEntity;
import io.agrest.meta.AgEntityOverlay;
import io.agrest.processor.Processor;
import io.agrest.processor.ProcessorOutcome;
import io.agrest.runtime.processor.delete.DeleteContext;
import org.apache.cayenne.di.Inject;

/**
* @since 5.0
*/
public class DeleteStartStage implements Processor<DeleteContext<?>> {

private final AgSchema schema;

public DeleteStartStage(@Inject AgSchema schema) {
this.schema = schema;
}

@Override
public ProcessorOutcome execute(DeleteContext<?> context) {
initAgEntity(context);
return ProcessorOutcome.CONTINUE;
}

protected <T> void initAgEntity(DeleteContext<T> context) {
AgEntityOverlay<T> overlay = context.getEntityOverlay();
AgEntity<T> entity = schema.getEntity(context.getType());
context.setAgEntity(entity.resolveOverlay(schema, overlay));
AgEntity<T> entity = context.getSchema().getEntity(context.getType());
context.setAgEntity(entity);
}
}

0 comments on commit b0b5fd3

Please sign in to comment.