View Javadoc
1   /*
2    * Copyright (C) 2003-2011 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.service.rest.api.models;
18  
19  import java.util.HashMap;
20  
21  import org.exoplatform.social.core.activity.model.ActivityStream;
22  import org.exoplatform.social.core.identity.model.Identity;
23  import org.exoplatform.social.core.identity.provider.OrganizationIdentityProvider;
24  import org.exoplatform.social.core.manager.IdentityManager;
25  import org.exoplatform.social.core.space.model.Space;
26  import org.exoplatform.social.core.space.spi.SpaceService;
27  import org.exoplatform.social.service.rest.Util;
28  
29  
30  /**
31   * The Activity Stream model for Social Rest APIs.
32   *
33   * @author <a href="http://hoatle.net">hoatle (hoatlevan at gmail dot com)</a>
34   * @since 1.2.3
35   */
36  public class ActivityStreamRestOut extends HashMap<String, Object> {
37  
38    /**
39     * The enum fields as json keys
40     */
41    public static enum Field {
42      TYPE("type"),
43      PRETTY_ID("prettyId"),
44      FULL_NAME("fullName"),
45      FAVICON_URL("faviconUrl"),
46      TITLE("title"),
47      PERMA_LINK("permaLink");
48  
49      /**
50       * field name
51       */
52      private final String fieldName;
53  
54      /**
55       * Private constructor.
56       *
57       * @param str the field name
58       */
59      private Field(final String str) {
60        fieldName = str;
61      }
62  
63      /**
64       * Gets the string field name.
65       *
66       * @return the field name
67       */
68      @Override
69      public String toString() {
70        return fieldName;
71      }
72    }
73  
74    /**
75     * Default constructor for initializing default values.
76     */
77    public ActivityStreamRestOut() {
78      initialize();
79    }
80  
81    /**
82     * Constructor to construct object from {@link ActivityStream} instance.
83     *
84     * @param activityStream the activity stream instance.
85     */
86    public ActivityStreamRestOut(final ActivityStream activityStream, String portalContainerName) {
87      initialize();
88      this.setType(activityStream.getType().toString());
89      this.setPrettyId(activityStream.getPrettyId());
90      this.setFaviconUrl(activityStream.getFaviconUrl());
91      this.setTitle(activityStream.getTitle());
92      this.setPermaLink(activityStream.getPermaLink()); //TODO make sure absolute link
93      
94      if(activityStream.getType() != null){
95        ActivityStream.Type activityStreamType =  activityStream.getType();
96        if(activityStreamType.equals(ActivityStream.Type.SPACE)){
97          SpaceService spaceService = Util.getSpaceService(portalContainerName);
98          Space space = spaceService.getSpaceByPrettyName(activityStream.getPrettyId());
99          if(space != null && space.getDisplayName() != null){
100           this.setFullName(space.getDisplayName());
101         }
102       } else if(activityStreamType.equals(ActivityStream.Type.USER)) {
103         IdentityManager identityManager = Util.getIdentityManager(portalContainerName);
104         Identity identity = identityManager.getOrCreateIdentity(
105                                 OrganizationIdentityProvider.NAME, activityStream.getPrettyId(), true);  
106         if(identity != null && identity.getProfile() != null && identity.getProfile().getFullName() != null){
107           this.setFullName(identity.getProfile().getFullName());
108         }
109       }
110     }
111   }
112 
113   /**
114    * Gets the activity stream type.
115    *
116    * @return the activity stream type.
117    */
118   public String getType() {
119     return (String) get(Field.TYPE.toString());
120   }
121 
122   /**
123    * Sets the activity stream type.
124    *
125    * @param type the type
126    */
127   public void setType(String type) {
128     if (type == null) {
129       put(Field.TYPE.toString(), "");
130     } else {
131       put(Field.TYPE.toString(), type);
132     }
133   }
134 
135   /**
136    * Gets the pretty id.
137    *
138    * @return the pretty id
139    */
140   public String getPrettyId() {
141     return (String) get(Field.PRETTY_ID.toString());
142   }
143 
144   /**
145    * Sets the pretty id.
146    *
147    * @param prettyId the pretty id
148    */
149   public void setPrettyId(final String prettyId) {
150     if (prettyId == null) {
151       put(Field.PRETTY_ID.toString(), "");
152     } else {
153       put(Field.PRETTY_ID.toString(), prettyId);
154     }
155   }
156 
157   
158   /**
159    * Gets the full name.
160    *
161    * @return the full name.
162    */
163   public String getFullName() {
164     return (String) get(Field.FULL_NAME.toString());
165   }
166 
167   /**
168    * Sets the full name.
169    *
170    * @param fullName the full name.
171    */
172   public void setFullName(final String fullName) {
173     if (fullName == null) {
174       put(Field.FULL_NAME.toString(), "");
175     } else {
176       put(Field.FULL_NAME.toString(), fullName);
177     }
178   }
179   
180   /**
181    * Gets the activity stream title.
182    *
183    * @return the activity stream title
184    */
185   public String getTitle() {
186     return (String) get(Field.TITLE.toString());
187   }
188 
189   /**
190    * Sets the activity stream title.
191    *
192    * @param title the activity stream title
193    */
194   public void setTitle(final String title) {
195     if (title == null) {
196       put(Field.TITLE.toString(), "");
197     } else {
198       put(Field.TITLE.toString(), title);
199     }
200   }
201 
202   /**
203    * Gets the perma link.
204    *
205    * @return the perma link
206    */
207   public String getPermaLink() {
208     return (String) get(Field.PERMA_LINK.toString());
209   }
210 
211   /**
212    * Sets the perma link of activity stream.
213    *
214    * @param permalink the perma link
215    */
216   public void setPermaLink(final String permalink) {
217     if (permalink == null) {
218       put(Field.PERMA_LINK.toString(), "");
219     } else {
220       put(Field.PERMA_LINK.toString(), Util.getBaseUrl() + permalink);
221     }
222   }
223 
224   /**
225    * Gets the favicon url of activity stream.
226    *
227    * @return the favicon url
228    */
229   public String getFaviconUrl() {
230     return (String) get(Field.FAVICON_URL.toString());
231   }
232 
233   /**
234    * Sets the favicon url.
235    *
236    * @param faviconUrl the favicon url
237    */
238   public void setFaviconUrl(String faviconUrl) {
239     if (faviconUrl == null) {
240       put(Field.FAVICON_URL.toString(), "");
241     } else {
242       put(Field.FAVICON_URL.toString(), Util.getBaseUrl() + faviconUrl);
243     }
244   }
245 
246 
247   /**
248    * Inits default values
249    */
250   private void initialize() {
251     setType("");
252     setPrettyId("");
253     setFullName(null);
254     setFaviconUrl(null);
255     setTitle("");
256     setPermaLink(null);
257   }
258 
259 }