/**
   * Apply the <b>cm:name</b> to the child association. If the child name is <tt>null</tt> then a
   * GUID is generated as a substitute.
   *
   * <p>Unknown associations or associations that do not require unique name checking will use a
   * GUID for the child name and the CRC value used <b>will be negative</b>.
   *
   * @param childName the <b>cm:name</b> applying to the association.
   */
  public static Pair<String, Long> getChildNameUnique(
      DictionaryService dictionaryService, QName assocTypeQName, String childName) {
    if (childName == null) {
      throw new IllegalArgumentException("Child name may not be null.  Use the Node ID ...");
    }

    String childNameNewShort; //
    long childNameNewCrc = -1L; // By default, they don't compete

    AssociationDefinition assocDef = dictionaryService.getAssociation(assocTypeQName);
    if (assocDef == null || !assocDef.isChild()) {
      if (logger.isWarnEnabled()) {
        logger.warn("No child association of this type could be found: " + assocTypeQName);
      }
      childNameNewShort = GUID.generate();
      childNameNewCrc = -1L * getChildNodeNameCrc(childNameNewShort);
    } else {
      ChildAssociationDefinition childAssocDef = (ChildAssociationDefinition) assocDef;
      if (childAssocDef.getDuplicateChildNamesAllowed()) {
        childNameNewShort = GUID.generate();
        childNameNewCrc = -1L * getChildNodeNameCrc(childNameNewShort);
      } else {
        String childNameNewLower = childName.toLowerCase();
        childNameNewShort = getChildNodeNameShort(childNameNewLower);
        childNameNewCrc = getChildNodeNameCrc(childNameNewLower);
      }
    }
    return new Pair<String, Long>(childNameNewShort, childNameNewCrc);
  }
Esempio n. 2
0
    /*
     * Performs join of the tables corresponding to the association and to the target type
     * Returns the local name of the target type of the association
     */
    private String leftJoin(String associationName_) {
      AssociationDefinition associationDefinition =
          dictionaryService.getAssociation(QName.createQName(namespaceURI, associationName_));
      if (associationDefinition == null) {
        throw new InvalidAssociationException(associationName_);
      }

      QName sourceClassQName = associationDefinition.getSourceClass().getName();
      QName targetClassQName = associationDefinition.getTargetClass().getName();

      String sourceTableName =
          databaseDictionary.resolveClassAsTableName(sourceClassQName.getLocalName());
      String targetTableName =
          databaseDictionary.resolveClassAsTableName(targetClassQName.getLocalName());
      String associationTableName =
          databaseDictionary.resolveAssociationAsTableName(associationName_);

      // Check whether the association table was already taken into account
      if (!joinedTables.containsKey(associationTableName)) {
        // Check whether the join has been previously made on source or target tables
        if (!typeName.equals(sourceClassQName.getLocalName())
            && !joinedTables.containsKey(sourceTableName)) {
          // There is a problem, an association can be added only if the source type was previously
          // joined (as a table)
          throw new AlfrescoRuntimeException(
              "Cannot join the association \""
                  + associationName_
                  + "\" since source type \""
                  + sourceClassQName.getLocalName()
                  + "\" has not yet been joined.");
        } else {
          if (logger.isDebugEnabled())
            logger.debug(
                "Join condition is made explicitely on column with name \""
                    + TYPE_TABLE_ID_COLUMN_NAME
                    + "\"");

          // the source type name is the "master" type name or has already been defined previously
          // as a left join
          join(
              associationTableName,
              databaseDictionary.getSourceAlias(associationName_),
              sourceTableName,
              TYPE_TABLE_ID_COLUMN_NAME,
              JoinType.LEFT_JOIN);
          join(
              targetTableName,
              TYPE_TABLE_ID_COLUMN_NAME,
              associationTableName,
              databaseDictionary.getTargetAlias(associationName_),
              JoinType.LEFT_JOIN);
        }
      }

      return targetClassQName.getLocalName();
    }
  public boolean generateArcsInstances(IStructure structure) throws Exception {
    List<IArc> arcsInstances = new ArrayList<IArc>();

    Collection<AssociationDefinition> associations =
        ((AlfrescoModelStructure) structure).getAssociations();

    for (AssociationDefinition associationDefinition : associations) {

      List<IArc> arcsInstancesByAssociation = new ArrayList<IArc>();

      ClassDefinition sourceType = associationDefinition.getSourceClass();
      Collection<INode> sourcesNodes = generatorServices.getGeneratedNodesByType(sourceType);

      boolean sourceMultiplicity = associationDefinition.isSourceMany();

      ClassDefinition targetType = associationDefinition.getTargetClass();
      Collection<INode> targetsNodes = generatorServices.getGeneratedNodesByType(targetType);

      boolean targetMultiplicity = associationDefinition.isTargetMany();

      if (!sourcesNodes.isEmpty() && !targetsNodes.isEmpty()) {
        if (!sourceMultiplicity && !targetMultiplicity) {
          arcsInstancesByAssociation =
              generateArcsInstancesCase11(
                  sourcesNodes, targetsNodes, associationDefinition, associations, arcsInstances);
        } else if (!sourceMultiplicity && targetMultiplicity) {
          arcsInstancesByAssociation =
              generateArcsInstancesCase1N(
                  sourcesNodes, targetsNodes, associationDefinition, associations, arcsInstances);
        } else if (sourceMultiplicity && !targetMultiplicity) {
          arcsInstancesByAssociation =
              generateArcsInstancesCaseN1(
                  sourcesNodes, targetsNodes, associationDefinition, associations, arcsInstances);
        } else if (sourceMultiplicity && targetMultiplicity) {
          arcsInstancesByAssociation =
              generateArcsInstancesCaseNN(sourcesNodes, targetsNodes, associationDefinition);
        }
        arcsInstances.addAll(arcsInstancesByAssociation);
      }
    }
    ((AlfrescoModelData) alfrescoModelDatas).setGeneratedAssociationsInstances(arcsInstances);
    return true;
  }
 public IArc createRandomlyArc(
     INode source, INode target, AssociationDefinition associationDefinition) {
   IArc arc = null;
   boolean isCreated = RandomMethods.randomGenerator.nextBoolean();
   if (associationDefinition.isTargetMandatory()) {
     isCreated = true;
   }
   if (isCreated) {
     arc = ((Instance) instance).instanciation(source, target, associationDefinition);
   }
   return arc;
 }
 private List<IArc> generateArcsInstancesCase1N(
     Collection<INode> sourcesNodes,
     Collection<INode> targetsNodes,
     AssociationDefinition associationDefinition,
     Collection<AssociationDefinition> associations,
     Collection<IArc> generatedArcs) {
   List<IArc> arcsInstances = new ArrayList<IArc>();
   while (!sourcesNodes.isEmpty()) {
     INode source = RandomMethods.selectRandomlyNode(sourcesNodes);
     int numberOfArcs = 0;
     while (numberOfArcs < numberOfOutputArcs && !targetsNodes.isEmpty()) {
       INode target = RandomMethods.selectRandomlyNode(targetsNodes);
       IArc arc = createRandomlyArc(source, target, associationDefinition);
       AssociationDefinition invAssoc =
           generatorServices.searchInverseAssoc(
               source, target, associationDefinition, associations);
       if (invAssoc != null && (invAssoc.isTargetMandatory() || invAssoc.isSourceMandatory())) {
         arc = null;
       }
       if (invAssoc != null) {
         IArc invArc =
             generatorServices.getInverseGeneratedArc(source, target, invAssoc, generatedArcs);
         if (invArc != null) {
           arc = null;
         }
       }
       if (arc != null) {
         arcsInstances.add(arc);
         numberOfArcs++;
       }
       targetsNodes.remove(target);
     }
     sourcesNodes.remove(source);
   }
   return arcsInstances;
 }
  @Override
  protected void processAssociationPersist(
      NodeRef nodeRef,
      Map<QName, AssociationDefinition> assocDefs,
      Map<QName, ChildAssociationDefinition> childAssocDefs,
      FieldData fieldData,
      List<org.alfresco.repo.forms.processor.node.AbstractAssocCommand> assocCommands) {
    if (getLogger().isDebugEnabled())
      getLogger().debug("Processing field " + fieldData + " for association persistence");

    String fieldName = fieldData.getName();
    Matcher m =
        this.associationNamePattern.matcher(
            fieldName.replaceAll(DOT_CHARACTER_REPLACEMENT, DOT_CHARACTER));
    if (m.matches()) {
      String qNamePrefix = m.group(1);
      String localName = m.group(2);
      String assocSuffix = m.group(3);

      QName fullQName = QName.createQName(qNamePrefix, localName, namespaceService);

      // ensure that the association being persisted is defined in the model
      AssociationDefinition assocDef = assocDefs.get(fullQName);

      // TODO: if the association is not defined on the node, check for the association
      // in all models, however, the source of an association can be critical so we
      // can't just look up the association in the model regardless. We need to
      // either check the source class of the node and the assoc def match or we
      // check that the association was defined as part of an aspect (where by it's
      // nature can have any source type)

      // SIDE : since forms and model are generated by SIDE we make the assertion that fields are
      // valid, so no advanced validation are done
      if (assocDef == null) {
        if (getLogger().isWarnEnabled()) {
          getLogger()
              .debug(
                  "Field '"
                      + fieldName
                      + "' as an association definition can not be found in the current model");
        }
        assocDef = this.dictionaryService.getAssociation(fullQName);

        if (assocDef == null) {
          if (getLogger().isWarnEnabled()) {
            getLogger()
                .warn(
                    "Ignoring field '"
                        + fieldName
                        + "' as an association definition can not be found in ANY models ");
          }
          return;
        } else {
          if (getLogger().isDebugEnabled()) {
            getLogger()
                .debug(
                    "Field '"
                        + fieldName
                        + "' Found as an association definition in another model : "
                        + assocDef.getModel().getName());
          }
        }
      }

      String value = (String) fieldData.getValue();
      String[] nodeRefs = value.split(",");

      // Each element in this array will be a new target node in association
      // with the current node.
      for (String nextTargetNode : nodeRefs) {
        if (nextTargetNode.length() > 0) {
          if (NodeRef.isNodeRef(nextTargetNode)) {
            if (assocSuffix.equals(ASSOC_DATA_ADDED_SUFFIX)) {
              if (assocDef.isChild()) {
                assocCommands.add(
                    new AddChildAssocCommand(nodeRef, new NodeRef(nextTargetNode), fullQName));
              } else {
                assocCommands.add(
                    new AddAssocCommand(nodeRef, new NodeRef(nextTargetNode), fullQName));
              }
            } else if (assocSuffix.equals(ASSOC_DATA_REMOVED_SUFFIX)) {
              if (assocDef.isChild()) {
                assocCommands.add(
                    new RemoveChildAssocCommand(nodeRef, new NodeRef(nextTargetNode), fullQName));
              } else {
                assocCommands.add(
                    new RemoveAssocCommand(nodeRef, new NodeRef(nextTargetNode), fullQName));
              }
            } else {
              if (getLogger().isWarnEnabled()) {
                StringBuilder msg = new StringBuilder();
                msg.append("Ignoring 'fieldName ")
                    .append(fieldName)
                    .append("' as it does not have one of the expected suffixes (")
                    .append(ASSOC_DATA_ADDED_SUFFIX)
                    .append(" or ")
                    .append(ASSOC_DATA_REMOVED_SUFFIX)
                    .append(")");
                getLogger().warn(msg.toString());
              }
            }
          } else {
            if (getLogger().isWarnEnabled()) {
              StringBuilder msg = new StringBuilder();
              msg.append("targetNode ")
                  .append(nextTargetNode)
                  .append(" is not a valid NodeRef and has been ignored.");
              getLogger().warn(msg.toString());
            }
          }
        }
      }
    } else if (getLogger().isWarnEnabled()) {
      getLogger().warn("Ignoring unrecognised field '" + fieldName + "'");
    }
  }