とりあえず、S2JDBCToDataAccessRule を実装してみたけど、testDataFieldType08と、testDataFieldType09で 以下の例外が発生してしまった。うーん、何がわるいんだろう。。。test用にs2jdbc.diconや、allTestService.diconを読み込むようにしたつもりですが、どこがいってしまっているかも。。。(ソースはhttps://sisioh.googlecode.com/svn/sandbox/trunk/buri-core はここ)
org.escafe.buri.exception.BuriDataFieldErrorException: [EBRI0001]DataField org.escafe.buri.entity.BuriTestINTでInsertが必要です。Daoを自動設定している場合は、Daoがあるかどうか、diconをincludeしているかどうかを確認してください at org.escafe.buri.compiler.util.impl.rules.DataAccessorProcRule.finishCheck(DataAccessorProcRule.java:41) at org.escafe.buri.compiler.util.impl.BuriDataFieldCompilePreprocessorImpl.finishProcess(BuriDataFieldCompilePreprocessorImpl.java:55) at org.escafe.buri.compiler.util.impl.BuriDataFieldCompilePreprocessorImpl.checkDataAccess(BuriDataFieldCompilePreprocessorImpl.java:48) at org.escafe.buri.compiler.util.impl.BuriDataFieldCompilePreprocessorImpl.preprocess(BuriDataFieldCompilePreprocessorImpl.java:33) at org.escafe.buri.compiler.util.impl.DataFieldS2JDBCTest.testDataFieldType08(DataFieldS2JDBCTest.java:120) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at junit.framework.TestCase.runTest(TestCase.java:168) at org.seasar.framework.unit.S2FrameworkTestCase.doRunTest(S2FrameworkTestCase.java:519) at org.seasar.extension.unit.S2TestCase.doRunTest(S2TestCase.java:103) at org.seasar.framework.unit.S2FrameworkTestCase.runBare(S2FrameworkTestCase.java:308) at junit.framework.TestResult$1.protect(TestResult.java:110) at junit.framework.TestResult.runProtected(TestResult.java:128) at junit.framework.TestResult.run(TestResult.java:113) at junit.framework.TestCase.run(TestCase.java:124) at junit.framework.TestSuite.runTest(TestSuite.java:232) at junit.framework.TestSuite.run(TestSuite.java:227) at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:81) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
package org.escafe.buri.compiler.util.impl.rules; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Iterator; import java.util.List; import javax.persistence.Id; import javax.persistence.Table; import org.escafe.buri.common.util.BuriConfiguration; import org.escafe.buri.common.util.ClassDefUtil; import org.escafe.buri.common.util.ClassDefUtilImpl; import org.escafe.buri.event.util.caller.DataAccessRuleEventCaller; import org.escafe.buri.oouo.internal.structure.BuriDataFieldType; import org.seasar.framework.beans.BeanDesc; import org.seasar.framework.beans.PropertyDesc; import org.seasar.framework.beans.factory.BeanDescFactory; import org.seasar.framework.container.S2Container; import org.seasar.framework.log.Logger; import org.seasar.framework.util.ClassUtil; import org.seasar.framework.util.StringUtil; public class S2JDBCToDataAccessRule extends AbstractBuriDataFieldProcRule { private static Logger logger = Logger.getLogger(S2JDBCToDataAccessRule.class); protected String serviceKeyName = "service"; public final String SERVICEKEY = "service"; private BuriConfiguration configuration; private S2Container container; private ClassDefUtil classDefUtil; private DataAccessRuleEventCaller dataAccessRuleEventCaller; @Override public boolean getRequiredRule(BuriDataFieldType src) { if (hasName(src, "preprocess")) { return false; } return true; } public boolean isRequiredNegotiate(BuriDataFieldType src) { if (src.getKeys().size() == 0) { return true; } if (!hasName(src, "select")) { return true; } if (!hasName(src, "insert")) { return true; } if (!hasName(src, "update")) { return true; } if (!hasName(src, "delete")) { return true; } // if (!hasName(src, "tableName")) { // return true; // } return false; } @Override public boolean fstCheckProcess(BuriDataFieldType src) { if (!isRequiredNegotiate(src)) { return false; } if (src.getCache().containsKey(serviceKeyName)) { } else if (hasName(src, SERVICEKEY)) { src.getCache().put(serviceKeyName, getNameVal(src, SERVICEKEY)); } dataAccessRuleEventCaller.determinedRule(this, src); return true; } @Override public boolean process(BuriDataFieldType src) { // if( ! src.getCache().containsKey(serviceKeyName) ) { // return false; // } if (src.getCache().containsKey(serviceKeyName + "_end")) { return false; } negotiateService(src); dataAccessRuleEventCaller.endNegotiateDao(this, src); src.getCache().put(serviceKeyName + "_end", Boolean.TRUE); return false; } protected void negotiateService(BuriDataFieldType src) { String dtoClassName = src.getId(); String serviceName = getServiceName(src, dtoClassName); if (serviceName != null) { src.getCache().put(serviceKeyName, serviceName); Class serviceClass = container .getRoot() .getComponentDef(serviceName) .getComponentClass(); pkeySetup(src); if (src.getKeys().size() == 1) { String keyName = src.getKeys().keySet().toArray()[0].toString(); BeanDesc beanDesc = BeanDescFactory .getBeanDesc(ClassUtil.forName(dtoClassName)); Class tgtClass = beanDesc.getPropertyDesc(keyName).getPropertyType(); src.getCache().put(serviceKeyName + "_KeyType", tgtClass); src.getCache().put(serviceKeyName + "_KeyName", keyName); findAndSetupAllMethod(src, beanDesc, serviceClass); } } } protected void findAndSetupAllMethod(BuriDataFieldType src, BeanDesc beanDesc, Class serviceClass) { Method methods[] = serviceClass.getDeclaredMethods(); for (Method method : methods) { String methodName = method.getName(); selectSetup(src, method, methodName, beanDesc); updateSetup(src, method, methodName, beanDesc); deleteSetup(src, method, methodName, beanDesc); selectManySetup(src, method, methodName, beanDesc); insertSetup(src, method, methodName, beanDesc); tableNameSetup(src); } } protected void tableNameSetup(BuriDataFieldType src) { if (!StringUtil.isEmpty(src.getTableName())) { return; } Class tgt = ClassUtil.forName(src.getId()); String tableName = tgt.getSimpleName(); Table table = (Table) tgt.getAnnotation(Table.class); if (table != null) { tableName = table.name(); } if (!StringUtil.isEmpty(tableName)) { src.setTableName(tableName); } } protected void selectManySetup(BuriDataFieldType src, Method method, String methodName, BeanDesc beanDesc) { if (!StringUtil.isEmpty(src.getSelectMany())) { return; } if (isSelectManyMethod(src, method, methodName)) { String serviceName = src.getCache().get(serviceKeyName).toString(); src.setSelectMany(serviceName + "." + methodName + "(#data)"); } } protected boolean isSelectManyMethod(BuriDataFieldType src, Method method, String methodName) { if (methodName.startsWith("get") || methodName.startsWith("select")) { if (method.getParameterTypes().length == 1) { if (method.getParameterTypes()[0].isAssignableFrom(List.class)) { return true; } } } return false; } protected void deleteSetup(BuriDataFieldType src, Method method, String methodName, BeanDesc beanDesc) { if (!StringUtil.isEmpty(src.getDelete())) { return; } if (isDeleteMethod(src, method, methodName)) { String serviceName = src.getCache().get(serviceKeyName).toString(); src.setDelete(serviceName + "." + methodName + "(#data)"); } } protected boolean isDeleteMethod(BuriDataFieldType src, Method method, String methodName) { if (methodName.startsWith("del")) { if (method.getParameterTypes().length == 1) { String clazzName = classDefUtil.getClassName(method.getParameterTypes()[0]); if (clazzName.equals(src.getId())) { return true; } } } return false; } protected void updateSetup(BuriDataFieldType src, Method method, String methodName, BeanDesc beanDesc) { if (!StringUtil.isEmpty(src.getUpdate())) { return; } if (isUpdateMethod(src, method, methodName)) { String serviceName = src.getCache().get(serviceKeyName).toString(); src.setUpdate(serviceName + "." + methodName + "(#data)"); } } protected boolean isUpdateMethod(BuriDataFieldType src, Method method, String methodName) { if (methodName.startsWith("update")) { if (method.getParameterTypes().length == 1) { String clazzName = classDefUtil.getClassName(method.getParameterTypes()[0]); if (clazzName.equals(src.getId())) { return true; } } } return false; } protected void insertSetup(BuriDataFieldType src, Method method, String methodName, BeanDesc beanDesc) { if (!StringUtil.isEmpty(src.getInsert())) { return; } if (isInsertMethod(src, method, methodName)) { String serviceName = src.getCache().get(serviceKeyName).toString(); src.setInsert(serviceName + "." + methodName + "(#data)"); } } protected boolean isInsertMethod(BuriDataFieldType src, Method method, String methodName) { if (methodName.startsWith("insert")) { if (method.getParameterTypes().length == 1) { String clazzName = classDefUtil.getClassName(method.getParameterTypes()[0]); if (clazzName.equals(src.getId())) { return true; } } } return false; } protected void selectSetup(BuriDataFieldType src, Method method, String methodName, BeanDesc beanDesc) { if (!StringUtil.isEmpty(src.getSelect())) { return; } if (isSelectMethod(src, method, methodName)) { String keyName = src.getCache().get(serviceKeyName + "_KeyName").toString(); String serviceName = src.getCache().get(serviceKeyName).toString(); src.setSelect(serviceName + "." + methodName + "(#data." + keyName + ")"); } } protected boolean isSelectMethod(BuriDataFieldType src, Method method, String methodName) { if (methodName.startsWith("get") || methodName.startsWith("select") || methodName.startsWith("find")) { if (method.getParameterTypes().length == 1) { Class tgtClass = (Class) src.getCache().get(serviceKeyName + "_KeyType"); if (method.getParameterTypes()[0].equals(tgtClass)) { return true; } } } return false; } protected void pkeySetup(BuriDataFieldType src) { if ((src.getKeys().size() > 0) || hasName(src, "pkey")) { return; } Class<?> entityClass = ClassUtil.forName(src.getId()); BeanDesc bd = BeanDescFactory.getBeanDesc(entityClass); for (int i = 0; i < bd.getPropertyDescSize(); i++) { PropertyDesc pd = bd.getPropertyDesc(i); Field f = pd.getField(); Id id = f.getAnnotation(Id.class); if (id != null) { String condition = createPkeyCondition(pd.getPropertyName(), pd .getPropertyType()); src.getKeys().put(pd.getPropertyName(), condition); } } } protected String createPkeyCondition(String pkeyName, Class propType) { String condition = null; if (propType.equals(Long.TYPE)) { condition = pkeyName + " != 0"; } else if (propType.isAssignableFrom(Number.class)) { condition = pkeyName + " != 0"; } else { condition = pkeyName + " != null"; } return condition; } protected String getServiceName(BuriDataFieldType src, String dtoClassName) { String dao = null; String shtName = createServiceName(src, dtoClassName); String shtName2 = shtName.substring(0, 1).toLowerCase() + shtName.substring(1); if (container.getRoot().hasComponentDef(shtName)) { dao = shtName; } else if (container.getRoot().hasComponentDef(shtName2)) { dao = shtName2; } else { dao = findServiceClass(shtName, dtoClassName); } if (dao == null) { dao = findServiceClassFromNamespace(shtName, dtoClassName); } return dao; } protected String findServiceClassFromNamespace(String shtName, String dtoClassName) { String service = null; List serviceNamespace = configuration.getValList("Namespace"); Iterator ite = serviceNamespace.iterator(); while (ite.hasNext()) { String nameSpace = ite.next().toString(); String componentName = nameSpace + "." + shtName; if (container.getRoot().hasComponentDef(componentName)) { return componentName; } } return service; } protected String findServiceClass(String shtName, String dtoClassName) { String service = null; List servicePackageName = configuration.getValList("ServicePackageName"); Iterator ite = servicePackageName.iterator(); while (ite.hasNext()) { String pacName = ite.next().toString(); String fullName = pacName + "." + shtName; service = classNameToService(fullName); if (service != null) { break; } } /* * if(dao == null) { dao = genarateDaoClass(shtName,dtoClassName); } */ return service; } /* * protected String genarateDaoClass(String shtName,String dtoClassName) { * Class tgtClass = ClassUtil.forName(dtoClassName); String packageName = * tgtClass.getPackage().getName(); packageName = * packageName.replaceAll("dto","dao") + "." + shtName; packageName = * classNameToDao(packageName); return packageName; } */ protected String classNameToService(String fullName) { String service = null; if (ClassDefUtilImpl.isClassName(fullName)) { Class serviceClass = ClassUtil.forName(fullName); if (container.getRoot().hasComponentDef(serviceClass)) { service = container .getRoot() .getComponentDef(serviceClass) .getComponentName(); } } return service; } protected String createServiceName(BuriDataFieldType src, String dtoClassName) { if (hasName(src, SERVICEKEY)) { return getNameVal(src, SERVICEKEY); } if (dtoClassName.indexOf("$$") > -1) { int enhansPos = dtoClassName.indexOf("$$"); dtoClassName = dtoClassName.substring(0, enhansPos); } Class tgtClass = ClassUtil.forName(dtoClassName); String shtName = ClassUtil.getShortClassName(tgtClass); if (shtName.length() > 3) { if (shtName.substring(shtName.length() - 3).equalsIgnoreCase("Dto")) { shtName = shtName.substring(0, shtName.length() - 3); } } if (shtName.length() > 6) { if (shtName.substring(shtName.length() - 6).equalsIgnoreCase( "Entity")) { shtName = shtName.substring(0, shtName.length() - 6); } } shtName = shtName + "Service"; return shtName; } public S2Container getContainer() { return container; } public void setContainer(S2Container container) { this.container = container; } public BuriConfiguration getConfiguration() { return configuration; } public void setConfiguration(BuriConfiguration configuration) { this.configuration = configuration; } public ClassDefUtil getClassDefUtil() { return classDefUtil; } public void setClassDefUtil(ClassDefUtil classDefUtil) { this.classDefUtil = classDefUtil; } public DataAccessRuleEventCaller getDataAccessRuleEventCaller() { return dataAccessRuleEventCaller; } public void setDataAccessRuleEventCaller( DataAccessRuleEventCaller dataAccessRuleEventCaller) { this.dataAccessRuleEventCaller = dataAccessRuleEventCaller; } }
/* * 作成日: 2006/07/05 * */ package org.escafe.buri.compiler.util.impl; import org.escafe.buri.common.util.BuriConfiguration; import org.escafe.buri.compiler.util.BuriDataFieldCompilePreprocessor; import org.escafe.buri.exception.BuriDataFieldErrorException; import org.escafe.buri.oouo.internal.structure.BuriDataFieldType; import org.escafe.buri.oouo.internal.structure.BuriExtendedAttributeType; import org.seasar.extension.unit.S2TestCase; import org.seasar.framework.beans.PropertyNotFoundRuntimeException; public class DataFieldS2JDBCTest extends S2TestCase { private String PATH = "buri/dicon/buri-share.dicon"; private BuriDataFieldCompilePreprocessor bdfcp; private BuriConfiguration configuration; public DataFieldS2JDBCTest(String name) { super(name); } @Override protected void setUp() throws Exception { super.setUp(); include(PATH); include("org/escafe/buri/dicon/allTestService.dicon"); } public void testDataFieldType01() { BuriDataFieldType dft = new BuriDataFieldType(); dft.setId("org.escafe.buri.entity.BuriTestINT"); setBuriDataFieldType(dft, "pkey", "testID,testID!=0"); setBuriDataFieldType(dft, "insert", "service.insert(#data)"); setBuriDataFieldType(dft, "update", "service.update(#data)"); setBuriDataFieldType(dft, "select", "service.select(#data)"); setBuriDataFieldType(dft, "delete", "service.delete(#data)"); setBuriDataFieldType(dft, "tableName", "BuriTestINT"); BuriDataFieldType dst = bdfcp.preprocess(dft); assertEquals(dst.getKeys().toString(), "{testID=testID!=0}"); assertEquals(dst.getInsert(), "service.insert(#data)"); assertEquals(dst.getUpdate(), "service.update(#data)"); assertEquals(dst.getSelect(), "service.select(#data)"); assertEquals(dst.getDelete(), "service.delete(#data)"); assertEquals(dst.getTableName(), "BuriTestINT"); } public void testDataFieldType04() { BuriDataFieldType dft = new BuriDataFieldType(); dft.setId("org.escafe.buri.entity.BuriTestINT"); setBuriDataFieldType(dft, "pkey", "id,id!=0"); setBuriDataFieldType(dft, "insert", "service.insert(#data)"); setBuriDataFieldType(dft, "update", "service.update(#data)"); setBuriDataFieldType(dft, "select", "service.select(#data)"); setBuriDataFieldType(dft, "delete", "service.delete(#data)"); setBuriDataFieldType(dft, "tableName", "BuriTestINT"); try { bdfcp.preprocess(dft); fail(); } catch (BuriDataFieldErrorException ex) { assertTrue(true); } catch (PropertyNotFoundRuntimeException ex) { assertTrue(true); } } public void testDataFieldType05() { BuriDataFieldType dft = new BuriDataFieldType(); dft.setId("org.escafe.buri.entity.BuriTestINT"); setBuriDataFieldType(dft, "preprocess", "service.select(#data)"); BuriDataFieldType dst = bdfcp.preprocess(dft); assertEquals(dst.getPreprocess(), "service.select(#data)"); } public void testDataFieldType06() { BuriDataFieldType dft = new BuriDataFieldType(); dft.setId("org.escafe.buri.entity.BuriTestINT"); setBuriDataFieldType(dft, "preprocess", "service.select(#data)"); setBuriDataFieldType(dft, "pkey", "testID,testID!=0"); setBuriDataFieldType(dft, "insert", "service.insert(#data)"); setBuriDataFieldType(dft, "update", "service.update(#data)"); setBuriDataFieldType(dft, "select", "service.select(#data)"); setBuriDataFieldType(dft, "delete", "service.delete(#data)"); setBuriDataFieldType(dft, "tableName", "BuriTestINT"); BuriDataFieldType dst = bdfcp.preprocess(dft); assertEquals(dst.getKeys().toString(), "{testID=testID!=0}"); assertEquals(dst.getInsert(), "service.insert(#data)"); assertEquals(dst.getUpdate(), "service.update(#data)"); assertEquals(dst.getSelect(), "service.select(#data)"); assertEquals(dst.getDelete(), "service.delete(#data)"); assertEquals(dst.getPreprocess(), "service.select(#data)"); } public void testDataFieldType07() { BuriDataFieldType dft = new BuriDataFieldType(); configuration.addValue("DtoPackageName", "org.escafe.buri.entity"); dft.setId("BuriTestINT"); setBuriDataFieldType(dft, "pkey", "testID,testID!=0"); setBuriDataFieldType(dft, "insert", "service.insert(#data)"); setBuriDataFieldType(dft, "update", "service.update(#data)"); setBuriDataFieldType(dft, "select", "service.select(#data)"); setBuriDataFieldType(dft, "delete", "service.delete(#data)"); setBuriDataFieldType(dft, "tableName", "BuriTestINT"); BuriDataFieldType dst = bdfcp.preprocess(dft); assertEquals(dst.getKeys().toString(), "{testID=testID!=0}"); assertEquals(dst.getInsert(), "service.insert(#data)"); assertEquals(dst.getUpdate(), "service.update(#data)"); assertEquals(dst.getSelect(), "service.select(#data)"); assertEquals(dst.getDelete(), "service.delete(#data)"); } public void testDataFieldType08() { BuriDataFieldType dft = new BuriDataFieldType(); configuration.addValue("DtoPackageName", "org.escafe.buri.entity"); configuration.addValue("ServicePackageName", "org.escafe.buri.service"); dft.setId("BuriTestINT"); BuriDataFieldType dst = bdfcp.preprocess(dft); System.out.println(dst.getKeys().toString()); assertEquals(dst.getKeys().toString(), "{testID=testID != 0}"); assertEquals(dst.getInsert(), "BuriTestINTService.insert(#data)"); assertEquals(dst.getUpdate(), "BuriTestINTService.update(#data)"); assertEquals( dst.getSelect(), "BuriTestINTService.getBuriTestINT(#data.testID)"); assertEquals(dst.getDelete(), "BuriTestINTService.delete(#data)"); assertEquals( dst.getSelectMany(), "BuriTestINTService.getBuriTestINTByIds(#data)"); } public void testDataFieldType09() { BuriDataFieldType dft = new BuriDataFieldType(); dft.setId("org.escafe.buri.entity.BuriTestINT"); BuriDataFieldType dst = bdfcp.preprocess(dft); System.out.println(dst.getKeys().toString()); assertEquals(dst.getKeys().toString(), "{testID=testID != 0}"); assertEquals(dst.getInsert(), "BuriTestINTService.insert(#data)"); assertEquals(dst.getUpdate(), "BuriTestINTService.update(#data)"); assertEquals( dst.getSelect(), "BuriTestINTService.getBuriTestINT(#data.testID)"); assertEquals(dst.getDelete(), "BuriTestINTService.delete(#data)"); assertEquals( dst.getSelectMany(), "BuriTestINTService.getBuriTestINTByIds(#data)"); } private void setBuriDataFieldType(BuriDataFieldType dft, String name, String val) { BuriExtendedAttributeType beat = new BuriExtendedAttributeType(); beat.setName(name); beat.setValue(val); dft.addExtendedAttribute(beat); } }