package edu.kit.ipd.sdq.vitruvius.framework.vsum.helper;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;

import edu.kit.ipd.sdq.vitruvius.framework.contracts.datatypes.VURI;
import edu.kit.ipd.sdq.vitruvius.framework.vsum.VSUMConstants;

public class FileSystemHelper {
    /** Utility classes should not have a public or default constructor. */
    private FileSystemHelper() {
    }

    public static VURI createCorrespondenceInstanceURI(final VURI[] mmURIs) {
        IProject correspondenceProject = getVSUMProject();
        IFolder correspondenceFolder = getCorrespondenceFolder(correspondenceProject);
        VURI[] copyOfMMURIs = Arrays.copyOf(mmURIs, mmURIs.length);
        Arrays.sort(copyOfMMURIs);
        String fileName = "";
        for (VURI uri : copyOfMMURIs) {
            fileName += uri.getLastSegment() + uri.toString().hashCode();
        }
        IFile correspondenceFile = correspondenceFolder.getFile(fileName);
        return VURI.getInstance(correspondenceFile);
    }

    public static IProject createVSUMProject() {
        try {
            IProject project = getVSUMProject();
            project.create(null);
            project.open(null);
            // IProjectDescription description = project.getDescription();
            // description.setNatureIds(new String[] { VITRUVIUSNATURE.ID });
            // project.setDescription(description, null);
            IFolder correspondenceFolder = getCorrespondenceFolder(project);
            correspondenceFolder.create(false, true, null);
            return project;
        } catch (CoreException e) {
            // soften
            throw new RuntimeException(e);
        }
    }

    public static void saveVSUMvURIsToFile(final Set<String> stringSet) {
        String fileName = getVSUMMapFileName();
        saveStringSetToFile(stringSet, fileName);
    }

    private static void saveStringSetToFile(final Set<String> stringSet, final String fileName) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(fileName);
            ObjectOutputStream oos = new ObjectOutputStream(fileOutputStream);
            oos.writeObject(stringSet);
            oos.flush();
            oos.close();
        } catch (IOException e) {
            throw new RuntimeException("Could not save map: " + stringSet + "to file " + fileName + e);
        }
    }

    public static Set<String> loadVSUMvURIsFromFile() {
        String fileName = getVSUMMapFileName();
        return loadStringSetFromFile(fileName);
    }

    private static Set<String> loadStringSetFromFile(final String fileName) {
        try {
            FileInputStream fileInputStream = new FileInputStream(fileName);
            ObjectInputStream ois = new ObjectInputStream(fileInputStream);
            Object obj = ois.readObject();
            ois.close();
            Set<?> set = (Set<?>) obj;
            Set<String> stringSet = (Set<String>) set;
            return stringSet;
        } catch (FileNotFoundException e) {
            return Collections.emptySet();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    private static IProject getVSUMProject() {
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        return root.getProject(VSUMConstants.VSUM_PROJECT_NAME);
    }

    private static IFolder getCorrespondenceFolder(final IProject project) {
        IFolder correspondenceFolder = project.getFolder(VSUMConstants.CORRESPONDENCE_FOLDER_NAME);
        return correspondenceFolder;
    }

    private static String getVSUMMapFileName() {
        IFile file = getVSUMProject().getFolder(VSUMConstants.VSUM_FOLDER_NAME).getFile(
                VSUMConstants.VSUM_INSTANCES_FILE_NAME);
        return file.getLocation().toOSString();
    }

}

@@ -51,16 +51,37 @@
 elper;%0A%0A
+import java.io.File;%0A
 import j
@@ -96,32 +96,32 @@
 ileInputStream;%0A
-
 import java.io.F
@@ -1528,680 +1528,8 @@
  %7D%0A%0A
-    public static IProject createVSUMProject() %7B%0A        try %7B%0A            IProject project = getVSUMProject();%0A            project.create(null);%0A            project.open(null);%0A            // IProjectDescription description = project.getDescription();%0A            // description.setNatureIds(new String%5B%5D %7B VITRUVIUSNATURE.ID %7D);%0A            // project.setDescription(description, null);%0A            IFolder correspondenceFolder = getCorrespondenceFolder(project);%0A            correspondenceFolder.create(false, true, null);%0A            return project;%0A        %7D catch (CoreException e) %7B%0A            // soften%0A            throw new RuntimeException(e);%0A        %7D%0A    %7D%0A%0A
     
@@ -1807,32 +1807,153 @@
 %7B%0A        try %7B%0A
+            File f = new File(fileName);%0A            if (!f.exists()) %7B%0A                f.createNewFile();%0A            %7D%0A
             File
@@ -1998,39 +1998,32 @@
 leOutputStream(f
-ileName
 );%0A            O
@@ -3325,22 +3325,38 @@
         
-return
+IProject vsumProject =
  root.ge
@@ -3394,24 +3394,749 @@
 JECT_NAME);%0A
+        if (!vsumProject.exists()) %7B%0A            createProject(vsumProject);%0A        %7D%0A        return vsumProject;%0A%0A    %7D%0A%0A    public static void createProject(final IProject project) %7B%0A        try %7B%0A            project.create(null);%0A            project.open(null);%0A            // IProjectDescription description = project.getDescription();%0A            // description.setNatureIds(new String%5B%5D %7B VITRUVIUSNATURE.ID %7D);%0A            // project.setDescription(description, null);%0A            IFolder correspondenceFolder = getCorrespondenceFolder(project);%0A            correspondenceFolder.create(false, true, null);%0A        %7D catch (CoreException e) %7B%0A            // soften%0A            throw new RuntimeException(e);%0A        %7D%0A
     %7D%0A%0A    p

create VSUM project dynamically