Search in sources :

Example 6 with IIdentifier

use of com.archimatetool.model.IIdentifier in project archi by archimatetool.

the class TreeStateHelper method saveStateOnApplicationClose.

/**
 * Save expanded state of tree elements on Application close
 * @param memento
 */
void saveStateOnApplicationClose(IMemento memento) {
    Hashtable<File, String> map = new Hashtable<File, String>();
    IMemento expandedMem = memento.createChild(MEMENTO_EXPANDED);
    for (Object element : fTreeViewer.getVisibleExpandedElements()) {
        if (element instanceof IIdentifier && element instanceof IArchimateModelObject) {
            // Only store if saved in a file
            File file = ((IArchimateModelObject) element).getArchimateModel().getFile();
            if (file != null) {
                String id = ((IIdentifier) element).getId();
                String string = map.get(file);
                if (string == null) {
                    string = id;
                } else {
                    string += ELEMENT_SEP_CHAR + id;
                }
                map.put(file, string);
            }
        }
    }
    for (File file : map.keySet()) {
        IMemento elementMem = expandedMem.createChild(MEMENTO_MODEL);
        elementMem.putString(MEMENTO_FILE, file.getAbsolutePath());
        elementMem.putString(MEMENTO_ELEMENTS, map.get(file));
    }
}
Also used : IIdentifier(com.archimatetool.model.IIdentifier) IArchimateModelObject(com.archimatetool.model.IArchimateModelObject) Hashtable(java.util.Hashtable) IArchimateModelObject(com.archimatetool.model.IArchimateModelObject) EObject(org.eclipse.emf.ecore.EObject) File(java.io.File) IMemento(org.eclipse.ui.IMemento)

Example 7 with IIdentifier

use of com.archimatetool.model.IIdentifier in project archi-modelrepository-plugin by archi-contribs.

the class GraficoModelImporter method importAsModel.

/**
 * Import the grafico XML files as a IArchimateModel
 * @throws IOException
 */
public IArchimateModel importAsModel() throws IOException {
    // Create folders for model and images
    File modelFolder = new File(fLocalRepoFolder, IGraficoConstants.MODEL_FOLDER);
    modelFolder.mkdirs();
    File imagesFolder = new File(fLocalRepoFolder, IGraficoConstants.IMAGES_FOLDER);
    imagesFolder.mkdirs();
    // If the top folder.xml does not exist then there is nothing to import, so return null
    if (!(new File(modelFolder, IGraficoConstants.FOLDER_XML)).isFile()) {
        return null;
    }
    // Create ResourceSet
    fResourceSet = new ResourceSetImpl();
    // $NON-NLS-1$
    fResourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", new XMLResourceFactoryImpl());
    // Reset the ID -> Object lookup table
    fIDLookup = new HashMap<String, IIdentifier>();
    // Load the Model from files (it will contain unresolved proxies)
    fModel = loadModel(modelFolder);
    // Remove model from its resource (needed to save it back to a .archimate file)
    fResourceSet.getResource(URI.createFileURI((new File(modelFolder, IGraficoConstants.FOLDER_XML)).getAbsolutePath()), true).getContents().remove(fModel);
    // Resolve proxies
    fUnresolvedObjects = null;
    resolveProxies();
    // Load images
    loadImages(imagesFolder);
    return fModel;
}
Also used : ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) XMLResourceFactoryImpl(org.eclipse.emf.ecore.xmi.impl.XMLResourceFactoryImpl) IIdentifier(com.archimatetool.model.IIdentifier) File(java.io.File)

Example 8 with IIdentifier

use of com.archimatetool.model.IIdentifier in project archi-modelrepository-plugin by archi-contribs.

the class GraficoModelImporter method resolve.

/**
 * Check if 'object' is a proxy. if yes, replace it with real object from mapping table.
 */
private EObject resolve(IIdentifier object, IIdentifier parent) {
    if (object != null && object.eIsProxy()) {
        URI objectURI = EcoreUtil.getURI(object);
        String objectID = EcoreUtil.getURI(object).fragment();
        // Get proxy object
        IIdentifier newObject = fIDLookup.get(objectID);
        // If proxy has not been resolved
        if (newObject == null) {
            // Add to list
            if (fUnresolvedObjects == null) {
                fUnresolvedObjects = new ArrayList<UnresolvedObject>();
            }
            fUnresolvedObjects.add(new UnresolvedObject(objectURI, parent));
        }
        return newObject == null ? object : newObject;
    } else {
        return object;
    }
}
Also used : IIdentifier(com.archimatetool.model.IIdentifier) URI(org.eclipse.emf.common.util.URI)

Example 9 with IIdentifier

use of com.archimatetool.model.IIdentifier 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 10 with IIdentifier

use of com.archimatetool.model.IIdentifier in project archi-modelrepository-plugin by archi-contribs.

the class GraficoModelLoader method getRestoredObjectsAsString.

/**
 * @return The list of resolved objects as a message string or null
 */
public String getRestoredObjectsAsString() {
    if (fRestoredObjects == null) {
        return null;
    }
    String s = Messages.GraficoModelLoader_0;
    for (IIdentifier id : fRestoredObjects) {
        if (id instanceof INameable) {
            String name = ((INameable) id).getName();
            String className = id.eClass().getName();
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            s += "\n" + (StringUtils.isSet(name) ? name + " (" + className + ")" : className);
        }
    }
    return s;
}
Also used : IIdentifier(com.archimatetool.model.IIdentifier) INameable(com.archimatetool.model.INameable)

Aggregations

IIdentifier (com.archimatetool.model.IIdentifier)13 EObject (org.eclipse.emf.ecore.EObject)8 File (java.io.File)6 IArchimateModel (com.archimatetool.model.IArchimateModel)5 ArrayList (java.util.ArrayList)4 INameable (com.archimatetool.model.INameable)2 IOException (java.io.IOException)2 TreeEditElementRequest (com.archimatetool.editor.views.tree.TreeEditElementRequest)1 IArchimateConcept (com.archimatetool.model.IArchimateConcept)1 IArchimateModelObject (com.archimatetool.model.IArchimateModelObject)1 IArchimateRelationship (com.archimatetool.model.IArchimateRelationship)1 IDiagramModelArchimateComponent (com.archimatetool.model.IDiagramModelArchimateComponent)1 IDiagramModelComponent (com.archimatetool.model.IDiagramModelComponent)1 IDocumentable (com.archimatetool.model.IDocumentable)1 IFolder (com.archimatetool.model.IFolder)1 ITemplate (com.archimatetool.templates.model.ITemplate)1 Hashtable (java.util.Hashtable)1 Iterator (java.util.Iterator)1 UnresolvedObject (org.archicontribs.modelrepository.grafico.GraficoModelImporter.UnresolvedObject)1 URI (org.eclipse.emf.common.util.URI)1