Пример #1
0
  public static com.liferay.opensocial.model.Gadget getGadget(PortletPreferences portletPreferences)
      throws Exception {

    String url = portletPreferences.getValue("url", StringPool.BLANK);

    if (Validator.isNull(url)) {
      return null;
    }

    com.liferay.opensocial.model.Gadget gadget = new GadgetImpl();

    GadgetSpec gadgetSpec = null;

    try {
      gadgetSpec = ShindigUtil.getGadgetSpec(url);
    } catch (Exception e) {
      throw new GadgetURLException(e);
    }

    ModulePrefs modulePrefs = gadgetSpec.getModulePrefs();

    gadget.setName(modulePrefs.getTitle());
    gadget.setUrl(url);

    return gadget;
  }
  protected OAuthToken getOAuthToken(
      SecurityToken securityToken, String serviceName, String tokenName) 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);
    }

    OAuthToken oAuthToken = null;

    try {
      oAuthToken =
          OAuthTokenLocalServiceUtil.getOAuthToken(
              userId, gadget.getGadgetId(), serviceName, securityToken.getModuleId(), tokenName);
    } catch (Exception e) {
    }

    return oAuthToken;
  }
  /**
   * Adds the gadget to the database. Also notifies the appropriate model listeners.
   *
   * @param gadget the gadget
   * @return the gadget that was added
   * @throws SystemException if a system exception occurred
   */
  @Indexable(type = IndexableType.REINDEX)
  @Override
  public Gadget addGadget(Gadget gadget) throws SystemException {
    gadget.setNew(true);

    return gadgetPersistence.update(gadget);
  }
  /**
   * Converts the soap model instance into a normal model instance.
   *
   * @param soapModel the soap model instance to convert
   * @return the normal model instance
   */
  public static Gadget toModel(GadgetSoap soapModel) {
    Gadget model = new GadgetImpl();

    model.setUuid(soapModel.getUuid());
    model.setGadgetId(soapModel.getGadgetId());
    model.setCompanyId(soapModel.getCompanyId());
    model.setCreateDate(soapModel.getCreateDate());
    model.setModifiedDate(soapModel.getModifiedDate());
    model.setName(soapModel.getName());
    model.setUrl(soapModel.getUrl());
    model.setPortletCategoryNames(soapModel.getPortletCategoryNames());

    return model;
  }
  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 int compareTo(Gadget gadget) {
    int value = 0;

    value = getName().compareTo(gadget.getName());

    if (value != 0) {
      return value;
    }

    return 0;
  }
  public void setTokenInfo(
      SecurityToken securityToken,
      ConsumerInfo consumerInfo,
      String serviceName,
      String tokenName,
      TokenInfo tokenInfo)
      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);
    }

    try {
      OAuthTokenLocalServiceUtil.addOAuthToken(
          userId,
          gadget.getGadgetId(),
          serviceName,
          securityToken.getModuleId(),
          tokenInfo.getAccessToken(),
          tokenName,
          tokenInfo.getTokenSecret(),
          tokenInfo.getSessionHandle(),
          tokenInfo.getTokenExpireMillis());
    } catch (Exception e) {
      throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e);
    }
  }
  @Override
  public boolean equals(Object obj) {
    if (obj == null) {
      return false;
    }

    Gadget gadget = null;

    try {
      gadget = (Gadget) obj;
    } catch (ClassCastException cce) {
      return false;
    }

    long primaryKey = gadget.getPrimaryKey();

    if (getPrimaryKey() == primaryKey) {
      return true;
    } else {
      return false;
    }
  }
  @Override
  protected void doImportStagedModel(PortletDataContext portletDataContext, Gadget gadget)
      throws Exception {

    ServiceContext serviceContext = portletDataContext.createServiceContext(gadget);

    Gadget importedGadget = null;

    if (portletDataContext.isDataStrategyMirror()) {
      Gadget existingGadget =
          GadgetLocalServiceUtil.fetchGadgetByUuidAndCompanyId(
              gadget.getUuid(), portletDataContext.getCompanyId());

      if (existingGadget == null) {
        serviceContext.setUuid(gadget.getUuid());

        importedGadget =
            GadgetLocalServiceUtil.addGadget(
                portletDataContext.getCompanyId(),
                gadget.getUrl(),
                gadget.getPortletCategoryNames(),
                serviceContext);
      } else {
        existingGadget.setName(gadget.getName());
        existingGadget.setUrl(gadget.getUrl());
        existingGadget.setPortletCategoryNames(gadget.getPortletCategoryNames());

        importedGadget = GadgetLocalServiceUtil.updateGadget(existingGadget);
      }
    } else {
      importedGadget =
          GadgetLocalServiceUtil.addGadget(
              portletDataContext.getCompanyId(),
              gadget.getUrl(),
              gadget.getPortletCategoryNames(),
              serviceContext);
    }

    portletDataContext.importClassedModel(gadget, importedGadget);
  }
 @Override
 public String getDisplayName(Gadget gadget) {
   return gadget.getName();
 }