package com.uas.report.axis;

import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.activation.DataSource;
import javax.activation.FileDataSource;

public class FileResourceHandler extends BasicResourceHandler {

	@Override
	public Class<?> getResourceType() {
		return FileResource.class;
	}

	@Override
	public Map<String, DataSource> getAttachments(Map<String, String> arguments, ResourceDescriptor descriptor)
			throws WSException {
		Map<String, DataSource> attachments = new LinkedHashMap<>();
		if (arguments.containsKey(Argument.NO_RESOURCE_DATA_ATTACHMENT)) {
			descriptor.setHasData(false);
		} else {
			// Retrieve resource data...
			descriptor.setHasData(true);
			// FileResourceData fileData =
			// repositoryManagementService.getRepository().getResourceData(null,descriptor.getUriString());
			// ResourceDataSource attachmentSource = new ResourceDataSource("",
			// fileData);
			// attachments.addAttachment("attachment", attachmentSource);
			FileDataSource dataSource = new FileDataSource(getFile(descriptor.getUriString()));
			attachments.put("attachment", dataSource);
		}
		return attachments;
	}

	@Override
	protected File checkFile(String uriString) throws IOException {
		File file = getFile(uriString);
		if (file.isDirectory()) {
			throw new IOException("资源为文件夹！");
		}
		return file;
	}
}
