Skip to content
Snippets Groups Projects
Commit 206c9a7f authored by Sebastian Bergemann's avatar Sebastian Bergemann
Browse files

Added draft for hash creation in reuse (+ update detection)

Issue-Ref: 4149
Issue-Url: af3#4149




Signed-off-by: default avatarSebastian Bergemann <bergemann@fortiss.org>
parent ab311b33
No related branches found
No related tags found
1 merge request!181Added correct hash creation in reuse (+ update detection)
ReuseLibraryUIUtils.java 18f5bdc0a80ebdaab235f912217df85884e88d01 GREEN
ReuseLibraryUIUtils.java b5428afdfe9e56c8b1c0c0ff155e9e6a8e992451 YELLOW
ReuseLibraryViewUtils.java 34a852dc692ec56cb3e9fd8dcea99d64f31503b3 GREEN
......@@ -90,7 +90,8 @@ public class ReuseLibraryUIUtils {
*/
public static EObject checkForElementInLibrary(ReuseLibrary library, ReuseElementSpec referencingSpec,
String dialogTitle) {
EObject originalElementInLibrary = getElementInsideLibrary(library, referencingSpec);
String targetUuid = referencingSpec.getElementUUID();
EObject originalElementInLibrary = getElementInsideLibrary(library, targetUuid);
if (originalElementInLibrary == null) {
warnOfMissingReuseElem(referencingSpec, dialogTitle);
}
......
ReuseLibraryModelElementFactory.java 2e7e8f0b3b6a93225204bfbfc6223e28c2bd69b4 GREEN
ReuseLibraryUtils.java 0dce9ab39ccc7646c9cddad2bf882ec1e4cfd777 GREEN
ReuseLibraryModelElementFactory.java e563ea647ae80a99bb8f3b0c8cbf7149896049c1 YELLOW
ReuseLibraryUtils.java 986a0d6d07656aa565e9eb39a74969c527ec4e69 YELLOW
......@@ -16,6 +16,7 @@
package org.fortiss.tooling.ext.reuse.utils;
import static org.eclipse.emf.ecore.util.EcoreUtil.generateUUID;
import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtils.createReuseHashAsString;
import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtils.getReuseElementName;
import java.util.Date;
......@@ -78,7 +79,7 @@ public class ReuseLibraryModelElementFactory {
ReuseElementSpec spec = AF3ReuseFactory.eINSTANCE.createReuseElementSpec();
spec.setElementUUID(generateReuseUUID());
spec.setElementName(getReuseElementName(element));
spec.setElementHash("todo"); // TODO #4149 (low prio) create and use hash to detect updates
spec.setElementHash(createReuseHashAsString(element));
spec.setSourceLibUUID(sourceLibrary.getLibraryUUID());
spec.setSourceLibName(sourceLibrary.getName());
spec.setLastUpdate(new Date());
......
......@@ -248,6 +248,29 @@ public class ReuseLibraryUtils {
return "";
}
/**
* Returns the hash code string of the given reuse element for its origin inside
* a specific (given) {@link ReuseLibrary}. If it cannot be found, an empty
* {@link String} will be returned.
*
* @param element The target reuse element
* @param sourceLibrary The library of the original reuse element for the hash
* @return The requested hash code as string (or empty string)
*/
public static String getReuseElementHashForLibrary(EObject element, ReuseLibrary sourceLibrary) {
String libraryUuid = sourceLibrary.getLibraryUUID();
if (element instanceof IModelElement) {
List<ReuseElementSpec> specs = getAllReuseSpecs((IModelElement) element);
for (ReuseElementSpec spec : specs) {
String referenceLibraryUuid = spec.getSourceLibUUID();
if (referenceLibraryUuid.equals(libraryUuid)) {
return spec.getElementHash();
}
}
}
return "";
}
/**
* Returns the Last Update Date of the given reuse element as {link Date} as
* long as it has (already) a {@link ReuseElementSpec}. If this is not the case,
......@@ -317,7 +340,7 @@ public class ReuseLibraryUtils {
* @return A list of all found reuse specifications (or empty list)
*/
public static List<ReuseElementSpec> getAllReuseSpecs(IModelElement element) {
List<ReuseElementSpec> specs = new ArrayList<ReuseElementSpec>();
List<ReuseElementSpec> specs = new ArrayList<>();
for (IModelElementSpecification spec : element.getSpecifications()) {
if (spec instanceof ReuseElementSpec) {
// (cast is save due to instance check)
......@@ -434,7 +457,10 @@ public class ReuseLibraryUtils {
/**
* Returns the {@link ReuseLibrary} that is mentioned as source library in the
* first {link ReuseElementSpec} of the given reuse element. If no {link
* ReuseElementSpec} exists, the return will be null.
* ReuseElementSpec} exists, the return will be null. This method is faster than
* calling getAllSourceLibrariesOfElement() and returning the first library,
* because it does not fetch all libraries - only the one of the first {link
* ReuseElementSpec}.
*
* @param element The target reuse element
* @return The requested reuse library (or null)
......@@ -451,6 +477,29 @@ public class ReuseLibraryUtils {
return null;
}
/**
* Returns a list of {@link ReuseLibrary}s that are each mentioned as source
* library in the {link ReuseElementSpec}s of the given reuse element. If no
* {link ReuseElementSpec} exists, the return will be an empty list.
*
* @param element The target reuse element
* @return All found reuse libraries for the given element (or empty list)
*/
public static List<ReuseLibrary> getAllSourceLibrariesOfElement(EObject element) {
List<ReuseLibrary> foundLibraries = new ArrayList<>();
if (element instanceof IModelElement) {
List<ReuseElementSpec> reuseSpecs = getAllReuseSpecs((IModelElement) element);
for (ReuseElementSpec reuseSpec : reuseSpecs) {
String libraryUUID = reuseSpec.getSourceLibUUID();
ReuseLibrary sourceLibrary = getLocalReuseLibraryByID(libraryUUID);
if (sourceLibrary != null) {
foundLibraries.add(sourceLibrary);
}
}
}
return foundLibraries;
}
/**
* Creates a new {@link ReuseLibrary} with the given name. If no name is
* received, the default library name will be used.
......@@ -471,16 +520,16 @@ public class ReuseLibraryUtils {
* referenced by the given {@link ReuseElementSpec}. Returns null if it could
* not be found.
*
* @param library The target reuse library
* @param referencingSpec The reuse specification used as link/trace/reference
* @param library The target reuse library
* @param elementUUID The UUID string of the requested element
* @return The requested reuse element (or null)
*/
public static EObject getElementInsideLibrary(ReuseLibrary library, ReuseElementSpec referencingSpec) {
public static EObject getElementInsideLibrary(ReuseLibrary library, String elementUUID) {
// find original element inside the library by identical UUID
for (EObject libraryElement : library.getReuseElementList()) {
for (IModelElementSpecification spec : ((IModelElement) libraryElement).getSpecifications()) {
if (spec instanceof ReuseElementSpec) {
if (((ReuseElementSpec) spec).getElementUUID().equals(referencingSpec.getElementUUID())) {
if (((ReuseElementSpec) spec).getElementUUID().equals(elementUUID)) {
return libraryElement;
}
}
......@@ -530,6 +579,7 @@ public class ReuseLibraryUtils {
// needs to be updated and set up
referencingSpec.setLastUpdate(new Date());
referencingSpec.setElementName(newName);
referencingSpec.setElementHash(createReuseHashAsString(newElement));
EObject newLibraryElement = copy(newElement);
setReuseElementName(newLibraryElement, newName);
......@@ -801,4 +851,59 @@ public class ReuseLibraryUtils {
}
}
/**
* Creates a hash code for the given {@link EObject} as reuse element and
* returns it as {@link String}. This has code format is used for detecting
* changes between reuse elements with the same origin. Returns null if given
* element is invalid (e.g. not a reuse element).
*
* @param element The element of which a hash code should be created
* @return String of the created hash code integer (or null for an invalid
* input)
*/
public static String createReuseHashAsString(EObject element) {
if (element != null) {
// TODO hashCode() is not enough for component differences
// maybe use service to get hash functions for all possible reuse elements
// (components, etc.)
int hash = element.hashCode();
String hashString = String.valueOf(hash);
return hashString;
}
return null;
}
/**
* Checks whether the given {@link EObject} has an origin in a
* {@link ReuseLibrary} and then returns true if the origin has a different hash
* code than the reuse (given) element (meaning that its origin in the library
* was updated since its reuse). If the given {@link EObject} has no connection
* to a {@link ReuseLibrary} or the original element in the library is still
* identical, the function will return false.
*
* @param element The element whose reuse origin should be checked for updates
* @return True if the given element has a reuse origin and this origin was
* updated in the meantime, otherwise false
*/
public static List<EObject> getAllUpdatedOrigins(EObject element) {
// TODO: use this method for detecting updates and informing user
List<EObject> updatedOrigins = new ArrayList<>();
List<ReuseLibrary> foundLibraries = getAllSourceLibrariesOfElement(element);
String uuid = getReuseElementUUID(element);
for (ReuseLibrary sourceLibrary : foundLibraries) {
EObject originElement = getElementInsideLibrary(sourceLibrary, uuid);
String storedHashForThisLibrary = getReuseElementHashForLibrary(element, sourceLibrary);
String currentHashForThisLibrary = getReuseElementHashForLibrary(originElement, sourceLibrary);
// if hashes are valid but different, the origin was updated
if (!storedHashForThisLibrary.isEmpty() && !currentHashForThisLibrary.isEmpty()) {
if (!storedHashForThisLibrary.equals(currentHashForThisLibrary)) {
updatedOrigins.add(originElement);
}
}
}
return updatedOrigins;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment