Esempio n. 1
0
 protected void createInitializers(final JavaClassSource entity)
     throws FacetNotFoundException, FileNotFoundException {
   boolean dirtyBit = false;
   for (FieldSource<JavaClassSource> field : entity.getFields()) {
     if (field.hasAnnotation(OneToOne.class)) {
       AnnotationSource<JavaClassSource> oneToOne = field.getAnnotation(OneToOne.class);
       if (oneToOne.getStringValue("mappedBy") == null
           && oneToOne.getStringValue("cascade") == null) {
         oneToOne.setEnumValue("cascade", CascadeType.ALL);
         dirtyBit = true;
       }
       String methodName = "new" + StringUtils.capitalize(field.getName());
       if (!entity.hasMethodSignature(methodName)) {
         entity
             .addMethod()
             .setName(methodName)
             .setReturnTypeVoid()
             .setPublic()
             .setBody("this." + field.getName() + " = new " + field.getType().getName() + "();");
         dirtyBit = true;
       }
     }
   }
   for (MethodSource<JavaClassSource> method : entity.getMethods()) {
     if (method.hasAnnotation(OneToOne.class)) {
       AnnotationSource<JavaClassSource> oneToOne = method.getAnnotation(OneToOne.class);
       if (oneToOne.getStringValue("mappedBy") == null
           && oneToOne.getStringValue("cascade") == null) {
         oneToOne.setEnumValue("cascade", CascadeType.ALL);
         dirtyBit = true;
       }
       String fieldName = StringUtils.camelCase(method.getName().substring(3));
       String methodName = "new" + StringUtils.capitalize(fieldName);
       if (!entity.hasMethodSignature(methodName)) {
         entity
             .addMethod()
             .setName(methodName)
             .setReturnTypeVoid()
             .setPublic()
             .setBody("this." + fieldName + " = new " + method.getReturnType().getName() + "();");
         dirtyBit = true;
       }
     }
   }
   if (dirtyBit) {
     this.project.getFacet(JavaSourceFacet.class).saveJavaSource(entity);
   }
 }
Esempio n. 2
0
  private void setPrimaryKeyMetaData(Map<Object, Object> context, final JavaClassSource entity) {
    String pkName = "id";
    String pkType = "Long";
    String nullablePkType = "Long";
    for (MemberSource<JavaClassSource, ?> m : entity.getMembers()) {
      if (m.hasAnnotation(Id.class)) {
        if (m instanceof Field) {
          Field<?> field = (Field<?>) m;
          pkName = field.getName();
          pkType = field.getType().getQualifiedName();
          nullablePkType = pkType;
          break;
        }

        MethodSource<?> method = (MethodSource<?>) m;
        pkName = method.getName().substring(3);
        if (method.getName().startsWith("get")) {
          pkType = method.getReturnType().getQualifiedName();
        } else {
          pkType = method.getParameters().get(0).getType().getQualifiedName();
        }
        nullablePkType = pkType;
        break;
      }
    }

    if (Types.isJavaLang(pkType)) {
      nullablePkType = Types.toSimpleName(pkType);
    } else if ("int".equals(pkType)) {
      nullablePkType = Integer.class.getSimpleName();
    } else if ("short".equals(pkType)) {
      nullablePkType = Short.class.getSimpleName();
    } else if ("byte".equals(pkType)) {
      nullablePkType = Byte.class.getSimpleName();
    } else if ("long".equals(pkType)) {
      nullablePkType = Long.class.getSimpleName();
    }

    context.put("primaryKey", pkName);
    context.put("primaryKeyCC", StringUtils.capitalize(pkName));
    context.put("primaryKeyType", pkType);
    context.put("nullablePrimaryKeyType", nullablePkType);
  }