예제 #1
0
 /**
  * Boolean used to hide/show the confirmation dialog's remove button.
  *
  * <p>The remove button show only be displayed if the reverse relationship for the related eo is
  * not mandatory and isEntityRemoveable returns true.
  */
 public Boolean showRemoveButton() {
   if (_showRemoveButton == null) {
     boolean isRemoveable =
         ERXValueUtilities.booleanValueWithDefault(
             d2wContext().valueForKey("isEntityRemoveable"), false);
     EODataSource ds = dataSource();
     if (ds != null && ds instanceof EODetailDataSource) {
       EODetailDataSource dds = (EODetailDataSource) ds;
       EOEnterpriseObject masterObj = (EOEnterpriseObject) dds.masterObject();
       EOEntity masterEntity = ERXEOAccessUtilities.entityForEo(masterObj);
       EORelationship relationship = masterEntity.relationshipNamed(dds.detailKey());
       EORelationship reverseRelationship = relationship.inverseRelationship();
       if (isRemoveable) {
         if (reverseRelationship == null) {
           _showRemoveButton = Boolean.TRUE;
         } else {
           _showRemoveButton = !reverseRelationship.isMandatory();
         }
       } else {
         _showRemoveButton = Boolean.FALSE;
       }
     } else {
       _showRemoveButton = Boolean.valueOf(isRemoveable);
     }
   }
   return _showRemoveButton;
 }
 /**
  * @param qualifier sub qualifier
  * @param entityName of the sub qualification
  * @param relationshipName relationship name
  */
 public ERXQualifierInSubquery(EOQualifier qualifier, String entityName, String relationshipName) {
   this.qualifier = qualifier;
   this.entityName = entityName;
   if (relationshipName != null) {
     this.relationshipName = relationshipName;
     EORelationship rel =
         ERXEOAccessUtilities.entityNamed(null, entityName).relationshipNamed(relationshipName);
     this.attributeName = (String) ((EOAttribute) rel.sourceAttributes().lastObject()).name();
     this.destinationAttName =
         (String) ((EOAttribute) rel.destinationAttributes().lastObject()).name();
   }
 }
  /**
   * Ensures we don't have references to EOs before using this in the background thread.
   *
   * @param fs
   * @return a clone of the fetchSpecification with the EOQualifier converted to a schema-based
   *     qualifier or the same {@link EOFetchSpecification} if there is no qualifier.
   */
  private EOFetchSpecification schemaBasedFetchSpecification(
      EOFetchSpecification fetchSpecification) {

    EOQualifier q = fetchSpecification.qualifier();
    if (q != null) {

      // Clone the fetchSpec
      fetchSpecification = (EOFetchSpecification) fetchSpecification.clone();

      EOEditingContext ec = ERXEC.newEditingContext();
      ec.lock();
      try {
        EOEntity entity =
            ERXEOAccessUtilities.entityMatchingString(ec, fetchSpecification.entityName());
        // Convert the qualifier to a schema-based qualifier
        q = entity.schemaBasedQualifier(q);
        fetchSpecification.setQualifier(q);
      } finally {
        ec.unlock();
      }
    } // ~ if (q != null)
    return fetchSpecification;
  }
예제 #4
0
    @Override
    @SuppressWarnings("unchecked")
    public String sqlStringForSQLExpression(EOQualifier eoqualifier, EOSQLExpression e) {
      ERXToManyQualifier qualifier = (ERXToManyQualifier) eoqualifier;
      StringBuilder result = new StringBuilder();
      EOEntity targetEntity = e.entity();

      NSArray<String> toManyKeys = NSArray.componentsSeparatedByString(qualifier.key(), ".");
      EORelationship targetRelationship = null;
      for (int i = 0; i < toManyKeys.count() - 1; i++) {
        targetRelationship = targetEntity.anyRelationshipNamed(toManyKeys.objectAtIndex(i));
        targetEntity = targetRelationship.destinationEntity();
      }
      targetRelationship = targetEntity.relationshipNamed(toManyKeys.lastObject());
      targetEntity = targetRelationship.destinationEntity();

      if (targetRelationship.joins() == null || targetRelationship.joins().isEmpty()) {
        // we have a flattened many to many
        String definitionKeyPath = targetRelationship.definition();
        NSArray<String> definitionKeys =
            NSArray.componentsSeparatedByString(definitionKeyPath, ".");
        EOEntity lastStopEntity = targetRelationship.entity();
        EORelationship firstHopRelationship =
            lastStopEntity.relationshipNamed(definitionKeys.objectAtIndex(0));
        EOEntity endOfFirstHopEntity = firstHopRelationship.destinationEntity();
        EOJoin join = firstHopRelationship.joins().objectAtIndex(0); // assumes 1 join
        EOAttribute sourceAttribute = join.sourceAttribute();
        EOAttribute targetAttribute = join.destinationAttribute();
        EORelationship secondHopRelationship =
            endOfFirstHopEntity.relationshipNamed(definitionKeys.objectAtIndex(1));
        join = secondHopRelationship.joins().objectAtIndex(0); // assumes 1 join
        EOAttribute secondHopSourceAttribute = join.sourceAttribute();

        NSMutableArray<String> lastStopPKeyPath = toManyKeys.mutableClone();
        lastStopPKeyPath.removeLastObject();
        lastStopPKeyPath.addObject(firstHopRelationship.name());
        lastStopPKeyPath.addObject(targetAttribute.name());
        String firstHopRelationshipKeyPath = lastStopPKeyPath.componentsJoinedByString(".");
        result.append(e.sqlStringForAttributeNamed(firstHopRelationshipKeyPath));
        result.append(" IN ( SELECT ");

        result.append(lastStopEntity.externalName());
        result.append('.');
        result.append(lastStopEntity.primaryKeyAttributes().objectAtIndex(0).columnName());

        result.append(" FROM ");

        result.append(lastStopEntity.externalName());
        result.append(',');

        lastStopPKeyPath.removeLastObject();
        String tableAliasForJoinTable =
            (String)
                e.aliasesByRelationshipPath()
                    .objectForKey(
                        lastStopPKeyPath.componentsJoinedByString(".")); // "j"; //+random#
        result.append(endOfFirstHopEntity.externalName());
        result.append(' ');
        result.append(tableAliasForJoinTable);

        result.append(" WHERE ");

        appendColumnForAttributeToStringBuilder(sourceAttribute, result);
        result.append('=');
        result.append(e.sqlStringForAttributeNamed(firstHopRelationshipKeyPath));

        if (qualifier.elements() != null) {
          NSArray pKeys = ERXEOAccessUtilities.primaryKeysForObjects(qualifier.elements());
          result.append(" AND ");

          result.append(tableAliasForJoinTable);
          result.append('.');
          result.append(secondHopSourceAttribute.columnName());

          result.append(" IN (");
          EOAttribute pk = targetEntity.primaryKeyAttributes().lastObject();
          for (int i = 0; i < pKeys.count(); i++) {

            Object key = pKeys.objectAtIndex(i);
            String keyString = e.formatValueForAttribute(key, pk);
            // AK: default is is broken
            if ("NULL".equals(keyString)) {
              keyString = "" + key;
            }
            result.append(keyString);
            if (i < pKeys.count() - 1) {
              result.append(',');
            }
          }
          result.append(") ");
        }
        result.append(" GROUP BY ");
        appendColumnForAttributeToStringBuilder(sourceAttribute, result);

        result.append(" HAVING COUNT(*)");
        if (qualifier.minCount() <= 0) {
          result.append("=" + qualifier.elements().count());
        } else {
          result.append(">=" + qualifier.minCount());
        }
        result.append(" )");
      } else {
        throw new RuntimeException("not implemented!!");
      }
      return result.toString();
    }
예제 #5
0
  /**
   * Create the prototype cache for the given model by walking a search order.
   *
   * @param model
   */
  public static void createPrototypes(EOModel model) {
    // Remove password for logging
    NSMutableDictionary dict = model.connectionDictionary().mutableClone();
    if (dict.objectForKey("password") != null) {
      dict.setObjectForKey("<deleted for log>", "password");
    }
    log.info("Creating prototypes for model: " + model.name() + "->" + dict);
    synchronized (_EOGlobalModelLock) {
      StringBuilder debugInfo = null;
      if (log.isDebugEnabled()) {
        debugInfo = new StringBuilder();
        debugInfo.append("Model = " + model.name());
      }
      model._prototypesByName = new NSMutableDictionary();
      String name = model.name();
      NSArray adaptorPrototypes = NSArray.EmptyArray;
      EOAdaptor adaptor = EOAdaptor.adaptorWithModel(model);
      try {
        adaptorPrototypes = adaptor.prototypeAttributes();
      } catch (Exception e) {
        log.error(e, e);
      }
      addAttributesToPrototypesCache(model, adaptorPrototypes);
      NSArray prototypesToHide =
          attributesFromEntity(model._group.entityNamed("EOPrototypesToHide"));
      model._prototypesByName.removeObjectsForKeys(namesForAttributes(prototypesToHide));

      String plugin = null;
      if (adaptor instanceof JDBCAdaptor && !model.name().equalsIgnoreCase("erprototypes")) {
        plugin = (String) model.connectionDictionary().objectForKey("plugin");
        if (plugin == null) {
          plugin = ERXEOAccessUtilities.guessPluginName(model);
        } // ~ if (plugin == null)
        if (plugin != null && plugin.toLowerCase().endsWith("plugin")) {
          plugin = plugin.substring(0, plugin.length() - "plugin".length());
        }
        if (log.isDebugEnabled()) debugInfo.append("; plugin = " + plugin);
      }

      addAttributesToPrototypesCache(model, model._group.entityNamed("EOPrototypes"));
      addAttributesToPrototypesCache(
          model, model._group.entityNamed("EO" + model.adaptorName() + "Prototypes"));
      if (log.isDebugEnabled())
        debugInfo.append(
            "; Prototype Entities Searched = EOPrototypes, "
                + "EO"
                + model.adaptorName()
                + "Prototypes");
      if (plugin != null) {
        addAttributesToPrototypesCache(
            model, model._group.entityNamed("EOJDBC" + plugin + "Prototypes"));
        if (log.isDebugEnabled()) debugInfo.append(", " + "EOJDBC" + plugin + "Prototypes");
      }

      addAttributesToPrototypesCache(model, model._group.entityNamed("EOCustomPrototypes"));
      addAttributesToPrototypesCache(
          model, model._group.entityNamed("EO" + model.adaptorName() + "CustomPrototypes"));
      if (log.isDebugEnabled())
        debugInfo.append(
            ", EOCustomPrototypes, " + "EO" + model.adaptorName() + "CustomPrototypes");
      if (plugin != null) {
        addAttributesToPrototypesCache(
            model, model._group.entityNamed("EOJDBC" + plugin + "CustomPrototypes"));
        if (log.isDebugEnabled()) debugInfo.append(", " + "EOJDBC" + plugin + "CustomPrototypes");
      }

      addAttributesToPrototypesCache(model, model._group.entityNamed("EO" + name + "Prototypes"));
      addAttributesToPrototypesCache(
          model, model._group.entityNamed("EO" + model.adaptorName() + name + "Prototypes"));
      if (log.isDebugEnabled())
        debugInfo.append(
            ", "
                + "EO"
                + name
                + "Prototypes"
                + ", "
                + "EO"
                + model.adaptorName()
                + name
                + "Prototypes");
      if (plugin != null) {
        addAttributesToPrototypesCache(
            model, model._group.entityNamed("EOJDBC" + plugin + name + "Prototypes"));
        if (log.isDebugEnabled()) debugInfo.append(", " + "EOJDBC" + plugin + name + "Prototypes");
      }

      if (log.isDebugEnabled()) log.debug(debugInfo.toString());
    }
  }