/* (non-Javadoc)
   * @see org.openvpms.component.business.domain.im.archetype.descriptor.Descriptor#clone()
   */
  @Override
  public Object clone() throws CloneNotSupportedException {
    ArchetypeDescriptor copy = (ArchetypeDescriptor) super.clone();
    copy.nodeDescriptors = new LinkedHashMap<String, NodeDescriptor>(this.nodeDescriptors);
    copy.primary = this.primary;
    copy.type = (ArchetypeId) (type == null ? null : this.type.clone());

    return copy;
  }
Exemple #2
0
 /**
  * Resolves the state corresponding to a property.
  *
  * @param name the property name
  * @return the resolved state
  * @throws PropertyResolverException if the name is invalid
  */
 @Override
 @SuppressWarnings({"deprecation"})
 public PropertyState resolve(String name) {
   PropertyState state;
   IMObject object = root;
   ArchetypeDescriptor archetype = this.archetype;
   int index;
   while ((index = name.indexOf(".")) != -1) {
     String nodeName = name.substring(0, index);
     NodeDescriptor node = archetype.getNodeDescriptor(nodeName);
     if (node == null) {
       throw new NodeResolverException(InvalidProperty, name);
     }
     Object value = getValue(object, node);
     if (value == null) {
       // object missing.
       object = null;
       break;
     } else if (!(value instanceof IMObject)) {
       throw new NodeResolverException(InvalidObject, name);
     }
     object = (IMObject) value;
     archetype = service.getArchetypeDescriptor(object.getArchetypeId());
     name = name.substring(index + 1);
   }
   if (object != null) {
     NodeDescriptor leafNode = archetype.getNodeDescriptor(name);
     Object value;
     if (leafNode == null) {
       if ("displayName".equals(name)) {
         value = archetype.getDisplayName();
       } else if ("shortName".equals(name)) {
         value = object.getArchetypeId().getShortName();
       } else if ("uid".equals(name)) {
         value = object.getId();
       } else {
         throw new NodeResolverException(InvalidProperty, name);
       }
     } else {
       value = getValue(object, leafNode);
     }
     state = new PropertyState(object, archetype, name, leafNode, value);
   } else {
     state = new PropertyState();
   }
   return state;
 }
  /**
   * 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);
        }
      }
    }
  }