Search in sources :

Example 1 with UnresolvedObject

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;
}
Also used : IIdentifier(com.archimatetool.model.IIdentifier) UnresolvedObject(org.archicontribs.modelrepository.grafico.GraficoModelImporter.UnresolvedObject) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Repository(org.eclipse.jgit.lib.Repository) EObject(org.eclipse.emf.ecore.EObject) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) File(java.io.File) IArchimateModel(com.archimatetool.model.IArchimateModel) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 2 with UnresolvedObject

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();
}
Also used : UnresolvedObject(org.archicontribs.modelrepository.grafico.GraficoModelImporter.UnresolvedObject) EObject(org.eclipse.emf.ecore.EObject)

Example 3 with UnresolvedObject

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;
}
Also used : UnresolvedObject(org.archicontribs.modelrepository.grafico.GraficoModelImporter.UnresolvedObject) IArchimateModel(com.archimatetool.model.IArchimateModel)

Aggregations

UnresolvedObject (org.archicontribs.modelrepository.grafico.GraficoModelImporter.UnresolvedObject)3 IArchimateModel (com.archimatetool.model.IArchimateModel)2 EObject (org.eclipse.emf.ecore.EObject)2 IIdentifier (com.archimatetool.model.IIdentifier)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 ObjectLoader (org.eclipse.jgit.lib.ObjectLoader)1 Repository (org.eclipse.jgit.lib.Repository)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)1