Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cfb6d75aaa5ed5245d2140d4bce5425bffcf8d08 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*******************************************************************************
 * Copyright (c) 2010 THALES GLOBAL SERVICES.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.sirius.ecore.extender.tool.internal;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceImpl;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;

import org.eclipse.sirius.ecore.extender.business.api.permission.IPermissionAuthority;
import org.eclipse.sirius.ecore.extender.business.api.permission.PermissionAuthorityRegistry;

/**
 * Class responsible for resolving some references.
 * 
 * @author cbrun
 * 
 */
public class ReferencesResolver {

    private Predicate<EReference> filter;

    private ResourceSet set;

    /**
     * Create a new references resolver.
     * 
     * @param filter
     *            filter telling which references to resolve.
     * @param set
     *            resource set to resolve.
     */
    public ReferencesResolver(ResourceSet set, Predicate<EReference> filter) {
        this.filter = filter;
        this.set = set;
    }

    /**
     * Resolve the references.
     * 
     * @param monitor
     *            monitor to track progress.
     */
    public void resolve(IProgressMonitor monitor) {
        final IPermissionAuthority authority = PermissionAuthorityRegistry.getDefault().getPermissionAuthority(set);
        final List<Resource> cachedIdsResources = ReferencesResolver.prepareResolveAll(set, authority);
        monitor.beginTask("Resolving non contained references", set.getResources().size());
        for (Resource res : Lists.newArrayList(set.getResources())) {
            doResolveAll(res);
            monitor.worked(1);
        }
        monitor.done();
        ReferencesResolver.unprepareResolveAll(authority, cachedIdsResources);
    }

    /**
     * Visits all proxies in the resource and tries to resolve them.
     * 
     * @param resource
     *            the objects to visit.
     * @param filter
     *            the filter telling which references to resolve.
     */
    private void doResolveAll(Resource resource) {
        Iterator<EObject> i = resource.getAllContents();
        while (i.hasNext()) {
            EObject eObject = i.next();
            resolveCrossReferences(eObject);
        }
    }

    private void resolveCrossReferences(EObject eObject) {
        Iterator<EReference> it = Iterators.filter(eObject.eClass().getEAllReferences().iterator(), filter);
        while (it.hasNext()) {
            eObject.eGet(it.next());
        }
    }

    /**
     * Prepare the whole resolving by setting accelerating ID resolving maps.
     * 
     * @param set
     *            the resource set to resolve.
     * @param authority
     *            the permission authority.
     * @return the list of resources with a cached ID map.
     */
    public static List<Resource> prepareResolveAll(final ResourceSet set, final IPermissionAuthority authority) {
        if (authority != null) {
            authority.setListening(false);
        }
        final List<Resource> cachedIdsResources = new LinkedList<Resource>();
        Iterator<Resource> iterResources = set.getResources().iterator();
        while (iterResources.hasNext()) {
            final Resource resource = iterResources.next();
            if (resource instanceof ResourceImpl && ((ResourceImpl) resource).getIntrinsicIDToEObjectMap() == null) {
                ((ResourceImpl) resource).setIntrinsicIDToEObjectMap(new HashMap<String, EObject>());
                cachedIdsResources.add(resource);
            }
        }
        return cachedIdsResources;
    }

    /**
     * Reinit resources changed having a chached ID map.
     * 
     * @param authority
     *            the permission authority.
     * @param cachedIdsResources
     *            the cached ID resources.
     */
    public static void unprepareResolveAll(final IPermissionAuthority authority, final List<Resource> cachedIdsResources) {
        Iterator<Resource> iterResourcesBis = cachedIdsResources.iterator();
        while (iterResourcesBis.hasNext()) {
            final Resource resource = iterResourcesBis.next();
            if (resource instanceof ResourceImpl && ((ResourceImpl) resource).getIntrinsicIDToEObjectMap() != null) {
                ((ResourceImpl) resource).setIntrinsicIDToEObjectMap(null);
            }
        }
        if (authority != null) {
            authority.setListening(true);
        }
    }

}

Back to the top