private void setDsmRuleToTnContext(
     UserProfile userProfile, TnContext tc, boolean isAddAllDsmRule) {
   DsmRuleHolder dsmHolder = ResourceHolderManager.getResourceHolder(HolderType.DSM_TYPE);
   if (dsmHolder != null) {
     Map map = dsmHolder.getDsmResponses(userProfile, tc);
     if (map != null) {
       Iterator it = map.keySet().iterator();
       while (it.hasNext()) {
         String key = (String) it.next();
         if (isAddAllDsmRule) {
           tc.addProperty(key, (String) map.get(key));
         } else {
           if (isDsmRuleNeedRegister(key)) {
             tc.addProperty(key, (String) map.get(key));
           }
         }
       }
     }
   }
 }
  public TnContext getTnContext(UserProfile userProfile, ExecutorContext context) {

    String userID = userProfile.getUserId();

    long lUserID = -1;
    if (userID != null && userID.length() > 0) {
      lUserID = Long.parseLong(userID);
    }

    ContextMgrService cms = new ContextMgrService();
    ContextMgrStatus myStatus = null;

    if (logger.isDebugEnabled()) {
      logger.debug("needRegister:" + needRegister);
    }

    TnContext tc = new TnContext();
    try {
      if (needRegister) {
        // get the tncontext from DSM db
        // tc = getTnContextFromDsmDB(userProfile.getMin());
        // set user profile into tncontext
        setUserProfileToTnContext(tc, userProfile, context);
        // set the need register dsm rule into tncontext
        setNeedRegisterDsmRuleToTnContext(userProfile, tc);
        if (logger.isDebugEnabled()) {
          logger.debug("tc -->" + tc);
          logger.debug("tc.length = " + tc.toContextString().length());
        }
        myStatus = cms.registerContext(lUserID, Constants.CALLER_CSERVER, tc);

      } else {
        // set user profile into tncontext
        setUserProfileToTnContext(tc, userProfile, context);
        myStatus = cms.updateContext(tc);
      }
    } catch (Exception e) {
      logger.fatal(e, e);
    }

    // set all the dsm rules in to tncontext
    setAllDsmRuleToTnContext(userProfile, tc);

    DevicePropertiesHolder deviceHolder =
        (DevicePropertiesHolder) ResourceHolderManager.getResourceHolder(HolderType.DEVICE_TYPE);
    if (deviceHolder != null) {
      DeviceProperties deviceProperties = deviceHolder.getDeviceProperties(userProfile, tc);
      if (deviceProperties != null) {
        tc.addProperty(
            GENERATE_LANE_INFO,
            String.valueOf(deviceProperties.getBooleanDefFalse(NAV_ENABLE_LANE_ASSIST)));
      }
    }

    if (myStatus == null || myStatus.getStatusCode() != Error.NO_ERROR) {
      CliTransaction cli =
          com.telenav.cserver.framework.cli.CliTransactionFactory.getInstance(
              CliConstants.TYPE_MODULE);
      cli.setFunctionName("getTnContext");
      cli.addData(
          "cms_status(has issue)", myStatus != null ? myStatus.getStatusCode() + "" : "null");

      tc = getDefaultTnContext(tc);
      if (logger.isDebugEnabled()) {
        logger.debug("tnContext has issue.");
      }

      cli.complete();
    }
    logger.debug("final tc:>>" + tc);
    return tc;
  }