catch up with branch daily

Signed-off-by: Ralf Mollik <ramollik@compex-commerce.com>
diff --git a/org.eclipse.osbp.ide.core.ui.softwarefactory/src/org/eclipse/osbp/ide/core/ui/softwarefactory/builder/OSBP2Builder.java b/org.eclipse.osbp.ide.core.ui.softwarefactory/src/org/eclipse/osbp/ide/core/ui/softwarefactory/builder/OSBP2Builder.java
index a5b1341..07ae06f 100644
--- a/org.eclipse.osbp.ide.core.ui.softwarefactory/src/org/eclipse/osbp/ide/core/ui/softwarefactory/builder/OSBP2Builder.java
+++ b/org.eclipse.osbp.ide.core.ui.softwarefactory/src/org/eclipse/osbp/ide/core/ui/softwarefactory/builder/OSBP2Builder.java
@@ -14,11 +14,9 @@
  */
 package org.eclipse.osbp.ide.core.ui.softwarefactory.builder;
 
-import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -32,71 +30,32 @@
 import org.eclipse.core.resources.IResourceDelta;
 import org.eclipse.core.resources.IResourceDeltaVisitor;
 import org.eclipse.core.resources.IResourceVisitor;
-import org.eclipse.core.resources.IStorage;
+import org.eclipse.core.resources.IWorkspace;
 import org.eclipse.core.resources.IncrementalProjectBuilder;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.OperationCanceledException;
 import org.eclipse.core.runtime.ProgressMonitorWrapper;
 import org.eclipse.core.runtime.SubMonitor;
-import org.eclipse.emf.common.util.EList;
 import org.eclipse.emf.common.util.URI;
 import org.eclipse.emf.common.util.WrappedException;
-import org.eclipse.emf.ecore.EObject;
 import org.eclipse.emf.ecore.resource.Resource;
 import org.eclipse.emf.ecore.resource.ResourceSet;
-import org.eclipse.emf.ecore.util.EcoreUtil;
-import org.eclipse.osbp.dsl.semantic.common.types.LType;
-import org.eclipse.osbp.dsl.semantic.common.types.LTypedPackage;
-import org.eclipse.osbp.dsl.semantic.entity.LBean;
-import org.eclipse.osbp.dsl.semantic.entity.LEntity;
-import org.eclipse.osbp.dsl.semantic.entity.LEntityAttribute;
-import org.eclipse.osbp.dsl.semantic.entity.LEntityModel;
 import org.eclipse.osbp.ide.core.ui.softwarefactory.constants.Constants;
 import org.eclipse.osbp.ide.core.ui.softwarefactory.extender.IModelExtenderProvider;
 import org.eclipse.osbp.ide.core.ui.softwarefactory.extender.IModelExtenderProvider.Event.Listener;
 import org.eclipse.osbp.ide.core.ui.softwarefactory.extender.ModelExtendedEvent;
 import org.eclipse.osbp.ide.core.ui.softwarefactory.extender.ModelExtenderUtils;
 import org.eclipse.osbp.ide.core.ui.softwarefactory.extender.ModelInstanceDescription;
-import org.eclipse.osbp.xtext.action.ActionModel;
-import org.eclipse.osbp.xtext.action.ActionPackage;
-import org.eclipse.osbp.xtext.action.ActionToolbar;
-import org.eclipse.osbp.xtext.addons.EObjectHelper;
-import org.eclipse.osbp.xtext.authorizationdsl.AuthorizationModel;
-import org.eclipse.osbp.xtext.authorizationdsl.Role;
-import org.eclipse.osbp.xtext.datainterchange.DataInterchange;
-import org.eclipse.osbp.xtext.datainterchange.DataInterchangeGroup;
-import org.eclipse.osbp.xtext.datainterchange.DataInterchangeModel;
-import org.eclipse.osbp.xtext.datainterchange.DataInterchangePackage;
-import org.eclipse.osbp.xtext.datamartdsl.AxisEnum;
-import org.eclipse.osbp.xtext.datamartdsl.DatamartAttribute;
-import org.eclipse.osbp.xtext.datamartdsl.DatamartAxis;
-import org.eclipse.osbp.xtext.datamartdsl.DatamartDSLFactory;
-import org.eclipse.osbp.xtext.datamartdsl.DatamartDSLPackage;
-import org.eclipse.osbp.xtext.datamartdsl.DatamartDefinition;
-import org.eclipse.osbp.xtext.datamartdsl.DatamartEntity;
-import org.eclipse.osbp.xtext.datamartdsl.DatamartModel;
-import org.eclipse.osbp.xtext.datamartdsl.DatamartPackage;
-import org.eclipse.xtext.resource.IReferenceDescription;
 import org.eclipse.xtext.resource.XtextResourceSet;
 import org.eclipse.xtext.ui.editor.findrefs.IReferenceFinder;
-import org.eclipse.xtext.ui.resource.IStorage2UriMapper;
 import org.eclipse.xtext.ui.resource.XtextResourceSetProvider;
-import org.eclipse.xtext.util.IAcceptor;
-import org.eclipse.xtext.util.Pair;
 import org.eclipse.xtext.util.concurrent.IUnitOfWork;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.inject.Inject;
 
-class DatamartMetadata {
-	IProject project;
-	Resource resource;
-	DatamartModel datamartModel;
-	List<DatamartEntity> trackingEntities;
-}
-
 public class OSBP2Builder extends IncrementalProjectBuilder {
 
 	public static final String BUILDER_ID = Constants.BUILDER_ID;
@@ -118,32 +77,60 @@
 	public static final String UIMODEL_MODEL_EXTENSION = "ui";
 	public static final String PERSPECTIVE_MODEL_EXTENSION = "perspective";
 	public static final String MENU_MODEL_EXTENSION = "menu";
-
-	Set<String> doNotTouchExtensions = new HashSet<>();
-	{
-		doNotTouchExtensions.add(ENTITY_MODEL_EXTENSION);
-		doNotTouchExtensions.add(DTO_MODEL_EXTENSION);
-		doNotTouchExtensions.add(SERVICE_MODEL_EXTENSION);
-	}
+	
+	@SuppressWarnings("serial")
+	private Map<String, List<String>> dependencyTree = new HashMap<String, List<String>>() {{ //NOSONAR
+		put("datatype", new ArrayList<String>() {{	// NOSONAR
+			add("entity");
+			add("dialog");
+		}});
+		put("entity", new ArrayList<String>() {{	// NOSONAR
+			add("datamart");
+			add("datainterchange");
+		}});
+		put("dto", new ArrayList<String>() {{	// NOSONAR
+			add("dialog");
+			add("ui");
+			add("statemachine");
+			add("functionlibrary");
+			add("entitymock");
+			add("blip");
+		}});
+		put("datamart", new ArrayList<String>() {{	// NOSONAR
+			add("report");
+			add("table");
+			add("chart");
+		}});
+		put("data", new ArrayList<String>() {{	// NOSONAR
+			add("action");
+			add("entitymock");
+		}});
+		put("functionlibrary", new ArrayList<String>() {{	// NOSONAR
+			add("action");
+			add("statemachine");
+			add("blip");
+		}});
+		put("authorization", new ArrayList<String>() {{	// NOSONAR
+			add("dialog");
+		}});
+		put("message", new ArrayList<String>() {{	// NOSONAR
+			add("action");
+		}});
+	}};
 
 	@Inject
 	private XtextResourceSetProvider rsProvider;
 
 	@Inject
-	private IReferenceFinder referenceFinder;
-
-	@Inject
-	private IStorage2UriMapper uriStorageMapper;
-
-	@Inject
 	private IModelExtenderProvider.Event.Source modelExtenderBuilder;
 
+	@Inject
+	private IWorkspace workspace;
+	
 	private boolean firstBuild = true;
 
 	@Override
 	protected IProject[] build(int kind, Map<String, String> args, IProgressMonitor monitor) throws CoreException {
-		LOGGER.info("build started for {}", getProject());
-		long startTime = System.currentTimeMillis();
 		SubMonitor progress = null;
 
 		boolean isModelExtending = false;
@@ -203,7 +190,6 @@
 				if (progress != null) {
 					progress.done();
 				}
-				LOGGER.info("Build {} in {} ms", getProject().getName(), System.currentTimeMillis() - startTime);
 			}
 		}
 		if (!extensionsRebuilt.isEmpty()) {
@@ -220,29 +206,46 @@
 	}
 
 	private boolean checkFile(IFile file) {
-		if (file.getFileExtension() != null) {
-			try {
-				if (file.getFileExtension().equals(ENTITY_MODEL_EXTENSION)) {
-					modifyModel(file);
-					touchReferenced(file);
+		return buildUpstream(file.getFileExtension());
+	}
+
+	private boolean buildUpstream(String extension) {
+		if(!dependencyTree.containsKey(extension)) {
+			return false;
+		}
+		for(String depExt:dependencyTree.get(extension)) {
+			LOGGER.debug("****** must rebuild {}", depExt);
+			for(IProject project:workspace.getRoot().getProjects()) {
+				if(depExt.equals(project.getFileExtension())) {
+					LOGGER.debug("****** rebuild {}", project.getName());
+					XtextResourceSet resourceSet = (XtextResourceSet) rsProvider.get(project);
+					Set<String> result = new HashSet<>();
+					try {
+						project.accept(new IResourceVisitor() {
+
+							@Override
+							public boolean visit(IResource resource) throws CoreException {
+								if (resource.getType() == IResource.FILE) {
+									IFile file = (IFile) resource;
+									if (depExt.equalsIgnoreCase(file.getFileExtension()) && !file.getFullPath().toString().contains("target/classes")) {
+										result.add(file.getFullPath().toString());
+									}
+								}
+								return true;
+							}
+						});
+					} catch (CoreException e) {
+						LOGGER.error("{}", e);
+					}
+					for(String uri:result) {
+						org.eclipse.emf.common.util.URI dslURI = org.eclipse.emf.common.util.URI.createPlatformResourceURI(uri, false);
+						Resource resource = resourceSet.getResource(dslURI, true);
+						ModelExtenderUtils.writeResource(resource, project.getName());
+					}
 				}
-				if (file.getFileExtension().equals(AUTHORIZATION_MODEL_EXTENSION)) {
-					touchReferenced(file);
-				}
-				if (file.getFileExtension().equals(DATAMART_MODEL_EXTENSION)) {
-					touchReferenced(file);
-				}
-				if (file.getFileExtension().equals(ACTION_MODEL_EXTENSION)) {
-					touchReferenced(file);
-				}
-				if (file.getFileExtension().equals(DATAINTERCHANGE_MODEL_EXTENSION)) {
-					touchReferenced(file);
-				}
-			} catch (IOException e) { // NOSONAR - is logged
-				LOGGER.error(e.getLocalizedMessage());
 			}
 		}
-		return false;
+		return true;
 	}
 
 	/**
@@ -321,255 +324,6 @@
 	}
 
 	/**
-	 * touches all model resources referencing this root model resource file
-	 * 
-	 * @param modifiedFile
-	 */
-	protected void touchReferenced(IFile modifiedFile) throws IOException {
-		if (modifiedFile == null) {
-			return;
-		}
-
-		EObject model = loadSemanticModel(modifiedFile);
-		if (model == null) {
-			LOGGER.error("Skipping build since SemanticModel could not be loaded from {}", modifiedFile.getName());
-			return;
-		}
-
-		if (model instanceof LEntityModel) {
-			LEntityModel entityModel = (LEntityModel) model;
-			Set<URI> lTypes = new HashSet<>();
-			for (LTypedPackage entityPkg : entityModel.getPackages()) {
-				for (LType lType : entityPkg.getTypes()) {
-					lTypes.add(EcoreUtil.getURI(lType));
-				}
-			}
-			touchReferencingModel(modifiedFile.getName(), lTypes, model.eResource().getResourceSet());
-		}
-
-		if (model instanceof AuthorizationModel) {
-			AuthorizationModel authorizationModel = (AuthorizationModel) model;
-			Set<URI> roles = new HashSet<>();
-			for (Role role : authorizationModel.getPckg().getRoles()) {
-				roles.add(EcoreUtil.getURI(role));
-			}
-			touchReferencingModel(modifiedFile.getName(), roles, model.eResource().getResourceSet());
-		}
-
-		if (model instanceof DatamartModel) {
-			DatamartModel datamartModel = (DatamartModel) model;
-			Set<URI> datamarts = new HashSet<>();
-			for (DatamartPackage datamartPkg : datamartModel.getPackages()) {
-				for (DatamartDefinition datamart : datamartPkg.getDefinitions()) {
-					datamarts.add(EcoreUtil.getURI(datamart));
-				}
-			}
-			touchReferencingModel(modifiedFile.getName(), datamarts, model.eResource().getResourceSet());
-		}
-		if (model instanceof ActionModel) {
-			ActionModel actionModel = (ActionModel) model;
-			Set<URI> actionToolbars = new HashSet<>();
-			for (ActionPackage actionPkg : actionModel.getPackages()) {
-				for (ActionToolbar actionToolbar : actionPkg.getToolbars()) {
-					actionToolbars.add(EcoreUtil.getURI(actionToolbar)); // NOSONAR
-				}
-			}
-			touchReferencingModel(modifiedFile.getName(), actionToolbars, model.eResource().getResourceSet());
-		}
-		if (model instanceof DataInterchangeModel) {
-			DataInterchangeModel dataInterchangeModel = (DataInterchangeModel) model;
-			Set<URI> dataInterchanges = new HashSet<>();
-			for (DataInterchangePackage actionPkg : dataInterchangeModel.getPackages()) {
-				for (DataInterchangeGroup group : actionPkg.getGroups()) {
-					for (DataInterchange dataInterchange : group.getDatInts()) {
-						dataInterchanges.add(EcoreUtil.getURI(dataInterchange)); // NOSONAR
-					}
-				}
-			}
-			touchReferencingModel(modifiedFile.getName(), dataInterchanges, model.eResource().getResourceSet());
-		}
-	}
-
-	protected boolean shouldTouch(IReferenceDescription t) {
-		URI sourceResourceURI = t.getSourceEObjectUri().trimFragment();
-		URI targetResourceURI = t.getTargetEObjectUri().trimFragment();
-		if (sourceResourceURI.equals(targetResourceURI)) {
-			// reference points to an EObject in same resource
-			return false;
-		}
-
-		// if defined in set to not touch
-		return !doNotTouchExtensions.contains(sourceResourceURI.fileExtension());
-	}
-
-	protected void touchReferencingModel(String triggerModel, Set<URI> items, ResourceSet sourceResourceSet) {
-		final List<IReferenceDescription> referenceTargets = new ArrayList<>();
-		referenceFinder.findAllReferences(items, new LocalResourceAccess(sourceResourceSet),
-				new IAcceptor<IReferenceDescription>() {
-					@Override
-					public void accept(IReferenceDescription t) {
-						if (shouldTouch(t) && !referenceTargets.contains(t)) {
-							referenceTargets.add(t);
-						}
-					}
-				}, null);
-
-		List<Resource> affectedResources = new LinkedList<>();
-		Map<IProject, XtextResourceSet> targetResourceSets = new HashMap<>();
-		for (IReferenceDescription desc : referenceTargets) {
-			IProject project = null;
-			Iterable<Pair<IStorage, IProject>> pairs = uriStorageMapper.getStorages(desc.getSourceEObjectUri());
-			if (pairs.iterator().hasNext()) {
-				project = pairs.iterator().next().getSecond();
-			}
-
-			if (project == null) {
-				LOGGER.error("No project could be found for {}", desc.getSourceEObjectUri());
-				continue;
-			}
-
-			XtextResourceSet resourceSet;
-			if (targetResourceSets.containsKey(project)) {
-				resourceSet = targetResourceSets.get(project);
-			} else {
-				resourceSet = (XtextResourceSet) rsProvider.get(getProject());
-				targetResourceSets.put(project, resourceSet);
-			}
-
-			// load the resource by the resource set
-			Resource modelResource = resourceSet.getResource(desc.getSourceEObjectUri().trimFragment(), true);
-			if (!affectedResources.contains(modelResource)) {
-				affectedResources.add(modelResource);
-			}
-		}
-		for (Resource resource : affectedResources) {
-			ModelExtenderUtils.writeResource(resource, triggerModel);
-		}
-	}
-
-	/**
-	 * Modifies the datamart model for which the changed entity model has a
-	 * tracking reference match.
-	 * 
-	 * @param modifiedFile
-	 */
-	protected void modifyModel(IFile modifiedFile) {
-		if (modifiedFile == null) {
-			return;
-		}
-
-		LEntityModel lEntityModel = loadSemanticModel(modifiedFile);
-		if (lEntityModel == null) {
-			LOGGER.error("Skipping datamart build since SemanticModel can not be loaded from file {}", modifiedFile.getName());
-			return;
-		}
-
-		Set<URI> entities = new HashSet<>();
-		for (LTypedPackage lPkg : lEntityModel.getPackages()) {
-			for (LType lType : lPkg.getTypes()) {
-				if (lType instanceof LEntity) {
-					entities.add(EcoreUtil.getURI(lType));
-				}
-			}
-		}
-
-		final List<IReferenceDescription> referenceTargets = new ArrayList<>();
-		referenceFinder.findAllReferences(entities, new LocalResourceAccess(lEntityModel.eResource().getResourceSet()),
-				new IAcceptor<IReferenceDescription>() {
-					@Override
-					public void accept(IReferenceDescription t) {
-						if (t.getEReference() == DatamartDSLPackage.Literals.DATAMART_ENTITY__ENTITY_REF) {
-							referenceTargets.add(t);
-						}
-					}
-				}, null);
-
-		List<Resource> affectedResources = new LinkedList<>();
-		Map<IProject, XtextResourceSet> datamartResourceSets = new HashMap<>();
-		for (IReferenceDescription desc : referenceTargets) {
-			IProject datamartProject = null;
-			Iterable<Pair<IStorage, IProject>> pairs = uriStorageMapper.getStorages(desc.getSourceEObjectUri());
-			if (pairs.iterator().hasNext()) {
-				datamartProject = pairs.iterator().next().getSecond();
-			}
-
-			if (datamartProject == null) {
-				LOGGER.error("No project could be found for {}", desc.getSourceEObjectUri());
-				continue;
-			}
-
-			XtextResourceSet resourceSet;
-			if (datamartResourceSets.containsKey(datamartProject)) {
-				resourceSet = datamartResourceSets.get(datamartProject);
-			} else {
-				resourceSet = (XtextResourceSet) rsProvider.get(getProject());
-				datamartResourceSets.put(datamartProject, resourceSet);
-			}
-
-			// load the resource by the resource set
-			Resource datamartModelResource = resourceSet.getResource(desc.getSourceEObjectUri().trimFragment(), true);
-			DatamartEntity dmEntity = (DatamartEntity) EObjectHelper.getSemanticElement(datamartModelResource,
-					desc.getSourceEObjectUri().fragment());
-
-			if (!dmEntity.isTracking()) {
-				continue;
-			}
-
-			if (!affectedResources.contains(datamartModelResource)) {
-				affectedResources.add(datamartModelResource);
-			}
-
-			// clear properties section
-			DatamartDSLFactory dmFactory = DatamartDSLFactory.eINSTANCE;
-			EList<DatamartAttribute> properties = dmEntity.getAttributes();
-			if (properties != null) {
-				properties.clear();
-				// rebuild properties section
-				for (LEntityAttribute eAttr : dmEntity.getEntityRef().getAllAttributes()) {
-					if (!eAttr.isId() && !eAttr.isUuid() && !eAttr.isVersion() && !(eAttr.getType() instanceof LBean)) {
-						DatamartAxis axis = dmFactory.createDatamartAxis();
-						axis.setName(AxisEnum.COLUMNS);
-						DatamartAttribute attribute = dmFactory.createDatamartAttribute();
-						attribute.setAggregated(false);
-						attribute.setScaled(false);
-						attribute.setAxis(axis);
-						attribute.setAttributeRef(eAttr);
-						properties.add(attribute);
-					}
-				}
-			}
-		}
-
-		for (Resource resource : affectedResources) {
-			ModelExtenderUtils.writeResource(resource, DATAMART_MODEL_EXTENSION);
-		}
-	}
-
-	/**
-	 * Returns the entityModel which is to be build, identified by file.
-	 * 
-	 * @param file
-	 * @return
-	 */
-	protected <A extends EObject> A loadSemanticModel(IFile file) {
-		LOGGER.info("loading {}", file.getName());
-		org.eclipse.emf.common.util.URI entityDSLURI = org.eclipse.emf.common.util.URI
-				.createPlatformResourceURI(file.getFullPath().toString(), false);
-		XtextResourceSet rs = (XtextResourceSet) rsProvider.get(getProject());
-		Resource entityResource = rs.getResource(entityDSLURI, true);
-		try {
-			entityResource.load(null);
-		} catch (IOException e) { // NOSONAR - is logged
-			LOGGER.error(e.getLocalizedMessage());
-			return null;
-		}
-		@SuppressWarnings("unchecked")
-		A entityModel = (A) EObjectHelper.getSemanticElement(entityResource);
-		LOGGER.info("finished loading {}", file.getName());
-		return entityModel;
-	}
-
-	/**
 	 * Returns true, if delta is from our projectentity is contained in
 	 * entityModel.
 	 * 
diff --git a/org.eclipse.osbp.ide.core.ui.softwarefactory/src/org/eclipse/osbp/ide/core/ui/softwarefactory/extender/ModelExtenderUtils.java b/org.eclipse.osbp.ide.core.ui.softwarefactory/src/org/eclipse/osbp/ide/core/ui/softwarefactory/extender/ModelExtenderUtils.java
index af74623..5653634 100644
--- a/org.eclipse.osbp.ide.core.ui.softwarefactory/src/org/eclipse/osbp/ide/core/ui/softwarefactory/extender/ModelExtenderUtils.java
+++ b/org.eclipse.osbp.ide.core.ui.softwarefactory/src/org/eclipse/osbp/ide/core/ui/softwarefactory/extender/ModelExtenderUtils.java
@@ -93,10 +93,8 @@
 	public static void writeResource(Resource resource, String id) {
 		StringBuilder errors = new StringBuilder();
 		try {
-			LOGGER.info("running diagnostic for {}", resource.getURI());
 			XtextResource xR = (XtextResource) resource;
 			if (xR.getErrors().isEmpty()) {
-				LOGGER.info("writing {}", id);
 				resource.save(SaveOptions.newBuilder().format().getOptions().toOptionsMap());
 			} else  {
 				for(org.eclipse.emf.ecore.resource.Resource.Diagnostic error:xR.getErrors()) {
@@ -107,7 +105,7 @@
 				LOGGER.error("model {} diagnosic error {}", resource.getURI(), errors);
 				LOGGER.error("****************************");
 			}
-			LOGGER.info("finished writing {}", resource.getURI());
+			LOGGER.info("****** finished writing {}", id);
 		} catch (IOException e) {
 			LOGGER.error("{}", e);
 		}