テーブルモジュールの設計

ブログおよびブログ記事のテーブルモジュール BlogTableBlogEntryTable を作成します。

BlogTable

BlogTable には対応するデータアクセスオブジェクト BlogDao が存在するため、 SimpleTableModule を継承して実装を再利用しています。

public class BlogTable extends SimpleTableModule<Blog> {

    /** ブログデータアクセスオブジェクト */
    private BlogDao blogDao;

    @Override
    protected SimpleFindable<Blog> getSimpleDao() {
        return blogDao;
    }

    /**
     * コードを持つブログを取得します。
     *
     * @param code コード
     * @return ブログ
     */
    public Blog findByCode(String code) {
        return blogDao.findByCode(code);
    }

    /**
     * 指定した所有者のブログリストを取得します。
     *
     * @param ownerId 所有者のユーザ ID
     * @return ブログリスト
     */
    public List<Blog> findByOwnerId(int ownerId) {
        return blogDao.findByOwnerId(ownerId);
    }

    // setters
}

BlogEntryTable

対応するデータアクセスオブジェクトを持たない BlogEntryTable はインターフェース DomainFactory を直接実装します。

public class BlogEntryTable {

    /** メッセージテーブル */
    private MessageTable messageTable;

    /** ドメインファクトリ */
    private DomainFactory domainFactory;

    /**
     * ブログ記事を取得します。
     *
     * @param id ブログ記事 ID
     * @return ブログ記事
     */
    public BlogEntry find(int id) {
        Message entry = messageTable.find(id);
        if (entry == null) {
            return null;
        }
        return domainFactory.prototype(new BlogEntry(entry));
    }

    /**
     * 記事コードを持つブログ記事を取得します。
     *
     * @param code 記事コード(メッセージコード)
     * @return ブログ記事、見つからなければ null
     */
    public BlogEntry findByCode(String code) {
        Message entry = messageTable.findByCode(code);
        if (entry == null) {
            return null;
        }
        return domainFactory.prototype(new BlogEntry(entry));
    }

    // setters
}

Bean 定義

この時点で applicationContext-domain.xml にテーブルモジュールを定義しておきます。

<bean id="communication.blogTable" class="org.unitedfront2.domain.communication.BlogTable">
  <property name="blogDao" ref="blogDao"/>
</bean>

<bean id="communication.blogEntryTable" class="org.unitedfront2.domain.communication.BlogEntryTable">
  <property name="domainFactory" ref="domainFactory"/>
  <property name="messageTable" ref="communication.messageTable"/>
</bean>