Skip to content
Snippets Groups Projects
Commit 76b90e1c authored by Andreas Bayha's avatar Andreas Bayha
Browse files

Merge branch '4344' into 'master'

Add preparation possibility for reuse providers (incl. specs cleaning)

Closes af3#4344

See merge request !218
parents ad2f0c1d 69b7b1e8
No related branches found
No related tags found
1 merge request!218Add preparation possibility for reuse providers (incl. specs cleaning)
Showing with 383 additions and 243 deletions
...@@ -13,7 +13,8 @@ Bundle-Activator: org.fortiss.tooling.ext.reuse.ToolingReuseActivator ...@@ -13,7 +13,8 @@ Bundle-Activator: org.fortiss.tooling.ext.reuse.ToolingReuseActivator
Require-Bundle: org.eclipse.core.runtime, Require-Bundle: org.eclipse.core.runtime,
org.eclipse.emf.ecore;visibility:=reexport, org.eclipse.emf.ecore;visibility:=reexport,
org.fortiss.tooling.base;visibility:=reexport, org.fortiss.tooling.base;visibility:=reexport,
org.fortiss.tooling.kernel;visibility:=reexport org.fortiss.tooling.kernel;visibility:=reexport,
org.fortiss.tooling.ext.variability;bundle-version="2.24.0"
Export-Package: org.fortiss.tooling.ext.reuse.model, Export-Package: org.fortiss.tooling.ext.reuse.model,
org.fortiss.tooling.ext.reuse.model.impl, org.fortiss.tooling.ext.reuse.model.impl,
org.fortiss.tooling.ext.reuse.model.util, org.fortiss.tooling.ext.reuse.model.util,
......
IReuseProvider.java 51dd0df5954bff99b80244cae94403300c4aadb1 GREEN IReuseProvider.java 18d293f7f1f072883188f16fb6eeb52c8d6042cf GREEN
IReuseProviderService.java 6cf3ef4f67ab7515a1a78df2448439e8d6d97737 GREEN IReuseProviderService.java bcc70de0bb8d39c330e3a25d884abc7092dc1b7e GREEN
LayoutedReuseProviderBase.java b0e4ce3cda818b0723ec37b925a4c4c3d0c41909 GREEN LayoutedReuseProviderBase.java b0e4ce3cda818b0723ec37b925a4c4c3d0c41909 GREEN
ReuseProviderBase.java 0ee51474bda060f3d326094397bc0a7fd7247152 GREEN ReuseProviderBase.java ac47f5ecafed0bdef6ef5183c7194aa756f723fd GREEN
ReuseProviderService.java 748eea63e16d286f1738aa7091219dc63e7ad16e GREEN ReuseProviderService.java c4ef33283002d6dac6167f9c6c8f71d2c2ce39d1 GREEN
/*-------------------------------------------------------------------------+ /*-------------------------------------------------------------------------+
| Copyright 2021 fortiss GmbH | | Copyright 2023 fortiss GmbH |
| | | |
| Licensed under the Apache License, Version 2.0 (the "License"); | | Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. | | you may not use this file except in compliance with the License. |
...@@ -26,19 +26,32 @@ import org.fortiss.tooling.kernel.service.base.IEObjectAware; ...@@ -26,19 +26,32 @@ import org.fortiss.tooling.kernel.service.base.IEObjectAware;
* *
* @author bergemann * @author bergemann
* *
* @param <T> The subclass of {@link EObject} for which this provider shall be * @param <T>
* The subclass of {@link EObject} for which this provider shall be
* invoked. * invoked.
*/ */
public interface IReuseProvider<T extends EObject> extends IEObjectAware<T> { public interface IReuseProvider<T extends EObject> extends IEObjectAware<T> {
/**
* Prepares the given <T> for its storage in a reuse library. This might e.g. include cleaning
* it from attributes that are not needed anymore when "exported" into a library.
*
* @param element
* The <T> that should be prepared.
* @return True if preparation was successful, otherwise false.
*/
public boolean prepareReuseElementForLibrary(T element);
/** /**
* Updates the first given <T>, which is a reuse element, based on the second * Updates the first given <T>, which is a reuse element, based on the second
* given <T>. The update is a replacement of the old element with the new one, * given <T>. The update is a replacement of the old element with the new one,
* while also all references in depending elements are updated/replaced. * while also all references in depending elements are updated/replaced.
* *
* @param elementToBeUpdated The <T> that should be updated/replaced. * @param elementToBeUpdated
* @param newElement The <T> with which the first one should be replaced * The <T> that should be updated/replaced.
* (updated). * @param newElement
* The <T> with which the first one should be replaced
* (updated).
* @return True if update was successful, otherwise false * @return True if update was successful, otherwise false
*/ */
public boolean updateReuseElement(T elementToBeUpdated, T newElement); public boolean updateReuseElement(T elementToBeUpdated, T newElement);
...@@ -46,8 +59,10 @@ public interface IReuseProvider<T extends EObject> extends IEObjectAware<T> { ...@@ -46,8 +59,10 @@ public interface IReuseProvider<T extends EObject> extends IEObjectAware<T> {
/** /**
* Sets the name of the given <T> used for reuse with the given {@link String}. * Sets the name of the given <T> used for reuse with the given {@link String}.
* *
* @param reuseElement The <T> whose name should be set. * @param reuseElement
* @param name The {@link String} for the name. * The <T> whose name should be set.
* @param name
* The {@link String} for the name.
* @return True if update was successful, otherwise false * @return True if update was successful, otherwise false
*/ */
public boolean setReuseElementName(T reuseElement, String name); public boolean setReuseElementName(T reuseElement, String name);
...@@ -55,7 +70,8 @@ public interface IReuseProvider<T extends EObject> extends IEObjectAware<T> { ...@@ -55,7 +70,8 @@ public interface IReuseProvider<T extends EObject> extends IEObjectAware<T> {
/** /**
* Gets the name of the given <T> used for reuse. * Gets the name of the given <T> used for reuse.
* *
* @param reuseElement The <T> whose name is requested. * @param reuseElement
* The <T> whose name is requested.
* @return The name as {@link String}. * @return The name as {@link String}.
*/ */
public String getReuseElementName(T reuseElement); public String getReuseElementName(T reuseElement);
...@@ -64,8 +80,10 @@ public interface IReuseProvider<T extends EObject> extends IEObjectAware<T> { ...@@ -64,8 +80,10 @@ public interface IReuseProvider<T extends EObject> extends IEObjectAware<T> {
* Sets the comment of the given <T> used for reuse with the given * Sets the comment of the given <T> used for reuse with the given
* {@link String}. * {@link String}.
* *
* @param reuseElement The <T> whose name should be set. * @param reuseElement
* @param comment The {@link String} for the comment. * The <T> whose name should be set.
* @param comment
* The {@link String} for the comment.
* @return True if update was successful, otherwise false * @return True if update was successful, otherwise false
*/ */
public boolean setReuseElementComment(T reuseElement, String comment); public boolean setReuseElementComment(T reuseElement, String comment);
...@@ -73,7 +91,8 @@ public interface IReuseProvider<T extends EObject> extends IEObjectAware<T> { ...@@ -73,7 +91,8 @@ public interface IReuseProvider<T extends EObject> extends IEObjectAware<T> {
/** /**
* Gets the comment of the given <T>} used for reuse. * Gets the comment of the given <T>} used for reuse.
* *
* @param reuseElement The <T> whose name is requested. * @param reuseElement
* The <T> whose name is requested.
* @return The comment as {@link String}. * @return The comment as {@link String}.
*/ */
public String getReuseElementComment(T reuseElement); public String getReuseElementComment(T reuseElement);
......
/*-------------------------------------------------------------------------+ /*-------------------------------------------------------------------------+
| Copyright 2021 fortiss GmbH | | Copyright 2023 fortiss GmbH |
| | | |
| Licensed under the Apache License, Version 2.0 (the "License"); | | Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. | | you may not use this file except in compliance with the License. |
...@@ -36,7 +36,7 @@ public interface IReuseProviderService extends IIntrospectiveKernelService { ...@@ -36,7 +36,7 @@ public interface IReuseProviderService extends IIntrospectiveKernelService {
/** /**
* Retrieves the singleton instance of {@link ReuseProviderService}. * Retrieves the singleton instance of {@link ReuseProviderService}.
* *
* @return The singleton instance of this service * @return The singleton instance of this service.
*/ */
public static IReuseProviderService getInstance() { public static IReuseProviderService getInstance() {
return ReuseProviderService.getInstance(); return ReuseProviderService.getInstance();
...@@ -45,23 +45,38 @@ public interface IReuseProviderService extends IIntrospectiveKernelService { ...@@ -45,23 +45,38 @@ public interface IReuseProviderService extends IIntrospectiveKernelService {
/** /**
* Registers the given {@link IReuseProvider} in this service. * Registers the given {@link IReuseProvider} in this service.
* *
* @param provider The {@link IReuseProvider} to register. * @param provider
* @param cls The subclass of {@link EObject} the given provider shall be * The {@link IReuseProvider} to register.
* used for. * @param cls
* The subclass of {@link EObject} the given provider shall be
* used for.
*/ */
public <T extends EObject> void registerReuseProvider(IReuseProvider<T> provider, Class<T> cls); public <T extends EObject> void registerReuseProvider(IReuseProvider<T> provider, Class<T> cls);
/**
* Prepares the given reuse element as {@link EObject} for storage in a reuse library. This
* might include e.g. cleaning the reuse element from all attributes that are not needed anymore
* when "exported" into a library.
*
* @param reuseElement
* The reuse element that should be prepared.
* @return True if preparation was successful, otherwise false.
*/
public boolean prepareReuseElementForLibrary(EObject reuseElement);
/** /**
* Updates the first given {@link EObject}, which is a reuse element, based on * Updates the first given {@link EObject}, which is a reuse element, based on
* the second given {@link EObject}. The update is a replacement of the old * the second given {@link EObject}. The update is a replacement of the old
* element with the new one, while also all references in depending elements are * element with the new one, while also all references in depending elements are
* updated/replaced. * updated/replaced.
* *
* @param elementToBeUpdated The {@link EObject} that should be * @param elementToBeUpdated
* updated/replaced. * The {@link EObject} that should be
* @param newElement The {@link EObject} with which the first one should * updated/replaced.
* be replaced (updated). * @param newElement
* @return True if update was successful, otherwise false * The {@link EObject} with which the first one should
* be replaced (updated).
* @return True if update was successful, otherwise false.
*/ */
public boolean updateReuseElement(EObject elementToBeUpdated, EObject newElement); public boolean updateReuseElement(EObject elementToBeUpdated, EObject newElement);
...@@ -69,16 +84,19 @@ public interface IReuseProviderService extends IIntrospectiveKernelService { ...@@ -69,16 +84,19 @@ public interface IReuseProviderService extends IIntrospectiveKernelService {
* Sets the name of the given {@link EObject} used for reuse with the given * Sets the name of the given {@link EObject} used for reuse with the given
* {@link String}. * {@link String}.
* *
* @param reuseElement The {@link EObject} whose name should be set. * @param reuseElement
* @param name The {@link String} for the name. * The {@link EObject} whose name should be set.
* @return True if update was successful, otherwise false * @param name
* The {@link String} for the name.
* @return True if update was successful, otherwise false.
*/ */
public boolean setReuseElementName(EObject reuseElement, String name); public boolean setReuseElementName(EObject reuseElement, String name);
/** /**
* Gets the name of the given {@link EObject} used for reuse. * Gets the name of the given {@link EObject} used for reuse.
* *
* @param reuseElement The {@link EObject} whose name is requested. * @param reuseElement
* The {@link EObject} whose name is requested.
* @return The name as {@link String}. * @return The name as {@link String}.
*/ */
public String getReuseElementName(EObject reuseElement); public String getReuseElementName(EObject reuseElement);
...@@ -87,16 +105,19 @@ public interface IReuseProviderService extends IIntrospectiveKernelService { ...@@ -87,16 +105,19 @@ public interface IReuseProviderService extends IIntrospectiveKernelService {
* Sets the comment of the given {@link EObject} used for reuse with the given * Sets the comment of the given {@link EObject} used for reuse with the given
* {@link String}. * {@link String}.
* *
* @param reuseElement The {@link EObject} whose name should be set. * @param reuseElement
* @param comment The {@link String} for the comment. * The {@link EObject} whose name should be set.
* @return True if update was successful, otherwise false * @param comment
* The {@link String} for the comment.
* @return True if update was successful, otherwise false.
*/ */
public boolean setReuseElementComment(EObject reuseElement, String comment); public boolean setReuseElementComment(EObject reuseElement, String comment);
/** /**
* Gets the comment of the given {@link EObject} used for reuse. * Gets the comment of the given {@link EObject} used for reuse.
* *
* @param reuseElement The {@link EObject} whose name is requested. * @param reuseElement
* The {@link EObject} whose name is requested.
* @return The comment as {@link String}. * @return The comment as {@link String}.
*/ */
public String getReuseElementComment(EObject reuseElement); public String getReuseElementComment(EObject reuseElement);
......
/*-------------------------------------------------------------------------+ /*-------------------------------------------------------------------------+
| Copyright 2021 fortiss GmbH | | Copyright 2023 fortiss GmbH |
| | | |
| Licensed under the Apache License, Version 2.0 (the "License"); | | Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. | | you may not use this file except in compliance with the License. |
...@@ -16,12 +16,17 @@ ...@@ -16,12 +16,17 @@
package org.fortiss.tooling.ext.reuse.service; package org.fortiss.tooling.ext.reuse.service;
import static org.eclipse.emf.ecore.util.EcoreUtil.replace; import static org.eclipse.emf.ecore.util.EcoreUtil.replace;
import static org.fortiss.tooling.ext.variability.util.VariabilityUtils.getOptVarPointSpecification;
import static org.fortiss.tooling.kernel.utils.EcoreUtils.replaceEObjectReferences; import static org.fortiss.tooling.kernel.utils.EcoreUtils.replaceEObjectReferences;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EObject;
import org.fortiss.tooling.base.model.element.IHierarchicElement;
import org.fortiss.tooling.base.model.element.IHierarchicElementContainer;
import org.fortiss.tooling.base.model.element.IModelElement;
import org.fortiss.tooling.ext.variability.model.OptionalVariationPointSpecification;
import org.fortiss.tooling.kernel.model.INamedCommentedElement; import org.fortiss.tooling.kernel.model.INamedCommentedElement;
import org.fortiss.tooling.kernel.model.INamedElement; import org.fortiss.tooling.kernel.model.INamedElement;
...@@ -31,11 +36,21 @@ import org.fortiss.tooling.kernel.model.INamedElement; ...@@ -31,11 +36,21 @@ import org.fortiss.tooling.kernel.model.INamedElement;
* *
* @author bergemann * @author bergemann
* *
* @param <T> The subclass of {@link EObject} for which this provider base shall * @param <T>
* The subclass of {@link EObject} for which this provider base shall
* be extended. * be extended.
*/ */
public class ReuseProviderBase<T extends EObject> implements IReuseProvider<T> { public class ReuseProviderBase<T extends EObject> implements IReuseProvider<T> {
/** {@inheritDoc} */
@Override
public boolean prepareReuseElementForLibrary(T reuseElement) {
if(reuseElement instanceof IModelElement) {
removeVariabilitySpecifications((IModelElement)reuseElement);
}
return true;
}
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public boolean updateReuseElement(T elementToBeUpdated, T newElement) { public boolean updateReuseElement(T elementToBeUpdated, T newElement) {
...@@ -47,8 +62,8 @@ public class ReuseProviderBase<T extends EObject> implements IReuseProvider<T> { ...@@ -47,8 +62,8 @@ public class ReuseProviderBase<T extends EObject> implements IReuseProvider<T> {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public boolean setReuseElementName(T reuseElement, String name) { public boolean setReuseElementName(T reuseElement, String name) {
if (reuseElement instanceof INamedElement) { if(reuseElement instanceof INamedElement) {
((INamedElement) reuseElement).setName(name); ((INamedElement)reuseElement).setName(name);
return true; return true;
} }
return false; return false;
...@@ -57,8 +72,8 @@ public class ReuseProviderBase<T extends EObject> implements IReuseProvider<T> { ...@@ -57,8 +72,8 @@ public class ReuseProviderBase<T extends EObject> implements IReuseProvider<T> {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public String getReuseElementName(T reuseElement) { public String getReuseElementName(T reuseElement) {
if (reuseElement instanceof INamedElement) { if(reuseElement instanceof INamedElement) {
return ((INamedElement) reuseElement).getName(); return ((INamedElement)reuseElement).getName();
} }
return null; return null;
} }
...@@ -66,8 +81,8 @@ public class ReuseProviderBase<T extends EObject> implements IReuseProvider<T> { ...@@ -66,8 +81,8 @@ public class ReuseProviderBase<T extends EObject> implements IReuseProvider<T> {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public boolean setReuseElementComment(T reuseElement, String comment) { public boolean setReuseElementComment(T reuseElement, String comment) {
if (reuseElement instanceof INamedCommentedElement) { if(reuseElement instanceof INamedCommentedElement) {
((INamedCommentedElement) reuseElement).setComment(comment); ((INamedCommentedElement)reuseElement).setComment(comment);
return true; return true;
} }
return false; return false;
...@@ -76,8 +91,8 @@ public class ReuseProviderBase<T extends EObject> implements IReuseProvider<T> { ...@@ -76,8 +91,8 @@ public class ReuseProviderBase<T extends EObject> implements IReuseProvider<T> {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public String getReuseElementComment(T reuseElement) { public String getReuseElementComment(T reuseElement) {
if (reuseElement instanceof INamedCommentedElement) { if(reuseElement instanceof INamedCommentedElement) {
return ((INamedCommentedElement) reuseElement).getComment(); return ((INamedCommentedElement)reuseElement).getComment();
} }
return null; return null;
} }
...@@ -85,8 +100,28 @@ public class ReuseProviderBase<T extends EObject> implements IReuseProvider<T> { ...@@ -85,8 +100,28 @@ public class ReuseProviderBase<T extends EObject> implements IReuseProvider<T> {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public List<Class<? extends EObject>> getPossibleExternalReferenceClasses() { public List<Class<? extends EObject>> getPossibleExternalReferenceClasses() {
// can only be specified for specific elements (if wanted) --> here only empty // Can only be specified for specific elements (if wanted) --> here only empty.
return new ArrayList<Class<? extends EObject>>(); return new ArrayList<Class<? extends EObject>>();
} }
/**
* Removes unnecessary variability specifications from the given element and from all of its
* contained elements. The method is recursion-based!
*/
private void removeVariabilitySpecifications(IModelElement nextElement) {
// Removal.
OptionalVariationPointSpecification unnecessarySpec =
getOptVarPointSpecification(nextElement);
if(unnecessarySpec != null) {
nextElement.getSpecifications().remove(unnecessarySpec);
}
// Recursion.
if(nextElement instanceof IHierarchicElementContainer) {
for(IHierarchicElement elem : ((IHierarchicElementContainer)nextElement)
.getContainedElements()) {
removeVariabilitySpecifications(elem);
}
}
}
} }
/*-------------------------------------------------------------------------+ /*-------------------------------------------------------------------------+
| Copyright 2021 fortiss GmbH | | Copyright 2023 fortiss GmbH |
| | | |
| Licensed under the Apache License, Version 2.0 (the "License"); | | Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. | | you may not use this file except in compliance with the License. |
...@@ -45,7 +45,8 @@ public class ReuseProviderService extends EObjectAwareServiceBase<IReuseProvider ...@@ -45,7 +45,8 @@ public class ReuseProviderService extends EObjectAwareServiceBase<IReuseProvider
* List of {@link EObject} {@link Class}es that might exist inside reuse * List of {@link EObject} {@link Class}es that might exist inside reuse
* elements as external references. * elements as external references.
*/ */
private static List<Class<? extends EObject>> possibleExternalReferenceClasses = new ArrayList<>(); private static List<Class<? extends EObject>> possibleExternalReferenceClasses =
new ArrayList<>();
/** /**
* Returns the singleton instance of {@link ReuseProviderService}. * Returns the singleton instance of {@link ReuseProviderService}.
...@@ -79,22 +80,30 @@ public class ReuseProviderService extends EObjectAwareServiceBase<IReuseProvider ...@@ -79,22 +80,30 @@ public class ReuseProviderService extends EObjectAwareServiceBase<IReuseProvider
/** {@inheritDoc} */ /** {@inheritDoc} */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public <T extends EObject> void registerReuseProvider(IReuseProvider<T> provider, Class<T> cls) { public <T extends EObject> void registerReuseProvider(IReuseProvider<T> provider,
addHandler(cls, (IReuseProvider<EObject>) provider); Class<T> cls) {
if (!reuseElementClasses.contains(cls)) { addHandler(cls, (IReuseProvider<EObject>)provider);
if(!reuseElementClasses.contains(cls)) {
reuseElementClasses.add(cls); reuseElementClasses.add(cls);
} }
possibleExternalReferenceClasses.addAll(provider.getPossibleExternalReferenceClasses()); possibleExternalReferenceClasses.addAll(provider.getPossibleExternalReferenceClasses());
} }
/** {@inheritDoc} */
@Override
public boolean prepareReuseElementForLibrary(EObject reuseElement) {
IReuseProvider<EObject> provider = getReuseProvider(reuseElement);
if(provider != null) {
return provider.prepareReuseElementForLibrary(reuseElement);
}
return false;
}
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public boolean updateReuseElement(EObject elemToBeUpdated, EObject newElem) { public boolean updateReuseElement(EObject elemToBeUpdated, EObject newElem) {
List<IReuseProvider<EObject>> registeredHandlers = getRegisteredHandlers(elemToBeUpdated.getClass()); IReuseProvider<EObject> provider = getReuseProvider(elemToBeUpdated);
// there should only be one handler registered per class for the reuse case if(provider != null) {
if (registeredHandlers != null && registeredHandlers.size() == 1) {
// get(0) is safe due to size check above
IReuseProvider<EObject> provider = registeredHandlers.get(0);
return provider.updateReuseElement(elemToBeUpdated, newElem); return provider.updateReuseElement(elemToBeUpdated, newElem);
} }
return false; return false;
...@@ -103,11 +112,8 @@ public class ReuseProviderService extends EObjectAwareServiceBase<IReuseProvider ...@@ -103,11 +112,8 @@ public class ReuseProviderService extends EObjectAwareServiceBase<IReuseProvider
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public boolean setReuseElementName(EObject reuseElement, String name) { public boolean setReuseElementName(EObject reuseElement, String name) {
List<IReuseProvider<EObject>> registeredHandlers = getRegisteredHandlers(reuseElement.getClass()); IReuseProvider<EObject> provider = getReuseProvider(reuseElement);
// there should only be one handler registered per class for the reuse case if(provider != null) {
if (registeredHandlers != null && registeredHandlers.size() == 1) {
// get(0) is safe due to size check above
IReuseProvider<EObject> provider = registeredHandlers.get(0);
return provider.setReuseElementName(reuseElement, name); return provider.setReuseElementName(reuseElement, name);
} }
return false; return false;
...@@ -116,11 +122,8 @@ public class ReuseProviderService extends EObjectAwareServiceBase<IReuseProvider ...@@ -116,11 +122,8 @@ public class ReuseProviderService extends EObjectAwareServiceBase<IReuseProvider
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public String getReuseElementName(EObject reuseElement) { public String getReuseElementName(EObject reuseElement) {
List<IReuseProvider<EObject>> registeredHandlers = getRegisteredHandlers(reuseElement.getClass()); IReuseProvider<EObject> provider = getReuseProvider(reuseElement);
// there should only be one handler registered per class for the reuse case if(provider != null) {
if (registeredHandlers != null && registeredHandlers.size() == 1) {
// get(0) is safe due to size check above
IReuseProvider<EObject> provider = registeredHandlers.get(0);
return provider.getReuseElementName(reuseElement); return provider.getReuseElementName(reuseElement);
} }
return null; return null;
...@@ -129,11 +132,8 @@ public class ReuseProviderService extends EObjectAwareServiceBase<IReuseProvider ...@@ -129,11 +132,8 @@ public class ReuseProviderService extends EObjectAwareServiceBase<IReuseProvider
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public boolean setReuseElementComment(EObject reuseElement, String comment) { public boolean setReuseElementComment(EObject reuseElement, String comment) {
List<IReuseProvider<EObject>> registeredHandlers = getRegisteredHandlers(reuseElement.getClass()); IReuseProvider<EObject> provider = getReuseProvider(reuseElement);
// there should only be one handler registered per class for the reuse case if(provider != null) {
if (registeredHandlers != null && registeredHandlers.size() == 1) {
// get(0) is safe due to size check above
IReuseProvider<EObject> provider = registeredHandlers.get(0);
return provider.setReuseElementComment(reuseElement, comment); return provider.setReuseElementComment(reuseElement, comment);
} }
return false; return false;
...@@ -142,11 +142,8 @@ public class ReuseProviderService extends EObjectAwareServiceBase<IReuseProvider ...@@ -142,11 +142,8 @@ public class ReuseProviderService extends EObjectAwareServiceBase<IReuseProvider
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public String getReuseElementComment(EObject reuseElement) { public String getReuseElementComment(EObject reuseElement) {
List<IReuseProvider<EObject>> registeredHandlers = getRegisteredHandlers(reuseElement.getClass()); IReuseProvider<EObject> provider = getReuseProvider(reuseElement);
// there should only be one handler registered per class for the reuse case if(provider != null) {
if (registeredHandlers != null && registeredHandlers.size() == 1) {
// get(0) is safe due to size check above
IReuseProvider<EObject> provider = registeredHandlers.get(0);
return provider.getReuseElementComment(reuseElement); return provider.getReuseElementComment(reuseElement);
} }
return null; return null;
...@@ -187,4 +184,19 @@ public class ReuseProviderService extends EObjectAwareServiceBase<IReuseProvider ...@@ -187,4 +184,19 @@ public class ReuseProviderService extends EObjectAwareServiceBase<IReuseProvider
public void startService() { public void startService() {
IKernelIntrospectionSystemService.getInstance().registerService(this); IKernelIntrospectionSystemService.getInstance().registerService(this);
} }
}
\ No newline at end of file /**
* Returns the reuse provider/handler for the class of the given element. If none can be found,
* null is returned.
*/
private IReuseProvider<EObject> getReuseProvider(EObject element) {
List<IReuseProvider<EObject>> registeredHandlers =
getRegisteredHandlers(element.getClass());
// There should only be one handler registered per class for the reuse case.
if(registeredHandlers != null && registeredHandlers.size() == 1) {
// get(0) is safe due to size check above.
return registeredHandlers.get(0);
}
return null;
}
}
ReuseLibraryModelElementFactory.java 4ee3eb7449e212643992a3dec6cfb8f4278efb70 GREEN ReuseLibraryModelElementFactory.java 4ee3eb7449e212643992a3dec6cfb8f4278efb70 GREEN
ReuseLibraryUtilsBasics.java 1d60936f98bd09e1ba1715192f5263f89b4942fe GREEN ReuseLibraryUtilsBasics.java 1d60936f98bd09e1ba1715192f5263f89b4942fe GREEN
ReuseLibraryUtilsManipulation.java 77a646db5a63ba7c61664dbcaf34a9036003fde5 GREEN ReuseLibraryUtilsManipulation.java fbb36279eb20cb46a4f2b79fed33dd5395d6da3e GREEN
/*-------------------------------------------------------------------------+ /*-------------------------------------------------------------------------+
| Copyright 2022 fortiss GmbH | | Copyright 2023 fortiss GmbH |
| | | |
| Licensed under the Apache License, Version 2.0 (the "License"); | | Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. | | you may not use this file except in compliance with the License. |
...@@ -22,6 +22,16 @@ import static org.fortiss.tooling.ext.reuse.storage.ReuseLibraryStorageManager.d ...@@ -22,6 +22,16 @@ import static org.fortiss.tooling.ext.reuse.storage.ReuseLibraryStorageManager.d
import static org.fortiss.tooling.ext.reuse.storage.ReuseLibraryStorageManager.saveReuseLibrary; import static org.fortiss.tooling.ext.reuse.storage.ReuseLibraryStorageManager.saveReuseLibrary;
import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryModelElementFactory.createReuseElementSpec; import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryModelElementFactory.createReuseElementSpec;
import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryModelElementFactory.createReuseLibrary; import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryModelElementFactory.createReuseLibrary;
import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.getAllReuseSpecs;
import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.getDateAsString;
import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.getFirstReuseElementUUID;
import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.getFirstSourceLibraryOfElement;
import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.getReuseElementName;
import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.getReuseElementUUIDForLibrary;
import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.getReuseSpecLinkedToThisLibraryElement;
import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.setReuseElementComment;
import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.setReuseElementLastUpdateNow;
import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.setReuseElementName;
import static org.fortiss.tooling.kernel.utils.EcoreUtils.copy; import static org.fortiss.tooling.kernel.utils.EcoreUtils.copy;
import static org.fortiss.tooling.kernel.utils.EcoreUtils.getAllReferencesSetting; import static org.fortiss.tooling.kernel.utils.EcoreUtils.getAllReferencesSetting;
import static org.fortiss.tooling.kernel.utils.EcoreUtils.replaceEObjectReferences; import static org.fortiss.tooling.kernel.utils.EcoreUtils.replaceEObjectReferences;
...@@ -62,11 +72,12 @@ public class ReuseLibraryUtilsManipulation { ...@@ -62,11 +72,12 @@ public class ReuseLibraryUtilsManipulation {
* Fixes the IDs of the model containing the given {@link EObject}, since * Fixes the IDs of the model containing the given {@link EObject}, since
* updating reuse elements can produce duplicated IDs. * updating reuse elements can produce duplicated IDs.
* *
* @param element A reuse element of the model * @param element
* A reuse element of the model
*/ */
public static void fixIDs(EObject element) { public static void fixIDs(EObject element) {
EObject root = element; EObject root = element;
while (root.eContainer() != null) { while(root.eContainer() != null) {
root = root.eContainer(); root = root.eContainer();
} }
removeDuplicateIds(root); removeDuplicateIds(root);
...@@ -79,14 +90,16 @@ public class ReuseLibraryUtilsManipulation { ...@@ -79,14 +90,16 @@ public class ReuseLibraryUtilsManipulation {
* changes between reuse elements with the same origin. Returns null if given * changes between reuse elements with the same origin. Returns null if given
* element is invalid (e.g. not a reuse element). * element is invalid (e.g. not a reuse element).
* *
* @param element The element of which a hash code should be created * @param element
* @param modifyDate The date of the last update/modification of the given * The element of which a hash code should be created
* element * @param modifyDate
* The date of the last update/modification of the given
* element
* @return String of the created hash code integer (or null for an invalid * @return String of the created hash code integer (or null for an invalid
* input) * input)
*/ */
public static String createReuseHashAsString(EObject element, ZonedDateTime modifyDate) { public static String createReuseHashAsString(EObject element, ZonedDateTime modifyDate) {
if (element != null && modifyDate != null) { if(element != null && modifyDate != null) {
// hashCode() does not change through element differences caused by simple // hashCode() does not change through element differences caused by simple
// changes like name or new ports, etc. // changes like name or new ports, etc.
// Therefore, we are currently modifying the hash with the date of the last // Therefore, we are currently modifying the hash with the date of the last
...@@ -94,8 +107,7 @@ public class ReuseLibraryUtilsManipulation { ...@@ -94,8 +107,7 @@ public class ReuseLibraryUtilsManipulation {
// e.g. if the original reuse element in the library was updated by someone else // e.g. if the original reuse element in the library was updated by someone else
// in the meantime). // in the meantime).
int hash = element.hashCode(); int hash = element.hashCode();
String hashString = String.valueOf(hash) + "__" String hashString = String.valueOf(hash) + "__" + getDateAsString(modifyDate, false);
+ ReuseLibraryUtilsBasics.getDateAsString(modifyDate, false);
return hashString; return hashString;
} }
return null; return null;
...@@ -112,24 +124,27 @@ public class ReuseLibraryUtilsManipulation { ...@@ -112,24 +124,27 @@ public class ReuseLibraryUtilsManipulation {
* compare() method that returns 0 if both given {@link IConnector}s are * compare() method that returns 0 if both given {@link IConnector}s are
* matching, otherwise +/-1. * matching, otherwise +/-1.
* *
* @param target The target element (e.g. the replaced * @param target
* element) * The target element (e.g. the replaced
* @param source The source element (e.g. the replacing * element)
* element) * @param source
* @param connectorMatchComparator A comparator that will return 0 with * The source element (e.g. the replacing
* compare() if both given {@link IConnector}s * element)
* are matching * @param connectorMatchComparator
* A comparator that will return 0 with
* compare() if both given {@link IConnector}s
* are matching
*/ */
public static void removeNonMatchingConnections(IHierarchicElement target, IHierarchicElement source, public static void removeNonMatchingConnections(IHierarchicElement target,
Comparator<IConnector> connectorMatchComparator) { IHierarchicElement source, Comparator<IConnector> connectorMatchComparator) {
IConnector matchedConnector = null; IConnector matchedConnector = null;
for (IConnector targetConnector : target.getConnectors()) { for(IConnector targetConnector : target.getConnectors()) {
// check if a matching connector can be found // check if a matching connector can be found
boolean saveConnections = false; boolean saveConnections = false;
for (IConnector sourceConnector : source.getConnectors()) { for(IConnector sourceConnector : source.getConnectors()) {
// matching is done via custom compare method (individual comparators possible) // matching is done via custom compare method (individual comparators possible)
if (connectorMatchComparator.compare(targetConnector, sourceConnector) == 0) { if(connectorMatchComparator.compare(targetConnector, sourceConnector) == 0) {
matchedConnector = sourceConnector; matchedConnector = sourceConnector;
saveConnections = true; saveConnections = true;
replaceEObjectReferences(targetConnector, matchedConnector); replaceEObjectReferences(targetConnector, matchedConnector);
...@@ -139,27 +154,27 @@ public class ReuseLibraryUtilsManipulation { ...@@ -139,27 +154,27 @@ public class ReuseLibraryUtilsManipulation {
// remove all connections or replace one end of them if a match was found // remove all connections or replace one end of them if a match was found
List<IConnection> connectionsToBeRemoved = new ArrayList<IConnection>(); List<IConnection> connectionsToBeRemoved = new ArrayList<IConnection>();
for (IConnection incoming : targetConnector.getIncoming()) { for(IConnection incoming : targetConnector.getIncoming()) {
if (saveConnections) { if(saveConnections) {
incoming.setTarget(matchedConnector); incoming.setTarget(matchedConnector);
} else { } else {
connectionsToBeRemoved.add(incoming); connectionsToBeRemoved.add(incoming);
} }
} }
for (IConnection outgoing : targetConnector.getOutgoing()) { for(IConnection outgoing : targetConnector.getOutgoing()) {
if (saveConnections) { if(saveConnections) {
outgoing.setSource(matchedConnector); outgoing.setSource(matchedConnector);
} else { } else {
connectionsToBeRemoved.add(outgoing); connectionsToBeRemoved.add(outgoing);
} }
} }
for (IConnection connection : connectionsToBeRemoved) { for(IConnection connection : connectionsToBeRemoved) {
delete(connection); delete(connection);
} }
// delete all external references // delete all external references
if (!saveConnections) { if(!saveConnections) {
for (Setting setting : getAllReferencesSetting(targetConnector)) { for(Setting setting : getAllReferencesSetting(targetConnector)) {
delete(setting.getEObject()); delete(setting.getEObject());
} }
} }
...@@ -169,46 +184,54 @@ public class ReuseLibraryUtilsManipulation { ...@@ -169,46 +184,54 @@ public class ReuseLibraryUtilsManipulation {
/** /**
* Setups a given reuse element for its placement inside a {@link ReuseLibrary}. * Setups a given reuse element for its placement inside a {@link ReuseLibrary}.
* *
* @param element The target reuse element * @param element
* @param connectingSpec The reuse specification used as link/trace/reference * The target reuse element
* @param connectingSpec
* The reuse specification used as link/trace/reference
*/ */
public static void setupElementForLibrary(IModelElement element, ReuseElementSpec connectingSpec) { public static void setupElementForLibrary(IModelElement element,
// currently, it is needed that only the original specification for this library ReuseElementSpec connectingSpec) {
// should exist (and also only the newest one) -> remove all others // Currently, it is needed that only the original specification for this library
for (ReuseElementSpec spec : ReuseLibraryUtilsBasics.getAllReuseSpecs(element)) { // should exist (and also only the newest one) -> remove all others.
if (!(connectingSpec.getElementUUID().equals(spec.getElementUUID()) for(ReuseElementSpec spec : getAllReuseSpecs(element)) {
&& connectingSpec.getLastUpdate().equals(spec.getLastUpdate()))) { if(!(connectingSpec.getElementUUID().equals(spec.getElementUUID()) &&
connectingSpec.getLastUpdate().equals(spec.getLastUpdate()))) {
element.getSpecifications().remove(spec); element.getSpecifications().remove(spec);
} }
} }
// Execute all other more specific preparations (type dependent).
IReuseProviderService.getInstance().prepareReuseElementForLibrary(element);
} }
/** /**
* Creates a reuse element based on a given {@link EObject} for its placement * Creates a reuse element based on a given {@link EObject} for its placement
* inside a {@link ReuseLibrary}. * inside a {@link ReuseLibrary}.
* *
* @param originalElement The original element that should be used as base for * @param originalElement
* the new reuse element * The original element that should be used as base for
* @param library The reuse library in which the new reuse element * the new reuse element
* should be added (later) * @param library
* @param saveName The name of the new reuse element with which it should * The reuse library in which the new reuse element
* be saved in the library * should be added (later)
* @param saveName
* The name of the new reuse element with which it should
* be saved in the library
* *
* @return The created reuse element for the library * @return The created reuse element for the library
*/ */
public static EObject createElementForLibrary(EObject originalElement, ReuseLibrary library, String saveName) { public static EObject createElementForLibrary(EObject originalElement, ReuseLibrary library,
String saveName) {
// add to the original element a reuse reference to the new library // add to the original element a reuse reference to the new library
// (cast is safe: elements within the ReuseElementList are always IModelElement // (cast is safe: elements within the ReuseElementList are always IModelElement
// by design) // by design)
ReuseElementSpec spec = ReuseLibraryUtilsManipulation.addReuseSpecToElement((IModelElement) originalElement, ReuseElementSpec spec = addReuseSpecToElement((IModelElement)originalElement, library);
library);
spec.setElementName(saveName); spec.setElementName(saveName);
ReuseLibraryUtilsManipulation.fixIDs(originalElement); fixIDs(originalElement);
// copy, because origin should still exist further (but change name of copy) // copy, because origin should still exist further (but change name of copy)
EObject newElementForLibrary = copy(originalElement); EObject newElementForLibrary = copy(originalElement);
ReuseLibraryUtilsBasics.setReuseElementName(newElementForLibrary, saveName); setReuseElementName(newElementForLibrary, saveName);
setupElementForLibrary((IModelElement) newElementForLibrary, spec); setupElementForLibrary((IModelElement)newElementForLibrary, spec);
return newElementForLibrary; return newElementForLibrary;
} }
...@@ -217,11 +240,14 @@ public class ReuseLibraryUtilsManipulation { ...@@ -217,11 +240,14 @@ public class ReuseLibraryUtilsManipulation {
* Adds a {@link ReuseElementSpec} to the given element pointing to the given * Adds a {@link ReuseElementSpec} to the given element pointing to the given
* {@link ReuseLibrary} (as source). * {@link ReuseLibrary} (as source).
* *
* @param element The target reuse element * @param element
* @param library The connected reuse library * The target reuse element
* @param library
* The connected reuse library
* @return The created reuse specification * @return The created reuse specification
*/ */
public static ReuseElementSpec addReuseSpecToElement(IModelElement element, ReuseLibrary library) { public static ReuseElementSpec addReuseSpecToElement(IModelElement element,
ReuseLibrary library) {
ReuseElementSpec spec = createReuseElementSpec(element, library); ReuseElementSpec spec = createReuseElementSpec(element, library);
element.getSpecifications().add(spec); element.getSpecifications().add(spec);
return spec; return spec;
...@@ -230,12 +256,15 @@ public class ReuseLibraryUtilsManipulation { ...@@ -230,12 +256,15 @@ public class ReuseLibraryUtilsManipulation {
/** /**
* Adds a given element to the given {@link ReuseLibrary}. * Adds a given element to the given {@link ReuseLibrary}.
* *
* @param element The reuse element that should be added * @param element
* @param library The target reuse library * The reuse element that should be added
* @param library
* The target reuse library
* @throws IOException * @throws IOException
* @throws CoreException * @throws CoreException
*/ */
public static void addElementToLibrary(EObject element, ReuseLibrary library) throws IOException, CoreException { public static void addElementToLibrary(EObject element, ReuseLibrary library)
throws IOException, CoreException {
library.getReuseElementList().add(element); library.getReuseElementList().add(element);
saveReuseLibrary(library); saveReuseLibrary(library);
} }
...@@ -244,15 +273,15 @@ public class ReuseLibraryUtilsManipulation { ...@@ -244,15 +273,15 @@ public class ReuseLibraryUtilsManipulation {
* Creates a new {@link ReuseLibrary} with the given name. If no name is * Creates a new {@link ReuseLibrary} with the given name. If no name is
* received, the default library name will be used. * received, the default library name will be used.
* *
* @param libraryName The name of the new reuse library * @param libraryName
* The name of the new reuse library
* @return The requested reuse library * @return The requested reuse library
*/ */
public static ReuseLibrary createAndAddReuseLibrary(String libraryName) { public static ReuseLibrary createAndAddReuseLibrary(String libraryName) {
if (libraryName == null || libraryName.equals("")) { if(libraryName == null || libraryName.equals("")) {
return createReuseLibrary(); return createReuseLibrary();
} else {
return createReuseLibrary(libraryName);
} }
return createReuseLibrary(libraryName);
} }
/** /**
...@@ -261,15 +290,19 @@ public class ReuseLibraryUtilsManipulation { ...@@ -261,15 +290,19 @@ public class ReuseLibraryUtilsManipulation {
* given name in the end). The {@link ReuseElementSpec} references the original * given name in the end). The {@link ReuseElementSpec} references the original
* element (with its library). * element (with its library).
* *
* @param originalElementInLibrary The target reuse element inside a reuse * @param originalElementInLibrary
* library that should be replace by the new * The target reuse element inside a reuse
* reuse element * library that should be replace by the new
* @param newElement The new reuse element which is the * reuse element
* replacement of the current element in the * @param newElement
* given library * The new reuse element which is the
* @param referencingSpec The reuse specification that points to the * replacement of the current element in the
* original reuse element in the library * given library
* @param newName The new name of the element after replacement * @param referencingSpec
* The reuse specification that points to the
* original reuse element in the library
* @param newName
* The new name of the element after replacement
*/ */
public static void replaceElementInLibrary(EObject originalElementInLibrary, EObject newElement, public static void replaceElementInLibrary(EObject originalElementInLibrary, EObject newElement,
ReuseElementSpec referencingSpec, String newName) { ReuseElementSpec referencingSpec, String newName) {
...@@ -278,11 +311,11 @@ public class ReuseLibraryUtilsManipulation { ...@@ -278,11 +311,11 @@ public class ReuseLibraryUtilsManipulation {
ZonedDateTime now = ZonedDateTime.now(); ZonedDateTime now = ZonedDateTime.now();
referencingSpec.setLastUpdate(now); referencingSpec.setLastUpdate(now);
referencingSpec.setElementName(newName); referencingSpec.setElementName(newName);
referencingSpec.setElementHash(ReuseLibraryUtilsManipulation.createReuseHashAsString(newElement, now)); referencingSpec.setElementHash(createReuseHashAsString(newElement, now));
EObject newLibraryElement = copy(newElement); EObject newLibraryElement = copy(newElement);
ReuseLibraryUtilsBasics.setReuseElementName(newLibraryElement, newName); setReuseElementName(newLibraryElement, newName);
setupElementForLibrary((IModelElement) newLibraryElement, referencingSpec); setupElementForLibrary((IModelElement)newLibraryElement, referencingSpec);
replace(originalElementInLibrary, newLibraryElement); replace(originalElementInLibrary, newLibraryElement);
} }
...@@ -293,54 +326,59 @@ public class ReuseLibraryUtilsManipulation { ...@@ -293,54 +326,59 @@ public class ReuseLibraryUtilsManipulation {
* the given name in the end). Returns the new element in the project if the * the given name in the end). Returns the new element in the project if the
* replacement/update was successful, otherwise null. * replacement/update was successful, otherwise null.
* *
* @param elementInProject The new reuse element which is the * @param elementInProject
* replacement of the current element in the * The new reuse element which is the
* given library * replacement of the current element in the
* @param originalElementInLibrary The target reuse element inside a reuse * given library
* library that should be replace by the new * @param originalElementInLibrary
* reuse element * The target reuse element inside a reuse
* @param newName The new name of the element after * library that should be replace by the new
* replacement * reuse element
* @param overrideAdditionalReferences Flag whether all other possibly existing * @param newName
* references in the (old) project element * The new name of the element after
* should be overridden/deleted (true) or if * replacement
* they should be kept (false) * @param keepAdditionalReferences
* Flag whether all other possibly existing
* references in the (old) project element
* should be overridden/deleted (false) or if
* they should be kept (true)
* *
* @return The new element in the project if the replacement/update was * @return The new element in the project if the replacement/update was
* successful, otherwise null. * successful, otherwise null.
*/ */
public static EObject replaceElementInProject(EObject elementInProject, EObject originalElementInLibrary, public static EObject replaceElementInProject(EObject elementInProject,
String newName, boolean keepAdditionalReferences) { EObject originalElementInLibrary, String newName, boolean keepAdditionalReferences) {
if (originalElementInLibrary instanceof IModelElement && elementInProject instanceof IModelElement) { if(originalElementInLibrary instanceof IModelElement &&
IModelElement sourceElement = copy((IModelElement) originalElementInLibrary); elementInProject instanceof IModelElement) {
IModelElement modelElementInProject = (IModelElement) elementInProject; IModelElement sourceElement = copy((IModelElement)originalElementInLibrary);
IModelElement modelElementInProject = (IModelElement)elementInProject;
// first, the information of the reference and the new element // first, the information of the reference and the new element
// needs to be updated and set up // needs to be updated and set up
List<ReuseElementSpec> specs = ReuseLibraryUtilsBasics.getAllReuseSpecs(sourceElement); List<ReuseElementSpec> specs = getAllReuseSpecs(sourceElement);
ZonedDateTime now = ZonedDateTime.now(); ZonedDateTime now = ZonedDateTime.now();
if (specs.size() == 1) { if(specs.size() == 1) {
// get(0) is safe due to size check // get(0) is safe due to size check
specs.get(0).setLastUpdate(now); specs.get(0).setLastUpdate(now);
} else { } else {
return null; return null;
} }
ReuseLibraryUtilsBasics.setReuseElementName(sourceElement, newName); setReuseElementName(sourceElement, newName);
// if additional references should not be overridden but kept, they need to be // if additional references should not be overridden but kept, they need to be
// saved/added in the source element (otherwise they will be overridden anyway) // saved/added in the source element (otherwise they will be overridden anyway)
if (keepAdditionalReferences) { if(keepAdditionalReferences) {
List<ReuseElementSpec> oldSpecs = ReuseLibraryUtilsBasics.getAllReuseSpecs(modelElementInProject); List<ReuseElementSpec> oldSpecs = getAllReuseSpecs(modelElementInProject);
// remove the specification of the current update/replacement (only additional // remove the specification of the current update/replacement (only additional
// ones added) // ones added)
ReuseElementSpec mainSpec = ReuseLibraryUtilsBasics ReuseElementSpec mainSpec = getReuseSpecLinkedToThisLibraryElement(
.getReuseSpecLinkedToThisLibraryElement(modelElementInProject, sourceElement); modelElementInProject, sourceElement);
oldSpecs.remove(mainSpec); oldSpecs.remove(mainSpec);
sourceElement.getSpecifications().addAll(oldSpecs); sourceElement.getSpecifications().addAll(oldSpecs);
} }
boolean success = IReuseProviderService.getInstance().updateReuseElement(modelElementInProject, boolean success = IReuseProviderService.getInstance()
sourceElement); .updateReuseElement(modelElementInProject, sourceElement);
if (success) { if(success) {
return sourceElement; return sourceElement;
} }
} }
...@@ -350,16 +388,17 @@ public class ReuseLibraryUtilsManipulation { ...@@ -350,16 +388,17 @@ public class ReuseLibraryUtilsManipulation {
/** /**
* Deletes the given {@link ReuseLibrary}. * Deletes the given {@link ReuseLibrary}.
* *
* @param library The target reuse library that should be deleted * @param library
* The target reuse library that should be deleted
*/ */
public static void deleteReuseLibrary(ReuseLibrary library) { public static void deleteReuseLibrary(ReuseLibrary library) {
// currently, everything is based only on the reuse folder/file structure, which // currently, everything is based only on the reuse folder/file structure, which
// is why it is enough to just delete the local folder // is why it is enough to just delete the local folder
try { try {
deleteReuseLibraryFolder(library); deleteReuseLibraryFolder(library);
} catch (CoreException e) { } catch(CoreException e) {
error(getDefault(), "Error during deleting reuse library folder", e); error(getDefault(), "Error during deleting reuse library folder", e);
showError("Cannot delete reuse library: " + ReuseLibraryUtilsBasics.getReuseElementName(library)); showError("Cannot delete reuse library: " + getReuseElementName(library));
} }
} }
...@@ -367,32 +406,35 @@ public class ReuseLibraryUtilsManipulation { ...@@ -367,32 +406,35 @@ public class ReuseLibraryUtilsManipulation {
* Deletes the given reuse element by deleting its entry in the local file of * Deletes the given reuse element by deleting its entry in the local file of
* the given {@link ReuseLibrary} (if it exists there). * the given {@link ReuseLibrary} (if it exists there).
* *
* @param element The target reuse element that should be deleted * @param element
* @param sourceLibrary The reuse library in which the deletion should happen * The target reuse element that should be deleted
* @param sourceLibrary
* The reuse library in which the deletion should happen
*/ */
public static void deleteReuseElementInLibraryFile(EObject element, ReuseLibrary sourceLibrary) { public static void deleteReuseElementInLibraryFile(EObject element,
ReuseLibrary sourceLibrary) {
// the objects might not be the same anymore, which is why we need to identify // the objects might not be the same anymore, which is why we need to identify
// the correct object via the reuse UUID // the correct object via the reuse UUID
EObject correctLibraryElement = null; EObject correctLibraryElement = null;
EList<EObject> libraryElementList = sourceLibrary.getReuseElementList(); EList<EObject> libraryElementList = sourceLibrary.getReuseElementList();
for (EObject libraryElement : libraryElementList) { for(EObject libraryElement : libraryElementList) {
if (ReuseLibraryUtilsBasics.getFirstReuseElementUUID(libraryElement) if(getFirstReuseElementUUID(libraryElement)
.equals(ReuseLibraryUtilsBasics.getReuseElementUUIDForLibrary(element, sourceLibrary))) { .equals(getReuseElementUUIDForLibrary(element, sourceLibrary))) {
correctLibraryElement = libraryElement; correctLibraryElement = libraryElement;
break; break;
} }
} }
if (correctLibraryElement == null) { if(correctLibraryElement == null) {
return; return;
} }
libraryElementList.remove(correctLibraryElement); libraryElementList.remove(correctLibraryElement);
try { try {
saveReuseLibrary(sourceLibrary); saveReuseLibrary(sourceLibrary);
} catch (IOException | CoreException e) { } catch(IOException | CoreException e) {
error(getDefault(), "Error during removing reuse element from reuse library", e); error(getDefault(), "Error during removing reuse element from reuse library", e);
showError("Cannot remove reuse element: " + ReuseLibraryUtilsBasics.getReuseElementName(element) showError("Cannot remove reuse element: " + getReuseElementName(element) +
+ " from this reuse library: " + ReuseLibraryUtilsBasics.getReuseElementName(sourceLibrary)); " from this reuse library: " + getReuseElementName(sourceLibrary));
} }
} }
...@@ -401,11 +443,12 @@ public class ReuseLibraryUtilsManipulation { ...@@ -401,11 +443,12 @@ public class ReuseLibraryUtilsManipulation {
* the {@link ReuseLibrary} that is mentioned as first (or only) source library * the {@link ReuseLibrary} that is mentioned as first (or only) source library
* for this reuse element (in its {link ReuseElementSpec}s). * for this reuse element (in its {link ReuseElementSpec}s).
* *
* @param element The target reuse element that should be deleted * @param element
* The target reuse element that should be deleted
*/ */
public static void deleteReuseElementInLibraryFile(EObject element) { public static void deleteReuseElementInLibraryFile(EObject element) {
ReuseLibrary sourceLibrary = ReuseLibraryUtilsBasics.getFirstSourceLibraryOfElement(element); ReuseLibrary sourceLibrary = getFirstSourceLibraryOfElement(element);
if (sourceLibrary != null) { if(sourceLibrary != null) {
deleteReuseElementInLibraryFile(element, sourceLibrary); deleteReuseElementInLibraryFile(element, sourceLibrary);
} }
} }
...@@ -417,71 +460,73 @@ public class ReuseLibraryUtilsManipulation { ...@@ -417,71 +460,73 @@ public class ReuseLibraryUtilsManipulation {
* {@link ReuseLibrary} is where the reuse element will be updated with the new * {@link ReuseLibrary} is where the reuse element will be updated with the new
* name. * name.
* *
* @param element The target reuse element that should be updated * @param element
* @param newName The new name as string * The target reuse element that should be updated
* @param sourceLibrary The reuse library in which the update should happen * @param newName
* The new name as string
* @param sourceLibrary
* The reuse library in which the update should happen
*/ */
public static void updateReuseElementFileWithName(EObject element, String newName, ReuseLibrary sourceLibrary) { public static void updateReuseElementFileWithName(EObject element, String newName,
if (element == null || newName == null || newName.isBlank()) { ReuseLibrary sourceLibrary) {
if(element == null || newName == null || newName.isBlank()) {
return; return;
} }
boolean modifiedLibrary = false; boolean modifiedLibrary = false;
ReuseLibrary libraryForDeletion = null; ReuseLibrary libraryForDeletion = null;
String errorObject = ""; String errorObject = "";
if (element instanceof ReuseLibrary) { if(element instanceof ReuseLibrary) {
// since the folder and file names of a reuse library contain the library name // since the folder and file names of a reuse library contain the library name
// (beside its UUID), the easiest way to rename it is to save it as new library // (beside its UUID), the easiest way to rename it is to save it as new library
// (where only the name is changed, not UUID, etc.) and delete its previous // (where only the name is changed, not UUID, etc.) and delete its previous
// library folder (save and deletion are done at the end of this method) // library folder (save and deletion are done at the end of this method)
sourceLibrary = (ReuseLibrary) element; sourceLibrary = (ReuseLibrary)element;
// copy, because origin should still exist further for deletion // copy, because origin should still exist further for deletion
libraryForDeletion = copy(sourceLibrary); libraryForDeletion = copy(sourceLibrary);
ReuseLibraryUtilsBasics.setReuseElementName(sourceLibrary, newName); setReuseElementName(sourceLibrary, newName);
for (EObject libraryElement : sourceLibrary.getReuseElementList()) { for(EObject libraryElement : sourceLibrary.getReuseElementList()) {
// cast is safe: elements within the ReuseElementList are always IModelElement // cast is safe: elements within the ReuseElementList are always IModelElement
// by design // by design
List<ReuseElementSpec> specList = ReuseLibraryUtilsBasics List<ReuseElementSpec> specList = getAllReuseSpecs(libraryElement);
.getAllReuseSpecs((IModelElement) libraryElement); for(ReuseElementSpec reuseSpec : specList) {
for (ReuseElementSpec reuseSpec : specList) {
reuseSpec.setSourceLibName(newName); reuseSpec.setSourceLibName(newName);
} }
} }
errorObject = "reuse library '" + ReuseLibraryUtilsBasics.getReuseElementName(sourceLibrary) + "'"; errorObject = "reuse library '" + getReuseElementName(sourceLibrary) + "'";
modifiedLibrary = true; modifiedLibrary = true;
} else { } else {
// the objects might not be the same anymore, which is why we need to identify // the objects might not be the same anymore, which is why we need to identify
// the correct object via the reuse UUID // the correct object via the reuse UUID
for (EObject libraryElement : sourceLibrary.getReuseElementList()) { for(EObject libraryElement : sourceLibrary.getReuseElementList()) {
if (ReuseLibraryUtilsBasics.getFirstReuseElementUUID(libraryElement) if(getFirstReuseElementUUID(libraryElement)
.equals(ReuseLibraryUtilsBasics.getReuseElementUUIDForLibrary(element, sourceLibrary))) { .equals(getReuseElementUUIDForLibrary(element, sourceLibrary))) {
ReuseLibraryUtilsBasics.setReuseElementName(libraryElement, newName); setReuseElementName(libraryElement, newName);
// cast is safe: elements within the ReuseElementList are always IModelElement // cast is safe: elements within the ReuseElementList are always IModelElement
// by design // by design
List<ReuseElementSpec> specList = ReuseLibraryUtilsBasics List<ReuseElementSpec> specList = getAllReuseSpecs(libraryElement);
.getAllReuseSpecs((IModelElement) libraryElement); for(ReuseElementSpec reuseSpec : specList) {
for (ReuseElementSpec reuseSpec : specList) {
reuseSpec.setElementName(newName); reuseSpec.setElementName(newName);
} }
ReuseLibraryUtilsBasics.setReuseElementLastUpdateNow(libraryElement); setReuseElementLastUpdateNow(libraryElement);
errorObject = "reuse element '" + ReuseLibraryUtilsBasics.getReuseElementName(libraryElement) errorObject = "reuse element '" + getReuseElementName(libraryElement) +
+ "' in reuse library '" + ReuseLibraryUtilsBasics.getReuseElementName(sourceLibrary) + "'"; "' in reuse library '" + getReuseElementName(sourceLibrary) + "'";
modifiedLibrary = true; modifiedLibrary = true;
break; break;
} }
} }
} }
if (modifiedLibrary) { if(modifiedLibrary) {
try { try {
saveReuseLibrary(sourceLibrary); saveReuseLibrary(sourceLibrary);
if (libraryForDeletion != null) { if(libraryForDeletion != null) {
deleteReuseLibraryFolder(libraryForDeletion); deleteReuseLibraryFolder(libraryForDeletion);
} }
} catch (IOException | CoreException e) { } catch(IOException | CoreException e) {
error(getDefault(), "Error during renaming " + errorObject, e); error(getDefault(), "Error during renaming " + errorObject, e);
showError("Cannot rename " + errorObject); showError("Cannot rename " + errorObject);
} }
...@@ -495,15 +540,17 @@ public class ReuseLibraryUtilsManipulation { ...@@ -495,15 +540,17 @@ public class ReuseLibraryUtilsManipulation {
* {@link ReuseLibrary}, which is mentioned in its {link ReuseElementSpec}s, is * {@link ReuseLibrary}, which is mentioned in its {link ReuseElementSpec}s, is
* where the reuse element will be updated with the new name. * where the reuse element will be updated with the new name.
* *
* @param element The target reuse element that should be updated * @param element
* @param newName The new name as string * The target reuse element that should be updated
* @param newName
* The new name as string
*/ */
public static void updateReuseElementFileWithName(EObject element, String newName) { public static void updateReuseElementFileWithName(EObject element, String newName) {
if (element instanceof ReuseLibrary) { if(element instanceof ReuseLibrary) {
updateReuseElementFileWithName(element, newName, null); updateReuseElementFileWithName(element, newName, null);
} else { } else {
ReuseLibrary sourceLibrary = ReuseLibraryUtilsBasics.getFirstSourceLibraryOfElement(element); ReuseLibrary sourceLibrary = getFirstSourceLibraryOfElement(element);
if (sourceLibrary != null) { if(sourceLibrary != null) {
updateReuseElementFileWithName(element, newName, sourceLibrary); updateReuseElementFileWithName(element, newName, sourceLibrary);
} }
} }
...@@ -516,42 +563,45 @@ public class ReuseLibraryUtilsManipulation { ...@@ -516,42 +563,45 @@ public class ReuseLibraryUtilsManipulation {
* {@link ReuseLibrary} is where the reuse element will be updated with the new * {@link ReuseLibrary} is where the reuse element will be updated with the new
* comment. * comment.
* *
* @param element The target reuse element that should be updated * @param element
* @param newComment The new comment as string * The target reuse element that should be updated
* @param sourceLibrary The reuse library in which the update should happen * @param newComment
* The new comment as string
* @param sourceLibrary
* The reuse library in which the update should happen
*/ */
public static void updateReuseElementFileWithComment(EObject element, String newComment, public static void updateReuseElementFileWithComment(EObject element, String newComment,
ReuseLibrary sourceLibrary) { ReuseLibrary sourceLibrary) {
boolean modifiedLibrary = false; boolean modifiedLibrary = false;
String errorObject = ""; String errorObject = "";
if (element instanceof ReuseLibrary) { if(element instanceof ReuseLibrary) {
sourceLibrary = (ReuseLibrary) element; sourceLibrary = (ReuseLibrary)element;
ReuseLibraryUtilsBasics.setReuseElementComment(sourceLibrary, newComment); setReuseElementComment(sourceLibrary, newComment);
errorObject = "reuse library '" + ReuseLibraryUtilsBasics.getReuseElementName(sourceLibrary) + "'"; errorObject = "reuse library '" + getReuseElementName(sourceLibrary) + "'";
modifiedLibrary = true; modifiedLibrary = true;
} else { } else {
// the objects might not be the same anymore, which is why we need to identify // the objects might not be the same anymore, which is why we need to identify
// the correct object via the reuse UUID // the correct object via the reuse UUID
EList<EObject> libraryElementList = sourceLibrary.getReuseElementList(); EList<EObject> libraryElementList = sourceLibrary.getReuseElementList();
for (EObject libraryElement : libraryElementList) { for(EObject libraryElement : libraryElementList) {
if (ReuseLibraryUtilsBasics.getFirstReuseElementUUID(libraryElement) if(getFirstReuseElementUUID(libraryElement)
.equals(ReuseLibraryUtilsBasics.getReuseElementUUIDForLibrary(element, sourceLibrary))) { .equals(getReuseElementUUIDForLibrary(element, sourceLibrary))) {
ReuseLibraryUtilsBasics.setReuseElementComment(libraryElement, newComment); setReuseElementComment(libraryElement, newComment);
ReuseLibraryUtilsBasics.setReuseElementLastUpdateNow(libraryElement); setReuseElementLastUpdateNow(libraryElement);
errorObject = "reuse element '" + ReuseLibraryUtilsBasics.getReuseElementName(libraryElement) errorObject = "reuse element '" + getReuseElementName(libraryElement) +
+ "' in reuse library '" + ReuseLibraryUtilsBasics.getReuseElementName(sourceLibrary) + "'"; "' in reuse library '" + getReuseElementName(sourceLibrary) + "'";
modifiedLibrary = true; modifiedLibrary = true;
break; break;
} }
} }
} }
if (modifiedLibrary) { if(modifiedLibrary) {
try { try {
saveReuseLibrary(sourceLibrary); saveReuseLibrary(sourceLibrary);
} catch (IOException | CoreException e) { } catch(IOException | CoreException e) {
error(getDefault(), "Error during modifying " + errorObject, e); error(getDefault(), "Error during modifying " + errorObject, e);
showError("Cannot modify " + errorObject); showError("Cannot modify " + errorObject);
} }
...@@ -565,15 +615,17 @@ public class ReuseLibraryUtilsManipulation { ...@@ -565,15 +615,17 @@ public class ReuseLibraryUtilsManipulation {
* {@link ReuseLibrary}, which is mentioned in its {link ReuseElementSpec}s, is * {@link ReuseLibrary}, which is mentioned in its {link ReuseElementSpec}s, is
* where the reuse element will be updated with the new comment. * where the reuse element will be updated with the new comment.
* *
* @param element The target reuse element that should be updated * @param element
* @param newComment The new comment as string * The target reuse element that should be updated
* @param newComment
* The new comment as string
*/ */
public static void updateReuseElementFileWithComment(EObject element, String newComment) { public static void updateReuseElementFileWithComment(EObject element, String newComment) {
if (element instanceof ReuseLibrary) { if(element instanceof ReuseLibrary) {
updateReuseElementFileWithComment(element, newComment, null); updateReuseElementFileWithComment(element, newComment, null);
} else { } else {
ReuseLibrary sourceLibrary = ReuseLibraryUtilsBasics.getFirstSourceLibraryOfElement(element); ReuseLibrary sourceLibrary = getFirstSourceLibraryOfElement(element);
if (sourceLibrary != null) { if(sourceLibrary != null) {
updateReuseElementFileWithComment(element, newComment, sourceLibrary); updateReuseElementFileWithComment(element, newComment, sourceLibrary);
} }
} }
......
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