use of com.yahoo.elide.core.exceptions.InternalServerErrorException in project elide by yahoo.
the class PersistentResource method addInverseRelation.
/**
* If a bidirectional relationship exists, attempts to add itself to the inverse
* relationship. Given A to B as the relationship, A corresponds to this and B is the inverse.
*
* @param relationName The name of the relationship on this (A) object.
* @param inverseObj The value (B) which has been added to this object.
*/
protected void addInverseRelation(String relationName, Object inverseObj) {
String inverseName = dictionary.getRelationInverse(type, relationName);
if (!"".equals(inverseName)) {
Type<?> inverseType = dictionary.getType(inverseObj, inverseName);
String uuid = requestScope.getUUIDFor(inverseObj);
PersistentResource inverseResource = new PersistentResource(inverseObj, this, relationName, uuid, requestScope);
Object inverseRelation = inverseResource.getValueUnchecked(inverseName);
if (COLLECTION_TYPE.isAssignableFrom(inverseType)) {
if (inverseRelation != null) {
inverseResource.modifyCollection((Collection) inverseRelation, inverseName, Set.of(this.getObject()), Collections.emptySet(), false);
} else {
inverseResource.setValueChecked(inverseName, Collections.singleton(this.getObject()));
}
} else if (inverseType.isAssignableFrom(this.getResourceType())) {
inverseResource.setValueChecked(inverseName, this.getObject());
} else {
throw new InternalServerErrorException("Relationship type mismatch");
}
inverseResource.markDirty();
RelationshipType inverseRelationType = inverseResource.getRelationshipType(inverseName);
if (inverseRelationType.isToOne()) {
// hook for updateToOneRelation
transaction.updateToOneRelation(transaction, inverseObj, inverseName, obj, requestScope);
} else {
// hook for updateToManyRelation
assert (inverseRelation == null || inverseRelation instanceof Collection) : inverseName + " not a collection";
transaction.updateToManyRelation(transaction, inverseObj, inverseName, Sets.newHashSet(obj), new LinkedHashSet<>(), requestScope);
}
}
}
use of com.yahoo.elide.core.exceptions.InternalServerErrorException in project elide by yahoo.
the class CollectionTerminalState method createObject.
private PersistentResource createObject(RequestScope requestScope) throws ForbiddenAccessException, InvalidObjectIdentifierException {
JsonApiDocument doc = requestScope.getJsonApiDocument();
JsonApiMapper mapper = requestScope.getMapper();
if (doc.getData() == null) {
throw new InvalidEntityBodyException("Invalid JSON-API document: " + doc);
}
Data<Resource> data = doc.getData();
Collection<Resource> resources = data.get();
Resource resource = (resources.size() == 1) ? IterableUtils.first(resources) : null;
if (resource == null) {
try {
throw new InvalidEntityBodyException(mapper.writeJsonApiDocument(doc));
} catch (JsonProcessingException e) {
throw new InternalServerErrorException(e);
}
}
String id = resource.getId();
Type<?> newObjectClass = requestScope.getDictionary().getEntityClass(resource.getType(), requestScope.getApiVersion());
if (newObjectClass == null) {
throw new UnknownEntityException("Entity " + resource.getType() + " not found");
}
if (!entityClass.isAssignableFrom(newObjectClass)) {
throw new InvalidValueException("Cannot assign value of type: " + resource.getType() + " to type: " + entityClass);
}
PersistentResource pResource = PersistentResource.createObject(parent.orElse(null), relationName.orElse(null), newObjectClass, requestScope, Optional.ofNullable(id));
Map<String, Object> attributes = resource.getAttributes();
if (attributes != null) {
for (Map.Entry<String, Object> entry : attributes.entrySet()) {
String fieldName = entry.getKey();
Object val = entry.getValue();
pResource.updateAttribute(fieldName, val);
}
}
Map<String, Relationship> relationships = resource.getRelationships();
if (relationships != null) {
for (Map.Entry<String, Relationship> entry : relationships.entrySet()) {
String fieldName = entry.getKey();
Relationship relationship = entry.getValue();
Set<PersistentResource> resourceSet = (relationship == null) ? null : relationship.toPersistentResources(requestScope);
pResource.updateRelation(fieldName, resourceSet);
}
}
return pResource;
}
use of com.yahoo.elide.core.exceptions.InternalServerErrorException in project elide by yahoo.
the class PersistentResource method deleteInverseRelation.
/**
* If a bidirectional relationship exists, attempts to delete itself from the inverse
* relationship. Given A to B as the relationship, A corresponds to this and B is the inverse.
*
* @param relationName The name of the relationship on this (A) object.
* @param inverseEntity The value (B) which has been deleted from this object.
*/
protected void deleteInverseRelation(String relationName, Object inverseEntity) {
String inverseField = getInverseRelationField(relationName);
if (!"".equals(inverseField)) {
Type<?> inverseType = dictionary.getType(inverseEntity, inverseField);
String uuid = requestScope.getUUIDFor(inverseEntity);
PersistentResource inverseResource = new PersistentResource(inverseEntity, this, relationName, uuid, requestScope);
Object inverseRelation = inverseResource.getValueUnchecked(inverseField);
if (inverseRelation == null) {
return;
}
if (inverseRelation instanceof Collection) {
inverseResource.modifyCollection((Collection) inverseRelation, inverseField, Collections.emptySet(), Set.of(this.getObject()), false);
} else if (inverseType.isAssignableFrom(this.getResourceType())) {
inverseResource.nullValue(inverseField, this);
} else {
throw new InternalServerErrorException("Relationship type mismatch");
}
inverseResource.markDirty();
RelationshipType inverseRelationType = inverseResource.getRelationshipType(inverseField);
if (inverseRelationType.isToOne()) {
// hook for updateToOneRelation
transaction.updateToOneRelation(transaction, inverseEntity, inverseField, null, requestScope);
} else {
// hook for updateToManyRelation
assert (inverseRelation instanceof Collection) : inverseField + " not a collection";
transaction.updateToManyRelation(transaction, inverseEntity, inverseField, new LinkedHashSet<>(), Sets.newHashSet(obj), requestScope);
}
}
}
Aggregations