package com.trs.example.check;

import com.trs.DreamFactory;
import com.trs.TRSWCMBaseTest;
import com.trs.infra.persistent.WCMFilter;
import com.trs.infra.util.CMyException;

public class IEmployerMgrTest extends TRSWCMBaseTest {
    private static org.apache.log4j.Logger logger = org.apache.log4j.Logger
            .getLogger(IEmployerMgrTest.class);

    public IEmployerMgrTest(String name) {
        super(name);
    }

    public void testSave() {
        /*
         * 测试新增
         */
        try {
            // 1 准备测试数据
            Employer employer = Employer.createNewInstance();
            employer.setEName("张三");
            employer.setEmail("zs@trs.com.cn");
            employer.setDepartment("产品二部");

            // 2 保存数据
            IEmployerMgr oEmployerMgr = (IEmployerMgr) DreamFactory
                    .createObjectById("IEmployerMgr");
            oEmployerMgr.save(employer);

            // 3 校验是否保存上了，并且数据对得上
            Employer oNewEmployer = queryBy(employer);
            assertNotNull("没有保存上！", oNewEmployer);
        } catch (Exception ex) {
            logger.error("测试[testSave]出现异常！", ex);
            fail("测试[testSave]出现异常！\n" + CMyException.getStackTraceText(ex));
        }
    }

    public void testDelete() {
        fail("Not yet implemented");
    }

    public void testQuery() {
        fail("Not yet implemented");
    }

    private Employer queryBy(Employer _employer) throws Exception {
        // 1 构造一个检索条件
        WCMFilter filter = new WCMFilter("",
                "EName=? and Email=? and Department=?", "");
        filter.addSearchValues(_employer.getEName());
        filter.addSearchValues(_employer.getEmail());
        filter.addSearchValues(_employer.getDepartment());

        // 2 从数据库中查询
        Employers employers = Employers.openWCMObjs(null, filter);
        if (employers.isEmpty())
            return null;
        return (Employer) employers.getAt(0);
    }

}
