/**
   * Updating status of DO in Database in case of the error and update the status of pervious node
   * to waiting. This function is called from removeDataObjectFromNode. Here we are not removing
   * entry from DB since we would require meta data information about DO at each node in workflow.
   *
   * @param workflowId
   * @param nodeId
   * @param dataObject
   */
  private void updateDataObjectGivenStatusInDBQueue(
      String workflowId, String nodeId, IHICData dataObject, String status) {
    HashMap<String, Object> doDetails = dataObject.getDataObjectDetails();
    try {
      // This makes sure that the current call is pertaining to Workflow tool
      if (!isPatientPresentInFormValues(dataObject)) return;

      Hashtable<String, Object> metaData =
          HashUtil.removeNonRequiredKeysFromHashtable(
              ApplicationConstant.REQUIRED_KEYS_FOR_UNIQUE_DO,
              (Hashtable<String, Object>) doDetails.get(CoreConstants.DATAOBJECT_FORM_VALUES));
      DataObjectQueueImpl.setDataObjectExecutionStatus(
          workflowId,
          nodeId,
          StringUtil.getString(doDetails.get(CoreConstants.DATAOBJECT_USER_ID)),
          StringUtil.getString(doDetails.get(CoreConstants.DATAOBJECT_UNIQUE_ID)),
          status,
          metaData);
    } catch (WorkflowComponentException e) {
      e.printStackTrace();
    }
  }
  @Override
  public IHICData getMatchingDataObjectForUser(IHICData incomingDataObject, String nodeExecStatus) {
    /*String tempUniqueId = incomingDataObject.getUniqueID();

    // Update DO unique id to Patient ID which is unique for each patient
    updateDataObjectUniqueId(incomingDataObject);*/

    // For UniqueId to be same across server restart, search should not be based on UniqueId
    List<Object[]> output =
        DataObjectQueueImpl.checkDataObjectForUser(incomingDataObject, nodeExecStatus, false);

    // Check for null if list of data object
    if (output != null) {
      boolean idSet = false;
      for (int i = 0; i < output.size(); i++) {
        Dataobject dObj = (Dataobject) output.get(i)[0];

        if (dObj != null) {
          // This check is added in case more than one dataoject is recieved for same node exection
          // status.
          // So a unqiue id check is added if the stored dataobject and incoming dataobject has same
          // unqiue id and then unqiue id set
          if (output.size() > 1) {
            incomingDataObject = UniqueIDUtil.updateUniqueId(incomingDataObject);
            String uniqueId = incomingDataObject.getUniqueID();
            if (uniqueId.equalsIgnoreCase(dObj.getUniqueid())) idSet = true;
          }
          // Update incoming data object
          incomingDataObject = NOLISRuntimeHandler.prepareHICData(incomingDataObject);
          incomingDataObject.getData().getFormPattern().setFormId(dObj.getFormpattern());
          incomingDataObject.getData().getDataPattern().setDataPatternId(dObj.getDatapattern());
          // TODO create User Pattern info also
          // incomingDataObject.getData().a
          incomingDataObject.getData().setStatus(dObj.getStatus());
          incomingDataObject.setUniqueID(dObj.getUniqueid());

          incomingDataObject
              .getData()
              .getWorkflowPattern()
              .setWorkflowPattern(dObj.getWorkflownodeinfo().getWorkflowinfo().getName());
          incomingDataObject
              .getData()
              .getWorkflowPattern()
              .setWorkflowNode(dObj.getWorkflownodeinfo().getNodeinfo().getNodename());

          // Fetch meta data for this data object
          /*List<Dataobjectmetadata> doMetadataList = DataObjectMetaDataImpl.getAllMetaDataForDataObject(dObj.getId());
          if (doMetadataList != null)
          {
          	// Create form values
          	Hashtable<String, Object> formValues = incomingDataObject.getData().getFormPattern().getFormValues();
          	if (formValues == null) formValues = new Hashtable<String, Object>();

          	ArrayList toolOutValues= new ArrayList();
          	for (int j = 0; j < doMetadataList.size(); j++)
          	{
          		Dataobjectmetadata metaData = doMetadataList.get(j);
          		if ((formValues.containsKey(WorkflowConstant.TOOL_OUTPUT_FILE))
          				&& (metaData.getDatakey().equals(WorkflowConstant.TOOL_OUTPUT_FILE)))
          		{
          			Object obj = formValues.get(WorkflowConstant.TOOL_OUTPUT_FILE);
          			if(obj instanceof ArrayList)
          			{
          				toolOutValues = (ArrayList)obj;

          				if (!toolOutValues.contains(metaData.getDatavalue()))
          					toolOutValues.add(metaData.getDatavalue());
          				formValues.put(WorkflowConstant.TOOL_OUTPUT_FILE,toolOutValues);
          			}
          			else
          			{
          				String toolValue = obj.toString();

          				if (!toolOutValues.contains(toolValue))
          					toolOutValues.add(toolValue);
          				if (!toolOutValues.contains(metaData.getDatavalue()))
          					toolOutValues.add(metaData.getDatavalue());
          				formValues.put(WorkflowConstant.TOOL_OUTPUT_FILE,toolOutValues);
          			}
          		}
          		else
          		{
          			// Add entries to Form Values; but do not over-write
          			if (!formValues.containsKey(metaData.getDatakey()))
          				formValues.put(metaData.getDatakey(), metaData.getDatavalue());
          		}
          	}

          	// Set Form Values
          	incomingDataObject.getData().getFormPattern().setFormValues(formValues);
          }*/

          // Sent back the first matching data according to input received.
          if (output.size() > 1 && i < output.size() && !idSet) {
            continue;
          } else break;
        }
      }
    }

    /*incomingDataObject.setUniqueID(tempUniqueId);*/

    return incomingDataObject;
  }
 /** This method is used to add the delete do object in case of the error */
 @Override
 public void deleteToDataObject(Dataobject doDb) throws WorkflowComponentException {
   DataObjectQueueImpl.deleteDataObject(doDb);
 }
  /**
   * Function for adding DO to Node Queue in database.
   *
   * @param workflowId
   * @param nodeId
   * @param dataObject
   */
  private void addDataObjectToDBQueue(String workflowId, String nodeId, IHICData dataObject) {
    try {
      // This makes sure that the current call is pertaining to Workflow tool
      if (!isPatientSchedulePresentInFormValues(dataObject)) return;

      // Disallow non-visual WF to store entry in DB
      Workflowinfo workflow = WorkFlowImpl.getWorkflowBasedOnWorkflowName(workflowId);
      Boolean isVisual = workflow.getIsVisual();
      if (isVisual == null) return;
      if (!isVisual.booleanValue()) return;

      /*// Update DO unique id to Patient ID which is unique for each patient
      updateDataObjectUniqueId(dataObject);*/

      // Get WorkflowNodeInfo for current nodeId
      Workflownodeinfo wfNodeInfo = WorkFlowNodeInfoImpl.getWorkFlowNodeInfo(workflowId, nodeId);

      /*
       * &&&&&&&&&&&&&&&&&&&&&& PRINTING &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
       */
      DataObjectListUtil.printDataObject(dataObject, "THIS IS WHAT HAS COME FOR SAVE", "*");
      WorkflowComponent.log(
          0,
          "[workflowId]"
              + workflowId
              + "\n[nodeId]"
              + nodeId
              + "\n[NODE_EXECUTION_STATUS]"
              + ((wfNodeInfo.getNodeinfo().getNodetype().indexOf("I") >= 0)
                  ? WorkflowConstant.NODE_EXECUTION_STATUS_WAITING
                  : WorkflowConstant.NODE_EXECUTION_STATUS_PROGRESS));
      /*
       * &&&&&&&&&&&&&&&&&&&&&& PRINTING &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
       */

      // Store DataObject
      HashMap<String, Object> doDetails = dataObject.getDataObjectDetails();

      // Create meta data based on which search is required
      Hashtable<String, Object> metaData =
          HashUtil.removeNonRequiredKeysFromHashtable(
              ApplicationConstant.REQUIRED_KEYS_FOR_UNIQUE_DO,
              (Hashtable<String, Object>) doDetails.get(CoreConstants.DATAOBJECT_FORM_VALUES));

      Dataobject doQueue =
          DataObjectQueueImpl.addDataObjectToDbQueueWithExistingCheck(
              StringUtil.getString(doDetails.get(CoreConstants.DATAOBJECT_FORM_PATTERN)),
              StringUtil.getString(doDetails.get(CoreConstants.DATAOBJECT_DATA_PATTERN)),
              StringUtil.getString(doDetails.get(CoreConstants.DATAOBJECT_STATUS)),
              wfNodeInfo,
              StringUtil.getString(doDetails.get(CoreConstants.DATAOBJECT_USER_PATTERN)),
              StringUtil.getString(doDetails.get(CoreConstants.DATAOBJECT_USER_ID)),
              StringUtil.getString(doDetails.get(CoreConstants.DATAOBJECT_UNIQUE_ID)),
              (wfNodeInfo.getNodeinfo().getNodetype().indexOf("I") >= 0)
                  ? WorkflowConstant.NODE_EXECUTION_STATUS_WAITING
                  : WorkflowConstant.NODE_EXECUTION_STATUS_PROGRESS,
              metaData);

      // Store DataObject Metadata
      appendToDataObjectMetaData(workflowId, nodeId, dataObject, doQueue);

    } catch (WorkflowComponentException e) {
      e.printStackTrace();
    }
  }