-
Notifications
You must be signed in to change notification settings - Fork 85
/
SessionContext.java
153 lines (128 loc) · 4.75 KB
/
SessionContext.java
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
145
146
147
148
149
150
151
152
153
/*******************************************************************************
* Copyright (c) 2006, 2020 THALES GLOBAL SERVICES.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Thales - initial API and implementation
*******************************************************************************/
package org.polarsys.capella.test.framework.context;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.sirius.business.api.session.Session;
import org.polarsys.capella.common.ef.ExecutionManager;
import org.polarsys.capella.common.ef.command.AbstractCommand;
import org.polarsys.capella.common.ef.command.AbstractReadWriteCommand;
import org.polarsys.capella.common.helpers.TransactionHelper;
import org.polarsys.capella.core.commands.preferences.service.AbstractPreferencesInitializer;
import org.polarsys.capella.core.data.capellacore.KeyValue;
import org.polarsys.capella.core.model.handler.command.CapellaResourceHelper;
import org.polarsys.capella.shared.id.handler.IScope;
import org.polarsys.capella.shared.id.handler.IdManager;
/**
*
*/
public class SessionContext {
/**
* Current session for semantic resource and diagram resource.
*/
protected Session session;
/** Map with needed semantic Object, useful */
private Map<String, EObject> semanticObjectMap;
/**
* @param session
*/
public SessionContext(Session session) {
this.session = session;
}
public Session getSession() {
return session;
}
public Map<String, EObject> getSemanticObjectMap() {
if (null == semanticObjectMap) {
semanticObjectMap = new HashMap<String, EObject>();
}
return semanticObjectMap;
}
/**
* Get the Capella Execution manager.
*
* @return a not <code>null</code> execution manager.
*/
public ExecutionManager getExecutionManager() {
return TransactionHelper.getExecutionManager(session);
}
@SuppressWarnings("unchecked")
public <T extends EObject> Collection<T> getSemanticElements(String... objectIdentifiers) {
Collection<T> result = new ArrayList<T>();
for (String value : objectIdentifiers) {
result.add((T) getSemanticElement(value));
}
return result;
}
@SuppressWarnings("unchecked")
public <T extends EObject> T getSemanticElement(String objectIdentifier) {
Map<String, EObject> map = getSemanticObjectMap();
if (!map.containsKey(objectIdentifier)) {
EObject object = IdManager.getInstance().getEObject(objectIdentifier, new IScope() {
@Override
public List<Resource> getResources() {
return session.getSemanticResources().stream().filter(CapellaResourceHelper::isCapellaResource).collect(Collectors.toList());
}
});
putSemanticElement(objectIdentifier, object);
}
return (T) map.get(objectIdentifier);
}
public <T extends EObject> Collection<T> getSemanticElements(Collection<String> objectIdentifiers) {
String[] identifiers = objectIdentifiers.toArray(new String[0]);
return getSemanticElements(identifiers);
}
public void removeSemanticElement(String objectIdentifier) {
getSemanticObjectMap().remove(objectIdentifier);
}
public void putSemanticElement(String objectIdentifier, EObject object) {
getSemanticObjectMap().put(objectIdentifier, object);
}
public void setPreference(String key, boolean value) {
AbstractPreferencesInitializer.preferencesManager.setValue(key, value);
}
public void setReusableComponents(String id) {
final AbstractCommand cmd = new AbstractReadWriteCommand() {
@Override
public void run() {
EObject obj = getSemanticElement(id);
if (obj instanceof KeyValue) {
KeyValue kv = (KeyValue) obj;
kv.setValue("ReusableComponents");
}
}
};
// Let's perform the job
getExecutionManager().execute(cmd);
}
public void setSingletonComponents(String id) {
final AbstractCommand cmd = new AbstractReadWriteCommand() {
@Override
public void run() {
EObject obj = getSemanticElement(id);
if (obj instanceof KeyValue) {
KeyValue kv = (KeyValue) obj;
kv.setValue("SingletonComponents");
}
}
};
// Let's perform the job
getExecutionManager().execute(cmd);
}
}