All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.jhonsapp.repository.implementation.RepositoryLocalHD Maven / Gradle / Ivy

package br.com.jhonsapp.repository.implementation;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.FileSystemNotFoundException;

import org.springframework.stereotype.Service;

import br.com.jhonsapp.repository.object.Object;
import br.com.jhonsapp.repository.object.ObjectImpl;
import br.com.jhonsapp.repository.util.PathUtil;
import br.com.jhonsapp.util.file.FileUtil;
import br.com.jhonsapp.util.string.StringUtil;

/**
 * This class is responsible for manipulating files in a local repository.
 * 
 * @author Jhonathan Camacho
 * 
 */
@Service
public class RepositoryLocalHD extends AbstractRepository {

	/**
	 * Generated serial version id created at 09 July 2017.
	 */
	private static final long serialVersionUID = -2271379497316905669L;

	public RepositoryLocalHD(String repositoryPath) {
		super(File.separator + PathUtil.pathFormat(repositoryPath));
	}

	@Override
	public boolean archive(Object object) {
		try {

			if (!this.createFolder(object.getFolder()))
				throw new FileSystemNotFoundException(
						"The directory was not created successfully, "
						+ "so it was not possible to continue the archiving operation of the object.");

			this.unarchive(object.getFolder(), object.getId());

			FileOutputStream fileOutput = new FileOutputStream(getRepositoryPath() + object.getAbsolutePath());
			fileOutput.write(object.getContentsInByte());
			fileOutput.close();

			return true;
		} catch (IOException e) {
			return false;
		}
	}

	@Override
	public boolean unarchive(String folder, String id) {

		if (this.hasArchive(folder, id)) {
			File file = this.findFile(folder, id);
			file.delete();
			return true;
		} else {
			return false;
		}
	}

	@Override
	public Object search(String folder, String id) {

		File file = this.findFile(folder, id);

		if (FileUtil.isValid(file)) {

			return ObjectImpl.createObject(folder, id, file);
		} else {
			return null;
		}
	}

	@Override
	public boolean hasArchive(String folder, String id) {
		return this.findFile(folder, id) != null;
	}

	@Override
	public boolean createFolder(String name) {

		if (hasFolder(name))
			return true;
		else
			return new File(this.getPath(name)).mkdirs();
	}

	@Override
	public boolean hasFolder(String name) {
		return new File(this.getPath(name)).exists();
	}

	private File findFile(String folder, String id) {

		if (StringUtil.isBlanck(folder))
			throw new IllegalArgumentException("The folder cannot be null.");

		if (StringUtil.isBlanck(id))
			throw new IllegalArgumentException("The id cannot be null.");

		File arquivo = null;

		File[] lista = new File(this.getPath(folder)).listFiles();

		for (int i = 0; i < lista.length; i++) {

			if (lista[i].getName().contains(id)) {

				arquivo = lista[i];
				break;
			}
		}

		return arquivo;
	}

	private String getPath(String folder) {
		return this.getRepositoryPath() + PathUtil.pathFormat(folder);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy