Becoming a D2 developer on the job

Step 2: Create the appropriate data structure for the method parameters

The first step is the determine the appropriate data structure, to record the parameters of the concerned MCDA method.

In our example, the parameters are weights; there may exist one weight per criterion and per decision maker. Therefore, we only need one table, named Param and containing items with the following structure

  • a primary key, named OID
  • a weight, named Weight and begin an unsigned float
  • a criterion id, named Criterion and linked to criteria table
  • a user id, named User and linked to users table

Extend plugin.xml with the data table

In the new project, open the file src/main/resources/plugin.xml. And modify the <tableAdditions> section such that it contains only the following lines

  <tableAddition>
        <table>Param</table>
        <columns>
                <column>
                        <name>OID</name>
                        <type>INT(10)</type>
                        <javaType>java.lang.Integer</javaType>
                        <hidden>true</hidden>
                        <primaryKey>true</primaryKey>
                </column>
                <column>
                        <name>Weight</name>
                        <type>FLOAT UNSIGNED</type>
                        <display>Weight</display>
                        <javaType>java.lang.Float</javaType>
                        <primaryKey>false</primaryKey>
                </column>
                <column>
                        <name>Criterion</name>
                        <type>INT(10)</type>
                        <display>Criterion</display>
                        <javaType>java.lang.Integer</javaType>
                        <primaryKey>false</primaryKey>
                        <references>Criteria(OID)</references>
                </column>
                <column>
                        <name>User</name>
                        <type>INT(10)</type>
                        <display>User</display>
                        <javaType>java.lang.Integer</javaType>
                        <primaryKey>false</primaryKey>
                        <references>User(OID)</references>
                </column>
        </columns>
  </tableAddition>

This will allow the plug-in to create the appropriate table in the database.

Update the data model and the DAOs

You will need to modify three files

  1. src/main/java/org/decisiondeck/mytest/model/MyBean.java
  2. src/main/java/org/decisiondeck/mytest/model/dao/MyBeanDao.java
  3. src/main/java/org/decisiondeck/mytest/MytestPlugin.java

Note: you may wish to refactor java file, renaming them to more specific names, e.g. WeightParam.java. instead of MyBean.java

  1. Open the file src/main/java/org/decisiondeck/mytest/model/MyBean.java.

    Remove the Name field of MyBean (as well as its getter and setter function) and add the following ones (as well as their getter and setter functions):

        private Float weight;
        private ICriteria criterion;
        private User user;
    

    It will then be necessary to add the following imports:

    import org.decisiondeck.model.evaluation.ICriteria;
    import org.decisiondeck.model.User;
    

    To complete the modification of the model, you still need to modify the toString function, removing reference to the Name field.

  2. Open the file src/main/java/org/decisiondeck/mytest/model/dao/MyBeanDao.java.

    This file has to be updated, such that it concerns the table we precisely defined in the file plugin.xml. Therefore, the getEntityTable function has to return "Param".

    On the other hand, the getAll and update functions have to address the appropriate SQL queries to select or update the table. In other words, the queries have to be modified to consider the fields we defined in plugin.xml.

    Finally, we will need to be able to select parameters for a given user. Functions have to be designed with that respect.

    The definition of the MyBeanDao class should look like:

     public class MyBeanDao extends MytestDao
     {
            
        @Override
        protected String getEntityTable()
        {
            return "Param";
        }
    
        public List getAll() throws DaoException
        {
            return getAll(null);
        }
        
        public List getAll(User user) throws DaoException
        {
            try
            {
                List<MyBean> list = new ArrayList<MyBean>();
                
                SelectQuery q = QueryFactory.getConnectedFactory().newSelectQuery();
                q.setDefaultTable(getTableName());
                
                String oidField = q.addSelectField("OID");
                String weightField = q.addSelectField("Weight");
                String criterionField = q.addSelectField("Criterion");
                String userField = q.addSelectField("User");
                
                if (user != null)
                {
                    q.addWhereEquality("User", user.getOid());
                }
                
                PracticalResultSet rs = q.executeSelect();
                
                while (rs.next())
                {
                    int oid = rs.getInt(oidField);
    
                    MyBean bean = new MyBean(oid);
                    
                    bean.setOid(rs.getInt(oidField));
                    bean.setWeight(rs.getFloat(weightField));
                    
                    bean.setCriterion(CriteriaService.getAllCriterias().getCriteria(rs.getInt(criterionField, true)));
                    bean.setUser(UserService.getService().getUser(rs.getInt(userField)));
                    
                    list.add(bean);
                }
                
                q.close();
                
                return list;
            }
            catch (Exception e)
            {
                throw new DaoException(e);
            }
        }
    
        public void update(Object o) throws DaoException
        {
            try
            {
                MyBean bean = (MyBean) o;
                
                InsertOrUpdateorDeleteQuery query = QueryFactory.getConnectedFactory().newUpdateQuery();
                query.setDefaultTable(getTableName());
                
                query.addSetEquality("Weight", bean.getWeight());
                query.addSetEquality("Criterion", bean.getCriterion().getKey());
                query.addSetEquality("User", bean.getUser().getOid());
                
                query.addWhereEquality("OID", bean.getOid());
                
                query.executeInsertUpdateOrDelete();
            }
            catch (Exception e)
            {
                throw new DaoException(e);
            }
        }
        
     }
    

    It will be necessary to add the following imports:

    import org.decisiondeck.model.User;
    import org.decisiondeck.service.CriteriaService;
    import org.decisiondeck.service.UserService;
    
  3. Open file src/main/java/org/decisiondeck/mytest/MytestPlugin.java

    You just need to check if the appropriate Daos are registered and unregistered. It has probably to be modified; look carefully to the dao fullname.