use of org.archicontribs.modelrepository.grafico.GraficoModelImporter.UnresolvedObject in project archi-modelrepository-plugin by archi-contribs.
the class GraficoModelLoader method restoreProblemObjects.
/**
* Find the problem object xml files from the commit history and restore them
* @param unresolvedObjects
* @return
* @throws IOException
*/
private IArchimateModel restoreProblemObjects(List<UnresolvedObject> unresolvedObjects) throws IOException {
fRestoredObjects = new ArrayList<IIdentifier>();
List<String> restoredIdentifiers = new ArrayList<String>();
try (Repository repository = Git.open(fRepository.getLocalRepositoryFolder()).getRepository()) {
try (RevWalk revWalk = new RevWalk(repository)) {
for (UnresolvedObject unresolved : unresolvedObjects) {
String missingFileName = unresolved.missingObjectURI.lastSegment();
String missingObjectID = unresolved.missingObjectURI.fragment();
// Already got this one
if (restoredIdentifiers.contains(missingObjectID)) {
continue;
}
boolean found = false;
// Reset RevWalk
revWalk.reset();
ObjectId id = repository.resolve(IGraficoConstants.REFS_HEADS_MASTER);
if (id != null) {
revWalk.markStart(revWalk.parseCommit(id));
}
// Iterate all commits
for (RevCommit commit : revWalk) {
try (TreeWalk treeWalk = new TreeWalk(repository)) {
treeWalk.addTree(commit.getTree());
treeWalk.setRecursive(true);
// We can't use a PathFilter for the file name as its path is not correct
while (!found && treeWalk.next()) {
// File is found
if (treeWalk.getPathString().endsWith(missingFileName)) {
// Save file
ObjectId objectId = treeWalk.getObjectId(0);
ObjectLoader loader = repository.open(objectId);
File file = new File(fRepository.getLocalRepositoryFolder(), treeWalk.getPathString());
file.getParentFile().mkdirs();
GraficoUtils.writeObjectToFileWithSystemLineEndings(file, loader);
restoredIdentifiers.add(missingObjectID);
found = true;
}
}
}
if (found) {
break;
}
}
}
revWalk.dispose();
}
}
// Then re-import
GraficoModelImporter importer = new GraficoModelImporter(fRepository.getLocalRepositoryFolder());
IArchimateModel graficoModel = importer.importAsModel();
// do this again
graficoModel.setFile(fRepository.getTempModelFile());
// Collect restored objects
for (Iterator<EObject> iter = graficoModel.eAllContents(); iter.hasNext(); ) {
EObject element = iter.next();
for (String id : restoredIdentifiers) {
if (element instanceof IIdentifier && id.equals(((IIdentifier) element).getId())) {
fRestoredObjects.add((IIdentifier) element);
}
}
}
return graficoModel;
}
use of org.archicontribs.modelrepository.grafico.GraficoModelImporter.UnresolvedObject in project archi-modelrepository-plugin by archi-contribs.
the class GraficoModelLoader method deleteProblemObjects.
@SuppressWarnings("unused")
private void deleteProblemObjects(List<UnresolvedObject> unresolvedObjects, IArchimateModel model) throws IOException {
for (UnresolvedObject unresolved : unresolvedObjects) {
String parentID = unresolved.parentObject.getId();
EObject eObject = ArchimateModelUtils.getObjectByID(model, parentID);
if (eObject != null) {
EcoreUtil.remove(eObject);
}
}
// And re-export to grafico xml files
GraficoModelExporter exporter = new GraficoModelExporter(model, fRepository.getLocalRepositoryFolder());
exporter.exportModel();
}
use of org.archicontribs.modelrepository.grafico.GraficoModelImporter.UnresolvedObject in project archi-modelrepository-plugin by archi-contribs.
the class GraficoModelLoader method loadModel.
/**
* Load the model
* @return
* @throws IOException
*/
public IArchimateModel loadModel() throws IOException {
fRestoredObjects = null;
GraficoModelImporter importer = new GraficoModelImporter(fRepository.getLocalRepositoryFolder());
IArchimateModel graficoModel = importer.importAsModel();
if (graficoModel == null) {
return null;
}
// Store ids of open diagrams
List<String> openModelIDs = null;
// Close the real model if it is already open
IArchimateModel model = fRepository.locateModel();
if (model != null) {
// Store ids of open diagrams
openModelIDs = getOpenDiagramModelIdentifiers(model);
IEditorModelManager.INSTANCE.closeModel(model);
}
// Set file name on the grafico model so we can locate it
graficoModel.setFile(fRepository.getTempModelFile());
// Resolve missing objects
List<UnresolvedObject> unresolvedObjects = importer.getUnresolvedObjects();
if (unresolvedObjects != null) {
graficoModel = restoreProblemObjects(unresolvedObjects);
}
// Open it with the new grafico model, this will do the necessary checks and add a command stack and an archive manager
IEditorModelManager.INSTANCE.openModel(graficoModel);
// And Save it to the temp file
IEditorModelManager.INSTANCE.saveModel(graficoModel);
// Re-open editors, if any
reopenEditors(graficoModel, openModelIDs);
return graficoModel;
}
Aggregations