Initialize the Library Editor
- Set dependencies of EMF model and editor plugins
- Enable JPOX Support for the EMF model project
- Initialization
- Resource Factory setting
- Create valid EMF Objects
Set dependencies of EMF model and editor plugins
The EMF persistency plugin (org.eclipse.emf.teneo.jpox) has to be added to the dependencies tab of the plugin.xml of both the library and the library.editor plugin project.
Enable JPOX Support for the EMF model project
Next JPOX support needs to be enabled for the library (model) project. Right click on the project and in the JPOX menu select 'Add JPOX Support' and then check 'Enable Auto Enhancement'.
Note: see here if you get the following message in the problem view: build path contains duplicate entry: .....
Initialization
To initialize the EMF/JPOX layer the following code has to be added to static inner class Implementation in the LibraryEditorPlugin class.
Note this code should not be added if you want to start the editor using the Resource Utility
public void start(BundleContext context) throws Exception { org.eclipse.osgi.framework.debug.Debug.DEBUG_GENERAL = true; Properties properties = new Properties(); properties.setProperty(PMFConfiguration.JDO_DATASTORE_DRIVERNAME_PROPERTY, "com.mysql.jdbc.Driver"); properties.setProperty(PMFConfiguration.JDO_DATASTORE_URL_PROPERTY, "jdbc:mysql://127.0.0.1:3306/mylibrary"); properties.setProperty(PMFConfiguration.JDO_DATASTORE_USERNAME_PROPERTY, "root"); properties.setProperty(PMFConfiguration.JDO_DATASTORE_PASSWORD_PROPERTY, "root"); // create/register the JpoxDataStore, set the db props and the epackages to persist, initialize creates // the database String pmfName = "library"; // the name of the JpoxDataStore JpoxDataStore jpoxDataStore = JpoxHelper.INSTANCE.createRegisterDataStore(pmfName); jpoxDataStore.setProperties(properties); jpoxDataStore.setEPackages(new EPackage[]{LibraryPackage.eINSTANCE}); jpoxDataStore.initialize(); super.start(context); }
Remarks:
- The properies have to be changed to contain your own database connection information.
- The above source code uses the database 'mylibrary'. This database has to exist (but can be empty).
- The name of the datastore is chosen on purpose. The extension of the resource name (library in this case) is used to find the datastore, so therefore here the name library is chosen.
Resource Factory setting
For this tutorial an EMF JPOX resource factory has to be specified. This is done by setting the library element of the org.eclipse.emf.ecore.extension_parser extension to: org.eclipse.emf.teneo.jpox.resource.JPOXResourceFactory (see image below). You can find this extension in the plugin.xml in your model project.
Remarks:
- Note that the JPOX resource is normally loaded without a JPOXResource.DS_NAME_PARAM parameter in the url. If there is no dsname parameter in the url then the JPOX resource will use the extension of the filename to find the PersistenceManagerFactory.
Create valid EMF Objects
The standard generated EMF Library example creates invalid objects. For example when you run the editor you can save a Library object with an empty name while this is a required element. The EMF/JPOX layer is more precise and will not allow this.
To prevent this add the following code in the performFinish method of the LibraryModelWizard class in the library editor project. This has to be added around line 216 after the rootObject variable has been set:
if (rootObject instanceof Library) { ((Library)rootObject).setName("My Library"); } else if (rootObject instanceof Book) { ((Book)rootObject).setTitle("My Title"); } else if (rootObject instanceof Writer) { ((Writer)rootObject).setName("My Name"); }
Please click here to go to the next step.