package org.orienteer.core.component.command;

import java.util.Collection;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;
import org.orienteer.core.component.BootstrapType;
import org.orienteer.core.component.FAIconType;
import org.orienteer.core.component.table.DataTableCommandsToolbar;
import org.orienteer.core.component.table.OrienteerDataTable;

import com.orientechnologies.orient.core.metadata.schema.OProperty;
import com.orientechnologies.orient.core.record.impl.ODocument;

/**
 * {@link Command} to release (remove link, but not a document) an {@link ODocument}
 */
public class ReleaseODocumentCommand extends
		AbstractCheckBoxEnabledCommand<ODocument>
{

	private IModel<ODocument> documentModel;
	private IModel<OProperty> propertyModel;

	public ReleaseODocumentCommand(OrienteerDataTable<ODocument, ?> table, IModel<ODocument> documentModel, IModel<OProperty> propertyModel)
	{
		super(new ResourceModel("command.release"), table);
		this.documentModel = documentModel;
		this.propertyModel = propertyModel;
	}
	
	@Override
	protected void onInstantiation() {
		super.onInstantiation();
		setIcon(FAIconType.times);
		setBootstrapType(BootstrapType.WARNING);
	}
	
	@Override
	protected void performMultiAction(AjaxRequestTarget target, List<ODocument> objects) {
		if(objects==null || objects.isEmpty()) return;
		ODocument doc = documentModel.getObject();
		if(doc!=null)
		{
			OProperty property = propertyModel.getObject();
			if(property!=null)
			{
				Collection<ODocument> collection = doc.field(property.getName());
				for (ODocument oDocument : objects)
				{
					collection.remove(oDocument);
				}
//				collection.removeAll(objects);
//				doc.field(property.getName(), collection);
				doc.save();
			}
		}
	}

	@Override
	public void detachModels() {
		super.detachModels();
		if(propertyModel!=null) propertyModel.detach();
		if(documentModel!=null) documentModel.detach();
	}

}

@@ -1648,16 +1648,44 @@
 ame());%0A
+%09%09%09%09if(collection!=null) %7B%0A%09
 %09%09%09%09for 
@@ -1720,18 +1720,20 @@
 ts)%0A%09%09%09%09
+%09
 %7B%0A
+%09
 %09%09%09%09%09col
@@ -1767,12 +1767,14 @@
 %09%09%09%09
+%09
 %7D%0A//
+%09
 %09%09%09%09
@@ -1806,16 +1806,17 @@
 cts);%0A//
+%09
 %09%09%09%09doc.
@@ -1854,24 +1854,25 @@
 ction);%0A%09%09%09%09
+%09
 doc.save();%0A
@@ -1871,16 +1871,22 @@
 save();%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D

Fix NPE in ReleaseODocumentCommand