Skip to content
This repository has been archived by the owner on Mar 21, 2022. It is now read-only.

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
JWegert committed May 10, 2018
1 parent bafeaaa commit 6465f17
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,10 @@ public class Application extends ResourceConfig {

public Application() {

api.registerAllCoreModels();

// GenericTypeFinder.typeMap.putIfAbsent("Timestamp", Timestamp.class);
// GenericTypeFinder.typeMap.putIfAbsent("Landscape", Landscape.class);
// GenericTypeFinder.typeMap.putIfAbsent("System",
// net.explorviz.model.landscape.System.class);
// GenericTypeFinder.typeMap.putIfAbsent("NodeGroup", NodeGroup.class);
// GenericTypeFinder.typeMap.putIfAbsent("Node", Node.class);
// GenericTypeFinder.typeMap.putIfAbsent("Application", Application.class);
// GenericTypeFinder.typeMap.putIfAbsent("Component", Component.class);
// GenericTypeFinder.typeMap.putIfAbsent("Clazz", Clazz.class);
// GenericTypeFinder.typeMap.putIfAbsent("ClazzCommunication",
// ClazzCommunication.class);
// GenericTypeFinder.typeMap.putIfAbsent("ApplicationCommunication",
// ApplicationCommunication.class);
// GenericTypeFinder.typeMap.putIfAbsent("AggregatedClazzCommunication",
// AggregatedClazzCommunication.class);
// GenericTypeFinder.typeMap.putIfAbsent("CumulatedClazzCommunication",
// CumulatedClazzCommunication.class);
// // GenericTypeFinder.typeMap.putIfAbsent("CommunicationAccumulator",
// // CommunicationAccumulator.class);
// // GenericTypeFinder.typeMap.putIfAbsent("CommunicationTileAccumulator",
// // CommunicationTileAccumulator.class);
// GenericTypeFinder.typeMap.putIfAbsent("DatabaseQuery", DatabaseQuery.class);
// GenericTypeFinder.typeMap.putIfAbsent("User", User.class);

// register the models that you wan't to parse to JSONAPI-conform JSON,
// i.e. exchange with frontend
api.registerAllCoreModels();
final ResourceConverterFactory factory = new ResourceConverterFactory();
// factory.registerClass(ComparisonModel.class);
// factory.registerClass(SubComparisonModel.class);

final AbstractBinder dependencyBinder = new ExtensionDependencyInjectionBinder();
dependencyBinder.bindFactory(factory).to(ResourceConverter.class).in(Singleton.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import net.explorviz.extension.comparison.repository.Merger;

/**
* This flag is attached to each entity of the meta-model. It shows whether an
* entity is modified. This information is needed for building the
* comparing-application object {@link Merger}.(static view) Plus it holds the
* difference of class instances and communication requests between the two
* models.(dynamic view)
* This flag is attached to each entity of the data model. It shows whether an
* entity is modified. This information is needed for building the merged
* application object {@link Merger}.
*
* @author josw
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
import org.slf4j.LoggerFactory;

import net.explorviz.api.ExtensionAPIImpl;
import net.explorviz.extension.comparison.resources.LandscapeResourceComparing;
import net.explorviz.model.application.Application;
import net.explorviz.model.landscape.Landscape;
import net.explorviz.model.landscape.Node;
import net.explorviz.model.landscape.NodeGroup;
import net.explorviz.server.main.Configuration;

/**
* Service for fetching landscapes (merged and replay) that is used by
* {@link LandscapeResourceComparing}.
*
* @author josw
*
*/
public class LandscapeFetchService {

static final Logger logger = LoggerFactory.getLogger(LandscapeFetchService.class.getName());
static final Logger LOGGER = LoggerFactory.getLogger(LandscapeFetchService.class.getName());
private static LandscapeFetchService instance;

private final ExtensionAPIImpl extensionApi = ExtensionAPIImpl.getInstance();
Expand All @@ -34,8 +37,15 @@ public Landscape fetchLandscapeForComparison(final long timestamp) {
return extensionApi.getLandscape(timestamp, Configuration.REPLAY_REPOSITORY);
}

// Right now this works for two landscapes, this can be extended to more than
// two landscapes in the future
/**
* Takes two {@link Landscape}s and builds one merged {@link Landscape}, i.e.
* {@link Landscape} with merged {@link Application}s.
*
* @param firstLandscape
* @param secondLandscape
* @return merged {@link Landscape}, i.e. {@link Landscape} with merged
* {@link Application}s
*/
public Landscape fetchMergedLandscape(final Landscape firstLandscape, final Landscape secondLandscape) {
final Landscape mergedLandscape = secondLandscape;
Application mergedApp = null;
Expand All @@ -48,7 +58,7 @@ public Landscape fetchMergedLandscape(final Landscape firstLandscape, final Land

final Application app1 = appContained(firstLandscape, app2Name);
if (app1 == null) {
logger.error(
LOGGER.error(
"You can not compare two complete different applications. The application {} is not contained in the other landscape.",
app2Name);
} else {
Expand All @@ -65,6 +75,13 @@ public Landscape fetchMergedLandscape(final Landscape firstLandscape, final Land
return mergedLandscape;
}

/**
* Searches {@link Application} with fully qualified name (appName).
*
* @param landscape
* @param appName
* @return if {@link Application} with name exists: return it, else: null
*/
private Application appContained(final Landscape landscape, final String appName) {
Application app = new Application();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.explorviz.extension.comparison.model.Status;
import net.explorviz.extension.comparison.util.EntityComparison;
import net.explorviz.extension.comparison.util.MergerHelper;
import net.explorviz.extension.comparison.util.PrepareForMerger;
import net.explorviz.model.application.AggregatedClazzCommunication;
import net.explorviz.model.application.Application;
import net.explorviz.model.application.Clazz;
Expand All @@ -19,15 +20,14 @@
/**
* Provides methods to merge two {@link Application}s.
*
* @author jweg
* @author josw
*
*/

public class Merger {

static final Logger logger = LoggerFactory.getLogger(Merger.class.getName());
static final Logger LOGGER = LoggerFactory.getLogger(Merger.class.getName());
private final EntityComparison entityComparison = new EntityComparison();
private final MergerHelper mergerHelper = new MergerHelper();

/**
* Takes two {@link Application}s and merges them into a new
Expand All @@ -52,29 +52,30 @@ public Application appMerge(final Application appVersion1, final Application app
final Application mergedApp = appVersion2;

final List<Component> componentsVersion1 = appVersion1.getComponents();
List<Component> flatComponentsFrom1 = mergerHelper.createFlatComponents(componentsVersion1);
List<Component> flatComponentsFrom1 = MergerHelper.createFlatComponents(componentsVersion1);

final List<Component> mergedComponents = mergedApp.getComponents();
List<Component> flatMergedComponents = mergerHelper.createFlatComponents(mergedComponents);
List<Component> flatMergedComponents = MergerHelper.createFlatComponents(mergedComponents);

// check, if components and clazzes from version 2 exist in version 1 (possible
// status: ORIGINAL, ADDED, EDITED)
checkComponentsAndClazzesAddedAndEdited(mergedComponents, flatComponentsFrom1);

// check, if components from version 1 exist in version 2 (possible
// status: ORIGINAL, DELETED)
checkComponentsDeleted(componentsVersion1, flatMergedComponents);

// check, if clazzes from version 1 exist in version 2 (possible
// status: ORIGINAL, DELETED)
final List<Clazz> flatClazzesFrom1 = mergerHelper
.createFlatClazzes(mergerHelper.createFlatComponents(appVersion1.getComponents()));
flatMergedComponents = mergerHelper.createFlatComponents(mergedApp.getComponents());
final List<Clazz> flatMergedClazzes = mergerHelper.createFlatClazzes(flatMergedComponents);
final List<Clazz> flatClazzesFrom1 = MergerHelper
.createFlatClazzes(MergerHelper.createFlatComponents(appVersion1.getComponents()));
flatMergedComponents = MergerHelper.createFlatComponents(mergedApp.getComponents());
final List<Clazz> flatMergedClazzes = MergerHelper.createFlatClazzes(flatMergedComponents);
checkClazzesDeleted(flatClazzesFrom1, flatMergedClazzes, flatMergedComponents);

flatComponentsFrom1 = mergerHelper.createFlatComponents(appVersion1.getComponents());
setDiffInstanceCountForMergedClazzes(mergerHelper.createFlatComponents(mergedApp.getComponents()),
mergerHelper.createFlatClazzes(flatComponentsFrom1));
// set difference of instance count
flatComponentsFrom1 = MergerHelper.createFlatComponents(appVersion1.getComponents());
setDiffInstanceCountForMergedClazzes(MergerHelper.createFlatComponents(mergedApp.getComponents()),
MergerHelper.createFlatClazzes(flatComponentsFrom1));

/** merge communication between clazzes */
final List<AggregatedClazzCommunication> aggregatedCommunications1 = appVersion1
Expand All @@ -97,7 +98,8 @@ public Application appMerge(final Application appVersion1, final Application app
* List<{@link Component}> with {@link Component}s (incl. children
* and clazzes) from version 2
* @param flatComponentsFrom1
* List<{@link Component}> with {@link Component}s from version 1
* List<{@link Component}> with {@link Component}s from version 1,
* flat for easier searching
*/

private void checkComponentsAndClazzesAddedAndEdited(final List<Component> mergedComponents,
Expand All @@ -113,8 +115,8 @@ private void checkComponentsAndClazzesAddedAndEdited(final List<Component> merge

if (mergedComponentIn1 == null) {
// case: mergedComponent does not exist in version 1 -> status ADDED
// status of clazzes in component: ADDED
mergerHelper.setStatusComponentCLazzesAndChildren(mergedComponent, Status.ADDED);
// status of clazzes and children in component: ADDED
MergerHelper.setStatusComponentCLazzesAndChildren(mergedComponent, Status.ADDED);
} else {
// case: mergedComponent does exist in version 1 -> status EDITED or ORIGINAL
// check EDITED
Expand Down Expand Up @@ -208,22 +210,19 @@ private void checkComponentsDeleted(final List<Component> componentsVersion1,
*/
private void createDeletedComponent(final Component component1, final List<Component> flatMergedComponents) {
final Component parent = component1.getParentComponent();
final boolean isRootComponent;
boolean isRootComponent;

if (parent == null) {
isRootComponent = true;
} else {
isRootComponent = false;
}
;

String parentFullName;
Component parentInMerged = null;

final Component newMergedComponent = component1;
// newMergedComponent.getExtensionAttributes().put(PrepareForMerger.STATUS,
// Status.DELETED);
mergerHelper.setStatusComponentCLazzesAndChildren(newMergedComponent, Status.DELETED);
MergerHelper.setStatusComponentCLazzesAndChildren(newMergedComponent, Status.DELETED);

if (isRootComponent) {
// case: component has no parent -> add newMergedComponent to components of
Expand All @@ -241,7 +240,7 @@ private void createDeletedComponent(final Component component1, final List<Compo
parentInMerged.getChildren().add(newMergedComponent);
newMergedComponent.setParentComponent(parentInMerged);
} else {
logger.error("parent of deleted component {} not found in merged component list.",
LOGGER.error("parent of deleted component {} not found in merged component list.",
component1.getFullQualifiedName());
}
}
Expand Down Expand Up @@ -280,7 +279,7 @@ private void checkClazzesDeleted(final List<Clazz> flatClazzesFrom1, final List<
parentInMerged.getClazzes().add(clazzFrom1);
clazzFrom1.setParent(parentInMerged);
} else {
logger.error("parent of deleted clazz {} not found in merged component list.",
LOGGER.error("parent of deleted clazz {} not found in merged component list.",
clazzFrom1.getFullQualifiedName());
}
}
Expand Down Expand Up @@ -310,7 +309,7 @@ private void setDiffInstanceCountForMergedClazzes(final List<Component> flatMerg
.filter(c -> clazz.getFullQualifiedName().equals(c.getFullQualifiedName())).findFirst()
.orElse(null);
if (clazzIn1 == null) {
logger.error("instanceCount: clazz {} with status ORIGINAL not found in clazzFrom1 list.",
LOGGER.error("instanceCount: clazz {} with status ORIGINAL not found in clazzFrom1 list.",
clazz.getFullQualifiedName());
} else {
clazz.getExtensionAttributes().put(PrepareForMerger.DIFF_INSTANCE_COUNT,
Expand All @@ -330,7 +329,7 @@ private void setDiffInstanceCountForMergedClazzes(final List<Component> flatMerg
* {@link CumulatedClazzCommunication}s. This method is used by
* {@link Merger#appMerge(Application, Application)}. Two
* {@link ClazzCommunication}s are identical, if they have the same source and
* target {@link Application} and the same methodName.
* target {@link Application} and the same operation name.
*
* @param aggregatedCommunications1
* list of {@link AggregatedClazzCommunication}s
Expand Down Expand Up @@ -364,7 +363,8 @@ private List<CumulatedClazzCommunication> checkCommunicationAll(
if (communication2ContainedIn1 != null) {
// communication is contained in version 1 and in version 2, thus the
// default status is not changed
// marked that communication exists in both versions, used for detection of
// add label, because communication exists in both versions, used for detection
// of
// deleted communication
communication2ContainedIn1.getExtensionAttributes().put("exists", true);

Expand All @@ -373,8 +373,8 @@ private List<CumulatedClazzCommunication> checkCommunicationAll(
}

}
// deleted communication exists in version one, but not in version two and is
// marked with "exists"
// deleted communication exists in version one, but not in version two and has
// label "exists"
final List<ClazzCommunication> deletedCommunications = clazzCommunications1.stream()
.filter(c1 -> c1.getSourceClazz().getFullQualifiedName()
.equals(aggregatedCommunication2.getSourceClazz().getFullQualifiedName()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@
import javax.ws.rs.Produces;

import net.explorviz.extension.comparison.repository.LandscapeFetchService;
import net.explorviz.model.application.Application;
import net.explorviz.model.landscape.Landscape;
import net.explorviz.server.security.Secured;

/**
* REST resource that provides a {@link Landscape} with merged
* {@link Application}s to the frontend.
*
* @author josw
*
*/
@Secured
@Path("/landscape")
public class LandscapeResourceComparing {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import net.explorviz.model.application.Component;

/**
* Class for methods that compare elements from the data model, e.g.
* Provides methods that compare elements from the data model, e.g.,
* {@link Component}, {@link Communication}.
*
* @author josw
Expand All @@ -15,9 +15,9 @@
public class EntityComparison {

/**
* Two {@link Component}s are identical, if they have the same
* fullQualifiedName, the same children and {@link Clazz}es. The children and
* {@link Clazz}es may be empty.
* Two {@link Component}s are identical, if they have the same fully qualified
* name, the same children and {@link Clazz}es. The children and {@link Clazz}es
* may be empty.
*
* @param component1
* @param component2
Expand All @@ -31,7 +31,7 @@ public boolean componentsIdentical(final Component component1, final Component c
final List<Component> children1 = component1.getChildren();
final List<Component> children2 = component2.getChildren();

// fullQualifiedNames are equal -> was already checked before
// fully qualified names are equal -> was already checked before
if (clazzes1.size() == clazzes2.size()) {
if (clazzesEqual(clazzes1, clazzes2)) {
if ((children1.size() == children2.size())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import net.explorviz.extension.comparison.model.Status;
import net.explorviz.extension.comparison.repository.Merger;
import net.explorviz.extension.comparison.repository.PrepareForMerger;
import net.explorviz.model.application.AggregatedClazzCommunication;
import net.explorviz.model.application.Clazz;
import net.explorviz.model.application.ClazzCommunication;
Expand All @@ -17,7 +16,12 @@
* @author josw
*
*/
public class MergerHelper {
public final class MergerHelper {

// because this is a utility class
private MergerHelper() {

}

/**
* Set the status of the {@link Component}, child{@link Component}s and all
Expand All @@ -26,7 +30,7 @@ public class MergerHelper {
* @param component
* @param status
*/
public void setStatusComponentCLazzesAndChildren(final Component component, final Status status) {
public static void setStatusComponentCLazzesAndChildren(final Component component, final Status status) {
component.getExtensionAttributes().put(PrepareForMerger.STATUS, status);
if (!component.getClazzes().isEmpty()) {
for (final Clazz clazz : component.getClazzes()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.explorviz.extension.comparison.repository;
package net.explorviz.extension.comparison.util;

import java.util.List;

Expand All @@ -19,13 +19,17 @@
* @author jweg
*
*/
public class PrepareForMerger {
public final class PrepareForMerger {

public static final String STATUS = "status";
public static final Status DEFAULT_STATUS = Status.ORIGINAL;
public static final String DIFF_INSTANCE_COUNT = "diffInstanceCount";
public static final Integer DEFAULT_DIFF_INSTANCE_COUNT = 0;

// because this is a utility class
private PrepareForMerger() {
}

/**
* Adds a {@link Status} attribute to every element in the {@link Application}.
* This serves as preparation for merging two {@link Application}s.
Expand Down
Loading

0 comments on commit 6465f17

Please sign in to comment.