Exemplo n.º 1
0
  public void execute(final WorkItem item, final WorkflowSession session, final MetaDataMap args)
      throws SocialModerationException {
    log.debug("In the workflow... (Flagged in JCR) ");
    try {
      validate(item);
      final JSONArray arr = new JSONArray((String) item.getWorkflowData().getPayload());
      final ResourceResolver resolver = getResourceResolver(session.getSession());

      for (int i = 0; i < arr.length(); i++) {
        final JSONObject obj = (JSONObject) arr.get(i);
        final String path = obj.getString(ModerationConstants.PATH);

        if (!obj.has(ModerationConstants.ACTION_FLAG)) {
          throw new SocialModerationException(
              FLAG_VERB_NOT_PRESENT_IN_PAYLOAD,
              ModerationConstants.ERR_FLAG_VERB_NOT_PRESENT_IN_PAYLOAD);
        }

        final boolean isFlagged = obj.getBoolean(ModerationConstants.ACTION_FLAG);
        final Resource resource = resolver.getResource(path);

        if (resource == null) {
          throw new SocialModerationException(
              ERR_SPECIFIED_RESOURCE_DOES_NOT_EXIST + path,
              ModerationConstants.ERR_SPECIFIED_RESOURCE_DOES_NOT_EXIST);
        } else {
          final Comment comment = resource.adaptTo(Comment.class);

          if (comment != null) {
            final Node node = resource.adaptTo(Node.class);
            node.setProperty(ModerationConstants.PROP_IS_FLAGGED, isFlagged);
            node.setProperty(ModerationConstants.PROP_IS_READ, true);
            addModHistory(
                profileMgr,
                resolver,
                node,
                ModerationConstants.PROP_IS_FLAGGED,
                String.valueOf(isFlagged),
                item.getWorkflow().getInitiator());
            node.getSession().save();
            replicator.replicate(
                session.getSession(),
                com.day.cq.replication.ReplicationActionType.ACTIVATE,
                path.substring(0, path.lastIndexOf('/')),
                null);
            // msgs[i] = "Comment " + verb + ": " + path;
          } else {
            throw new SocialModerationException(
                "Not a comment: " + path, ModerationConstants.ERR_NOT_A_COMMENT);
          }
        }
      }
    } catch (final JSONException je) {
      log.error(je.getLocalizedMessage(), je.getCause());
      throw new SocialModerationException(
          je.getLocalizedMessage(), ModerationConstants.ERR_FLAGGED_ACTION, je.getCause());
    } catch (final Exception e) {
      log.error(e.getLocalizedMessage(), e.getCause());
      throw new SocialModerationException(
          e.getLocalizedMessage(), ModerationConstants.ERR_FLAGGED_ACTION, e.getCause());
    }
  }