From 98e2adb6f21a1ad125d4fb7a692e094aa227b7e7 Mon Sep 17 00:00:00 2001 From: Ralph Soika Date: Sun, 14 May 2023 19:15:21 +0200 Subject: [PATCH] update isDirty flag Issue #246 --- .../glsp/model/BPMNSourceModelStorage.java | 26 ++++++++--- .../java/org/openbpmn/bpmn/BPMNModel.java | 45 ++++++++++++++----- .../openbpmn/bpmn/util/BPMNModelFactory.java | 1 - 3 files changed, 53 insertions(+), 19 deletions(-) diff --git a/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/model/BPMNSourceModelStorage.java b/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/model/BPMNSourceModelStorage.java index 32fd060b..8e04a3a5 100644 --- a/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/model/BPMNSourceModelStorage.java +++ b/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/model/BPMNSourceModelStorage.java @@ -22,9 +22,12 @@ import java.net.URISyntaxException; import java.util.Map; import java.util.Optional; -import java.util.logging.Logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.eclipse.glsp.server.actions.ActionDispatcher; import org.eclipse.glsp.server.actions.SaveModelAction; +import org.eclipse.glsp.server.actions.SetDirtyStateAction; import org.eclipse.glsp.server.features.core.model.RequestModelAction; import org.eclipse.glsp.server.features.core.model.SourceModelStorage; import org.eclipse.glsp.server.model.GModelState; @@ -48,17 +51,20 @@ * @version 1.0 */ public class BPMNSourceModelStorage implements SourceModelStorage { - private static Logger logger = Logger.getLogger(BPMNSourceModelStorage.class.getName()); + private static Logger logger = LogManager.getLogger(BPMNSourceModelStorage.class); @Inject protected BPMNGModelState modelState; + @Inject + protected ActionDispatcher actionDispatcher; + /** * Loads a source model into the modelState. */ @Override public void loadSourceModel(final RequestModelAction action) { - logger.fine("loading BPMN Meta model...."); + logger.debug("loading BPMN Meta model...."); Map options = action.getOptions(); boolean bNeedsClientLayout = Boolean.parseBoolean(options.get("needsClientLayout")); // resolve file location.... @@ -76,9 +82,17 @@ public void loadSourceModel(final RequestModelAction action) { model = BPMNModelFactory.read(file); // we store the BPMN meta model into the modelState modelState.setBpmnModel(model); + // if the model is dirty (because linked-file content has change) we send a + // DirtyState action... + if (model.isDirty()) { + logger.info("....external model content has changed."); + SetDirtyStateAction dirtyAction = new SetDirtyStateAction(); + dirtyAction.setDirty(true); + dirtyAction.setReason("Updated linked File Content"); + actionDispatcher.dispatchAfterNextUpdate(dirtyAction); + } } catch (BPMNModelException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + logger.error("Failed to load model source: " + e.getMessage()); } } @@ -107,7 +121,7 @@ public void saveSourceModel(final SaveModelAction action) { java.net.URI targetURI = new URI(uri); model.save(targetURI); } catch (URISyntaxException e) { - logger.severe("Invalid Target URI: " + e.getMessage()); + logger.error("Invalid Target URI: " + e.getMessage()); } } diff --git a/open-bpmn.metamodel/src/main/java/org/openbpmn/bpmn/BPMNModel.java b/open-bpmn.metamodel/src/main/java/org/openbpmn/bpmn/BPMNModel.java index 06e95d01..1c7d0539 100644 --- a/open-bpmn.metamodel/src/main/java/org/openbpmn/bpmn/BPMNModel.java +++ b/open-bpmn.metamodel/src/main/java/org/openbpmn/bpmn/BPMNModel.java @@ -80,6 +80,7 @@ public class BPMNModel { protected Set signals = null; protected Set messages = null; protected Element collaborationElement = null; + private boolean isDirty = false; private final Map URI_BY_NAMESPACE = new HashMap<>(); private final Map PREFIX_BY_NAMESPACE = new HashMap<>(); @@ -255,6 +256,25 @@ public void addNamespace(String namespace, String uri) { } } + /** + * Returns an internal isDirty flag + * + * @return + */ + public boolean isDirty() { + return isDirty; + } + + /** + * Set the internal isDirty flag. This flag can be used to mark a model as + * dirty. + * + * @param isDirty + */ + public void setDirty(boolean isDirty) { + this.isDirty = isDirty; + } + public Set getParticipants() { if (participants == null) { participants = new LinkedHashSet(); @@ -1296,7 +1316,7 @@ public void resolveFileLinksOnSave(Path path) { Path pathLinkFileContent = Paths.get(fileLink); try { // read content... - logger.info(element.getNodeName() + " has attribute open-bpmn:file-link: " + logger.fine(element.getNodeName() + " has attribute open-bpmn:file-link: " + fileLink); byte[] bytes = Files.readAllBytes(pathLinkFileContent); @@ -1337,13 +1357,16 @@ public void resolveFileLinksOnSave(Path path) { * * * The method compares the content of the corresponding file. In case of a - * mismatch we assume that the content of the BPMN file is actual and so we - * replace the file in the filesystem. - * If no file was found, the method creates an empty one. + * mismatch we assume that the file content is actual and so we + * mark the model immediately as dirty. + * + * The method marks the model as dirty if linked file content has changed + * + * @param path - file path + * @return boolean - true if the linked file content has changed. */ public void resolveFileLinksOnLoad(Path path) { // Resolve the parent path - Path parent = path.getParent(); long l = System.currentTimeMillis(); @@ -1358,21 +1381,20 @@ public void resolveFileLinksOnLoad(Path path) { Path pathLinkFileContent = Paths.get(fileLink); try { // read content... - logger.info(element.getNodeName() + " has attribute open-bpmn:file-link: " + logger.fine(element.getNodeName() + " has attribute open-bpmn:file-link: " + fileLink); byte[] bytes = Files.readAllBytes(pathLinkFileContent); String fileData = new String(bytes, StandardCharsets.UTF_8); - // Now we compare the content with the content of the CDATA Tag. If not equal we // update the file!! Because we assume the .bpmn file is always right. String bpmnContent = getElementContent(element); if (!bpmnContent.equals(fileData)) { - logger.warning( - "Updating content of open-bpmn:file-link '" + fileLink + "'... "); - // Update the file !! - Files.write(pathLinkFileContent, bpmnContent.getBytes(StandardCharsets.UTF_8)); + logger.fine( + "File content of open-bpmn:file-link '" + fileLink + "' updated."); + // mark model as dirty + this.setDirty(true); } // Now replace the content with the filename @@ -1390,7 +1412,6 @@ public void resolveFileLinksOnLoad(Path path) { } } logger.fine("...resolveFileLinksOnLoad took " + (System.currentTimeMillis() - l) + "ms"); - } /** diff --git a/open-bpmn.metamodel/src/main/java/org/openbpmn/bpmn/util/BPMNModelFactory.java b/open-bpmn.metamodel/src/main/java/org/openbpmn/bpmn/util/BPMNModelFactory.java index 56071ae5..66ccddf3 100644 --- a/open-bpmn.metamodel/src/main/java/org/openbpmn/bpmn/util/BPMNModelFactory.java +++ b/open-bpmn.metamodel/src/main/java/org/openbpmn/bpmn/util/BPMNModelFactory.java @@ -169,7 +169,6 @@ public static BPMNModel read(InputStream is, Path path) throws BPMNModelExceptio setOpenBPMNNameSpace(model.getDefinitions()); // resolve open-bpmn:file-link.... - model.resolveFileLinksOnLoad(path); return model;