Becoming a D2 developer on the job

Step 3: Create the appropriate data service

Inform Maven and Eclipse about D2-services

Before creating services, we need to provide Eclipse and Maven with information of the services of Decision-Deck.

  • Right click on your project (e.g. d2-mytest-plugin), and select Properties. In Java Build Path->Projects, add d2-computation-plugin.
  • In src/main/resources/plugin.xml, add the following requirement
      <require>
          <id>org.decisiondeck.Computation</id>
          <packageNames>
              <packageName>org.decisiondeck.computation</packageName>
          </packageNames>
      </require>
    
  • In pom.xml, add the following dependency
      <dependency>
          <groupId>org.decisiondeck.plugins</groupId>
          <artifactId>d2-computation-plugin</artifactId>
          <version>1.0</version>
      </dependency>
    

Add a data service

Although it is possible to access the DAO directly, it is recommended to only access them through the service layer.

Create a new package, named org.decisiondeck.mytest.service. In this package, create a new class named MyBeanService and extending AbstractService.

Eclipse should build a new java file, with some pre-defined functions. This new class will be responsible for creating new parameter (called AbstractEntity), as well as recovering parameters from the Dao. It makes sense to note that in our example, parameters are user-dependent; therefore, creating and getting functions should take care of user information. The service is also responsible for registering itself to the plugin.

Since our service follows the singleton pattern, it must have the information about its instance.

The following lines are appropriate to design this service.

import org.decisiondeck.mytest.model.MyBean;
import org.decisiondeck.model.User;
import org.decisiondeck.dao.DaoException;
import org.decisiondeck.service.ServiceException;
import java.util.List;
import java.util.ArrayList;
import org.decisiondeck.mytest.model.dao.MyBeanDao;
import org.decisiondeck.mytest.MytestPlugin;

public class MyBeanService extends AbstractService {

        private static final MyBeanService instance = new MyBeanService();
        
        public static MyBeanService getInstance()
        {
                return instance;
        }
        
        @Override
        protected AbstractEntity createEntity(int oid) {
                return new MyBean(oid);
        }
        
        public MyBean createEntity(User user)
        {
                return createEntity(user,true);
        }
        
        public MyBean createEntity(User user, boolean update)
        {
                try
                {
                        int oid = getDao().add();
                        MyBean bean = (MyBean) createEntity(oid);
                        bean.setUser(user);
                        if (update)
                        {
                                update(bean);
                        }
                        return bean;
                }
                catch (DaoException e)
                {
                        throw new ServiceException("Unable to create entity", e);
                }
        }
        
        @SuppressWarnings("unchecked")
        public List<AbstractEntity> getAll()
        {
                return new ArrayList<AbstractEntity>();
        }
        
        @SuppressWarnings("unchecked")
        public List<AbstractEntity> getAll(User user)
        {
                try
                {
                        List beans = new ArrayList();
                        if (user==null)
                        {
                                beans = super.getAll();
                        }
                        else
                        {
                                beans = ((MyBeanDao) getDao()).getAll(user);
                        }
                        
                        return beans;
                }
                catch (Exception e)
                {
                        throw new ServiceException(e);
                }
        }
        

        @Override
        public Class<?> getEntityClass() {
                return MyBean.class;
        }
        
        private MyBeanService()
        {
                super();
                MytestPlugin.register(this);
        }

}

We need also to modify org.decisiondeck.mytest/MytestPlugin.java, to add the register function, allowing to register services.

import org.decisiondeck.computation.service.IComputationService;
import org.decisiondeck.computation.service.ServiceState;
import java.util.ArrayList;
import java.util.List;

public class MytestPlugin extends DefaultPlugin
{

        (...)
        
    private static List<IComputationService> services = new ArrayList<IComputationService>();
    
    public static void register(IComputationService service)
    {
        services.add(service);
        if (service.state()==ServiceState.STOPPED)
        {
                service.start();
        }
    }
}