Skip to content

Commit

Permalink
Merge branch 'agrestio:master' into agrestio#585
Browse files Browse the repository at this point in the history
  • Loading branch information
m-dzianishchyts authored Nov 2, 2022
2 parents 77d4301 + 75a221d commit 500d065
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public class CayenneProcessor {
*/
public static CayenneResourceEntityExt getEntity(ResourceEntity<?> entity) {
String key = (entity instanceof RootResourceEntity) ? CAYENNE_ROOT_ENTITY_KEY : CAYENNE_RELATED_ENTITY_KEY;
return entity.getRequestProperty(key);
return entity.getProperty(key);
}

/**
* @since 5.0
*/
public static <T> CayenneRootResourceEntityExt<T> getRootEntity(RootResourceEntity<T> entity) {
return entity.getRequestProperty(CAYENNE_ROOT_ENTITY_KEY);
return entity.getProperty(CAYENNE_ROOT_ENTITY_KEY);
}

/**
Expand All @@ -38,11 +38,11 @@ public static <T> CayenneRootResourceEntityExt<T> getOrCreateRootEntity(RootReso
}

CayenneRootResourceEntityExt<T> newExt = new CayenneRootResourceEntityExt<>();
entity.setRequestProperty(CAYENNE_ROOT_ENTITY_KEY, newExt);
entity.setProperty(CAYENNE_ROOT_ENTITY_KEY, newExt);

// copy MapBy owner's query to MapBy to ensure its own resolvers work properly
if (entity.getMapBy() != null) {
entity.getMapBy().setRequestProperty(CAYENNE_ROOT_ENTITY_KEY, newExt);
entity.getMapBy().setProperty(CAYENNE_ROOT_ENTITY_KEY, newExt);
}

return newExt;
Expand All @@ -52,7 +52,7 @@ public static <T> CayenneRootResourceEntityExt<T> getOrCreateRootEntity(RootReso
* @since 5.0
*/
public static <T> CayenneRelatedResourceEntityExt getRelatedEntity(RelatedResourceEntity<T> entity) {
return entity.getRequestProperty(CAYENNE_RELATED_ENTITY_KEY);
return entity.getProperty(CAYENNE_RELATED_ENTITY_KEY);
}

/**
Expand All @@ -66,11 +66,11 @@ public static <T> CayenneRelatedResourceEntityExt getOrCreateRelatedEntity(Relat
}

CayenneRelatedResourceEntityExt newExt = new CayenneRelatedResourceEntityExt();
entity.setRequestProperty(CAYENNE_RELATED_ENTITY_KEY, newExt);
entity.setProperty(CAYENNE_RELATED_ENTITY_KEY, newExt);

// copy MapBy owner's query to MapBy to ensure its own resolvers work properly
if (entity.getMapBy() != null) {
entity.getMapBy().setRequestProperty(CAYENNE_RELATED_ENTITY_KEY, newExt);
entity.getMapBy().setProperty(CAYENNE_RELATED_ENTITY_KEY, newExt);
}

return newExt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class CayenneDeleteStartStage extends DeleteStartStage {
* by this stage.
*/
public static ObjectContext cayenneContext(ProcessingContext<?> context) {
return (ObjectContext) context.getAttribute(CayenneDeleteStartStage.DELETE_OBJECT_CONTEXT_ATTRIBUTE);
return (ObjectContext) context.getProperty(CayenneDeleteStartStage.DELETE_OBJECT_CONTEXT_ATTRIBUTE);
}

private final ICayennePersister persister;
Expand All @@ -42,7 +42,7 @@ public ProcessorOutcome execute(DeleteContext<?> context) {
}

protected <T> void initCayenneContext(DeleteContext<T> context) {
context.setAttribute(DELETE_OBJECT_CONTEXT_ATTRIBUTE, persister.newContext());
context.setProperty(DELETE_OBJECT_CONTEXT_ATTRIBUTE, persister.newContext());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class CayenneUnrelateStartStage extends UnrelateStartStage {
* by this stage.
*/
public static ObjectContext cayenneContext(ProcessingContext<?> context) {
return (ObjectContext) context.getAttribute(CayenneUnrelateStartStage.UNRELATE_OBJECT_CONTEXT_ATTRIBUTE);
return (ObjectContext) context.getProperty(CayenneUnrelateStartStage.UNRELATE_OBJECT_CONTEXT_ATTRIBUTE);
}

private final ICayennePersister persister;
Expand All @@ -31,7 +31,7 @@ public CayenneUnrelateStartStage(@Inject ICayennePersister persister) {

@Override
public ProcessorOutcome execute(UnrelateContext<?> context) {
context.setAttribute(UNRELATE_OBJECT_CONTEXT_ATTRIBUTE, persister.newContext());
context.setProperty(UNRELATE_OBJECT_CONTEXT_ATTRIBUTE, persister.newContext());
return ProcessorOutcome.CONTINUE;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class CayenneUpdateStartStage extends UpdateStartStage {
* by this stage.
*/
public static ObjectContext cayenneContext(ProcessingContext<?> context) {
return (ObjectContext) context.getAttribute(CayenneUpdateStartStage.UPDATE_OBJECT_CONTEXT_ATTRIBUTE);
return (ObjectContext) context.getProperty(CayenneUpdateStartStage.UPDATE_OBJECT_CONTEXT_ATTRIBUTE);
}

private ICayennePersister persister;
Expand All @@ -31,7 +31,7 @@ public CayenneUpdateStartStage(@Inject ICayennePersister persister) {

@Override
public ProcessorOutcome execute(UpdateContext<?> context) {
context.setAttribute(UPDATE_OBJECT_CONTEXT_ATTRIBUTE, persister.newContext());
context.setProperty(UPDATE_OBJECT_CONTEXT_ATTRIBUTE, persister.newContext());
return ProcessorOutcome.CONTINUE;
}
}
73 changes: 46 additions & 27 deletions agrest-engine/src/main/java/io/agrest/ResourceEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ public abstract class ResourceEntity<T> {
private final Map<String, AgAttribute> attributes;
private final Set<String> defaultAttributes;
private final Map<String, RelatedResourceEntity<?>> children;
private final Map<String, Object> requestProperties;
private final Map<String, Object> properties;

private String mapByPath;
private ResourceEntity<?> mapBy;
private final List<Sort> orderings;
private Exp exp;
Expand All @@ -45,7 +44,7 @@ public ResourceEntity(AgEntity<T> agEntity) {
this.defaultAttributes = new HashSet<>();
this.children = new HashMap<>();
this.orderings = new ArrayList<>(2);
this.requestProperties = new HashMap<>(5);
this.properties = new HashMap<>(5);
}

/**
Expand All @@ -70,9 +69,9 @@ public Exp getExp() {
}

/**
* @deprecated since 5.0 in favor of {@link #getExp()}
* @deprecated in favor of {@link #getExp()}
*/
@Deprecated
@Deprecated(since = "5.0")
public Exp getQualifier() {
return getExp();
}
Expand All @@ -85,9 +84,9 @@ public void andExp(Exp exp) {
}

/**
* @deprecated since 5.0 in favor of {@link #andExp(Exp)}
* @deprecated in favor of {@link #andExp(Exp)}
*/
@Deprecated
@Deprecated(since = "5.0")
public void andQualifier(Exp qualifier) {
andExp(qualifier);
}
Expand Down Expand Up @@ -185,16 +184,20 @@ public ResourceEntity<?> getMapBy() {
}

/**
* @deprecated in favor of {@link #mapBy(ResourceEntity)}. mapByPath parameter is ignored.
* @since 1.1
*/
@Deprecated(since = "5.0")
public ResourceEntity<T> mapBy(ResourceEntity<?> mapBy, String mapByPath) {
this.mapByPath = mapByPath;
this.mapBy = mapBy;
return this;
return mapBy(mapBy);
}

public String getMapByPath() {
return mapByPath;
/**
* @since 5.0
*/
public ResourceEntity<T> mapBy(ResourceEntity<?> mapBy) {
this.mapBy = mapBy;
return this;
}

public Class<T> getType() {
Expand All @@ -209,9 +212,9 @@ public int getStart() {
}

/**
* @deprecated since 5.0 in favor of {@link #getStart()}
* @deprecated in favor of {@link #getStart()}
*/
@Deprecated
@Deprecated(since = "5.0")
public int getFetchOffset() {
return getStart();
}
Expand All @@ -224,9 +227,9 @@ public void setStart(int start) {
}

/**
* @deprecated since 5.0 in favor of {@link #setStart(int)}
* @deprecated in favor of {@link #setStart(int)}
*/
@Deprecated
@Deprecated(since = "5.0")
public void setFetchOffset(int fetchOffset) {
setStart(fetchOffset);
}
Expand All @@ -239,9 +242,9 @@ public int getLimit() {
}

/**
* @deprecated since 5.0 in favor of {@link #getLimit()}
* @deprecated in favor of {@link #getLimit()}
*/
@Deprecated
@Deprecated(since = "5.0")
public int getFetchLimit() {
return getLimit();
}
Expand All @@ -254,9 +257,9 @@ public void setLimit(int limit) {
}

/**
* @deprecated since 5.0 in favor of {@link #setLimit(int)}
* @deprecated in favor of {@link #setLimit(int)}
*/
@Deprecated
@Deprecated(since = "5.0")
public void setFetchLimit(int fetchLimit) {
setLimit(fetchLimit);
}
Expand All @@ -269,23 +272,39 @@ public boolean isFiltered() {
}

/**
* Returns a previously stored object for a given name. Request properties mechanism allows pluggable processing
* Returns a previously stored custom object for a given name. The properties mechanism allows pluggable processing
* pipelines to store and exchange data within a given request.
*
* @since 3.7
* @since 5.0
*/
public <P> P getRequestProperty(String name) {
return (P) requestProperties.get(name);
public <P> P getProperty(String name) {
return (P) properties.get(name);
}

/**
* Sets a property value for a given name. Request properties mechanism allows pluggable processing pipelines to
* Sets a property value for a given name. The properties mechanism allows pluggable processing pipelines to
* store and exchange data within a given request.
*
* @since 3.7
* @since 5.0
*/
public void setProperty(String name, Object value) {
properties.put(name, value);
}

/**
* @deprecated in favor of {@link #getProperty(String)}
*/
@Deprecated(since = "5.0")
public <P> P getRequestProperty(String name) {
return getProperty(name);
}

/**
* @deprecated in favor of {@link #setProperty(String, Object)}
*/
@Deprecated(since = "5.0")
public void setRequestProperty(String name, Object value) {
requestProperties.put(name, value);
setProperty(name, value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,17 @@

public class MapByEncoder implements Encoder {

private final String mapByPath;
private final List<DataReader> mapByReaders;
private final Encoder collectionEncoder;
private final boolean byId;
private final ValueStringConverter fieldNameConverter;

public MapByEncoder(
String mapByPath,
List<DataReader> mapByReaders,
Encoder collectionEncoder,
boolean byId,
ValueStringConverter fieldNameConverter) {

this.mapByPath = mapByPath;
this.mapByReaders = mapByReaders;
this.collectionEncoder = collectionEncoder;
this.byId = byId;
Expand Down Expand Up @@ -88,10 +85,9 @@ private Map<String, List<Object>> mapBy(List<?> objects) {
}

// disallow nulls as JSON keys...
// note that converter below will throw an NPE if we pass NULL
// further down... the error here has more context.
// note that converter below will throw an NPE if we pass "null" further down. The error here has more context.
if (key == null) {
throw AgException.internalServerError("Null mapBy value for key '%s'", mapByPath);
throw AgException.internalServerError("Null mapBy value for object '%s'", o);
}

String keyString = fieldNameConverter.asString(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public BaseProcessingContext(Class<T> type, Injector injector) {
* @since 5.0
*/
@Override
public <T> T service(Class<T> type) {
public <S> S service(Class<S> type) {
return injector.getInstance(type);
}

Expand All @@ -38,7 +38,7 @@ public <T> T service(Class<T> type) {
* @since 5.0
*/
@Override
public <T> T service(Key<T> key) {
public <S> S service(Key<S> key) {
return injector.getInstance(key);
}

Expand All @@ -58,12 +58,12 @@ public Class<T> getType() {
}

@Override
public Object getAttribute(String name) {
public Object getProperty(String name) {
return attributes != null ? attributes.get(name) : null;
}

@Override
public void setAttribute(String name, Object value) {
public void setProperty(String name, Object value) {

// Presumably BaseProcessingContext is single-threaded (one per request), so lazy init and using HashMap is ok.
if (attributes == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,46 @@ public interface ProcessingContext<T> {
Class<T> getType();

/**
* Returns a previously stored context attribute or null if none was set for
* a given key.
* Returns a previously stored context attribute or null if none was set for a given key.
*
* @since 5.0
*/
Object getAttribute(String name);
Object getProperty(String name);

/**
* Allows to store an arbitrary attribute in the context during processing.
*
* @since 5.0
*/
void setProperty(String name, Object value);

/**
* @deprecated in favor of {@link #getProperty(String)}
*/
@Deprecated(since = "5.0")
default Object getAttribute(String name) {
return getProperty(name);
}

/**
* @deprecated in favor of {@link #setProperty(String, Object)}
*/
void setAttribute(String name, Object value);
@Deprecated(since = "5.0")
default void setAttribute(String name, Object value) {
setProperty(name, value);
}

/**
* Provides access to a desired service from the Agrest stack that created the current context.
*
* @since 5.0
*/
<T> T service(Class<T> type);
<S> S service(Class<S> type);

/**
* Provides access to a desired service from the Agrest stack that created the current context.
*
* @since 5.0
*/
<T> T service(Key<T> key);
<S> S service(Key<S> key);
}
Loading

0 comments on commit 500d065

Please sign in to comment.