/**
   * Constructor
   *
   * @param metadataId (required)
   * @param aspectName (required)
   * @param governorPhysicalTypeMetadata (required)
   * @param parent can be <code>null</code>
   * @param projectMetadata (required)
   * @param crudAnnotationValues the CRUD-related annotation values (required)
   * @param plural the plural form of the entity (required)
   * @param identifierField the entity's identifier field (required)
   * @param entityName the JPA entity name (required)
   */
  public JpaActiveRecordMetadata(
      final String metadataId,
      final JavaType aspectName,
      final PhysicalTypeMetadata governorPhysicalTypeMetadata,
      final JpaActiveRecordMetadata parent,
      final JpaCrudAnnotationValues crudAnnotationValues,
      final String plural,
      final FieldMetadata identifierField,
      final String entityName,
      final boolean isGaeEnabled) {
    super(metadataId, aspectName, governorPhysicalTypeMetadata);
    Assert.isTrue(
        isValid(metadataId),
        "Metadata identification string '" + metadataId + "' does not appear to be a valid");
    Assert.notNull(crudAnnotationValues, "CRUD-related annotation values required");
    Assert.notNull(identifierField, "Identifier required for '" + metadataId + "'");
    Assert.hasText(entityName, "Entity name required for '" + metadataId + "'");
    Assert.hasText(plural, "Plural required for '" + metadataId + "'");

    if (!isValid()) {
      return;
    }

    this.crudAnnotationValues = crudAnnotationValues;
    this.entityName = entityName;
    this.identifierField = identifierField;
    this.isGaeEnabled = isGaeEnabled;
    this.parent = parent;
    this.plural = StringUtils.capitalize(plural);

    // Determine the entity's "entityManager" field, which is guaranteed to be accessible to the
    // ITD.
    builder.addField(getEntityManagerField());

    // Add helper methods
    builder.addMethod(getPersistMethod());
    builder.addMethod(getRemoveMethod());
    builder.addMethod(getFlushMethod());
    builder.addMethod(getClearMethod());
    builder.addMethod(getMergeMethod());

    // Add static methods
    builder.addMethod(getEntityManagerMethod());
    builder.addMethod(getCountMethod());
    builder.addMethod(getFindAllMethod());
    builder.addMethod(getFindMethod());
    builder.addMethod(getFindEntriesMethod());

    builder.putCustomData(CustomDataKeys.DYNAMIC_FINDER_NAMES, getDynamicFinders());

    // Create a representation of the desired output ITD
    itdTypeDetails = builder.build();
  }
  public void addEnumConstant(
      final String physicalTypeIdentifier, final JavaSymbolName constantName) {
    Assert.hasText(physicalTypeIdentifier, "Type identifier not provided");
    Assert.notNull(constantName, "Constant name required");

    // Obtain the physical type and itd mutable details
    PhysicalTypeMetadata ptm = (PhysicalTypeMetadata) metadataService.get(physicalTypeIdentifier);
    Assert.notNull(
        ptm,
        "Java source code unavailable for type "
            + PhysicalTypeIdentifier.getFriendlyName(physicalTypeIdentifier));
    PhysicalTypeDetails ptd = ptm.getMemberHoldingTypeDetails();
    Assert.notNull(
        ptd,
        "Java source code details unavailable for type "
            + PhysicalTypeIdentifier.getFriendlyName(physicalTypeIdentifier));
    ClassOrInterfaceTypeDetailsBuilder cidBuilder =
        new ClassOrInterfaceTypeDetailsBuilder((ClassOrInterfaceTypeDetails) ptd);

    // Ensure it's an enum
    Assert.isTrue(
        cidBuilder.getPhysicalTypeCategory() == PhysicalTypeCategory.ENUMERATION,
        PhysicalTypeIdentifier.getFriendlyName(physicalTypeIdentifier) + " is not an enum");

    cidBuilder.addEnumConstant(constantName);
    createOrUpdateTypeOnDisk(cidBuilder.build());
  }
Exemple #3
0
  public JsonMetadata(
      String identifier,
      JavaType aspectName,
      PhysicalTypeMetadata governorPhysicalTypeMetadata,
      String typeNamePlural,
      JsonAnnotationValues annotationValues) {
    super(identifier, aspectName, governorPhysicalTypeMetadata);
    Assert.notNull(annotationValues, "Annotation values required");
    Assert.hasText(typeNamePlural, "Plural of the target type required");
    Assert.isTrue(
        isValid(identifier),
        "Metadata identification string '" + identifier + "' does not appear to be a valid");

    if (!isValid()) {
      return;
    }

    this.annotationValues = annotationValues;
    this.typeNamePlural = typeNamePlural;

    builder.addMethod(getToJsonMethod());
    builder.addMethod(getFromJsonMethod());
    builder.addMethod(getToJsonArrayMethod());
    builder.addMethod(getFromJsonArrayMethod());

    // Create a representation of the desired output ITD
    itdTypeDetails = builder.build();
  }
  public List<JavaSymbolName> getFinders(
      MemberDetails memberDetails, String plural, int depth, Set<JavaSymbolName> exclusions) {
    Assert.notNull(memberDetails, "Member details required");
    Assert.hasText(plural, "Plural required");
    Assert.notNull(
        depth, "The depth of combinations used for finder signatures combinations required");
    Assert.notNull(exclusions, "Exclusions required");

    SortedSet<JavaSymbolName> finders = new TreeSet<JavaSymbolName>();

    List<FieldMetadata> fields = memberDetails.getFields();
    for (int i = 0; i < depth; i++) {
      SortedSet<JavaSymbolName> tempFinders = new TreeSet<JavaSymbolName>();
      for (FieldMetadata field : fields) {
        // Ignoring java.util.Map field types (see ROO-194)
        if (field == null || field.getFieldType().equals(new JavaType(Map.class.getName()))) {
          continue;
        }
        if (exclusions.contains(field.getFieldName())) {
          continue;
        }
        if (i == 0) {
          tempFinders.addAll(createFinders(field, finders, "find" + plural + "By", true));
        } else {
          tempFinders.addAll(createFinders(field, finders, "And", false));
          tempFinders.addAll(createFinders(field, finders, "Or", false));
        }
      }
      finders.addAll(tempFinders);
    }

    return Collections.unmodifiableList(new ArrayList<JavaSymbolName>(finders));
  }
 /**
  * Constructor
  *
  * @param key the unique key for this method (required)
  * @param name the Java name of this method (required)
  */
 private RepositoryMongoLayerMethod(final String name, final MethodMetadataCustomDataKey... keys) {
   Assert.hasText(name, "Name is required");
   Assert.isTrue(keys.length > 0, "One or more ids are required");
   this.ids = new ArrayList<String>();
   for (final MethodMetadataCustomDataKey key : keys) {
     this.ids.add(key.name());
   }
   this.name = name;
 }
 /**
  * Constructor
  *
  * @param metadataId the ID to assign this {@link org.springframework.roo.metadata.MetadataItem}
  *     (must satisfy {@link PhysicalTypeIdentifier#isValid(String)})
  * @param physicalLocationCanonicalPath the canonical path of the file containing this Java type
  *     (required)
  * @param memberHoldingTypeDetails the members of this type (required)
  */
 public DefaultPhysicalTypeMetadata(
     final String metadataId,
     final String physicalLocationCanonicalPath,
     final MemberHoldingTypeDetails memberHoldingTypeDetails) {
   super(metadataId);
   Assert.isTrue(
       PhysicalTypeIdentifier.isValid(metadataId),
       "Metadata id '" + metadataId + "' is not a valid physical type identifier");
   Assert.hasText(physicalLocationCanonicalPath, "Physical location canonical path required");
   Assert.notNull(memberHoldingTypeDetails, "Member holding type details required");
   this.memberHoldingTypeDetails = memberHoldingTypeDetails;
   this.physicalLocationCanonicalPath = physicalLocationCanonicalPath;
 }
  public QueryHolder getQueryHolder(
      MemberDetails memberDetails, JavaSymbolName finderName, String plural, String entityName) {
    Assert.notNull(memberDetails, "Member details required");
    Assert.notNull(finderName, "Finder name required");
    Assert.hasText(plural, "Plural required");

    List<Token> tokens;
    try {
      tokens = tokenize(memberDetails, finderName, plural);
    } catch (FinderFieldTokenMissingException e) {
      return null;
    } catch (InvalidFinderException e) {
      return null;
    }

    String simpleTypeName = getConcreteJavaType(memberDetails).getSimpleTypeName();
    String jpaQuery = getJpaQuery(tokens, simpleTypeName, finderName, plural, entityName);
    List<JavaType> parameterTypes = getParameterTypes(tokens, finderName, plural);
    List<JavaSymbolName> parameterNames = getParameterNames(tokens, finderName, plural);
    return new QueryHolder(jpaQuery, parameterTypes, parameterNames, tokens);
  }
  public void installEmail(
      final String hostServer,
      final MailProtocol protocol,
      final String port,
      final String encoding,
      final String username,
      final String password) {
    Assert.hasText(hostServer, "Host server name required");

    final String contextPath = getApplicationContextPath();
    final Document document = XmlUtils.readXml(fileManager.getInputStream(contextPath));
    final Element root = document.getDocumentElement();

    boolean installDependencies = true;
    final Map<String, String> props = new HashMap<String, String>();

    Element mailBean =
        XmlUtils.findFirstElement(
            "/beans/bean[@class = 'org.springframework.mail.javamail.JavaMailSenderImpl']", root);
    if (mailBean != null) {
      root.removeChild(mailBean);
      installDependencies = false;
    }

    mailBean = document.createElement("bean");
    mailBean.setAttribute("class", "org.springframework.mail.javamail.JavaMailSenderImpl");
    mailBean.setAttribute("id", "mailSender");

    final Element property = document.createElement("property");
    property.setAttribute("name", "host");
    property.setAttribute("value", "${email.host}");
    mailBean.appendChild(property);
    root.appendChild(mailBean);
    props.put("email.host", hostServer);

    if (protocol != null) {
      final Element pElement = document.createElement("property");
      pElement.setAttribute("value", "${email.protocol}");
      pElement.setAttribute("name", "protocol");
      mailBean.appendChild(pElement);
      props.put("email.protocol", protocol.getProtocol());
    }

    if (StringUtils.hasText(port)) {
      final Element pElement = document.createElement("property");
      pElement.setAttribute("name", "port");
      pElement.setAttribute("value", "${email.port}");
      mailBean.appendChild(pElement);
      props.put("email.port", port);
    }

    if (StringUtils.hasText(encoding)) {
      final Element pElement = document.createElement("property");
      pElement.setAttribute("name", "defaultEncoding");
      pElement.setAttribute("value", "${email.encoding}");
      mailBean.appendChild(pElement);
      props.put("email.encoding", encoding);
    }

    if (StringUtils.hasText(username)) {
      final Element pElement = document.createElement("property");
      pElement.setAttribute("name", "username");
      pElement.setAttribute("value", "${email.username}");
      mailBean.appendChild(pElement);
      props.put("email.username", username);
    }

    if (StringUtils.hasText(password)) {
      final Element pElement = document.createElement("property");
      pElement.setAttribute("name", "password");
      pElement.setAttribute("value", "${email.password}");
      mailBean.appendChild(pElement);
      props.put("email.password", password);

      if (SMTP.equals(protocol)) {
        final Element javaMailProperties = document.createElement("property");
        javaMailProperties.setAttribute("name", "javaMailProperties");
        final Element securityProps = document.createElement("props");
        javaMailProperties.appendChild(securityProps);
        final Element prop = document.createElement("prop");
        prop.setAttribute("key", "mail.smtp.auth");
        prop.setTextContent("true");
        securityProps.appendChild(prop);
        final Element prop2 = document.createElement("prop");
        prop2.setAttribute("key", "mail.smtp.starttls.enable");
        prop2.setTextContent("true");
        securityProps.appendChild(prop2);
        mailBean.appendChild(javaMailProperties);
      }
    }

    DomUtils.removeTextNodes(root);

    fileManager.createOrUpdateTextFileIfRequired(
        contextPath, XmlUtils.nodeToString(document), false);

    if (installDependencies) {
      updateConfiguration();
    }

    propFileOperations.addProperties(
        Path.SPRING_CONFIG_ROOT, "email.properties", props, true, true);
  }
Exemple #9
0
 /**
  * Indicates whether the presented canonical path is a child of the current {@link FileDetails}
  * instance. Put differently, returning true indicates the current instance is a parent directory
  * of the presented possibleChildCanonicalPath.
  *
  * <p>This method will return true if the presented child is a child of the current instance, or
  * if the presented child is identical to the current instance.
  *
  * @param possibleChildCanonicalPath to evaluate (required)
  * @return true if the presented possible child is indeed a child of the current instance
  */
 public boolean isParentOf(final String possibleChildCanonicalPath) {
   Assert.hasText(possibleChildCanonicalPath, "Possible child to evaluate is required");
   return FileUtils.ensureTrailingSeparator(possibleChildCanonicalPath)
       .startsWith(FileUtils.ensureTrailingSeparator(getCanonicalPath()));
 }