private static void _populateContextFromThreadLocals(Map<String, Serializable> context) {

    if (!context.containsKey("companyId")) {
      context.put("companyId", CompanyThreadLocal.getCompanyId());
    }

    if (!context.containsKey("defaultLocale")) {
      context.put("defaultLocale", LocaleThreadLocal.getDefaultLocale());
    }

    if (!context.containsKey("groupId")) {
      context.put("groupId", GroupThreadLocal.getGroupId());
    }

    if (!context.containsKey("principalName")) {
      context.put("principalName", PrincipalThreadLocal.getName());
    }

    if (!context.containsKey("principalPassword")) {
      context.put("principalPassword", PrincipalThreadLocal.getPassword());
    }

    if (!context.containsKey("siteDefaultLocale")) {
      context.put("siteDefaultLocale", LocaleThreadLocal.getSiteDefaultLocale());
    }

    if (!context.containsKey("themeDisplayLocale")) {
      context.put("themeDisplayLocale", LocaleThreadLocal.getThemeDisplayLocale());
    }
  }
  private static void _populateThreadLocalsFromContext(Map<String, Serializable> context) {

    long companyId = GetterUtil.getLong(context.get("companyId"));

    if (companyId > 0) {
      CompanyThreadLocal.setCompanyId(companyId);
    }

    Locale defaultLocale = (Locale) context.get("defaultLocale");

    if (defaultLocale != null) {
      LocaleThreadLocal.setDefaultLocale(defaultLocale);
    }

    long groupId = GetterUtil.getLong(context.get("groupId"));

    if (groupId > 0) {
      GroupThreadLocal.setGroupId(groupId);
    }

    String principalName = GetterUtil.getString(context.get("principalName"));

    if (Validator.isNotNull(principalName)) {
      PrincipalThreadLocal.setName(principalName);
    }

    PermissionChecker permissionChecker = null;

    if (Validator.isNotNull(principalName)) {
      try {
        User user = UserLocalServiceUtil.fetchUser(PrincipalThreadLocal.getUserId());

        permissionChecker = PermissionCheckerFactoryUtil.create(user);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }

    if (permissionChecker != null) {
      PermissionThreadLocal.setPermissionChecker(permissionChecker);
    }

    String principalPassword = GetterUtil.getString(context.get("principalPassword"));

    if (Validator.isNotNull(principalPassword)) {
      PrincipalThreadLocal.setPassword(principalPassword);
    }

    Locale siteDefaultLocale = (Locale) context.get("siteDefaultLocale");

    if (siteDefaultLocale != null) {
      LocaleThreadLocal.setSiteDefaultLocale(siteDefaultLocale);
    }

    Locale themeDisplayLocale = (Locale) context.get("themeDisplayLocale");

    if (themeDisplayLocale != null) {
      LocaleThreadLocal.setThemeDisplayLocale(themeDisplayLocale);
    }
  }