1 package org.exoplatform.wcm.connector.collaboration;
2
3 import java.text.DateFormat;
4 import java.text.SimpleDateFormat;
5 import java.util.ArrayList;
6 import java.util.Calendar;
7 import java.util.Collections;
8 import java.util.Date;
9 import java.util.List;
10
11 import javax.jcr.ItemNotFoundException;
12 import javax.jcr.Node;
13 import javax.jcr.RepositoryException;
14 import javax.ws.rs.GET;
15 import javax.ws.rs.Path;
16 import javax.ws.rs.PathParam;
17 import javax.ws.rs.QueryParam;
18 import javax.ws.rs.core.MediaType;
19 import javax.ws.rs.core.Response;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.exoplatform.ecm.utils.comparator.PropertyValueComparator;
23 import org.exoplatform.services.cms.documents.FavoriteService;
24 import org.exoplatform.services.cms.drives.DriveData;
25 import org.exoplatform.services.cms.drives.ManageDriveService;
26 import org.exoplatform.services.log.ExoLogger;
27 import org.exoplatform.services.log.Log;
28 import org.exoplatform.services.rest.resource.ResourceContainer;
29
30
31
32
33
34
35
36
37 @Path("/favorite/")
38 public class FavoriteRESTService implements ResourceContainer {
39 private final FavoriteService favoriteService;
40
41 private ManageDriveService manageDriveService;
42
43 private static final String DATE_MODIFIED = "exo:dateModified";
44
45 private static final String TITLE = "exo:title";
46
47 private static final int NO_PER_PAGE = 10;
48
49
50 private static final String LAST_MODIFIED_PROPERTY = "Last-Modified";
51
52
53 private static final String IF_MODIFIED_SINCE_DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss z";
54
55 private static final Log LOG = ExoLogger.getLogger(FavoriteRESTService.class.getName());
56
57 public FavoriteRESTService(FavoriteService favoriteService, ManageDriveService manageDriveService) {
58 this.favoriteService = favoriteService;
59 this.manageDriveService = manageDriveService;
60 }
61
62
63
64
65
66
67
68
69
70
71
72
73
74 @GET
75 @Path("/all/{repoName}/{workspaceName}/{userName}")
76 public Response getFavoriteByUser(@PathParam("repoName") String repoName,
77 @PathParam("workspaceName") String workspaceName,
78 @PathParam("userName") String userName, @QueryParam("showItems") String showItems) throws Exception {
79 List<FavoriteNode> listFavorites = new ArrayList<FavoriteNode>();
80 List<DriveData> listDrive = manageDriveService.getAllDrives();
81 if (StringUtils.isEmpty(showItems)) {
82 showItems = String.valueOf(NO_PER_PAGE);
83 }
84 try {
85 List<Node> listNodes = favoriteService.getAllFavoriteNodesByUser(workspaceName,
86 repoName, userName);
87 Collections.sort(listNodes, new PropertyValueComparator(DATE_MODIFIED, PropertyValueComparator.DESCENDING_ORDER));
88 FavoriteNode favoriteNode;
89 for (Node favorite : listNodes) {
90 favoriteNode = new FavoriteNode();
91 favoriteNode.setName(favorite.getName());
92 favoriteNode.setTitle(getTitle(favorite));
93 favoriteNode.setDateAddFavorite(getDateFormat(favorite.getProperty(DATE_MODIFIED).getDate()));
94 favoriteNode.setDriveName(getDriveName(listDrive, favorite));
95 favoriteNode.setPath(favorite.getPath());
96 if (favoriteNode != null) {
97 if (listFavorites.size() < Integer.valueOf(showItems))
98 listFavorites.add(favoriteNode);
99 }
100 }
101 } catch (ItemNotFoundException e) {
102 if (LOG.isErrorEnabled()) {
103 LOG.error(e);
104 }
105 return Response.serverError().build();
106 } catch (RepositoryException e) {
107 if (LOG.isErrorEnabled()) {
108 LOG.error(e);
109 }
110 return Response.serverError().build();
111 } catch (Exception e) {
112 if (LOG.isErrorEnabled()) {
113 LOG.error(e);
114 }
115 return Response.serverError().build();
116 }
117 ListResultNode listResultNode = new ListResultNode();
118 listResultNode.setListFavorite(listFavorites);
119
120 DateFormat dateFormat = new SimpleDateFormat(IF_MODIFIED_SINCE_DATE_FORMAT);
121 return Response.ok(listResultNode, new MediaType("application", "json"))
122 .header(LAST_MODIFIED_PROPERTY, dateFormat.format(new Date()))
123 .build();
124 }
125
126 private String getTitle(Node node) throws Exception {
127 if (node.hasProperty(TITLE))
128 return node.getProperty(TITLE).getString();
129 return node.getName();
130 }
131
132 private String getDateFormat(Calendar date) {
133 return String.valueOf(date.getTimeInMillis());
134 }
135
136 private String getDriveName(List<DriveData> listDrive, Node node) throws RepositoryException{
137 String driveName = "";
138 for (DriveData drive : listDrive) {
139 if (node.getSession().getWorkspace().getName().equals(drive.getWorkspace())
140 && node.getPath().contains(drive.getHomePath()) && drive.getHomePath().equals("/")) {
141 driveName = drive.getName();
142 break;
143 }
144 }
145 return driveName;
146 }
147
148 public class ListResultNode {
149
150 private List<? extends FavoriteNode> listFavorite;
151
152 public List<? extends FavoriteNode> getListFavorite() {
153 return listFavorite;
154 }
155
156 public void setListFavorite(List<? extends FavoriteNode> listFavorite) {
157 this.listFavorite = listFavorite;
158 }
159 }
160
161 public class FavoriteNode {
162
163 private String name;
164 private String nodePath;
165 private String dateAddFavorite;
166 private String driveName;
167 private String title;
168 public String getName() {
169 return name;
170 }
171
172 public void setName(String name) {
173 this.name = name;
174 }
175
176 public void setPath(String path) {
177 this.nodePath = path;
178 }
179
180 public String getPath() {
181 return nodePath;
182 }
183
184 public void setDateAddFavorite(String dateAddFavorite) {
185 this.dateAddFavorite = dateAddFavorite;
186 }
187
188 public String getDateAddFavorite() {
189 return dateAddFavorite;
190 }
191
192 public void setDriveName(String driveName) {
193 this.driveName = driveName;
194 }
195
196 public String getDriveName() {
197 return driveName;
198 }
199
200 public void setTitle(String title) {
201 this.title = title;
202 }
203
204 public String getTitle() {
205 return title;
206 }
207 }
208 }