/** @return */
  public Map<String, Object> retrieveData() {
    Map<String, Object> map = new HashMap<String, Object>();

    for (Object object : getApplicationContext().getAllDataMappings()) {
      DataMapping dataMapping = (DataMapping) object;

      if (dataMapping.getDirection().equals(Direction.OUT)
          || dataMapping.getDirection().equals(Direction.IN_OUT)) {
        InputController inputCtrl = getTopLevelInputController(dataMapping.getId());
        Object value = getUnwrapValue(dataMapping.getId());

        // Handle Documents Specially
        if (null != value && inputCtrl instanceof DocumentInputController) {
          // Handle Unsaved Documents
          // Save Document, And if it's saved then again fetch the same and use it
          if (((DocumentInputController) inputCtrl).saveDocument()) {
            value = getUnwrapValue(dataMapping.getId());
          }
        }
        map.put(dataMapping.getId(), value);
      }
    }

    return map;
  }
 /**
  * @param dataMapping
  * @param allInMappings
  * @return
  */
 private boolean isWriteOnly(DataMapping dataMapping, List<Object> allInMappings) {
   if (Direction.IN == dataMapping.getDirection()
       || Direction.IN_OUT == dataMapping.getDirection()) {
     return false;
   } else if (Direction.OUT == dataMapping.getDirection()) {
     for (Object object : allInMappings) {
       DataMapping dm = (DataMapping) object;
       if (dm.getId().equals(dataMapping.getId())) {
         return false;
       }
     }
   }
   return true;
 }
  public void setData() {
    Map<String, Serializable> inDataValues =
        getWorkflowService()
            .getInDataValues(activityInstance.getOID(), getApplicationContext().getId(), null);

    for (Object object : getApplicationContext().getAllDataMappings()) {
      DataMapping dataMapping = (DataMapping) object;

      if (dataMapping.getDirection().equals(Direction.IN)
          || dataMapping.getDirection().equals(Direction.IN_OUT)) {
        Object value = inDataValues.get(dataMapping.getId());
        setValue(dataMapping.getId(), value);
      }
    }
  }
 /**
  * @param dataMapping
  * @return
  */
 private boolean isReadOnly(DataMapping dataMapping) {
   if (ModelUtils.isSystemDefinedReadOnlyData(dataMapping)) {
     return true;
   } else if (ModelUtils.isDMSReadOnlyData(getModel(), dataMapping)) {
     return true;
   }
   return Direction.IN == dataMapping.getDirection();
 }
  /**
   * @param dataMapping
   * @param maPath
   * @return
   */
  private Path createStructureDataMapping(DataMapping dataMapping, ManualActivityPath maPath) {
    Set<TypedXPath> xpaths = ModelUtils.getXPaths(getModel(), dataMapping);

    for (TypedXPath path : xpaths) {
      if (path.getParentXPath() == null) {
        return new XsdPath(
            maPath, path, dataMapping.getId(), Direction.IN == dataMapping.getDirection());
      }
    }
    return null;
  }
  /**
   * @param dataMapping
   * @param allInMappings
   * @param maPath
   * @return
   */
  private Path createDMSDataMapping(
      DataMapping dataMapping, List<Object> allInMappings, ManualActivityPath maPath) {
    if (ModelUtils.isDocumentType(getModel(), dataMapping)) // Document
    {
      if (!isWriteOnly(dataMapping, allInMappings)) {
        DocumentType documentType =
            org.eclipse.stardust.ui.web.viewscommon.utils.ModelUtils.getDocumentTypeFromData(
                getModel(), getModel().getData(dataMapping.getDataId()));

        if (documentType == null) {
          trace.debug(
              "Could not resolve type for Document:, "
                  + dataMapping.getQualifiedId()
                  + ". It may be set defualt by design");
        }

        return new DocumentPath(
            maPath,
            dataMapping.getId(),
            documentType,
            null,
            Direction.IN == dataMapping.getDirection());
      } else {
        trace.warn(
            "Skipping Data Mapping - Found it as Write Only - "
                + dataMapping.getId()
                + ":"
                + dataMapping.getName());
      }
    } else if (ModelUtils.isFolderType(getModel(), dataMapping)) // Folder
    {
      // Skip, Not supported
    } else // Only Meta Data
    {
      Path docTypePath = null;
      Data documentData = getModel().getData(dataMapping.getDataId());
      String metaDataTypeId =
          (String) documentData.getAttribute(DmsConstants.RESOURCE_METADATA_SCHEMA_ATT);

      if (StringUtils.isNotEmpty(metaDataTypeId)) {
        TypeDeclaration typeDeclaration = getModel().getTypeDeclaration(metaDataTypeId);
        Set<TypedXPath> allXPaths = StructuredTypeRtUtils.getAllXPaths(getModel(), typeDeclaration);

        for (TypedXPath path : allXPaths) {
          if ("properties".equals(dataMapping.getDataPath())) // Mapping to entire properties
          {
            if (null == path.getParentXPath()) {
              docTypePath =
                  new XsdPath(
                      maPath,
                      path,
                      dataMapping.getId(),
                      Direction.IN == dataMapping.getDirection());
              break;
            }
          } else if (dataMapping
              .getDataPath()
              .equals("properties/" + path.getXPath())) // Mapping to nested item in properties
          {
            docTypePath =
                new XsdPath(
                    maPath, path, dataMapping.getId(), Direction.IN == dataMapping.getDirection());
            break;
          }
        }
      }

      // if null means - Mapping to documenmt's attributes e.g. id, owner, etc
      if (null == docTypePath) {
        // This is the only possibility, but still check
        if (ModelUtils.isPrimitiveType(getModel(), dataMapping)) {
          docTypePath = createPrimitiveDataMapping(dataMapping, maPath);
        }
      }

      return docTypePath;
    }

    return null;
  }