Skip to content

Commit

Permalink
update isDirty flag
Browse files Browse the repository at this point in the history
Issue imixs#246
  • Loading branch information
rsoika committed May 14, 2023
1 parent 2350fb0 commit 98e2adb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, String> options = action.getOptions();
boolean bNeedsClientLayout = Boolean.parseBoolean(options.get("needsClientLayout"));
// resolve file location....
Expand All @@ -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());
}

}
Expand Down Expand Up @@ -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());
}

}
Expand Down
45 changes: 33 additions & 12 deletions open-bpmn.metamodel/src/main/java/org/openbpmn/bpmn/BPMNModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public class BPMNModel {
protected Set<Signal> signals = null;
protected Set<Message> messages = null;
protected Element collaborationElement = null;
private boolean isDirty = false;

private final Map<BPMNNS, String> URI_BY_NAMESPACE = new HashMap<>();
private final Map<BPMNNS, String> PREFIX_BY_NAMESPACE = new HashMap<>();
Expand Down Expand Up @@ -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<Participant> getParticipants() {
if (participants == null) {
participants = new LinkedHashSet<Participant>();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1337,13 +1357,16 @@ public void resolveFileLinksOnSave(Path path) {
* <![CDATA[file://imixs.script.js]]></bpmn2:script>
*
* 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();

Expand All @@ -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
Expand All @@ -1390,7 +1412,6 @@ public void resolveFileLinksOnLoad(Path path) {
}
}
logger.fine("...resolveFileLinksOnLoad took " + (System.currentTimeMillis() - l) + "ms");

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 98e2adb

Please sign in to comment.