View Javadoc
1   /*
2    * Copyright (C) 2003-2015 eXo Platform SAS.
3    *
4    * This is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU Lesser General Public License as
6    * published by the Free Software Foundation; either version 3 of
7    * the License, or (at your option) any later version.
8    *
9    * This software 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 GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this software; if not, write to the Free
16   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
18   */
19  package org.exoplatform.model;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.exoplatform.shareextension.service.ShareService.UploadInfo;
27  import org.exoplatform.utils.ExoConstants;
28  import org.exoplatform.utils.Utils;
29  
30  import android.os.Parcel;
31  import android.os.Parcelable;
32  
33  /**
34   * Created by The eXo Platform SAS
35   * 
36   * @author Philippe Aristote paristote@exoplatform.com
37   * @since Jun 16, 2015
38   */
39  public class SocialPostInfo implements Parcelable {
40  
41    public final static String TYPE_DEFAULT = "activity_default";
42  
43    public final static String TYPE_DOC     = "activity_doc";
44  
45    public final static String TYPE_LINK    = "activity_link";
46  
47    public ExoAccount          ownerAccount;
48  
49    public String              postMessage;
50  
51    public List<String>        postAttachedFiles;
52  
53    public SocialSpaceInfo     destinationSpace;
54  
55    public Map<String, String> templateParams;
56  
57    public String              activityType = TYPE_DEFAULT;
58  
59    public SocialPostInfo() {
60    }
61  
62    private SocialPostInfo(Parcel in) {
63      readFromParcel(in);
64    }
65  
66    public static final Parcelable.Creator<SocialPostInfo> CREATOR = new Parcelable.Creator<SocialPostInfo>() {
67      public SocialPostInfo createFromParcel(Parcel in) {
68        return new SocialPostInfo(in);
69      }
70  
71      public SocialPostInfo[] newArray(int size) {
72        return new SocialPostInfo[size];
73      }
74    };
75  
76    @Override
77    public int describeContents() {
78      return 0;
79    }
80  
81    @Override
82    public void writeToParcel(Parcel out, int flags) {
83      out.writeParcelable(ownerAccount, flags);
84      out.writeString(postMessage);
85      out.writeStringList(postAttachedFiles);
86      out.writeParcelable(destinationSpace, flags);
87      out.writeMap(templateParams);
88      out.writeString(activityType);
89  
90    }
91  
92    public void readFromParcel(Parcel in) {
93      ownerAccount = in.readParcelable(SocialPostInfo.class.getClassLoader());
94      postMessage = in.readString();
95      postAttachedFiles = new ArrayList<String>();
96      in.readStringList(postAttachedFiles);
97      destinationSpace = in.readParcelable(SocialPostInfo.class.getClassLoader());
98      in.readMap(templateParams, SocialPostInfo.class.getClassLoader());
99      activityType = in.readString();
100   }
101 
102   /*
103    * 
104    */
105 
106   /**
107    * @return true if the post's destination space is null or empty, i.e. the
108    *         post is public
109    */
110   public boolean isPublic() {
111     return (destinationSpace == null || "".equals(destinationSpace));
112   }
113 
114   /**
115    * @return true if the post's attachments list is not null and not empty
116    */
117   public boolean hasAttachment() {
118     return Utils.notEmpty(postAttachedFiles);
119   }
120 
121   /**
122    * Set the template param with the given name and value.<br/>
123    * Create a new Map if necessary.
124    * 
125    * @param name
126    * @param value
127    */
128   public void addTemplateParam(String name, String value) {
129     if (templateParams == null)
130       templateParams = new HashMap<String, String>(1);
131     templateParams.put(name, value);
132   }
133 
134   /**
135    * Get the template param with the given name. <br/>
136    * Return null if the Map is null or if no param is found under this name.
137    * 
138    * @param name
139    * @return
140    */
141   public String getTemplateParam(String name) {
142     if (templateParams == null)
143       return null;
144     return templateParams.get(name);
145   }
146 
147   /**
148    * Create TemplateParams for a DOC_ACTIVITY
149    * @param uploadInfo Info about the uploaded DOC
150    */
151   public void buildTemplateParams(UploadInfo uploadInfo) {
152     String docUrl = uploadInfo.getUploadedUrl();
153     templateParams = new HashMap<String, String>();
154     templateParams.put("WORKSPACE", uploadInfo.workspace);
155     templateParams.put("REPOSITORY", uploadInfo.repository);
156     String docLink = docUrl.substring(ownerAccount.serverUrl.length());
157     templateParams.put("DOCLINK", docLink);
158     StringBuffer beginPath = new StringBuffer(ExoConstants.DOCUMENT_JCR_PATH).append("/")
159                                                                              .append(uploadInfo.repository)
160                                                                              .append("/")
161                                                                              .append(uploadInfo.workspace);
162     String docPath = docLink.substring(beginPath.length());
163     templateParams.put("DOCPATH", docPath);
164     templateParams.put("DOCNAME", uploadInfo.fileToUpload.documentName);
165     String mimeType = uploadInfo.fileToUpload.documentMimeType;
166     if (mimeType != null && !mimeType.trim().isEmpty()) {
167       templateParams.put("mimeType", uploadInfo.fileToUpload.documentMimeType);
168     }
169   }
170 
171 }