/**
   * This method is called during the create phase of an archetype. It will check the assertion and
   * determine whether it needs to do any work.
   *
   * <p>If the node descriptor is a collection and it is not a parent child relationship then there
   * may be a need to create and associated default entries to the collection
   *
   * @param target the target object
   * @param node the node descriptor
   * @param assertion the particular assertion
   */
  public static void create(Object target, NodeDescriptor node, AssertionDescriptor assertion) {
    if ((node.isCollection()) && (!node.isParentChild())) {
      List<ArchetypeRangeInfo> atypes = getArchetypeRangeInfo(assertion);
      if (atypes.size() == 0) {
        return;
      }

      // iterate through all the archetype range and if a default value
      // has been specfied attempt to locate the entry and add it to
      // the collection
      for (ArchetypeRangeInfo type : atypes) {
        if (StringUtils.isEmpty(type.defaultValue)) {
          continue;
        }

        // okay a default value needs to be created
        ArchetypeDescriptor adesc =
            ArchetypeServiceHelper.getArchetypeService().getArchetypeDescriptor(type.shortName);
        if (adesc == null) {
          throw new AssertionRuntimeException(
              AssertionRuntimeException.ErrorCode.ArchetypeDoesNotExist,
              new Object[] {"archetypeRangeAssertion", "create", type.shortName});
        }

        try {
          ArchetypeId aid = adesc.getType();
          List<IMObject> objects =
              ArchetypeQueryHelper.get(
                      ArchetypeServiceHelper.getArchetypeService(),
                      aid.getEntityName(),
                      aid.getConcept(),
                      type.defaultValue,
                      true,
                      0,
                      ArchetypeQuery.ALL_RESULTS)
                  .getResults();

          // if can not find a matching entity
          if (objects.size() == 0) {
            throw new AssertionRuntimeException(
                AssertionRuntimeException.ErrorCode.FailedToFindArchetype,
                new Object[] {
                  "archetypeRangeAssertion", "create", type.shortName, type.defaultValue
                });
          }

          // only expect to retrieve a single object
          if (objects.size() > 1) {
            throw new AssertionRuntimeException(
                AssertionRuntimeException.ErrorCode.TooManyObjectOnCreate,
                new Object[] {
                  "archetypeRangeAssertion", "create", type.shortName, type.defaultValue
                });
          }

          // now add it to the collection
          node.addChildToCollection((IMObject) target, objects.get(0));
        } catch (Exception exception) {
          throw new AssertionRuntimeException(
              AssertionRuntimeException.ErrorCode.FailedInCreate,
              new Object[] {"archetypeRangeAssertion", "create", type.shortName},
              exception);
        }
      }
    }
  }