use of org.archicontribs.modelrepository.grafico.GraficoModelLoader in project archi-modelrepository-plugin by archi-contribs.
the class RestoreCommitAction method run.
@Override
public void run() {
// Offer to save the model if open and dirty
IArchimateModel model = getRepository().locateModel();
if (model != null && IEditorModelManager.INSTANCE.isModelDirty(model)) {
if (!offerToSaveModel(model)) {
return;
}
}
boolean response = MessageDialog.openConfirm(fWindow.getShell(), Messages.RestoreCommitAction_0, Messages.RestoreCommitAction_1);
if (!response) {
return;
}
// Delete the content folders first
try {
File modelFolder = new File(getRepository().getLocalRepositoryFolder(), IGraficoConstants.MODEL_FOLDER);
FileUtils.deleteFolder(modelFolder);
modelFolder.mkdirs();
File imagesFolder = new File(getRepository().getLocalRepositoryFolder(), IGraficoConstants.IMAGES_FOLDER);
FileUtils.deleteFolder(imagesFolder);
imagesFolder.mkdirs();
} catch (IOException ex) {
displayErrorDialog(Messages.RestoreCommitAction_0, ex);
return;
}
// Walk the tree and get the contents of the commit
try (Repository repository = Git.open(getRepository().getLocalRepositoryFolder()).getRepository()) {
try (TreeWalk treeWalk = new TreeWalk(repository)) {
treeWalk.addTree(fCommit.getTree());
treeWalk.setRecursive(true);
while (treeWalk.next()) {
ObjectId objectId = treeWalk.getObjectId(0);
ObjectLoader loader = repository.open(objectId);
File file = new File(getRepository().getLocalRepositoryFolder(), treeWalk.getPathString());
file.getParentFile().mkdirs();
GraficoUtils.writeObjectToFileWithSystemLineEndings(file, loader);
}
}
} catch (IOException ex) {
displayErrorDialog(Messages.RestoreCommitAction_0, ex);
return;
}
// Reload the model from the Grafico XML files
try {
IArchimateModel graficoModel = new GraficoModelLoader(getRepository()).loadModel();
// If this is null then it failed because of no model in this commit
if (graficoModel == null) {
// Reset
getRepository().resetToRef(IGraficoConstants.REFS_HEADS_MASTER);
MessageDialog.openError(fWindow.getShell(), Messages.RestoreCommitAction_0, Messages.RestoreCommitAction_2);
return;
}
} catch (IOException | GitAPIException ex) {
displayErrorDialog(Messages.RestoreCommitAction_0, ex);
}
// Commit changes
try {
// $NON-NLS-1$ //$NON-NLS-2$
getRepository().commitChanges(Messages.RestoreCommitAction_3 + " '" + fCommit.getShortMessage() + "'", false);
// Save the checksum
getRepository().saveChecksum();
} catch (GitAPIException | IOException ex) {
displayErrorDialog(Messages.RestoreCommitAction_0, ex);
}
notifyChangeListeners(IRepositoryListener.HISTORY_CHANGED);
}
Aggregations