private void updateOneToManyAssociation( final NakedObject object, final OneToManyAssociation field, final CollectionData collectionData) { final NakedObject collection = field.get(object); final CollectionFacet facet = CollectionFacetUtils.getCollectionFacetFromSpec(collection); final Vector original = new Vector(); final Enumeration elements = facet.elements(collection); while (elements.hasMoreElements()) { original.addElement(elements.nextElement()); } for (int j = 0; j < collectionData.elements.length; j++) { final NakedObject element = recreateReference(collectionData.elements[j]); if (!facet.contains(collection, element)) { LOG.debug(" association " + field + " changed, added " + element.getOid()); field.addElement(object, element); } else { field.removeElement(object, element); } } final int size = original.size(); for (int i = 0; i < size; i++) { final NakedObject element = (NakedObject) original.elementAt(i); LOG.debug(" association " + field + " changed, removed " + element.getOid()); field.removeElement(object, element); } }
@Override public void checkInstances(final Context context, final NakedObject[] objects) { final NakedObject collection = field.get(getTarget(context)); final CollectionFacet facet = CollectionFacetUtils.getCollectionFacetFromSpec(collection); final NakedObject target = getTarget(context); for (int i = 0; i < objects.length; i++) { final Consent valueValid = field.isValidToAdd(target, objects[i]); if (valueValid.isVetoed()) { objects[i] = null; } else if (facet.contains(collection, objects[i])) { objects[i] = null; } } }
@Override public Object invoke(final Object collectionObject, final Method method, final Object[] args) throws Throwable { // delegate final Object returnValueObj = delegate(method, args); if (interceptedMethods.contains(method)) { resolveIfRequired(domainObject); final InteractionEvent ev = new CollectionMethodEvent(getDelegate(), getCollection().getIdentifier(), getDomainObject(), method.getName(), args, returnValueObj); notifyListeners(ev); return returnValueObj; } if (vetoedMethods.contains(method)) { throw new UnsupportedOperationException(String.format("Method '%s' may not be called directly.", method.getName())); } return returnValueObj; }
protected AddItemToCollectionTask(final Context context, final NakedObject target, final OneToManyAssociation fld) { super(context, "Add to collection", "", target, 1); names[0] = fld.getName(); descriptions[0] = fld.getDescription(); fieldSpecifications[0] = fld.getSpecification(); initialState[0] = null; optional[0] = true; // TODO add defaults and options this.field = fld; }
public static Table createTable( final Context context, final String id, final NakedObject object, final OneToManyAssociation collectionField) { final NakedObject collection = collectionField.get(object); final String name = collectionField.getName(); final NakedObjectSpecification type = collectionField.getSpecification(); final String summary = "Table showing elements of " + name + " field"; return createTable(context, collectionField != null, collection, summary, type); }
@Override public void objectActionResult(final NakedObject result, final Location at) { // same as in TreeNodeBorder final OneToManyField internalCollectionContent = (OneToManyField) getContent(); final OneToManyAssociation field = internalCollectionContent.getOneToManyAssociation(); final NakedObject target = ((ObjectContent) getParent().getContent()).getObject(); final Consent valid = field.isValidToAdd(target, result); if (valid.isAllowed()) { field.addElement(target, result); } super.objectActionResult(result, at); }
private Object handleCollectionRemoveFromMethod( final Object[] args, final NakedObject targetAdapter, final OneToManyAssociation otma, final String methodName) { if (args.length != 1) { throw new IllegalArgumentException("Invoking a removeFrom should only have a single argument"); } resolveIfRequired(targetAdapter); final Object argumentObj = underlying(args[0]); if (argumentObj == null) { throw new IllegalArgumentException("Must provide a non-null object to remove"); } final NakedObject argumentAdapter = getRuntimeContext().adapterFor(argumentObj); final InteractionResult interactionResult = otma.isValidToRemove(targetAdapter, argumentAdapter).getInteractionResult(); notifyListenersAndVetoIfRequired(interactionResult); if (getExecutionMode() == ExecutionMode.EXECUTE) { otma.removeElement(targetAdapter, argumentAdapter); } objectChangedIfRequired(targetAdapter); return null; }
private Object handleGetterMethodOnCollection( final Method method, final Object[] args, final NakedObject targetAdapter, final OneToManyAssociation otma, final String memberName) { if (args.length != 0) { throw new IllegalArgumentException("Invoking a 'get' should have no arguments"); } resolveIfRequired(targetAdapter); final NakedObject currentReferencedAdapter = otma.get(targetAdapter); final Object currentReferencedObj = NakedObjectUtils.unwrap(currentReferencedAdapter); final CollectionAccessEvent ev = new CollectionAccessEvent(getDelegate(), otma.getIdentifier()); if (currentReferencedObj instanceof Collection) { final Collection<?> collectionViewObject = lookupViewObject(method, memberName, (Collection<?>) currentReferencedObj, otma); notifyListeners(ev); return collectionViewObject; } else if (currentReferencedObj instanceof Map) { final Map<?, ?> mapViewObject = lookupViewObject(method, memberName, (Map<?, ?>) currentReferencedObj, otma); notifyListeners(ev); return mapViewObject; } throw new IllegalArgumentException( String.format("Collection type '%s' not supported by framework", currentReferencedObj.getClass().getName())); }
public NakedObject drop(final Content sourceContent) { final NakedObject object = sourceContent.getNaked(); final NakedObject parent = field.getParent(); final Consent perm = canDrop(sourceContent); if (perm.isAllowed()) { getOneToManyAssociation().addElement(parent, object); } return null; }
@Override public void clear() { final NakedObject parentObject = getParent(); final OneToManyAssociation association = getOneToManyAssociation(); LOG.debug("remove " + element + " from " + parentObject); association.removeElement(parentObject, element); }
final NakedObject collection = oneToManyAssociation.get(fieldPlace.getObject()); final CollectionFacet facet = (CollectionFacet) collection.getSpecification().getFacet(CollectionFacet.class);
collection = oneToManyAssociation.get(object); final NakedObjectSpecification referencedTypeNos = oneToManyAssociation.getSpecification(); final String fullyQualifiedClassName = referencedTypeNos.getFullName(); .getSpecification().getFullName(), FacetUtil.getFacetsByType(oneToManyAssociation));
@Override protected void doExecute(final Context context, final ViewPane content, final NakedObject object, final String field) { final String id = context.mapObject(object); final NakedObjectSpecification specification = object.getSpecification(); final OneToManyAssociation collection = (OneToManyAssociation) specification.getAssociation(field); NakedObjectsContext.getPersistenceSession().resolveField(object, collection); context.addCollectionFieldCrumb(collection.getName()); content.add(context.getComponentFactory().createHeading(collection.getName())); final Table table = TableUtil.createTable(context, id, object, collection); content.add(table); if (collection.isUsable(NakedObjectsContext.getAuthenticationSession(), object).isAllowed()) { content.add(context.getComponentFactory().createAddOption(id, collection.getId())); } }
public Consent canDrop(final Content sourceContent) { if (sourceContent.getNaked() instanceof NakedObject) { final NakedObject sourceAdapter = sourceContent.getNaked(); final NakedObject parentAdapter = field.getParent(); final NakedObject collection = getNaked(); if (collection == null) { // TODO: move logic into Facet return new Veto("Collection not set up; can't add elements to a non-existant collection"); } final Consent usableInState = getOneToManyAssociation().isUsable(NakedObjectsContext.getAuthenticationSession(), parentAdapter); if (usableInState.isVetoed()) { return usableInState; } final NakedObjectSpecification specification = sourceAdapter.getSpecification(); final NakedObjectSpecification elementSpecification = getElementSpecification(); if (!specification.isOfType(elementSpecification)) { // TODO: move logic into Facet return new Veto(String.format("Only objects of type %s are allowed in this collection", elementSpecification .getSingularName())); } if (parentAdapter.isPersistent() && sourceAdapter.isTransient()) { // TODO: move logic into Facet return new Veto("Can't set field in persistent object with reference to non-persistent object"); } return getOneToManyAssociation().isValidToAdd(parentAdapter, sourceAdapter); } else { return Veto.DEFAULT; } }
@Override public void checkForValidity(final Context context) { final NakedObject target = getTarget(context); final NakedObject[] parameters = getEntries(context); final Consent valueValid = field.isValidToAdd(target, parameters[0]); errors[0] = valueValid.getReason(); }
@Override public Image getIconPicture(final int iconHeight) { final NakedObjectSpecification specification = getOneToManyAssociation().getSpecification(); Image icon = ImageFactory.getInstance().loadIcon(specification, iconHeight, null); if (icon == null) { icon = ImageFactory.getInstance().loadDefaultIcon(iconHeight, null); } return icon; }
@Override public Consent canClear() { final NakedObject parentObject = getParent(); final OneToManyAssociation association = getOneToManyAssociation(); final NakedObject associatedObject = getObject(); final Consent isEditable = isEditable(); if (isEditable.isVetoed()) { return isEditable; } final Consent consent = association.isValidToRemove(parentObject, associatedObject); if (consent.isAllowed()) { consent.setDescription("Clear the association to this object from '" + parentObject.titleString() + "'"); } return consent; }
public String getDescription() { return field.getName() + ": " + getOneToManyAssociation().getDescription(); }
public String getId() { return getOneToManyAssociation().getId(); }
private Object handleCollectionAddToMethod( final Object[] args, final NakedObject targetAdapter, final OneToManyAssociation otma, final String methodName) { if (args.length != 1) { throw new IllegalArgumentException("Invoking a addTo should only have a single argument"); } resolveIfRequired(targetAdapter); final Object argumentObj = underlying(args[0]); if (argumentObj == null) { throw new IllegalArgumentException("Must provide a non-null object to add"); } final NakedObject argumentNO = getRuntimeContext().adapterFor(argumentObj); final InteractionResult interactionResult = otma.isValidToAdd(targetAdapter, argumentNO).getInteractionResult(); notifyListenersAndVetoIfRequired(interactionResult); if (getExecutionMode() == ExecutionMode.EXECUTE) { otma.addElement(targetAdapter, argumentNO); } objectChangedIfRequired(targetAdapter); return null; }