The following documentation will give some basic example of manipulation of the Viewpoint Collaborative API . You can also refer to the advanced collaborative use case documentation to get additional informations about customization of default collaborative behavior.
Opening a collaborative session with Viewpoint API is exactly the same as opening a local session : you can use the
fr.obeo.dsl.viewpoint.business.api.session.factory.SessionFactory
or the
fr.obeo.dsl.viewpoint.business.api.session.SessionManager
.
// Step 1 : define the session URI
URI sessionResourceURI = URI.createPlatformResourceURI("myProject/session.aird");
// Step 2 : get the corresponding session
Session session = SessionFactory.createSession(sessionResourceURI);
// or
Session session = SessionManager.INSTANCE.getSession(sessionResourceURI);
// Step 3 : open the session
session.open();
// Step 4 : cast the session to CollaborativeSession
// to get additional API
if (session instanceof CollaborativeSession){
((CollaborativeSession)session).setCommitOnSave(false);
}
The opened session will implement the
CollaborativeSession interface .
Collaborative Sessions are closed exactly like local sessions : when calling
session.close()
, the
CDOSession
and
CDOTransaction
associated to the collaborative session will automatically be closed.
Implicit locks are automatically released.
Notice that as soon as a Remote Semantic Resource or Representation is referenced by the Collaborative Session, the session
will connect itself to the corresponding CDO Repository. However, if you want a Collaborative Session to be connected by default (even if empty) to a certain repository, you can refer to the
documentation of the connect(RepositoryConfig) method ..
The following example adds a
CDOResource
as a semantic model of the Collaborative Session created in the
previous section:
// Step 1 : get the repository manager of the CDORepository containing the resource to add
CDORepositoryManager manager = CDORepositoryManagerRegistry.getRepositoryManager("localhost", "2036", "repo1");
// Step 2 : get the cdo resource to add
// Version 2.A : if you want to modify the CDO Resource before adding it
CDOResource semanticResource = manager.getOrCreateTransaction(session).getOrCreateResource("myResourceToAdd.xmi");
// You can modify the CDO Resource here : add children...
URI semanticResourceURI = semanticResource.getURI();
// Version 2.B : if you do not want to modify the CDOResource
URI semanticResourceURI = URI.createURI("cdo://repo1/myResourceToAdd.xmi");
// Step 3 : add the resource using the AddRemoteResourceCommand
Command addSemanticResourceCommand = new AddRemoteResourceCommand((CollaborativeSession) selectedCollaborativeSession, manager, semanticResourceURI);
session.getTransactionalEditingDomain().getCommandStack().execute(addSemanticResourceCommand);
Now the Collaborative Session references a remote semantic resource, from which local and remote representations can be created.
In the
previous section, we added a semantic model located on a CDO Repository to a Collaborative Session.
Now we want our user to be able to create remote representations, that he will share in real-time with other users. These remote representations will be stored inside a
remote representations set (nothing more than a standard
fr.obeo.dsl.viewpoint.DAnalysis
located on a CDO Repository).
// Step 1 : get the resource that will contain remote representations
CDOResource remoteRepresentationsResource = manager.getOrCreateTransaction(session).getOrCreateResource("myRemoteDAnalysis.aird");
// notice that you can also write
CDOResource remoteRepresentationsResource =((CollaborativeSession)session).getRemoteResource("myRemoteDAnalysis.aird");
// Step 2 : create the root of the resource
DAnalysis representationContainer = ViewpointFactory.eINSTANCE.createDAnalysis();
remoteRepresentationsResource.getContents().add(representationContainer);
// Step 3 : add the representation container to the session
session.addRemoteRepresentationsSet("myRemoteDAnalysis.aird");
// As the resource already exists (created by an other user for example)
// we just have to add it to our session
session.addRemoteRepresentationsSet("myRemoteDAnalysis.aird");
The
session.setDefaultRepresentationContainer(URI danalysisURI)
method allows you to specify in which
DAnalysis
new representations should be stored. Using this method, you can easily forbidd the creation of local representations (to make sure that all created representations are located on a CDO Repository).
// Step 1 : get the Remote Representations Set URI
URI remoteRepresentationsSetURI = manager.getOrCreateTransaction(session).getOrCreateResource("myRemoteDAnalysis.aird").getURI();
// or
URI remoteRepresentationsSetURI = CDOURIUtil.createResourceURI(manager.getOrCreateTransaction(session), "myRemoteDAnalysis.aird");
// Step 2 : define the default container of all new representations
collaborativeSession.setDefaultRepresentationContainer(remoteRepresentationsSetURI);
If you want to define complex rules to determine in which representation container should new representations be created, you can use the
collaborativeSession.setAnalysisSelector()
method.
Once you defined where
new representations should be created , you just have to create the remote representation using the
DialectManager
.
// Step 1 : get the semantic resource on which the representation should be created
CDOResource semanticRemoteResource = session.getRemoteResource("myRemoteResource.xmi");
EObject representationRoot = semanticRemoteResource.getContents().get(0);
// Step 2 : create the representation
DialectManager.INSTANCE.createRepresentation("myNewRemoteRepresentationName", representationRoot, representationDescription, session, new NullProgressMonitor());
// Step 3 : commit changes
session.save(new NullProgressMonitor());
Viewpoint provide API for uploading and downloading models on/from the CDO Repository.