UIMiniConnectionsPortlet.java

  1. /*
  2.  * Copyright (C) 2003-2015 eXo Platform SAS.
  3.  *
  4.  * This program is free software: you can redistribute it and/or modify
  5.  * it under the terms of the GNU Affero General Public License as published by
  6.  * the Free Software Foundation, either version 3 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU Affero General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU Affero General Public License
  15.  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16.  */
  17. package org.exoplatform.social.user.portlet;

  18. import java.util.List;

  19. import javax.portlet.MimeResponse;
  20. import javax.portlet.ResourceRequest;

  21. import org.apache.commons.lang.StringEscapeUtils;
  22. import org.apache.commons.lang3.StringUtils;
  23. import org.exoplatform.social.core.identity.model.Identity;
  24. import org.exoplatform.social.core.identity.model.Profile;
  25. import org.exoplatform.social.core.profile.ProfileFilter;
  26. import org.exoplatform.social.webui.Utils;
  27. import org.exoplatform.webui.application.WebuiRequestContext;
  28. import org.exoplatform.webui.config.annotation.ComponentConfig;
  29. import org.exoplatform.webui.config.annotation.EventConfig;
  30. import org.exoplatform.webui.core.lifecycle.UIApplicationLifecycle;
  31. import org.exoplatform.webui.event.Event;
  32. import org.exoplatform.webui.event.EventListener;
  33. import org.json.JSONObject;

  34. @ComponentConfig(
  35.   lifecycle = UIApplicationLifecycle.class,
  36.   template = "app:/groovy/social/portlet/user/UIMiniConnectionsPortlet.gtmpl",
  37.   events = {
  38.     @EventConfig(listeners = UIMiniConnectionsPortlet.RemoveConnectionActionListener.class)
  39.   }
  40. )
  41. public class UIMiniConnectionsPortlet extends UIAbstractUserPortlet {
  42.   protected final static int MAX_DISPLAY = 12;
  43.   private static final String PROFILE_LOADING_RESOURCE = "profile-loading";
  44.   private static final String SIZE_LOADING_RESOURCE = "size-loading";
  45.   private static int connectionSize = 0;

  46.   public UIMiniConnectionsPortlet() throws Exception {
  47.   }
  48.  
  49.   @Override
  50.   public void afterProcessRender(WebuiRequestContext context) {
  51.     super.afterProcessRender(context);
  52.     context.getJavascriptManager().getRequireJS()
  53.            .require("SHARED/user-profile", "userprofile").addScripts("userprofile.loadingProfile('" + getId() + "');");
  54.   }

  55.   private int getAllSize() throws Exception {
  56.     return Utils.getRelationshipManager().getConnectionsByFilter(currentProfile.getIdentity(), new ProfileFilter()).getSize();
  57.   }

  58.   @Override
  59.   public void serveResource(WebuiRequestContext context) throws Exception {
  60.     super.serveResource(context);
  61.     ResourceRequest req = context.getRequest();
  62.     MimeResponse res = context.getResponse();
  63.     String resourceId = req.getResourceID();
  64.     if (PROFILE_LOADING_RESOURCE.equals(resourceId)) {
  65.       res.setContentType("text/html");
  66.       //
  67.       res.getWriter().write(profileListHTML());
  68.     } else if (SIZE_LOADING_RESOURCE.equals(resourceId)) {
  69.       res.setContentType("application/json");
  70.       int size = (connectionSize < MAX_DISPLAY) ? connectionSize : getAllSize();
  71.       JSONObject object = new JSONObject();
  72.       object.put("size", size);
  73.       object.put("showAll", (size > 0));
  74.       //
  75.       res.getWriter().write(object.toString());
  76.     }
  77.   }

  78.   private String profileListHTML() throws Exception {
  79.     StringBuilder html = new StringBuilder("");
  80.     List<Identity> identities = Utils.getRelationshipManager().getLastConnections(currentProfile.getIdentity(), MAX_DISPLAY);
  81.     connectionSize = identities.size();
  82.     for (Identity identity : identities) {
  83.       ProfileBean profile = new ProfileBean(identity);
  84.       html.append("<a href=\"").append(profile.getProfileURL()).append("\" class=\"avatarXSmall\" data-link=\"")
  85.           .append(event("RemoveConnection")).append("\">\n  <img alt=\"").append(StringEscapeUtils.escapeHtml(profile.getDisplayName()))
  86.           .append("\" src=\"").append(profile.getAvatarURL()).append("\"/>\n</a>\n");
  87.     }
  88.     return html.toString();
  89.   }

  90.   public static class RemoveConnectionActionListener extends EventListener<UIMiniConnectionsPortlet> {
  91.     @Override
  92.     public void execute(Event<UIMiniConnectionsPortlet> event) throws Exception {
  93.       event.getRequestContext().addUIComponentToUpdateByAjax(event.getSource());
  94.     }
  95.   }

  96.   protected class ProfileBean {
  97.     private final String avatarURL;
  98.     private final String displayName;
  99.     private final String profileURL;
  100.     private final String userId;

  101.     public ProfileBean(Identity identity) {
  102.       this.userId = identity.getRemoteId();
  103.       //
  104.       Profile profile = identity.getProfile();
  105.       this.displayName = profile.getFullName();
  106.       this.profileURL = profile.getUrl();
  107.       String avatarURL = profile.getAvatarUrl();
  108.       if (StringUtils.isBlank(avatarURL) || avatarURL.equalsIgnoreCase("null")) {
  109.         avatarURL = "/eXoSkin/skin/images/system/UserAvtDefault.png";
  110.       }
  111.       this.avatarURL = avatarURL;
  112.     }
  113.     public String getUserId() {
  114.       return userId;
  115.     }
  116.     public String getAvatarURL() {
  117.       return avatarURL;
  118.     }
  119.     public String getDisplayName() {
  120.       return displayName;
  121.     }
  122.     public String getProfileURL() {
  123.       return profileURL;
  124.     }
  125.   }
  126. }