private Method createCRUDMethod( final String name, final Parameter crudParameter, final Return crudReturn) { final Method method = new Method(); method.setName(name); method.setReturn(crudReturn); method.getParameter().add(crudParameter); return method; }
private ClassWriter generateCommandX(final Method method, final String resultClass) throws Exception { final String className = method.getName().substring(0, 1).toUpperCase() + method.getName().substring(1) + "Command"; final ClassWriter classWriter = new ClassWriter( this, getSrcGeneratedDirectory(), GeneratorHelper.getCommandPackage(domain), className); final Constructor constructor = new Constructor(); final Parameter eventBusParameter = new Parameter(); eventBusParameter.setName("eventBus"); eventBusParameter.setClazz("com.ponysdk.core.event.EventBus"); final List<Parameter> parameters = method.getParameter(); final List<Parameter> clonedParameters = new ArrayList<Parameter>(); for (final Parameter parameter : parameters) { final Parameter clonedParameter = new Parameter(); clonedParameter.setClazz(parameter.getClazz()); clonedParameter.setName(parameter.getName()); clonedParameter.setCollection(parameter.getCollection()); clonedParameters.add(clonedParameter); } final Constructor constructor2 = new Constructor(); constructor2.setConstructorParameters(new ArrayList<Parameter>(clonedParameters)); clonedParameters.add(0, eventBusParameter); constructor.setConstructorParameters(clonedParameters); constructor.setSuperConstructorParameters(Arrays.asList(eventBusParameter)); classWriter.addConstructor(constructor); classWriter.addConstructor(constructor2); for (final Parameter param : parameters) { classWriter.addClassMembers(param); } classWriter.addConstants( "private static " + GeneratorHelper.getServiceFullClassName(domain) + " service;"); final StringBuilder template = new StringBuilder(); template.append("@Override\n"); if (resultClass.equals("void") && method.getReturn().getValue() == null) { template.append("protected java.lang.Void execute0() throws Exception {\n"); } else { template.append("protected " + resultClass + " execute0() throws Exception {\n"); } template.append(" if (service == null) {\n"); template.append( " service = com.ponysdk.core.service.PonyServiceRegistry.getPonyService(" + GeneratorHelper.getServiceFullClassName(domain) + ".class);"); template.append(" }\n"); if (resultClass.equals("void") && method.getReturn().getValue() == null) { template.append(" service.%1$s(%2$s);\n"); template.append(" return null;\n"); } else { template.append(" return service.%1$s(%2$s);\n"); } template.append("}\n"); classWriter.addMethod( template.toString(), method.getName(), GeneratorHelper.getParameterNamesToString(method.getParameter())); return classWriter; }
private void generateCRUDMethos() throws Exception { // Insert CRUD methods if (domain.getService() != null && domain.getService().getCrudmethod() != null) { // Return final Return crudReturn = new Return(); crudReturn.setClazz(domain.getService().getCrudmethod().getClazz()); // Parameters final Parameter crudParameter = new Parameter(); crudParameter.setClazz(domain.getService().getCrudmethod().getClazz()); crudParameter.setName(GeneratorHelper.getFirstCharToLower(domain.getName())); final Parameter crudIDParameter = new Parameter(); crudIDParameter.setClazz("long"); crudIDParameter.setName(GeneratorHelper.getFirstCharToLower(domain.getName()) + "ID"); // Add CRUD methods final Method createMethod = createCRUDMethod("create" + domain.getName(), crudParameter, crudReturn); final Method readMethod = createCRUDMethod("read" + domain.getName(), crudIDParameter, crudReturn); final Method updateMethod = createCRUDMethod("update" + domain.getName(), crudParameter, crudReturn); final Method deleteMethod = createCRUDMethod("delete" + domain.getName(), crudIDParameter, new Return()); // Create the implementation class of these CRUD methods if the HibernateDAO is set if (domain.getService().getDao() != null) { final ClassWriter classWriter = new ClassWriter( this, getSrcGeneratedDirectory(), GeneratorHelper.getServerServicePackage(domain), GeneratorHelper.getServiceImplClassName(domain)); classWriter.addImplements(GeneratorHelper.getServiceClassName(domain)); classWriter.setGenerateGetter(true); classWriter.setGenerateSetter(true); // Add static logger classWriter.addConstants( "private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(" + GeneratorHelper.getServiceImplClassName(domain) + ".class);"); // Add service HibernateDAO object final Parameter daoParameter = new Parameter(); daoParameter.setClazz( GeneratorHelper.getDAOPackage(domain) + "." + GeneratorHelper.getDAOClassName(domain)); daoParameter.setName( GeneratorHelper.getFirstCharToLower(GeneratorHelper.getDAOClassName(domain))); classWriter.addClassMembers(daoParameter); for (final Method method : domain.getService().getMethod()) { final String resultClass = GeneratorHelper.getClassName(method.getReturn()); classWriter.addNewLine(); classWriter.addLine("@Override"); classWriter.addLine( "public " + resultClass + " " + method.getName() + "(" + GeneratorHelper.getParameterToString(method.getParameter()) + ") throws Exception {"); if (!"void".equals(resultClass)) { classWriter.addLine(" return null;"); } classWriter.addLine("}"); } final String domainClass = domain.getName(); final String domainDAOParameter = GeneratorHelper.getFirstCharToLower(GeneratorHelper.getDAOClassName(domain)); final String domainParameter = GeneratorHelper.getFirstCharToLower(domain.getName()); final String domainParameterID = domainParameter + "ID"; /* * CRUD method implementation */ // Create classWriter.addLine("@Override"); classWriter.addLine( "public " + GeneratorHelper.getClassName(createMethod.getReturn()) + " " + createMethod.getName() + " (" + GeneratorHelper.getParameterToString(createMethod.getParameter()) + ") throws Exception {"); classWriter.addLine(" " + domainDAOParameter + ".beginTransaction();"); classWriter.addLine(" try {"); classWriter.addLine(" " + domainDAOParameter + ".save(" + domainParameter + ");"); classWriter.addLine(" " + domainDAOParameter + ".commit();"); classWriter.addLine(" } catch (final Exception e) {"); classWriter.addLine( " log.error(\"final Cannot create " + domainClass + " in database\", e);"); classWriter.addLine(" " + domainDAOParameter + ".rollback();"); classWriter.addLine( " throw new Exception(\"Cannot create " + domainClass + " in database\", e);"); classWriter.addLine(" }"); classWriter.addLine(" return " + domainParameter + ";"); classWriter.addLine("}"); // Read classWriter.addLine("@Override"); classWriter.addLine( "public " + GeneratorHelper.getClassName(createMethod.getReturn()) + " " + readMethod.getName() + " (" + GeneratorHelper.getParameterToString(readMethod.getParameter()) + ") throws Exception {"); classWriter.addLine( " " + GeneratorHelper.getClassName(createMethod.getReturn()) + " " + domainParameter + " = null;"); classWriter.addLine(" " + domainDAOParameter + ".beginTransaction();"); classWriter.addLine(" try {"); classWriter.addLine( " " + domainParameter + " = " + domainDAOParameter + ".findById(" + domainParameterID + ");"); classWriter.addLine(" " + domainDAOParameter + ".commit();"); classWriter.addLine(" } catch (final Exception e) {"); classWriter.addLine( " log.error(\"final Cannot find " + domain.getName() + " in database\", e);"); classWriter.addLine(" " + domainDAOParameter + ".rollback();"); classWriter.addLine( " throw new Exception(\"Cannot find " + domain.getName() + " in database\", e);"); classWriter.addLine(" }"); classWriter.addLine(" return " + domainParameter + ";"); classWriter.addLine("}"); // Update classWriter.addLine("@Override"); classWriter.addLine( "public " + GeneratorHelper.getClassName(updateMethod.getReturn()) + " " + updateMethod.getName() + " (" + GeneratorHelper.getParameterToString(updateMethod.getParameter()) + ") throws Exception {"); classWriter.addLine(" " + domainDAOParameter + ".beginTransaction();"); classWriter.addLine(" try {"); classWriter.addLine( " " + domainDAOParameter + ".saveOrUpdate(" + domainParameter + ");"); classWriter.addLine(" " + domainDAOParameter + ".commit();"); classWriter.addLine(" } catch (final Exception e) {"); classWriter.addLine( " log.error(\"final Cannot update " + domainClass + " in database\", e);"); classWriter.addLine(" " + domainDAOParameter + ".rollback();"); classWriter.addLine( " throw new Exception(\"Cannot update " + domainClass + " in database\", e);"); classWriter.addLine(" }"); classWriter.addLine(" return " + domainParameter + ";"); classWriter.addLine("}"); // Delete classWriter.addLine("@Override"); classWriter.addLine( "public " + GeneratorHelper.getClassName(deleteMethod.getReturn()) + " " + deleteMethod.getName() + " (" + GeneratorHelper.getParameterToString(deleteMethod.getParameter()) + ") throws Exception {"); classWriter.addLine(" " + domainDAOParameter + ".beginTransaction();"); classWriter.addLine(" try {"); classWriter.addLine( " " + GeneratorHelper.getClassName(createMethod.getReturn()) + " " + domainParameter + " = " + domainDAOParameter + ".findById(" + domainParameterID + ");"); classWriter.addLine(" " + domainDAOParameter + ".delete(" + domainParameter + ");"); classWriter.addLine(" " + domainDAOParameter + ".commit();"); classWriter.addLine(" } catch (final Exception e) {"); classWriter.addLine( " log.error(\"final Cannot delete " + domain.getName() + " in database\", e);"); classWriter.addLine(" " + domainDAOParameter + ".rollback();"); classWriter.addLine( " throw new Exception(\"Cannot delete " + domain.getName() + " in database\", e);"); classWriter.addLine(" }"); classWriter.addLine("}"); classWriter.generateContentAndStore(); } domain.getService().getMethod().add(createMethod); domain.getService().getMethod().add(readMethod); domain.getService().getMethod().add(updateMethod); domain.getService().getMethod().add(deleteMethod); } }
private void generateService() throws Exception { final String className = GeneratorHelper.getServiceClassName(domain); final ClassWriter classWriter = new ClassWriter( this, getSrcGeneratedDirectory(), GeneratorHelper.getServicePackage(domain), className); classWriter.setInterface(true); classWriter.addExtend("com.ponysdk.core.service.PonyService"); if (domain.getService() != null) { for (final Method method : domain.getService().getMethod()) { final String returnClass = GeneratorHelper.getClassName(method.getReturn()); classWriter.addLine( "public " + returnClass + " " + method.getName() + "(" + GeneratorHelper.getParameterToString(method.getParameter()) + ") throws Exception;"); } for (final Pushmethod method : domain.getService().getPushmethod()) { if (method.getParameter().isEmpty()) { classWriter.addLine( "public com.ponysdk.core.event.HandlerRegistration " + method.getName() + "Registration(com.ponysdk.core.command.PushListener<" + method.getType() + "> listener);"); } else { classWriter.addLine( "public com.ponysdk.core.event.HandlerRegistration " + method.getName() + "Registration(" + GeneratorHelper.getParameterToString(method.getParameter()) + ", com.ponysdk.core.command.PushListener<" + method.getType() + "> listener);"); } } final Dao dao = domain.getService().getDao(); if (dao != null) { switch (dao.getDaoLayer()) { case HIBERNATE: generateHibernateDAO(dao); break; case MONGODB: generateMongoDBDAO(dao); break; default: System.err.println( dao.getDaoLayer() + " HibernateDAO generation ignored for class" + domain.getService().getDao().getClazz() + ". Only " + DaoLayer.HIBERNATE + " is supported"); } } } classWriter.generateContentAndStore(); }