package eu.scasefp7.eclipse.core.connect.uploader;

import java.io.File;
import java.util.ArrayList;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.QualifiedName;

import eu.scasefp7.eclipse.core.connect.Activator;

public class ProjectFileLoader {

	private static IContainer getProjectFolder(IProject project, String folderId) {
		String requirementsFolderLocation = null;
		try {
			requirementsFolderLocation = project.getPersistentProperty(new QualifiedName("", folderId));
		} catch (CoreException e) {
			Activator.log("Error retrieving project property (requirements folder location)", e);
		}
		IContainer container = project;
		if (requirementsFolderLocation != null) {
			if (project.findMember(new Path(requirementsFolderLocation)).exists())
				container = (IContainer) project.findMember(new Path(requirementsFolderLocation));
		}
		return container;
	}

	/**
	 * Finds the files of a container recursively.
	 * 
	 * @param container a workspace container (e.g. project or folder).
	 * @param files a list of files that is populated.
	 * @param extension the extension of the files that are to be retrieved.
	 */
	private static void processContainer(IContainer container, ArrayList<IFile> files, String extension) {
		try {
			IResource[] members = container.members();
			for (IResource member : members) {
				if (member instanceof IContainer) {
					processContainer((IContainer) member, files, extension);
				} else if (member instanceof IFile) {
					if (extension.equals("") || ((IFile) member).getName().endsWith("." + extension))
						files.add((IFile) member);
				}
			}
		} catch (CoreException e) {
			Activator.log("Error finding the files of a project", e);
		}
	}

	/**
	 * Returns the files of the given project.
	 * 
	 * @param project the project of which the files are returned.
	 * @param extension the extension of the returned files.
	 * @return a list of the files of the project with the given extension.
	 */
	protected static ArrayList<IFile> getFilesOfProject(IProject project, String extension) {
		ArrayList<IFile> files = new ArrayList<IFile>();
		IContainer container = null;
		if (extension.equals("rqs") || extension.equals("sbd"))
			container = getProjectFolder(project, "eu.scasefp7.eclipse.core.ui.rqsFolder");
		else if (extension.equals("scd") || extension.equals("sc") || extension.equals("cservice"))
			container = getProjectFolder(project, "eu.scasefp7.eclipse.core.ui.compFolder");
		else if (extension.equals("owl") || extension.equals("yaml"))
			container = getProjectFolder(project, "eu.scasefp7.eclipse.core.ui.modelsFolder");
		processContainer(container, files, extension);
		return files;
	}

	private static void processContainer(File container, ArrayList<File> files, String extension) {
		for (File member : container.listFiles()) {
			if (member.isDirectory()) {
				processContainer(member, files, extension);
			} else {
				if (extension.equals("") || member.getName().endsWith("." + extension))
					files.add(member);
			}
		}
	}

	public static ArrayList<File> getFilesOfProject(String projectFolder, String extension) {
		ArrayList<File> files = new ArrayList<File>();
		processContainer(new File(projectFolder), files, extension);
		return files;
	}

}

@@ -598,29 +598,17 @@
 %09String 
-requirementsF
+f
 olderLoc
@@ -634,29 +634,17 @@
 y %7B%0D%0A%09%09%09
-requirementsF
+f
 olderLoc
@@ -804,21 +804,8 @@
 ty (
-requirements 
 fold
@@ -870,29 +870,17 @@
 %0D%0A%09%09if (
-requirementsF
+f
 olderLoc
@@ -928,37 +928,25 @@
 er(new Path(
-requirementsF
+f
 olderLocatio
@@ -1021,21 +1021,9 @@
 ath(
-requirementsF
+f
 olde
@@ -2412,16 +2412,43 @@
 s(%22sbd%22)
+ %7C%7C extension.equals(%22uml%22)
 )%0D%0A%09%09%09co
@@ -2854,24 +2854,148 @@
 sFolder%22);%0D%0A
+%09%09else if (extension.equals(%22xmi%22))%0D%0A%09%09%09container = getProjectFolder(project, %22eu.scasefp7.eclipse.core.ui.outputFolder%22);%0D%0A
 %09%09processCon

Add support for reading uml and xmi files to assets registry connector