public OAuthConsumer addOAuthConsumer(
      long companyId,
      String gadgetKey,
      String serviceName,
      String consumerKey,
      String consumerSecret,
      String keyType)
      throws SystemException {

    if (keyType.equals(OAuthConsumerConstants.KEY_TYPE_RSA_PRIVATE)) {
      consumerSecret = StringPool.BLANK;
    }

    Date now = new Date();

    long oAuthConsumerId = counterLocalService.increment();

    OAuthConsumer oAuthConsumer = oAuthConsumerPersistence.create(oAuthConsumerId);

    oAuthConsumer.setCompanyId(companyId);
    oAuthConsumer.setCreateDate(now);
    oAuthConsumer.setModifiedDate(now);
    oAuthConsumer.setGadgetKey(gadgetKey);
    oAuthConsumer.setServiceName(serviceName);
    oAuthConsumer.setConsumerKey(consumerKey);
    oAuthConsumer.setConsumerSecret(consumerSecret);
    oAuthConsumer.setKeyType(keyType);

    oAuthConsumerPersistence.update(oAuthConsumer, false);

    return oAuthConsumer;
  }
  @Override
  public OAuthConsumer deleteOAuthConsumer(OAuthConsumer oAuthConsumer) throws SystemException {

    // OAuth consumer

    oAuthConsumerPersistence.remove(oAuthConsumer);

    // OAuth tokens

    oAuthTokenLocalService.deleteOAuthTokens(
        oAuthConsumer.getGadgetKey(), oAuthConsumer.getServiceName());

    return oAuthConsumer;
  }
  protected OAuthConsumer getOAuthConsumer(SecurityToken securityToken, String serviceName)
      throws GadgetException {

    long userId = GetterUtil.getLong(securityToken.getViewerId());

    User user = null;

    try {
      user = UserLocalServiceUtil.getUser(userId);
    } catch (Exception e) {
      throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e);
    }

    Gadget gadget = null;

    try {
      gadget = GadgetLocalServiceUtil.getGadget(user.getCompanyId(), securityToken.getAppUrl());
    } catch (Exception e) {
      throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e);
    }

    OAuthConsumer oAuthConsumer = null;

    try {
      oAuthConsumer =
          OAuthConsumerLocalServiceUtil.getOAuthConsumer(gadget.getGadgetId(), serviceName);
    } catch (Exception e) {
      return _oAuthConsumer;
    }

    if (oAuthConsumer.getKeyType().equals(OAuthConsumerConstants.KEY_TYPE_RSA_PRIVATE)) {

      if (_oAuthConsumer == null) {
        throw new GadgetException(
            GadgetException.Code.INTERNAL_SERVER_ERROR, "No OAuth key specified");
      }

      oAuthConsumer.setConsumerSecret(_oAuthConsumer.getConsumerSecret());
    }

    return oAuthConsumer;
  }
  public OAuthConsumer updateOAuthConsumer(
      long oAuthConsumerId,
      String consumerKey,
      String consumerSecret,
      String keyType,
      String keyName,
      String callbackURL)
      throws PortalException, SystemException {

    if (keyType.equals(OAuthConsumerConstants.KEY_TYPE_RSA_PRIVATE)) {
      consumerSecret = StringPool.BLANK;
    }

    OAuthConsumer oAuthConsumer = oAuthConsumerPersistence.findByPrimaryKey(oAuthConsumerId);

    oAuthConsumer.setConsumerKey(consumerKey);
    oAuthConsumer.setConsumerSecret(consumerSecret);
    oAuthConsumer.setKeyType(keyType);

    oAuthConsumerPersistence.update(oAuthConsumer, false);

    return oAuthConsumer;
  }
  public ConsumerInfo getConsumerKeyAndSecret(
      SecurityToken securityToken, String serviceName, OAuthServiceProvider oAuthServiceProvider)
      throws GadgetException {

    OAuthConsumer oAuthConsumer = getOAuthConsumer(securityToken, serviceName);

    if (oAuthConsumer == null) {
      throw new GadgetException(
          GadgetException.Code.INTERNAL_SERVER_ERROR,
          "No key for gadget " + securityToken.getAppUrl() + " and service " + serviceName);
    }

    net.oauth.OAuthConsumer netOAuthConsumer = null;

    String keyType = oAuthConsumer.getKeyType();

    if (keyType.equals(OAuthConsumerConstants.KEY_TYPE_RSA_PRIVATE)) {
      netOAuthConsumer =
          new net.oauth.OAuthConsumer(
              null, oAuthConsumer.getConsumerKey(), null, oAuthServiceProvider);

      netOAuthConsumer.setProperty(OAuth.OAUTH_SIGNATURE_METHOD, OAuth.RSA_SHA1);
      netOAuthConsumer.setProperty(RSA_SHA1.PRIVATE_KEY, oAuthConsumer.getConsumerSecret());
    } else {
      netOAuthConsumer =
          new net.oauth.OAuthConsumer(
              null,
              oAuthConsumer.getConsumerKey(),
              oAuthConsumer.getConsumerSecret(),
              oAuthServiceProvider);

      netOAuthConsumer.setProperty(OAuth.OAUTH_SIGNATURE_METHOD, OAuth.HMAC_SHA1);
    }

    String keyName = oAuthConsumer.getKeyName();

    String callbackURL = _callbackURL.replace("%host%", ShindigUtil.getHost());

    return new ConsumerInfo(netOAuthConsumer, keyName, callbackURL);
  }