SingletonRelationshipProvider.java revision 030ed4eea8f9f66e62777badde9f088627a178dc
/*
* 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.
*/
/**
* A {@link RelationshipProvider} representing a singleton relationship for the given field.
*/
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 schemaField The schema of the field representing this relationship in the parent object.
* @param activityLogger The audit activity logger to use
* @param managedObjectSyncService Service to send sync events to
*/
public SingletonRelationshipProvider(final ConnectionFactory connectionFactory, final ResourcePath resourcePath,
Resources.newSingleton(this));
this.requestHandler = router;
}
/** {@inheritDoc} */
public RequestHandler asRequestHandler() {
return requestHandler;
}
/** {@inheritDoc} */
public Promise<JsonValue, ResourceException> getRelationshipValueForResource(final Context context, final String resourceId) {
EventEntry measure = Publisher.start(Name.get("openidm/internal/relationship/singleton/getRelationshipValueForResource"), resourceId, context);
try {
return queryRelationship(context, resourceId).thenAsync(new AsyncFunction<ResourceResponse, JsonValue,
ResourceException>() {
public Promise<JsonValue, ResourceException> apply(ResourceResponse value) throws ResourceException {
}
});
} finally {
}
}
/**
* 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 the promise of the query results.
*/
final String managedObjectId) {
try {
if (schemaField.isReverseRelationship()) {
and(
and(
} else {
}
if (relationships.isEmpty()) {
return new NotFoundException().asPromise();
} else {
// This is a singleton relationship with more than 1 reference - this is an error.
// Collect all the erroneous references and add them to the error message.
if (schemaField.isReverseRelationship() &&
} else {
}
}
"Multiple references found for singleton relationship " + errorReferences);
}
} catch (ResourceException e) {
return e.asPromise();
}
}
public Promise<JsonValue, ResourceException> setRelationshipValueForResource(final boolean clearExisting,
EventEntry measure = Publisher.start(Name.get("openidm/internal/relationship/singleton/setRelationshipValueForResource"), resourceId, context);
try {
try {
// Update if we got an id, otherwise replace
return resourceResponse.getContent();
}
});
} else { // no id, replace current instance
if (!clearExisting) {
}
return createInstance(context, createRequest).then(new Function<ResourceResponse, JsonValue, ResourceException>() {
return resourceResponse.getContent();
}
});
}
} catch (ResourceException e) {
return e.asPromise();
}
} else {
if (!clearExisting) {
}
}
} finally {
}
}
/** {@inheritDoc} */
public Promise<JsonValue, ResourceException> clear(final Context context, final String resourceId) {
EventEntry measure = Publisher.start(Name.get("openidm/internal/relationship/singleton/clear"), resourceId, context);
try {
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;
}
}
});
} finally {
}
}
/** {@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
*/
}
});
}
/**
* Implemented to simply call validateRelationship for the single field, if it has changed.
*
* @param context context of the original request.
* @param oldValue old value of field to validate
* @param newValue new value of field to validate
* @throws BadRequestException when the relationship isn't valid, ResourceException otherwise.
* @see RelationshipValidator#validateRelationship(JsonValue, Context)
*/
throws ResourceException {
}
}
}