Esempio n. 1
0
  public void executeAction(
      WorkflowProcessor processor, Map<String, WorkflowActionClassParameter> params)
      throws WorkflowActionFailureException {

    String tweatThis = null;
    try {
      tweatThis = processor.getWorkflowMessage();
      if (!UtilMethods.isSet(tweatThis) && UtilMethods.isSet(params.get("fieldVar").getValue())) {
        tweatThis = processor.getContentlet().getStringProperty(params.get("fieldVar").getValue());
      }

      if (UtilMethods.isSet(tweatThis)) {

        if (tweatThis.length() > 140) {
          String error =
              LanguageUtil.get(
                  PublicCompanyFactory.getDefaultCompanyId(),
                  PublicCompanyFactory.getDefaultCompany().getLocale(),
                  "Tweet-too-long");
          if (error.equals("Tweet-too-long")) {
            error = error.replaceAll("-", " ");
          }

          throw new DotValidationException(error);
        }

        String consumerKey = null,
            consumerSecret = null,
            password = null,
            userName = null,
            accessToken = null,
            accessTokenSecret = null;

        consumerKey = params.get("consumerKey").getValue();
        consumerSecret = params.get("consumerSecret").getValue();
        accessToken = params.get("accessToken").getValue();
        accessTokenSecret = params.get("accessTokenSecret").getValue();

        String path =
            APILocator.getContentletAPI()
                .getUrlMapForContentlet(
                    processor.getContentlet(), APILocator.getUserAPI().getSystemUser(), false);

        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true)
            .setOAuthConsumerKey(consumerKey)
            .setOAuthConsumerSecret(consumerSecret)
            .setOAuthAccessToken(accessToken)
            .setOAuthAccessTokenSecret(accessTokenSecret);

        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = tf.getInstance();

        Status stat = twitter.updateStatus(tweatThis);

        WorkflowComment comment = new WorkflowComment();
        comment.setPostedBy(processor.getUser().getUserId());
        comment.setComment("Tweeted: " + tweatThis + " twitterId:" + stat.getId());
        comment.setWorkflowtaskId(processor.getTask().getId());
        try {
          APILocator.getWorkflowAPI().saveComment(comment);
        } catch (DotDataException e) {
          Logger.error(CommentOnWorkflowActionlet.class, e.getMessage(), e);
        }
      }

    } catch (Exception e) {
      Logger.error(TwitterActionlet.class, e.getMessage());
      throw new WorkflowActionFailureException(e.getMessage());
    }
  }