package tools.vitruv.framework.vsum.views;

import java.util.Collection;
import java.util.List;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;

import com.google.common.annotations.Beta;
import com.google.common.collect.FluentIterable;

import tools.vitruv.framework.change.description.PropagatedChange;

/**
 * A Vitruv view on the virtual model. Upon creation, each view must be
 * registered at the VSUM as change propagation listener.
 */
public interface View extends AutoCloseable {

	/**
	 * Provides the root model elements of this view.
	 * 
	 * @throws IllegalStateException if called on a closed view.
	 * @see View#isClosed()
	 */
	Collection<EObject> rootObjects();

	/**
	 * Provides all root model elements of this view that conform to a certain type.
	 * 
	 * @param clazz is requested root element type.
	 * @throws IllegalStateException if called on a closed view.
	 * @see View#isClosed()
	 */
	default <T> Collection<T> rootObjects(Class<T> clazz) {
		return FluentIterable.from(rootObjects()).filter(clazz).toList();
	};

	/**
	 * Specifies whether the view was modified.
	 */
	boolean isModified();

	/**
	 * Specifies whether the underlying virtual model has changed.
	 */
	boolean hasVSUMChanged();

	/**
	 * Updates the view from the underlying virtual model, thus invalidating its
	 * previous state and now providing a updated view on the virtual model. This
	 * can only be done for an unmodified view. TODO TS: Add issue - in the long
	 * term we need to changes this!
	 * 
	 * @throws UnsupportedOperationException if the update is called for a dirty
	 *                                       view.
	 * @throws IllegalStateException         if called on a closed view.
	 * @see View#isClosed()
	 */
	void update();

	/**
	 * Commits the changes made to the view and its containing elements to the
	 * virtual model. This explicitly includes all changes that have been made
	 * before calling this method. Whether changes will effectively be recorded
	 * depends on this view. It is permissible for a view not to record any changes
	 * if it deems them irrelevant. Note that committing changes will automatically
	 * be followed by an {@link #update} from the virtual model to keep both the
	 * view and the virtual model synchronized.
	 * 
	 * @return the changes resulting from propagating the recorded changes.
	 * @throws IllegalStateException if called on a closed view.
	 * @see View#isClosed()
	 */
	List<PropagatedChange> commitChanges(); // TODO TS: Some views may not always record changes, and thus require
											// transactional record-and-commit.

	/**
	 * Checks whether the view was closed. Closed view should not further be used.
	 */
	boolean isClosed();

	/**
	 * Persists the given object at the given {@link URI} and adds it as view root.
	 */
	@Deprecated
	@Beta
	// TODO We have to think about whether this is a proper level of abstraction and
	// whether passing URIs is necessary here, as root elements of a view must not
	// necessarily be root elements of a resource in the underlying virtual model
	void registerRoot(EObject object, URI persistAt);
	
	/**
	 * Moves the given object to the given {@link URI}.
	 */
	@Deprecated
	@Beta
	// TODO We have to think about whether this is a proper level of abstraction and
	// whether passing URIs is necessary here, as root elements of a view must not
	// necessarily be root elements of a resource in the underlying virtual model
	void moveRoot(EObject object, URI newLocation);

	/**
	 * Returns the {@link ViewSelection} with which this view has been created.
	 */
	ViewSelection getSelection();

	/**
	 * Returns the {@link ViewType} this view conforms to.
	 */
	ViewType<? extends ViewSelector> getViewType();
}

@@ -1459,75 +1459,8 @@
 iew.
- TODO TS: Add issue - in the long%0A%09 * term we need to changes this!
 %0A%09 *
@@ -1687,16 +1687,104 @@
 ()%0A%09 */%0A
+%09// TODO TS Add issue: in the long term we need to allow updates for modified%0A%09// views%0A
 %09void up
@@ -2486,48 +2486,8 @@
 */%0A%09
-List%3CPropagatedChange%3E commitChanges(); 
 // T
@@ -2553,26 +2553,16 @@
 require%0A
-%09%09%09%09%09%09%09%09%09%09
 %09// tran
@@ -2589,16 +2589,57 @@
 -commit.
+%0A%09List%3CPropagatedChange%3E commitChanges();
 %0A%0A%09/**%0A%09
@@ -2837,270 +2837,170 @@
 %0A%09 *
-/
 %0A%09
-@Deprecated%0A%09@Beta%0A%09// TODO We have to think about whether this is a prop
+ * This method is in beta state, as it is still und
 er 
-l
 ev
-el of abstraction and%0A%09// whether passing URIs is necessary here, as root elements of a view must not%0A%09// necessarily be root elements of a resource in the underlying virtual model
+aluation whether it is%0A%09 * sufficient and appropriate for registering root objects in views.%0A%09 */%0A%09@Beta
 %0A%09vo
@@ -3047,17 +3047,16 @@
 istAt);%0A
-%09
 %0A%09/**%0A%09 
@@ -3109,274 +3109,305 @@
 RI%7D.
-%0A%09 */%0A%09@Deprecated%0A%09@Beta%0A%09// TODO We have to think about whether this 
+ The given %7B@link EObject%7D%0A%09 * must already be a root object of the view, otherw
 is
+e
  a
- proper level of abstraction and%0A%09// whether passing URIs is necessary here, as root elements of a view must not%0A%09// necessarily be root elements of a resource in the underly
+n%0A%09 * %7B@link IllegalStateException%7D is thrown.%0A%09 *%0A%09 * This method is in beta state, as it is still under evaluation whether it is%0A%09 * sufficient and appropriate for registering root objects 
 in
-g
  vi
-rtual model
+ews.%0A%09 */%0A%09@Beta
 %0A%09vo

Document view interface and remove deprecated tags