/** Updates the values in the attribute bos from the attribute values map. */
 public void updateAttributeBoValues() {
   for (PeopleFlowAttributeBo attributeBo : this.attributeBos) {
     if (this.attributeValues.containsKey(attributeBo.getAttributeDefinition().getName())) {
       String attributeValue =
           this.attributeValues.get(attributeBo.getAttributeDefinition().getName());
       attributeBo.setValue(attributeValue);
     }
   }
 }
  @Override
  public Map<String, String> getAttributes() {
    Map<String, String> results = new HashMap<String, String>();

    if (null != this.attributeBos)
      for (PeopleFlowAttributeBo attr : this.attributeBos) {
        results.put(attr.getAttributeDefinition().getName(), attr.getValue());
      }

    return results;
  }
 /**
  * Updates the values in the attribute values map from the attribute bos and updates the members.
  */
 @PostLoad
 protected void postLoad() {
   this.attributeValues = new HashMap<String, String>();
   for (PeopleFlowAttributeBo attributeBo : attributeBos) {
     this.attributeValues.put(
         attributeBo.getAttributeDefinition().getName(), attributeBo.getValue());
   }
   for (PeopleFlowMemberBo member : members) {
     if (member.getMemberName() == null) {
       member.updateRelatedObject();
     }
     for (PeopleFlowDelegateBo delegate : member.getDelegates()) {
       if (delegate.getMemberName() == null) {
         delegate.updateRelatedObject();
       }
     }
   }
 }
  /** Invoked to rebuild the type attribute bos and attributes value map based on the type id */
  public void rebuildTypeAttributes() {
    this.attributeBos = new ArrayList<PeopleFlowAttributeBo>();
    this.attributeValues = new HashMap<String, String>();

    KewTypeDefinition typeDefinition =
        KewApiServiceLocator.getKewTypeRepositoryService().getTypeById(this.typeId);
    if ((typeDefinition.getAttributes() != null) && !typeDefinition.getAttributes().isEmpty()) {
      List<KewTypeAttribute> typeAttributes =
          new ArrayList<KewTypeAttribute>(typeDefinition.getAttributes());

      List<String> sortAttributes = new ArrayList<String>();
      sortAttributes.add(KEWPropertyConstants.SEQUENCE_NUMBER);
      Collections.sort(typeAttributes, new BeanPropertyComparator(sortAttributes));

      for (KewTypeAttribute typeAttribute : typeAttributes) {
        PeopleFlowAttributeBo attributeBo =
            PeopleFlowAttributeBo.from(typeAttribute.getAttributeDefinition(), null, this, null);
        this.attributeBos.add(attributeBo);
        this.attributeValues.put(typeAttribute.getAttributeDefinition().getName(), "");
      }
    }
  }
  /**
   * Translates from the given PeopleFlowDefinition to a PeopleFlowBo, optionally updating the given
   * "toUpdate" parameter instead of creating a new PeopleFlowBo. If it's not passed then a new
   * PeopleFlowBo will be created.
   */
  public static PeopleFlowBo fromAndUpdate(
      PeopleFlowDefinition peopleFlow, KewTypeDefinition kewTypeDefinition, PeopleFlowBo toUpdate) {

    PeopleFlowBo result = toUpdate;

    if (null == toUpdate) {
      result = new PeopleFlowBo();
    }

    result.setId(peopleFlow.getId());
    result.setName(peopleFlow.getName());
    result.setNamespaceCode(peopleFlow.getNamespaceCode());
    result.setTypeId(peopleFlow.getTypeId());
    result.setDescription(peopleFlow.getDescription());
    result.setActive(peopleFlow.isActive());
    result.setVersionNumber(peopleFlow.getVersionNumber());

    // we need to translate attributes over, this is a bit more work, first let's do some validation
    if (null == peopleFlow.getTypeId()) {
      if (null != kewTypeDefinition) {
        throw new RiceIllegalArgumentException(
            "PeopleFlow has no type id, but a KewTypeDefinition was "
                + "supplied when it should not have been.");
      }
    }
    if (null != peopleFlow.getTypeId()) {
      if (kewTypeDefinition == null) {
        throw new RiceIllegalArgumentException(
            "PeopleFlow has a type id of '"
                + peopleFlow.getTypeId()
                + "' but no KewTypeDefinition was supplied.");
      }
      if (!kewTypeDefinition.getId().equals(peopleFlow.getTypeId())) {
        throw new RiceIllegalArgumentException(
            "Type id of given KewTypeDefinition does not match PeopleFlow "
                + "type id:  "
                + kewTypeDefinition.getId()
                + " != "
                + peopleFlow.getTypeId());
      }
    }

    // now we need to effectively do a diff with the given attributes, first let's add new entries
    // and update
    // existing ones
    // TODO - ensure this is correct
    ArrayList attributesToAdd = new ArrayList<PeopleFlowAttributeBo>();
    // if type is null drop attributes
    if (null != peopleFlow.getTypeId()) {
      for (String key : peopleFlow.getAttributes().keySet()) {
        KewAttributeDefinition attributeDefinition =
            kewTypeDefinition.getAttributeDefinitionByName(key);
        if (null == attributeDefinition) {
          throw new RiceIllegalArgumentException(
              "There is no attribute definition for the given attribute " + "name '" + key + "'");
        }
        attributesToAdd.add(
            PeopleFlowAttributeBo.from(
                attributeDefinition, null, result, peopleFlow.getAttributes().get(key)));
      }
      result.setAttributeBos(attributesToAdd);
    }
    // TODO - END
    handleMembersUpdate(result, peopleFlow);

    return result;
  }