Snippet: Create Model Artifacts

From Eclipsepedia

Jump to: navigation, search

Contents

< To: Tigerstripe_APIs
This snippet illustrates how to create model artifacts through the Tigerstripe M-API.

Because this API is undergoing major re-factoring to leverage EMF, we are providing here 2 versions:

  • with the new EMF-based API.
  • with the soon to be deprecated API

Both examples assume that a Tigerstripe Model project is available.

EMF-based API

The EMF-based API leverages EMF and EMF Transactions to create/edit artifacts. It not only provides a better API but also is better integrated with the multi-threaded environment of Eclipse.

NOTE: the implementation of this new version of the M-API is not complete yet

// Let's assume we have a project
ITigerstripeModelProject project = ...
// Get the model Manager for the project
IModelManager mMgr = project.getModelManager();
// At this stage the default repository is hardcoded to be a PojoModelRepository
// This should be configurable in the future.
IModelRepository repo = mMgr.getDefaultRepository();
// Use the EMF-generated factory
IManagedEntityArtifact nMea = MetamodelFactory.eINSTANCE.createIManagedEntityArtifact();
// Set the Name&Package as the required attributes before the repository can store the Artifact
nMea.setName("Mea");
nMea.setPackage("com.mycompany.testNO");
// Then store it
repo.store(nMea, true);
// At this stage a Pojo src/com/mycompany/testNO/Mea.java was created on disk.

To further modify the Mea object, a transaction will be required (See Snippet: Work with Model Artifacts).

Old API

The old API uses the IArtifactManagerSession object on the project both as a factory to create artifacts as well as to query and look up artifacts in the project.

The old API could only persist Artifacts in POJOs.

// ... assuming a project is there
ITigerstripeModelProject project = ...
// First get the session
IArtifactManagerSession session = project.getArtifactManagerSession();
// Then use it as a factory to make an empty Managed Entity
// Note that default values are populated by the "makeArtifact"
IManagedEntityArtifact mea = (IManagedEntityArtifact) session
  .makeArtifact(IManagedEntityArtifact.class.getName());
// The minimum required attributes before a doSave(..) are Name and Package
mea.setName("Mea");
mea.setPackage("com.mycompany.testON");
mea.doSave(new NullProgressMonitor());
// This API doesn't rely on Eclipse's view of the filesystem (IResources)
// THIS IS MANDATORY so Eclipse picks up the newly created POJO.
IProject iProject = (IProject) project.getAdapter(IProject.class);
iProject.refreshLocal(IResource.DEPTH_INFINITE,
   new NullProgressMonitor());

At this stage a Pojo in src/com/mycompany/testON/Mea.java has been created on disk and should appear in the Tigerstripe Explorer.