Esempio n. 1
0
  public ComposeNewMessage(String id) {
    super(id);

    // current user
    final String userId = sakaiProxy.getCurrentUserId();

    // setup model
    NewMessageModel newMessage = new NewMessageModel();
    newMessage.setFrom(userId);

    // feedback for form submit action
    formFeedback = new Label("formFeedback");
    formFeedback.setOutputMarkupPlaceholderTag(true);
    add(formFeedback);

    // setup form
    final Form<NewMessageModel> form =
        new Form<NewMessageModel>("form", new Model<NewMessageModel>(newMessage));

    // close button
    /*
    WebMarkupContainer closeButton = new WebMarkupContainer("closeButton");
    closeButton.add(new AjaxFallbackLink<Void>("link") {
    	private static final long serialVersionUID = 1L;

    	public void onClick(AjaxRequestTarget target) {
    		if(target != null) {
    			target.prependJavascript("$('#" + thisPanel.getMarkupId() + "').slideUp();");
    			target.appendJavascript("setMainFrameHeight(window.name);");
    		}
    	}
    }.add(new ContextImage("img",new Model<String>(ProfileConstants.CLOSE_IMAGE))));
    form.add(closeButton);
    */

    // to label
    form.add(new Label("toLabel", new ResourceModel("message.to")));

    // get connections
    final List<Person> connections = connectionsLogic.getConnectionsForUser(userId);
    Collections.sort(connections);

    // list provider
    AutoCompletionChoicesProvider<Person> provider =
        new AutoCompletionChoicesProvider<Person>() {
          private static final long serialVersionUID = 1L;

          public Iterator<Person> getChoices(String input) {
            return connectionsLogic
                .getConnectionsSubsetForSearch(connections, input, true)
                .iterator();
          }
        };

    // renderer
    ObjectAutoCompleteRenderer<Person> renderer =
        new ObjectAutoCompleteRenderer<Person>() {
          private static final long serialVersionUID = 1L;

          protected String getIdValue(Person p) {
            return p.getUuid();
          }

          protected String getTextValue(Person p) {
            return p.getDisplayName();
          }
        };

    // autocompletefield builder
    ObjectAutoCompleteBuilder<Person, String> builder =
        new ObjectAutoCompleteBuilder<Person, String>(provider);
    builder.autoCompleteRenderer(renderer);
    builder.searchLinkImage(ResourceReferences.CROSS_IMG_LOCAL);
    builder.preselect();

    // autocompletefield
    final ObjectAutoCompleteField<Person, String> autocompleteField =
        builder.build("toField", new PropertyModel<String>(newMessage, "to"));
    toField = autocompleteField.getSearchTextField();
    toField.setMarkupId("messagerecipientinput");
    toField.setOutputMarkupId(true);
    toField.add(new AttributeModifier("class", true, new Model<String>("formInputField")));
    toField.setRequired(true);
    form.add(autocompleteField);

    // subject
    form.add(new Label("subjectLabel", new ResourceModel("message.subject")));
    final TextField<String> subjectField =
        new TextField<String>("subjectField", new PropertyModel<String>(newMessage, "subject"));
    subjectField.setMarkupId("messagesubjectinput");
    subjectField.setOutputMarkupId(true);
    subjectField.add(new RecipientEventBehavior("onfocus"));
    form.add(subjectField);

    // body
    form.add(new Label("messageLabel", new ResourceModel("message.message")));
    final TextArea<String> messageField =
        new TextArea<String>("messageField", new PropertyModel<String>(newMessage, "message"));
    messageField.setMarkupId("messagebodyinput");
    messageField.setOutputMarkupId(true);
    messageField.setRequired(true);
    messageField.add(new RecipientEventBehavior("onfocus"));
    form.add(messageField);

    // send button
    IndicatingAjaxButton sendButton =
        new IndicatingAjaxButton("sendButton", form) {
          private static final long serialVersionUID = 1L;

          protected void onSubmit(AjaxRequestTarget target, Form form) {

            // get the backing model
            NewMessageModel newMessage = (NewMessageModel) form.getModelObject();

            // generate the thread id
            String threadId = ProfileUtils.generateUuid();

            // save it, it will be abstracted into its proper parts and email notifications sent
            if (messagingLogic.sendNewMessage(
                newMessage.getTo(),
                newMessage.getFrom(),
                threadId,
                newMessage.getSubject(),
                newMessage.getMessage())) {

              // post event
              sakaiProxy.postEvent(
                  ProfileConstants.EVENT_MESSAGE_SENT, "/profile/" + newMessage.getTo(), true);

              // success
              formFeedback.setDefaultModel(new ResourceModel("success.message.send.ok"));
              formFeedback.add(new AttributeModifier("class", true, new Model<String>("success")));

              // target.appendJavascript("$('#" + form.getMarkupId() + "').slideUp();");
              target.appendJavaScript("setMainFrameHeight(window.name);");

              // PRFL-797 all fields when successful, to prevent multiple messages.
              // User can just click Compose message again to get a new form
              this.setEnabled(false);
              autocompleteField.setEnabled(false);
              subjectField.setEnabled(false);
              messageField.setEnabled(false);
              target.add(this);
              target.add(autocompleteField);
              target.add(subjectField);
              target.add(messageField);

            } else {
              // error
              formFeedback.setDefaultModel(new ResourceModel("error.message.send.failed"));
              formFeedback.add(
                  new AttributeModifier("class", true, new Model<String>("alertMessage")));
            }

            formFeedback.setVisible(true);
            target.add(formFeedback);
          }

          protected void onError(AjaxRequestTarget target, Form form) {

            // check which item didn't validate and update the feedback model
            if (!toField.isValid()) {
              formFeedback.setDefaultModel(new ResourceModel("error.message.required.to"));
            }
            if (!messageField.isValid()) {
              formFeedback.setDefaultModel(new ResourceModel("error.message.required.body"));
            }
            formFeedback.add(
                new AttributeModifier("class", true, new Model<String>("alertMessage")));

            target.add(formFeedback);
          }
        };
    form.add(sendButton);
    sendButton.setModel(new ResourceModel("button.message.send"));

    add(form);
  }
Esempio n. 2
0
  public MyStatusPanel(String id, UserProfile userProfile) {
    super(id);

    log.debug("MyStatusPanel()");

    // get info
    final String displayName = userProfile.getDisplayName();
    final String userId = userProfile.getUserUuid();

    // if superUser and proxied, can't update
    boolean editable = true;
    if (sakaiProxy.isSuperUserAndProxiedToUser(userId)) {
      editable = false;
    }

    // name
    Label profileName = new Label("profileName", displayName);
    add(profileName);

    // status component
    status = new ProfileStatusRenderer("status", userId, null, "tiny");
    status.setOutputMarkupId(true);
    add(status);

    WebMarkupContainer statusFormContainer = new WebMarkupContainer("statusFormContainer");

    // setup SimpleText object to back the single form field
    StringModel stringModel = new StringModel();

    // status form
    Form form = new Form("form", new Model(stringModel));
    form.setOutputMarkupId(true);

    // status field
    final TextField statusField =
        new TextField("message", new PropertyModel(stringModel, "string"));
    statusField.setOutputMarkupPlaceholderTag(true);
    form.add(statusField);

    // link the status textfield field with the focus/blur function via this dynamic js
    // also link with counter
    StringHeaderContributor statusJavascript =
        new StringHeaderContributor(
            "<script type=\"text/javascript\">"
                + "$(document).ready( function(){"
                + "autoFill('#"
                + statusField.getMarkupId()
                + "', '"
                + defaultStatus
                + "');"
                + "countChars('#"
                + statusField.getMarkupId()
                + "');"
                + "});"
                + "</script>");
    add(statusJavascript);

    // clear link
    final AjaxFallbackLink clearLink =
        new AjaxFallbackLink("clearLink") {
          private static final long serialVersionUID = 1L;

          public void onClick(AjaxRequestTarget target) {
            // clear status, hide and repaint
            if (statusLogic.clearUserStatus(userId)) {
              status.setVisible(false); // hide status
              this.setVisible(false); // hide clear link
              target.addComponent(status);
              target.addComponent(this);
            }
          }
        };
    clearLink.setOutputMarkupPlaceholderTag(true);
    clearLink.add(new Label("clearLabel", new ResourceModel("link.status.clear")));

    // set visibility of clear link based on status and if it's editable
    if (!status.isVisible() || !editable) {
      clearLink.setVisible(false);
    }
    add(clearLink);

    // submit button
    IndicatingAjaxButton submitButton =
        new IndicatingAjaxButton("submit", form) {

          private static final long serialVersionUID = 1L;

          protected void onSubmit(AjaxRequestTarget target, Form form) {

            // get the backing model
            StringModel stringModel = (StringModel) form.getModelObject();

            // get userId from sakaiProxy
            String userId = sakaiProxy.getCurrentUserId();

            // get the status. if its the default text, do not update, although we should clear the
            // model
            String statusMessage = StringUtils.trim(stringModel.getString());
            if (StringUtils.isBlank(statusMessage)
                || StringUtils.equals(statusMessage, defaultStatus)) {
              log.warn(
                  "Status for userId: "
                      + userId
                      + " was not updated because they didn't enter anything.");
              return;
            }

            // save status from userProfile
            if (statusLogic.setUserStatus(userId, statusMessage)) {
              log.info("Saved status for: " + userId);

              // post update event
              sakaiProxy.postEvent(
                  ProfileConstants.EVENT_STATUS_UPDATE, "/profile/" + userId, true);

              // update twitter
              externalIntegrationLogic.sendMessageToTwitter(userId, statusMessage);

              // post to walls if wall enabled
              if (true == sakaiProxy.isWallEnabledGlobally()) {
                wallLogic.addNewStatusToWall(statusMessage, sakaiProxy.getCurrentUserId());
              }

              // repaint status component
              ProfileStatusRenderer newStatus =
                  new ProfileStatusRenderer("status", userId, null, "tiny");
              newStatus.setOutputMarkupId(true);
              status.replaceWith(newStatus);
              newStatus.setVisible(true);

              // also show the clear link
              clearLink.setVisible(true);

              if (target != null) {
                target.addComponent(newStatus);
                target.addComponent(clearLink);
                status = newStatus; // update reference

                // reset the field
                target.appendJavascript(
                    "autoFill('#" + statusField.getMarkupId() + "', '" + defaultStatus + "');");

                // reset the counter
                target.appendJavascript("countChars('#" + statusField.getMarkupId() + "');");
              }

            } else {
              log.error("Couldn't save status for: " + userId);
              String js =
                  "alert('Failed to save status. If the problem persists, contact your system administrator.');";
              target.prependJavascript(js);
            }
          }
        };
    submitButton.setModel(new ResourceModel("button.sayit"));
    form.add(submitButton);

    // add form to container
    statusFormContainer.add(form);

    // if not editable, hide the entire form
    if (!editable) {
      statusFormContainer.setVisible(false);
    }

    add(statusFormContainer);
  }