@Test(expected = IllegalArgumentException.class) public void cannotRemoveNull() { association.removeElement(mockOwnerAdapter, null); }
private void updateOneToManyAssociation(final ObjectAdapter objectAdapter, final OneToManyAssociation otma, final CollectionData collectionData) { final ObjectAdapter collection = otma.get(objectAdapter, InteractionInitiatedBy.FRAMEWORK); final CollectionFacet facet = CollectionFacetUtils.getCollectionFacetFromSpec(collection); final List<ObjectAdapter> original = Lists.newArrayList(); for (final ObjectAdapter adapter : facet.iterable(collection)) { original.add(adapter); } final Data[] elements = collectionData.elements; for (final Data data : elements) { final ObjectAdapter elementAdapter = recreateReference(data); if (!facet.contains(collection, elementAdapter)) { if (LOG.isDebugEnabled()) { LOG.debug(" association {} changed, added {}", otma, elementAdapter.getOid()); } otma.addElement(objectAdapter, elementAdapter, InteractionInitiatedBy.FRAMEWORK); } else { otma.removeElement(objectAdapter, elementAdapter, InteractionInitiatedBy.FRAMEWORK); } } for (final ObjectAdapter element : original) { if (LOG.isDebugEnabled()) { LOG.debug(" association {} changed, removed {}", otma, element.getOid()); } otma.removeElement(objectAdapter, element, InteractionInitiatedBy.FRAMEWORK); } }
private void updateOneToManyAssociation(final ObjectAdapter object, final OneToManyAssociation field, final CollectionData collectionData) { final ObjectAdapter collection = field.get(object); final CollectionFacet facet = CollectionFacetUtils.getCollectionFacetFromSpec(collection); final List<ObjectAdapter> original = Lists.newArrayList(); for (final ObjectAdapter adapter : facet.iterable(collection)) { original.add(adapter); } final Data[] elements = collectionData.elements; for (final Data data : elements) { final ObjectAdapter element = recreateReference(data); if (!facet.contains(collection, element)) { if (LOG.isDebugEnabled()) { LOG.debug(" association " + field + " changed, added " + element.getOid()); } field.addElement(object, element); } else { field.removeElement(object, element); } } for (final ObjectAdapter element : original) { if (LOG.isDebugEnabled()) { LOG.debug(" association " + field + " changed, removed " + element.getOid()); } field.removeElement(object, element); } }
@Override @DELETE @Path("/{domainType}/{instanceId}/collections/{collectionId}") @Produces({ MediaType.APPLICATION_JSON, RestfulMediaType.APPLICATION_JSON_ERROR }) public Response removeFromCollection(@PathParam("domainType") String domainType, @PathParam("instanceId") final String instanceId, @PathParam("collectionId") final String collectionId) { init(Where.PARENTED_TABLES); final ObjectAdapter objectAdapter = getObjectAdapterElseThrowNotFound(domainType, instanceId); final DomainResourceHelper helper = new DomainResourceHelper(getResourceContext(), objectAdapter); final OneToManyAssociation collection = helper.getCollectionThatIsVisibleForIntent(collectionId, Intent.MUTATE, getResourceContext().getWhere()); final ObjectSpecification collectionSpec = collection.getSpecification(); final ObjectAdapter argAdapter = helper.parseAsMapWithSingleValue(collectionSpec, getResourceContext().getQueryString()); final Consent consent = collection.isValidToRemove(objectAdapter, argAdapter); if (consent.isVetoed()) { throw RestfulObjectsApplicationException.createWithMessage(HttpStatusCode.UNAUTHORIZED, consent.getReason()); } collection.removeElement(objectAdapter, argAdapter); return helper.collectionDetails(collectionId, MemberMode.MUTATING, Caching.NONE, getResourceContext().getWhere()); }
@Override @DELETE @Path("/{domainType}/{instanceId}/collections/{collectionId}") @Consumes({ MediaType.WILDCARD }) @Produces({ MediaType.APPLICATION_JSON, RestfulMediaType.APPLICATION_JSON_OBJECT_COLLECTION, RestfulMediaType.APPLICATION_JSON_ERROR, MediaType.APPLICATION_XML, RestfulMediaType.APPLICATION_XML_OBJECT_COLLECTION, RestfulMediaType.APPLICATION_XML_ERROR }) public Response removeFromCollection(@PathParam("domainType") String domainType, @PathParam("instanceId") final String instanceId, @PathParam("collectionId") final String collectionId) { init(Where.PARENTED_TABLES, RepresentationService.Intent.NOT_APPLICABLE); final ObjectAdapter objectAdapter = getObjectAdapterElseThrowNotFound(domainType, instanceId); final DomainResourceHelper helper = newDomainResourceHelper(objectAdapter); final ObjectAdapterAccessHelper accessHelper = new ObjectAdapterAccessHelper(getResourceContext(), objectAdapter); final OneToManyAssociation collection = accessHelper.getCollectionThatIsVisibleForIntent( collectionId, ObjectAdapterAccessHelper.Intent.MUTATE); final ObjectSpecification collectionSpec = collection.getSpecification(); final ObjectAdapter argAdapter = new JsonParserHelper(getResourceContext(), collectionSpec).parseAsMapWithSingleValue( getResourceContext().getUrlUnencodedQueryString()); final Consent consent = collection.isValidToRemove(objectAdapter, argAdapter, InteractionInitiatedBy.USER); if (consent.isVetoed()) { throw RestfulObjectsApplicationException.createWithMessage(HttpStatusCode.UNAUTHORIZED, consent.getReason()); } collection.removeElement(objectAdapter, argAdapter, InteractionInitiatedBy.USER); return helper.collectionDetails(collectionId, MemberReprMode.WRITE); }
@Override @DELETE @Path("/{oid}/collections/{collectionId}") @Produces({ MediaType.APPLICATION_JSON, RestfulMediaType.APPLICATION_JSON_ERROR }) public Response removeFromCollection(@PathParam("oid") final String oidStr, @PathParam("collectionId") final String collectionId) { init(Where.PARENTED_TABLES); final ObjectAdapter objectAdapter = getObjectAdapter(oidStr); final DomainResourceHelper helper = new DomainResourceHelper(getResourceContext(), objectAdapter); final OneToManyAssociation collection = helper.getCollectionThatIsVisibleAndUsable(collectionId, Intent.MUTATE, getResourceContext().getWhere()); final ObjectSpecification collectionSpec = collection.getSpecification(); final ObjectAdapter argAdapter = helper.parseAsMapWithSingleValue(collectionSpec, getResourceContext().getQueryString()); final Consent consent = collection.isValidToRemove(objectAdapter, argAdapter); if (consent.isVetoed()) { throw RestfulObjectsApplicationException.create(HttpStatusCode.UNAUTHORIZED, consent.getReason()); } collection.removeElement(objectAdapter, argAdapter); return helper.collectionDetails(objectAdapter, collectionId, MemberMode.MUTATING, Caching.NONE, getResourceContext().getWhere()); }
/** * Applies to both {@link OneToOneAssociation}s and * {@link OneToManyAssociation}s. */ @Override public ClearAssociationResponse clearAssociation(final ClearAssociationRequest request) { final AuthenticationSession session = request.getSession(); final String fieldIdentifier = request.getFieldIdentifier(); final IdentityData targetData = request.getTarget(); final IdentityData associateData = request.getAssociate(); if (LOG.isDebugEnabled()) { LOG.debug("request clearAssociation " + fieldIdentifier + " on " + targetData + " of " + associateData + " for " + session); } final ObjectAdapter targetAdapter = getPersistentObjectAdapter(session, targetData); final ObjectAdapter associateAdapter = getPersistentObjectAdapter(session, associateData); final ObjectSpecification specification = targetAdapter.getSpecification(); final ObjectAssociation association = specification.getAssociation(fieldIdentifier); if (!association.isVisible(session, targetAdapter).isAllowed() || association.isUsable(session, targetAdapter).isVetoed()) { throw new IsisException("can't modify field as not visible or editable"); } ensureAssociationModifiableElseThrowException(session, targetAdapter, association); if (association instanceof OneToOneAssociation) { ((OneToOneAssociation) association).clearAssociation(targetAdapter); } else { ((OneToManyAssociation) association).removeElement(targetAdapter, associateAdapter); } return new ClearAssociationResponse(getUpdates()); }
collection.removeElement(targetAdapter, argumentAdapter, getInteractionInitiatedBy()); } else { try { collection.removeElement(targetAdapter, argumentAdapter, getInteractionInitiatedBy()); } catch(Exception ignore) {
((OneToManyAssociation) field).removeElement(parent, row);