isami100のSwingin' Seasar日記

2004-07-13

従業員照会サービスの作成

ほとんどDaoに委譲するのですが、従業員名のlike検索用に文字列を「%」で囲む処理を行います。

サービスインタフェース:EmployeeRefService.java

サービス実装:EmployeeRefServiceImpl.java

diconファイル:EmployeeRefService.dicon

検索アクション

検索アクションと事前条件チェックを分けて実装。事前条件では、FROMとTOの大小関係を検査します。また、メッセージをいくつか追加。

SearchAction.java
package s2study.app.employeeref.client;

import java.util.Date;
import javax.swing.JFormattedTextField;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import s2study.app.employeeref.service.EmployeeRefService;

public class SearchAction implements MethodInterceptor {

    private EmployeeRefService _employeeRefService;
    private JFormattedTextField _txfEmpNoFrom;
    private JFormattedTextField _txfEmpNoTo;
    private JFormattedTextField _txfEmpName;
    private JFormattedTextField _txfHireDateFrom;
    private JFormattedTextField _txfHireDateTo;
    private JFormattedTextField _txfSalaryFrom;
    private JFormattedTextField _txfSalaryTo;

    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        return _employeeRefService.findEmployees(
            (Number) _txfEmpNoFrom.getValue(),
            (Number) _txfEmpNoTo.getValue(),
            convertNull( (String) _txfEmpName.getText() ),
            (Date) _txfHireDateFrom.getValue(),
            (Date) _txfHireDateTo.getValue(),
            (Number) _txfSalaryFrom.getValue(),
            (Number) _txfSalaryTo.getValue());
    }

    private String convertNull(String value) {
        if (value != null && value.length() > 0) {
            return value;
        }
        else {
            return null;
        }
    }

    public void setEmployeeRefService(EmployeeRefService employeeRefService) {
        _employeeRefService = employeeRefService;
    }

    public void setEmpNoFromField(JFormattedTextField txfEmpNoFrom) {
        _txfEmpNoFrom = txfEmpNoFrom;
    }
    public void setEmpNoToField(JFormattedTextField txfEmpNoTo) {
        _txfEmpNoTo = txfEmpNoTo;
    }
    public void setEmpNameField(JFormattedTextField txfEmpName) {
        _txfEmpName = txfEmpName;
    }
    public void setHireDateFromField(JFormattedTextField txfHireDateFrom) {
        _txfHireDateFrom = txfHireDateFrom;
    }
    public void setHireDateToField(JFormattedTextField txfHireDateTo) {
        _txfHireDateTo = txfHireDateTo;
    }
    public void setSalaryFromField(JFormattedTextField txfSalaryFrom) {
        _txfSalaryFrom = txfSalaryFrom;
    }
    public void setSalaryToField(JFormattedTextField txfSalaryTo) {
        _txfSalaryTo = txfSalaryTo;
    }
}
SearchRequirement.java
package s2study.app.employeeref.client;

import java.util.Date;
import javax.swing.JFormattedTextField;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.seasar.framework.exception.SRuntimeException;

public class SearchRequirement implements MethodInterceptor {

    private JFormattedTextField _txfEmpNoFrom;
    private JFormattedTextField _txfEmpNoTo;
    private JFormattedTextField _txfHireDateFrom;
    private JFormattedTextField _txfHireDateTo;
    private JFormattedTextField _txfSalaryFrom;
    private JFormattedTextField _txfSalaryTo;

    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        checkEmpNoConsistency();
        checkHireDateConsistency();
        checkSalaryConsistency();
        return methodInvocation.proceed();
    }

    private void checkEmpNoConsistency() {
        try {
            Number empNoFrom = (Number) _txfEmpNoFrom.getValue();
            Number empNoTo = (Number) _txfEmpNoTo.getValue();
            if (empNoFrom != null && empNoTo != null) {
                if (empNoFrom.intValue() > empNoTo.intValue()) {
                    throw new SRuntimeException(
                        "EAPP0102",
                        new Object[] { "EMP NO(FROM) <= EMP NO(TO)" });
                }
            }
        }
        catch (ClassCastException cce) {
            throw new SRuntimeException("EAPP0101", new Object[] { "EMP NO" });
        }
    }

    private void checkHireDateConsistency() {
        try {
            Date hireDateFrom = (Date) _txfHireDateFrom.getValue();
            Date hireDateTo = (Date) _txfHireDateTo.getValue();
            if (hireDateFrom != null && hireDateTo != null) {
                if (hireDateFrom.getTime() > hireDateTo.getTime()) {
                    throw new SRuntimeException(
                        "EAPP0102",
                        new Object[] { "HIRE DATE(FROM) <= HIRE DATE(TO)" });
                }
            }
        }
        catch (ClassCastException cce) {
            throw new SRuntimeException(
                "EAPP0101",
                new Object[] { "HIRE DATE" });
        }
    }

    private void checkSalaryConsistency() {
        try {
            Number salaryFrom = (Number) _txfSalaryFrom.getValue();
            Number salaryTo = (Number) _txfSalaryTo.getValue();
            if (salaryFrom != null && salaryTo != null) {
                if (salaryFrom.floatValue() > salaryTo.floatValue()) {
                    throw new SRuntimeException(
                        "EAPP0102",
                        new Object[] { "SALARY(FROM) <= SALARY(TO)" });
                }
            }
        }
        catch (ClassCastException cce) {
            throw new SRuntimeException("EAPP0101", new Object[] { "SALARY" });
        }
    }

    public void setEmpNoFromField(JFormattedTextField txfEmpNoFrom) {
        _txfEmpNoFrom = txfEmpNoFrom;
    }
    public void setEmpNoToField(JFormattedTextField txfEmpNoTo) {
        _txfEmpNoTo = txfEmpNoTo;
    }
    public void setHireDateFromField(JFormattedTextField txfHireDateFrom) {
        _txfHireDateFrom = txfHireDateFrom;
    }
    public void setHireDateToField(JFormattedTextField txfHireDateTo) {
        _txfHireDateTo = txfHireDateTo;
    }
    public void setSalaryFromField(JFormattedTextField txfSalaryFrom) {
        _txfSalaryFrom = txfSalaryFrom;
    }
    public void setSalaryToField(JFormattedTextField txfSalaryTo) {
        _txfSalaryTo = txfSalaryTo;
    }

}
APPMessages.properties
EAPP0001=user id or password is invalid.

EAPP0101={0} is invalid.
EAPP0102=it must be that {0}.

EAPP9999=Unexpected error occured.\n detail:{0}

IAPP0001={0} logined successful.

デバッグ用後処理Interceptor

アクション実行後の後処理として、アクションのリターンを標準出力に出力するInterceptorを作ります。

DebugConclusion.java
package s2study.ui.interceptors;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class DebugConclusion implements MethodInterceptor {

    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        Object ret = methodInvocation.proceed();
        System.out.println("******** DebugConlusion Dump ********");
        dump(ret);
        return ret;
    }

    private void dump(Object ret) {
        if (ret instanceof List) {
            dump((List) ret);
        }
        else if (ret instanceof Object[]) {
            dump((Object[]) ret);
        }
        else if (ret instanceof Map) {
            dump((Map) ret);
        }
        else {
            System.out.println(ret);
        }
    }

    private void dump(List ret) {
        Iterator iter = ret.iterator();
        while (iter.hasNext()) {
            dump(iter.next());
        }
    }

    private void dump(Object[] ret) {
        for (int i = 0; i < ret.length; i++) {
            dump(ret[i]);
        }
    }

    private void dump(Map ret) {
        StringBuffer sbuff = new StringBuffer('{');
        Iterator iter = ret.keySet().iterator();
        while (iter.hasNext()) {
            Object key = iter.next();
            Object value = ret.get(key);
            sbuff.append(key).append(':').append(value).append(',');
        }
        sbuff.deleteCharAt(sbuff.length()).append('}');
        System.out.println(sbuff.toString());
    }

}

diconファイル設定

フレーム用diconファイルのアクション定義部分を変更。

EmployeeRefFrame.dicon(変更箇所のみ)
    <!-- Interceptors -->
    <component name="centering" class="s2study.ui.interceptors.CenteringInterceptor"/>
    <component name="exitAction" class="s2study.app.action.ExitAction"/>
    <component name="messageBoxHandler" class="s2study.ui.interceptors.MessageBoxThrowsInterceptor"/>

    <component name="searchRequirement" class="s2study.app.employeeref.client.SearchRequirement">
        <property name="empNoFromField">txfEmpNoFrom</property>
        <property name="empNoToField">txfEmpNoTo</property>
        <property name="hireDateFromField">txfHireDateFrom</property>
        <property name="hireDateToField">txfHireDateTo</property>
        <property name="salaryFromField">txfSalaryFrom</property>
        <property name="salaryToField">txfSalaryTo</property>
    </component>
    <component name="searchAction" class="s2study.app.employeeref.client.SearchAction">
        <property name="employeeRefService">employeeRefService</property>
        <property name="empNoFromField">txfEmpNoFrom</property>
        <property name="empNoToField">txfEmpNoTo</property>
        <property name="empNameField">txfEmpName</property>
        <property name="hireDateFromField">txfHireDateFrom</property>
        <property name="hireDateToField">txfHireDateTo</property>
        <property name="salaryFromField">txfSalaryFrom</property>
        <property name="salaryToField">txfSalaryTo</property>
    </component>
    <component name="searchConclusion" class="s2study.ui.interceptors.DebugConclusion"/>
    <component name="searchAsyncronizer" class="s2study.ui.interceptors.AsyncInterceptor">
        <aspect pointcut="taskFailed">messageBoxHandler</aspect>
        <aspect pointcut="taskFinished">searchConclusion</aspect>
    </component>

    <!-- Listeners -->
    <component name="searchTrigger" class="s2study.ui.ActionAdapter">
        <initMethod>#self.putValue(@javax.swing.Action@NAME, "Search")</initMethod>
        <initMethod name="putValue">
            <arg>@javax.swing.Action@ACCELERATOR_KEY</arg>
            <arg>@javax.swing.KeyStroke@getKeyStroke(@java.awt.event.KeyEvent@VK_F3, 0)</arg>
        </initMethod>
        <initMethod>#self.putValue(@javax.swing.Action@MNEMONIC_KEY, @java.awt.event.KeyEvent@VK_S)</initMethod>
        <aspect pointcut="actionPerformed">searchAsyncronizer</aspect>
        <aspect pointcut="actionPerformed">searchRequirement</aspect>
        <aspect pointcut="actionPerformed">searchAction</aspect>
    </component>

HSQLDBを起動して、画面からSerchを実行すると、条件に一致したものがコンソールに出力されるようになりました。

実行結果(条件未指定)
DEBUG 2004-07-13 15:41:38,110 [Thread-3] SELECT emp.*, dept.dname dname_0, dept.loc loc_0 FROM emp, dept
WHERE
  emp.deptno = dept.deptno
  
  
  
  
  
  
  
ORDER BY
  emp.empno

DEBUG 2004-07-13 15:41:38,407 [Thread-3] 論理的なコネクションを閉じました
 ******** DebugConlusion Dump ********
7369, SMITH, CLERK, 7902, 1980-12-17 00:00:00.0, 800.0, null, 20 {20, RESEARCH, DALLAS}
7499, ALLEN, SALESMAN, 7698, 1981-02-20 00:00:00.0, 1600.0, 300.0, 30 {30, SALES, CHICAGO}
7521, WARD, SALESMAN, 7698, 1981-02-22 00:00:00.0, 1250.0, 500.0, 30 {30, SALES, CHICAGO}
7566, JONES, MANAGER, 7839, 1981-04-02 00:00:00.0, 2975.0, null, 20 {20, RESEARCH, DALLAS}
7654, MARTIN, SALESMAN, 7698, 1981-09-28 00:00:00.0, 1250.0, 1400.0, 30 {30, SALES, CHICAGO}
7698, BLAKE, MANAGER, 7839, 1981-05-01 00:00:00.0, 2850.0, null, 30 {30, SALES, CHICAGO}
7782, CLARK, MANAGER, 7839, 1981-06-09 00:00:00.0, 2450.0, null, 10 {10, ACCOUNTING, NEW YORK}
7788, SCOTT, ANALYST, 7566, 1982-12-09 00:00:00.0, 3000.0, null, 20 {20, RESEARCH, DALLAS}
7839, KING, PRESIDENT, null, 1981-11-17 00:00:00.0, 5000.0, null, 10 {10, ACCOUNTING, NEW YORK}
7844, TURNER, SALESMAN, 7698, 1981-09-08 00:00:00.0, 1500.0, 0.0, 30 {30, SALES, CHICAGO}
7876, ADAMS, CLERK, 7788, 1983-01-12 00:00:00.0, 1100.0, null, 20 {20, RESEARCH, DALLAS}
7900, JAMES, CLERK, 7698, 1981-12-03 00:00:00.0, 950.0, null, 30 {30, SALES, CHICAGO}
7902, FORD, ANALYST, 7566, 1981-12-03 00:00:00.0, 3000.0, null, 20 {20, RESEARCH, DALLAS}
7934, MILLER, CLERK, 7782, 1982-01-23 00:00:00.0, 1300.0, null, 10 {10, ACCOUNTING, NEW YORK}
実行結果(EMP NAME に"AL"を指定)
DEBUG 2004-07-13 15:41:49,875 [Thread-5] 論理的なコネクションを取得しました
DEBUG 2004-07-13 15:41:49,875 [Thread-5] SELECT emp.*, dept.dname dname_0, dept.loc loc_0 FROM emp, dept
WHERE
  emp.deptno = dept.deptno
  
  
         and emp.ename like  '%AL%'
  
  
  
  
ORDER BY
  emp.empno

DEBUG 2004-07-13 15:41:50,016 [Thread-5] 論理的なコネクションを閉じました
 ******** DebugConlusion Dump ********
7499, ALLEN, SALESMAN, 7698, 1981-02-20 00:00:00.0, 1600.0, 300.0, 30 {30, SALES, CHICAGO}

MirekMirek2012/06/04 04:38Too many compliments too ltilte space, thanks!

qtbthkysqtbthkys2012/06/04 15:09kWcInr <a href="http://aanzyqxthytk.com/">aanzyqxthytk</a>

oqkukogszoqkukogsz2012/06/05 10:12fyWNOT , [url=http://wdbcviwmctbn.com/]wdbcviwmctbn[/url], [link=http://xqlvtkmirrsu.com/]xqlvtkmirrsu[/link], http://hpqhtennpdig.com/

lvugrtlrlvugrtlr2012/06/05 20:40qVLEEd , [url=http://udottvcudcpv.com/]udottvcudcpv[/url], [link=http://pslfzkhooenk.com/]pslfzkhooenk[/link], http://mowfmffqsnth.com/