package org.orienteer.core.component.property;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxCheckBox;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.FormComponentPanel;
import org.apache.wicket.markup.html.form.upload.FileUpload;
import org.apache.wicket.markup.html.form.upload.FileUploadField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import ru.ydn.wicket.wicketorientdb.model.DynamicPropertyValueModel;
import ru.ydn.wicket.wicketorientdb.model.ODocumentPropertyModel;

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

/**
 * {@link FormComponentPanel} to edit binary parameters (upload)
 */
public class BinaryEditPanel extends FormComponentPanel<byte[]> {
	
	public static final String FILENAME_SUFFIX = "$fileName";

	protected final CheckBox clear;
	protected FileUploadField fileUploadField;
	
	private String tempName; 
	private IModel<String> nameModel;
	
	public BinaryEditPanel(String id, final IModel<ODocument> docModel, final IModel<OProperty> propModel) {
		this(id, docModel, propModel, new DynamicPropertyValueModel<byte[]>(docModel, propModel));
	}
	
	public BinaryEditPanel(String id, final IModel<ODocument> docModel, final IModel<OProperty> propModel, IModel<byte[]> valueModel) {
		this(id, valueModel);
		nameModel = new ODocumentPropertyModel<String>(docModel, propModel.getObject().getName()+"$fileName");
	}

	public BinaryEditPanel(String id, IModel<byte[]> model) {
		super(id, model);
		fileUploadField = new FileUploadField("data");
		add(fileUploadField);
		fileUploadField.setOutputMarkupId(true);

		clear = new AjaxCheckBox("clear", Model.of(Boolean.FALSE)) {
			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				Boolean shouldClear = clear.getConvertedInput();
				if (shouldClear) {
					fileUploadField.clearInput();
				}

				fileUploadField.setEnabled(!shouldClear);
				target.add(fileUploadField);
			}
		};

		add(clear);
	}

	@Override
	public void convertInput() {
		if (clear.getConvertedInput())
		{
			setConvertedInput(null);
			tempName = null;
			return;
		}

		FileUpload fileUpload = fileUploadField.getFileUpload();
		if(fileUpload!=null) {
			setConvertedInput(fileUpload.getBytes());
			tempName = fileUpload.getClientFileName();
		}
		else {
			setConvertedInput(getModelObject());
		}
	}
	
	@Override
	protected void onModelChanged() {
		super.onModelChanged();
		if(nameModel!=null) nameModel.setObject(tempName);
	}
}

@@ -41,16 +41,45 @@
 perty;%0A%0A
+import java.util.ArrayList;%0A%0A
 import o
@@ -1707,16 +1707,59 @@
 d(%22data%22
+, Model.ofList(new ArrayList%3CFileUpload%3E())
 );%0A%09%09add

Fix #509: Exception if file upload field named "data"