SingletonRelationshipProvider.java revision 1900d91c2967c010e7139eeb6a556273f01d3198
/*
* The contents of this file are subject to the terms of the Common Development and
* Distribution License (the License). You may not use this file except in compliance with the
* License.
*
* You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
* specific language governing permission and limitations under the License.
*
* When distributing Covered Software, include this CDDL Header Notice in each file and include
* the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
* Header, with the fields enclosed by brackets [] replaced by your own identifying
* information: "Portions copyright [year] [name of copyright owner]".
*
* Copyright 2015 ForgeRock AS.
*/
class SingletonRelationshipProvider extends RelationshipProvider implements SingletonResourceProvider {
private final RequestHandler requestHandler;
/**
* Create a new relationship set for the given managed resource
*
* @param connectionFactory Connection factory used to access the repository
* @param propertyName Name of property on first object represents the relationship
* @param uriPropertyName Property name used on the URI for nested routes
* @param isReverse If this provider represents a reverse relationship
* @param activityLogger The audit activity logger to use
* @param managedObjectSyncService Service to send sync events to
*/
public SingletonRelationshipProvider(final ConnectionFactory connectionFactory, final ResourcePath resourcePath,
final JsonPointer propertyName, final String uriPropertyName, final boolean isReverse, final ActivityLogger activityLogger,
super(connectionFactory, resourcePath, propertyName, isReverse, activityLogger, managedObjectSyncService);
Resources.newSingleton(this));
this.requestHandler = router;
}
/** {@inheritDoc} */
public RequestHandler asRequestHandler() {
return requestHandler;
}
/** {@inheritDoc} */
public Promise<JsonValue, ResourceException> getRelationshipValueForResource(final Context context, final String resourceId) {
return queryRelationship(context, resourceId).thenAsync(new AsyncFunction<ResourceResponse, JsonValue,
ResourceException>() {
public Promise<JsonValue, ResourceException> apply(ResourceResponse value) throws ResourceException {
}
});
}
/**
* Queries relationships, returning the relationship associated with this providers resource path and the specified
* relationship field.
*
* @param context The current context
* @param managedObjectId The id of the managed object to find relationships associated with
* @return
*/
private Promise<ResourceResponse, ResourceException> queryRelationship(final Context context, final String managedObjectId) {
try {
QueryFilter.equalTo(new JsonPointer(isReverse ? REPO_FIELD_SECOND_ID : REPO_FIELD_FIRST_ID), resourcePath.child(managedObjectId)),
));
if (relationships.isEmpty()) {
return new NotFoundException().asPromise();
} else {
// TODO OPENIDM-4094 - check size and throw illegal state if more than one?
}
} catch (ResourceException e) {
return e.asPromise();
}
}
/**
* Persist the supplied {@link JsonValue} {@code value} as the new state of this singleton relationship on
* {@code resourceId}.
*
* <em>This is currently the only means of creating an instance of this singleton</em>
*
* @param context The context of this request
* @param resourceId Id of the resource relation fields in value are to be memebers of
* @param value A {@link JsonValue} map of relationship fields and their values
*
* @return The persisted instance of {@code value}
*/
public Promise<JsonValue, ResourceException> setRelationshipValueForResource(Context context, String resourceId,
try {
// Update if we got an id, otherwise replace
return resourceResponse.getContent();
}
});
} else { // no id, replace current instance
return createInstance(context, createRequest).then(new Function<ResourceResponse, JsonValue, ResourceException>() {
return resourceResponse.getContent();
}
});
}
} catch (ResourceException e) {
return e.asPromise();
}
} else {
}
}
/** {@inheritDoc} */
public Promise<JsonValue, ResourceException> clear(final Context context, final String resourceId) {
return getRelationshipValueForResource(context, resourceId).then(new Function<JsonValue, JsonValue, ResourceException>() {
}
// Since we wish to clear here NotFound is not an error. Return empty json
if (e instanceof NotFoundException) {
} else {
throw e;
}
}
});
}
/** {@inheritDoc} */
final ReadRequest request) {
return getRelationshipId(context).thenAsync(new AsyncFunction<String, ResourceResponse, ResourceException>() {
public Promise<ResourceResponse, ResourceException> apply(String relationshipId) throws ResourceException {
}
});
}
/** {@inheritDoc} */
final UpdateRequest request) {
return getRelationshipId(context).thenAsync(new AsyncFunction<String, ResourceResponse, ResourceException>() {
public Promise<ResourceResponse, ResourceException> apply(String relationshipId) throws ResourceException {
}
});
}
/** {@inheritDoc} */
final PatchRequest request) {
return getRelationshipId(context).thenAsync(new AsyncFunction<String, ResourceResponse, ResourceException>() {
public Promise<ResourceResponse, ResourceException> apply(String relationshipId) throws ResourceException {
}
});
}
/** {@inheritDoc} */
final ActionRequest request) {
return getRelationshipId(context).thenAsync(new AsyncFunction<String, ActionResponse, ResourceException>() {
public Promise<ActionResponse, ResourceException> apply(String relationshipId) throws ResourceException {
}
});
}
/**
* Return the id of the relationship that this singleton represents. The {@link Context} is used to find the id of
* the managed object for this request.
*
* @param context The current context
* @return The id of the current relationship this singleton represents
*/
}
});
}
}