Exemplo n.º 1
0
 public static OJAnnotatedOperation buildSetter(OJAnnotatedClass owner, PropertyWrapper pWrap) {
   OJAnnotatedOperation setter = new OJAnnotatedOperation(pWrap.setter());
   if (pWrap.isReadOnly()) {
     setter.setVisibility(OJVisibilityKind.PROTECTED);
   }
   if (!pWrap.isMemberOfAssociationClass()) {
     setter.addParam(pWrap.fieldname(), pWrap.javaTypePath());
   } else {
     setter.addParam(
         pWrap.getAssociationClassFakePropertyName(), pWrap.javaTypePathWithAssociationClass());
   }
   setter.getBody().addToStatements(pWrap.clearer() + "()");
   OJIfStatement ifNotNull;
   if (!pWrap.isMemberOfAssociationClass()) {
     ifNotNull = new OJIfStatement(pWrap.fieldname() + " != null");
     ifNotNull.addToThenPart(pWrap.adder() + "(" + pWrap.fieldname() + ")");
   } else {
     ifNotNull = new OJIfStatement(pWrap.getAssociationClassFakePropertyName() + " != null");
     ifNotNull.addToThenPart(
         pWrap.adder() + "(" + pWrap.getAssociationClassFakePropertyName() + ")");
   }
   setter.getBody().addToStatements(ifNotNull);
   owner.addToOperations(setter);
   return setter;
 }
Exemplo n.º 2
0
  public static void buildManyAdder(
      OJAnnotatedClass owner,
      PropertyWrapper propertyWrapper,
      boolean indexed,
      boolean ignoreInverse) {
    OJAnnotatedOperation adder = new OJAnnotatedOperation(propertyWrapper.adder());
    if (!propertyWrapper.isMemberOfAssociationClass()) {
      if (indexed) {
        adder.addParam("index", "int");
      }
      adder.addParam(propertyWrapper.fieldname(), propertyWrapper.javaTypePath());
    } else {
      if (indexed) {
        adder.addParam("index", "int");
      }
      adder.addParam(
          propertyWrapper.getAssociationClassFakePropertyName(),
          propertyWrapper.javaTypePathWithAssociationClass());
    }

    if (!(owner instanceof OJAnnotatedInterface)) {
      if (!propertyWrapper.hasQualifiers()) {
        if (!propertyWrapper.isMemberOfAssociationClass()) {
          OJIfStatement ifNotNull =
              new OJIfStatement("!" + propertyWrapper.fieldname() + ".isEmpty()");
          ifNotNull.addToThenPart(
              "this."
                  + propertyWrapper.fieldname()
                  + ".addAll("
                  + propertyWrapper.fieldname()
                  + ")");
          adder.getBody().addToStatements(ifNotNull);
        } else {
          // iterate the association class set as it contains a Pair
          OJForStatement associationClassPairs =
              new OJForStatement(
                  "pair",
                  propertyWrapper.getAssociationClassPair(),
                  propertyWrapper.getAssociationClassFakePropertyName());
          associationClassPairs
              .getBody()
              .addToStatements(propertyWrapper.adder() + "(pair.getFirst(), pair.getSecond())");
          adder.getBody().addToStatements(associationClassPairs);
        }
      } else {
        String elementName = "_" + propertyWrapper.fieldname().substring(0, 1);
        OJForStatement forAll =
            new OJForStatement(
                elementName, propertyWrapper.javaBaseTypePath(), propertyWrapper.fieldname());
        forAll
            .getBody()
            .addToStatements("this." + propertyWrapper.adder() + "(" + elementName + ")");
        adder.getBody().addToStatements(forAll);
      }
    }
    owner.addToOperations(adder);

    OJAnnotatedOperation singleAdder =
        new OJAnnotatedOperation(
            !ignoreInverse ? propertyWrapper.adder() : propertyWrapper.adderIgnoreInverse());
    if (indexed) {
      singleAdder.addParam("index", "int");
    }
    singleAdder.addParam(propertyWrapper.fieldname(), propertyWrapper.javaBaseTypePath());
    if (propertyWrapper.isMemberOfAssociationClass()) {
      singleAdder.addParam(
          StringUtils.uncapitalize(propertyWrapper.getAssociationClass().getName()),
          UmlgClassOperations.getPathName(propertyWrapper.getAssociationClass()));
    }
    if (!(owner instanceof OJAnnotatedInterface)) {

      PropertyWrapper otherEnd = new PropertyWrapper(propertyWrapper.getOtherEnd());
      if (
      /*For bags the one side can have many edges to the same element*/ propertyWrapper.isUnique()
          && propertyWrapper.hasOtherEnd()
          && !propertyWrapper.isEnumeration()
          && otherEnd.isOne()) {
        OJIfStatement ifNotNull2 = new OJIfStatement(propertyWrapper.fieldname() + " != null");
        ifNotNull2.addToThenPart(propertyWrapper.fieldname() + "." + otherEnd.clearer() + "()");
        ifNotNull2.addToThenPart(
            propertyWrapper.fieldname()
                + ".initialiseProperty("
                + UmlgClassOperations.propertyEnumName(otherEnd.getOwningType())
                + "."
                + otherEnd.fieldname()
                + ", false)");
        ifNotNull2.addToThenPart(
            propertyWrapper.remover() + "(" + propertyWrapper.fieldname() + ")");
        owner.addToImports(
            UmlgClassOperations.getPathName(otherEnd.getOwningType())
                .append(UmlgClassOperations.propertyEnumName(otherEnd.getOwningType())));
        singleAdder.getBody().addToStatements(ifNotNull2);
      }
      OJIfStatement ifNotNull = new OJIfStatement(propertyWrapper.fieldname() + " != null");

      // Add in validations

      OJBlock block;
      if (propertyWrapper.isDataType()) {
        OJField failedConstraints =
            new OJField(
                "violations",
                new OJPathName("java.util.List")
                    .addToGenerics(UmlgGenerationUtil.UmlgConstraintViolation));
        failedConstraints.setInitExp(
            propertyWrapper.validator() + "(" + propertyWrapper.fieldname() + ")");
        ifNotNull.getThenPart().addToLocals(failedConstraints);
        OJIfStatement ifValidated = new OJIfStatement("violations.isEmpty()");
        ifValidated.addToElsePart(
            "throw new "
                + UmlgGenerationUtil.UmlgConstraintViolationException.getLast()
                + "(violations)");
        owner.addToImports(UmlgGenerationUtil.UmlgConstraintViolationException);
        ifNotNull.addToThenPart(ifValidated);
        block = ifValidated.getThenPart();
      } else {
        block = ifNotNull.getThenPart();
      }

      if (!propertyWrapper.isMemberOfAssociationClass()) {
        if (!indexed) {
          if (!ignoreInverse) {
            block.addToStatements(
                "this."
                    + propertyWrapper.fieldname()
                    + ".add("
                    + propertyWrapper.fieldname()
                    + ")");
          } else {
            block.addToStatements(
                "this."
                    + propertyWrapper.fieldname()
                    + ".addIgnoreInverse("
                    + propertyWrapper.fieldname()
                    + ")");
          }
        } else {
          block.addToStatements(
              "this."
                  + propertyWrapper.fieldname()
                  + ".add(index, "
                  + propertyWrapper.fieldname()
                  + ")");
        }
      } else {
        if (!indexed) {
          block.addToStatements(
              "this."
                  + propertyWrapper.fieldname()
                  + ".add("
                  + propertyWrapper.fieldname()
                  + ", "
                  + StringUtils.uncapitalize(propertyWrapper.getAssociationClass().getName())
                  + ")");
        } else {
          block.addToStatements(
              "this."
                  + propertyWrapper.fieldname()
                  + ".add(index, "
                  + propertyWrapper.fieldname()
                  + ", "
                  + StringUtils.uncapitalize(propertyWrapper.getAssociationClass().getName())
                  + ")");
        }
      }
      singleAdder.getBody().addToStatements(ifNotNull);
    }
    owner.addToOperations(singleAdder);

    //        //Add change listener
    //        if (propertyWrapper.isChangedListener()) {
    //            PropertyChangeNotificationBuilder.buildChangeNotification(owner, singleAdder,
    // propertyWrapper, PropertyChangeNotificationBuilder.CHANGE_TYPE.ADD);
    //        }

  }