/*
 * Copyright (c) 2014 Bernold Kraft, Sebastian Riemschüssel, Torsten Krämer (Berlin, Germany).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 * Initial commit by Riemi @ 14.04.2015.
 */
package de.bitub.step.p21.util;

import java.io.IOException;
import java.util.Collections;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;

import de.bitub.step.p21.persistence.P21Resource;
import de.bitub.step.p21.persistence.P21ResourceFactoryImpl;

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * 
 * @generated NOT
 * @author Riemi - 14.04.2015
 */
public class IOHelper
{

  private static final Logger LOGGER = Logger.getLogger(IOHelper.class.getName());

  static {
    try {

      // This block configure the logger with handler and formatter
      //
      FileHandler fh = new FileHandler("logs/" + IOHelper.class.getSimpleName() + ".log");
      SimpleFormatter formatter = new SimpleFormatter();
      fh.setFormatter(formatter);

      IOHelper.LOGGER.setLevel(Level.WARNING);
      IOHelper.LOGGER.addHandler(fh);
      IOHelper.LOGGER.setUseParentHandlers(true);
    }
    catch (SecurityException e) {
      e.printStackTrace();
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }

  // TODO (Riemschüssel 09.12.2015) Change serialisation from xmi to STEP instance file (.p21, .ifc, .stp)

  /**
   * Store the resource as XMI file.
   * Needed EcorePackacke must be initialzed first.
   *
   * @param eObject
   * @param uri
   */
  public static void storeAsXMI(EObject eObject, URI uri)
  {
    // Obtain a new resource set
    //
    ResourceSet resSet = new ResourceSetImpl();

    // Register the P21 resource factory for the .ifc extension
    //
    resSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl());

    // Get the resource
    //
    XMIResource resource = (XMIResource) resSet.createResource(uri, null);
    resource.getContents().add(eObject);

    try {
      resource.save(Collections.EMPTY_MAP);
      IOHelper.LOGGER.info(String.format("%s saved.", eObject));
    }
    catch (IOException exception) {

      IOHelper.LOGGER
          .warning(String.format("Failed to save %s to resource %s. See reason %s", eObject, uri, exception.getMessage()));
    }
    catch (Exception exception) {
      IOHelper.LOGGER.severe(
          String.format("Something unexpected happened while saving resource %s. See reason %s", uri, exception.getMessage()));
    }
  }

  /**
   * Load the resource from a given STEP instnace file (.ifc).
   * 
   * @param uri
   * @return
   */
  public static EObject load(URI uri)
  {
    // Obtain a new resource set
    //
    ResourceSet resSet = new ResourceSetImpl();

    // Register the P21 resource factory for the .ifc extension
    //
    resSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ifc", new P21ResourceFactoryImpl());

    // Get the resource
    //
    P21Resource resource = (P21Resource) resSet.createResource(uri);

    EObject eObject = null;

    try {
      resource.load(null);

      if (resource.getContents().size() > 0) {
        eObject = resource.getContents().get(0);
      }
      IOHelper.LOGGER.info(String.format("%s loaded.", eObject));
    }
    catch (IOException exception) {

      IOHelper.LOGGER.warning(String.format("Failed to load resource %s. See reason %s", uri, exception.getMessage()));
    }

    // Get the first model element and cast it to the right type, in my
    // example everything is hierarchical included in this first node
    //
    return eObject;
  }
}

@@ -1236,359 +1236,19 @@
 gger
-.getLogger(IOHelper.class.getName());%0A%0A  static %7B%0A    try %7B%0A%0A      // This block configure the logger with handler and formatter%0A      //%0A      FileHandler fh = new FileHandler(%22logs/%22 + IOHelper.class.getSimpleName() + %22.log%22);%0A      SimpleFormatter formatter = new SimpleFormatter();%0A      fh.setFormatter(formatter);%0A%0A      IOHelper.LOGGER.setLevel
+Helper.init
 (Lev
@@ -1257,24 +1257,17 @@
 .WARNING
-);%0A     
+,
  IOHelpe
@@ -1272,326 +1272,15 @@
 per.
-LOGGER.addHandler(fh);%0A      IOHelper.LOGGER.setUseParentHandlers(true);%0A    %7D%0A    catch (SecurityException e) %7B%0A      e.printStackTrace();%0A    %7D%0A    catch (IOException e) %7B%0A      e.printStackTrace();%0A    %7D%0A  %7D%0A%0A  // TODO (Riemsch%C3%BCssel 09.12.2015) Change serialisation from xmi to STEP instance file (.p21, .ifc, .stp)
+class);
 %0A%0A  
@@ -1612,35 +1612,35 @@
 actory for the .
-ifc
+xmi
  extension%0A    /
@@ -2082,27 +2082,16 @@
 r.LOGGER
-%0A          
 .warning
@@ -2165,24 +2165,34 @@
 Object, uri,
+%0A         
  exception.g
@@ -2275,27 +2275,16 @@
 .severe(
-%0A          
 String.f
@@ -2358,32 +2358,42 @@
 reason %25s%22, uri,
+%0A         
  exception.getMe

use LoggerUtil