/**
   * Gets the value.
   *
   * @return the value.
   */
  public Serializable getValue() {
    String value = streamOptions.getValue(streamOptions.getSelectedIndex());

    JSONObject jsonObject = StreamJsonRequestFactory.getEmptyRequest();

    if (value.equals(StreamJsonRequestFactory.FOLLOWED_BY_KEY)) {
      jsonObject = StreamJsonRequestFactory.setSourceAsFollowing(jsonObject);
    } else if (value.equals(StreamJsonRequestFactory.SAVED_KEY)) {
      jsonObject = StreamJsonRequestFactory.setSourceAsSaved(jsonObject);
    } else if (value.equals(StreamJsonRequestFactory.PARENT_ORG_KEY)) {
      jsonObject = StreamJsonRequestFactory.setSourceAsParentOrg(jsonObject);
    } else if (value.equals(StreamJsonRequestFactory.JOINED_GROUPS_KEY)) {
      jsonObject = StreamJsonRequestFactory.setSourceAsJoinedGroups(jsonObject);
    } else if (value.equals(StreamJsonRequestFactory.RECIPIENT_KEY)) {
      StreamJsonRequestFactory.initRecipient(jsonObject);

      for (StreamScope scope : (LinkedList<StreamScope>) scopes.getValue()) {
        jsonObject =
            StreamJsonRequestFactory.addRecipient(
                EntityType.valueOf(scope.getScopeType().toString()),
                scope.getUniqueKey(),
                jsonObject);
      }
    } else if (value.equals(StreamJsonRequestFactory.LIKER_KEY)) {
      StreamJsonRequestFactory.initLikers(jsonObject);

      for (StreamScope scope : (LinkedList<StreamScope>) scopes.getValue()) {
        jsonObject =
            StreamJsonRequestFactory.addLiker(
                EntityType.valueOf(scope.getScopeType().toString()),
                scope.getUniqueKey(),
                jsonObject);
      }
    } else if (value.equals(StreamJsonRequestFactory.AUTHOR_KEY)) {
      StreamJsonRequestFactory.initAuthors(jsonObject);

      for (StreamScope scope : (LinkedList<StreamScope>) scopes.getValue()) {
        jsonObject =
            StreamJsonRequestFactory.addAuthor(
                EntityType.valueOf(scope.getScopeType().toString()),
                scope.getUniqueKey(),
                jsonObject);
      }
    }

    return jsonObject.toString();
  }
  /**
   * Set selected by value.
   *
   * @param selectedValue the selected value.
   */
  private void setSelectedByValue(final String selectedValue) {
    for (int i = 0; i < streamOptions.getItemCount(); i++) {
      if (streamOptions.getValue(i).equals(selectedValue)) {
        streamOptions.setSelectedIndex(i);
      }
    }

    scopes.setVisible(hasStreamScopes(selectedValue));
  }
  /**
   * Default constructor.
   *
   * @param json the id of the default view.
   */
  public StreamListFormElement(final JSONObject json) {
    scopes =
        new StreamScopeFormElement(
            "scopes",
            new LinkedList<StreamScope>(),
            "",
            "Enter the name of an employee or group.",
            false,
            true,
            "/resources/autocomplete/entities/",
            MAX_NAME,
            MAX_ITEMS);

    this.addStyleName(StaticResourceBundle.INSTANCE.coreCss().streamLists());
    label.addStyleName(StaticResourceBundle.INSTANCE.coreCss().formLabel());
    this.add(label);

    this.add(streamOptions);

    streamOptions.addItem("Everyone", "");
    streamOptions.addItem("Following", StreamJsonRequestFactory.FOLLOWED_BY_KEY);
    streamOptions.addItem("Saved", StreamJsonRequestFactory.SAVED_KEY);
    streamOptions.addItem("Groups I've Joined", StreamJsonRequestFactory.JOINED_GROUPS_KEY);
    streamOptions.addItem("Posted To", StreamJsonRequestFactory.RECIPIENT_KEY);
    streamOptions.addItem("Authored By", StreamJsonRequestFactory.AUTHOR_KEY);
    streamOptions.addItem("Liked By", StreamJsonRequestFactory.LIKER_KEY);

    streamOptions.addChangeHandler(
        new ChangeHandler() {
          public void onChange(final ChangeEvent event) {
            scopes.setVisible(hasStreamScopes(getSelected()));
          }
        });

    if (json == null) {
      streamOptions.setSelectedIndex(0);
      scopes.setVisible(false);
    } else {
      if (json.containsKey(StreamJsonRequestFactory.RECIPIENT_KEY)) {
        setSelectedByValue(StreamJsonRequestFactory.RECIPIENT_KEY);
      } else if (json.containsKey(StreamJsonRequestFactory.SAVED_KEY)) {
        setSelectedByValue(StreamJsonRequestFactory.SAVED_KEY);
      } else if (json.containsKey(StreamJsonRequestFactory.PARENT_ORG_KEY)) {
        setSelectedByValue(StreamJsonRequestFactory.PARENT_ORG_KEY);
      } else if (json.containsKey(StreamJsonRequestFactory.FOLLOWED_BY_KEY)) {
        setSelectedByValue(StreamJsonRequestFactory.FOLLOWED_BY_KEY);
      } else if (json.containsKey(StreamJsonRequestFactory.AUTHOR_KEY)) {
        setSelectedByValue(StreamJsonRequestFactory.AUTHOR_KEY);
      } else if (json.containsKey(StreamJsonRequestFactory.LIKER_KEY)) {
        setSelectedByValue(StreamJsonRequestFactory.LIKER_KEY);
      } else if (json.containsKey(StreamJsonRequestFactory.JOINED_GROUPS_KEY)) {
        setSelectedByValue(StreamJsonRequestFactory.JOINED_GROUPS_KEY);
      } else {
        setSelectedByValue("");
      }

      if (hasStreamScopes(getSelected())) {
        Session.getInstance()
            .getEventBus()
            .addObserver(
                GotBulkEntityResponseEvent.class,
                new Observer<GotBulkEntityResponseEvent>() {
                  public void update(final GotBulkEntityResponseEvent event) {
                    JSONArray recipientArray = json.get(getSelected()).isArray();

                    for (int i = 0; i < recipientArray.size(); i++) {
                      JSONObject recipient = (JSONObject) recipientArray.get(i);
                      String uniqueId =
                          recipient
                              .get(StreamJsonRequestFactory.ENTITY_UNIQUE_ID_KEY)
                              .isString()
                              .stringValue();
                      String displayName =
                          getEntityDisplayName(
                              EntityType.valueOf(
                                  recipient
                                      .get(StreamJsonRequestFactory.ENTITY_TYPE_KEY)
                                      .isString()
                                      .stringValue()),
                              uniqueId,
                              event.getResponse());

                      ScopeType scopeType =
                          ScopeType.valueOf(
                              recipient
                                  .get(StreamJsonRequestFactory.ENTITY_TYPE_KEY)
                                  .isString()
                                  .stringValue());

                      StreamScope scope = new StreamScope(scopeType, uniqueId);
                      scope.setDisplayName(displayName);

                      Session.getInstance()
                          .getEventBus()
                          .notifyObservers(new StreamScopeAddedEvent(scope));
                    }

                    Session.getInstance()
                        .getEventBus()
                        .removeObserver(GotBulkEntityResponseEvent.class, this);
                  }
                });

        ArrayList<StreamEntityDTO> entities = new ArrayList<StreamEntityDTO>();

        JSONArray recipientArray = json.get(getSelected()).isArray();
        for (int i = 0; i < recipientArray.size(); i++) {
          JSONObject recipient = (JSONObject) recipientArray.get(i);
          StreamEntityDTO entity = new StreamEntityDTO();
          entity.setType(
              EntityType.valueOf(
                  recipient
                      .get(StreamJsonRequestFactory.ENTITY_TYPE_KEY)
                      .isString()
                      .stringValue()));
          entity.setUniqueIdentifier(
              recipient
                  .get(StreamJsonRequestFactory.ENTITY_UNIQUE_ID_KEY)
                  .isString()
                  .stringValue());
          entities.add(entity);
        }
        BulkEntityModel.getInstance().fetch(entities, false);
      }
    }

    this.add(scopes);
  }