package ua.com.fielden.platform.attachment;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import ua.com.fielden.platform.entity.AbstractEntity;
import ua.com.fielden.platform.entity.annotation.DescRequired;
import ua.com.fielden.platform.entity.annotation.DescTitle;
import ua.com.fielden.platform.entity.annotation.DisplayDescription;
import ua.com.fielden.platform.entity.annotation.IsProperty;
import ua.com.fielden.platform.entity.annotation.KeyReadonly;
import ua.com.fielden.platform.entity.annotation.KeyTitle;
import ua.com.fielden.platform.entity.annotation.KeyType;
import ua.com.fielden.platform.entity.annotation.MapEntityTo;
import ua.com.fielden.platform.entity.annotation.MapTo;
import ua.com.fielden.platform.entity.annotation.Observable;
import ua.com.fielden.platform.entity.annotation.Readonly;
import ua.com.fielden.platform.entity.annotation.Required;
import ua.com.fielden.platform.entity.annotation.Title;
import ua.com.fielden.platform.entity.validation.annotation.CompanionObject;
import ua.com.fielden.platform.entity.validation.annotation.NotNull;
import ua.com.fielden.platform.error.Result;

/**
 * Class representing file attachment. The convention is to use the name of the attached file as entitie's key.
 *
 * @author TG Team
 *
 */
@KeyType(String.class)
@KeyTitle(value = "File name", desc = "Unique name of the file attached.")
@DescTitle(value = "Description", desc = "A comment about the attached file, which can be used for search.")
@DescRequired
@KeyReadonly
@MapEntityTo("ATTACHMENTS")
@CompanionObject(IAttachmentController.class)
@DisplayDescription
public class Attachment extends AbstractEntity<String> {

    /** Used purely to represent a new file being attached. */
    private File file;

    @Override
    @Observable
    public Attachment setDesc(final String desc) {
        super.setDesc(desc);
        return this;
    }

    @Override
    @NotNull
    @Observable
    public Attachment setKey(final String key) {
        super.setKey(key);
        return this;
    }

    public File getFile() {
	return file;
    }

    public Attachment setFile(final File file) {
	this.file = file;
	setKey(file.getName());
	return this;
    }

    @Override
    protected Result validate() {
	final Result result = super.validate();
	if (!result.isSuccessful()) {
	    return result;
	} else if (file == null && !isPersisted()) {
	    return new Result(this, new IllegalStateException("Attachment is missing a file."));
	}
	return result;
    }

}

@@ -62,81 +62,8 @@
 ile;
-%0Aimport java.io.UnsupportedEncodingException;%0Aimport java.net.URLEncoder;
 %0A%0Aim
@@ -310,69 +310,8 @@
 on;%0A
-import ua.com.fielden.platform.entity.annotation.IsProperty;%0A
 impo
@@ -551,64 +551,8 @@
 To;%0A
-import ua.com.fielden.platform.entity.annotation.MapTo;%0A
 impo
@@ -612,182 +612,8 @@
 le;%0A
-import ua.com.fielden.platform.entity.annotation.Readonly;%0Aimport ua.com.fielden.platform.entity.annotation.Required;%0Aimport ua.com.fielden.platform.entity.annotation.Title;%0A
 impo

Optimised imports.