001/* 002 * Copyright 2020 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.mobileapp.action; 017 018import java.time.Instant; 019import java.time.LocalDate; 020import java.time.ZonedDateTime; 021import java.util.ArrayList; 022import java.util.Comparator; 023import java.util.Date; 024import java.util.HashMap; 025import java.util.List; 026import java.util.Map; 027import java.util.stream.Collectors; 028 029import org.apache.avalon.framework.service.ServiceException; 030import org.apache.avalon.framework.service.ServiceManager; 031import org.apache.cocoon.environment.Request; 032 033import org.ametys.cms.repository.Content; 034import org.ametys.cms.search.content.ContentSearcherFactory.ContentSearchSort; 035import org.ametys.core.right.RightManager; 036import org.ametys.plugins.mobileapp.PostConstants; 037import org.ametys.plugins.mobileapp.QueriesHelper; 038import org.ametys.plugins.queriesdirectory.Query; 039import org.ametys.plugins.repository.AmetysObjectIterable; 040import org.ametys.runtime.config.Config; 041import org.ametys.web.repository.site.Site; 042import org.ametys.web.repository.site.SiteManager; 043 044/** 045 * Returns the list of feeds for a user 046 */ 047public class GetFeedsContentsAction extends AbstractLoggedAction 048{ 049 /** The Ametys object resolver */ 050 protected QueriesHelper _queryHelper; 051 052 /** Authentication Token Manager */ 053 protected SiteManager _siteManager; 054 055 private RightManager _rightManager; 056 057 @Override 058 public void service(ServiceManager smanager) throws ServiceException 059 { 060 super.service(smanager); 061 _queryHelper = (QueriesHelper) smanager.lookup(QueriesHelper.ROLE); 062 _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE); 063 _rightManager = (RightManager) smanager.lookup(RightManager.ROLE); 064 } 065 066 @Override 067 protected Map<String, Object> doLoggedInAction(Request request, Map<String, Object> jsonParams) 068 { 069 Map<String, Object> result = new HashMap<>(); 070 071 String siteName = (String) getParameter(PostConstants.SITE_NAME, jsonParams, request); 072 073 Site site = _siteManager.getSite(siteName); 074 075 List<Query> queries = _queryHelper.getQueries(site, false); 076 077 List<QuerySearchresult> results = new ArrayList<>(); 078 079 for (Query query : queries) 080 { 081 if (_rightManager.hasReadAccess(_currentUserProvider.getUser(), query)) 082 { 083 Long limit = Config.getInstance().getValue(QueriesHelper.QUERY_LIMIT_CONF_ID); 084 List<ContentSearchSort> sort = _queryHelper.getSortProperty(query, queries.size() > 1); 085 AmetysObjectIterable<Content> searchResults = _queryHelper.executeQuery(query, sort, true, limit.intValue()); 086 if (searchResults != null) 087 { 088 ContentSearchSort firstSort = sort.get(0); 089 searchResults.stream() 090 .forEach(content -> results.add(new QuerySearchresult(query, firstSort, content))); 091 } 092 } 093 } 094 095 Comparator<QuerySearchresult> comparator = new Comparator<>() 096 { 097 public int compare(QuerySearchresult o1, QuerySearchresult o2) 098 { 099 Object o1Value = getDate(o1); 100 Object o2Value = getDate(o2); 101 return compareDates(o1Value, o2Value); 102 } 103 104 private Object getDate(QuerySearchresult queryResult) 105 { 106 String field = queryResult.getSort().sortField(); 107 Content content = queryResult.getContent(); 108 Object value; 109 if (content.hasValue(field)) 110 { 111 value = content.getValue(field, true, null); 112 } 113 else 114 { 115 value = content.getLastValidationDate(); 116 } 117 return value; 118 } 119 }; 120 121 List<Map<String, String>> jsonResults; 122 if (queries.size() > 1) 123 { 124 jsonResults = results.stream() 125 .sorted(comparator.reversed()) 126 .map(searchResult -> contentToJson(searchResult)) 127 .collect(Collectors.toList()); 128 } 129 else 130 { 131 jsonResults = results.stream() 132 .map(searchResult -> contentToJson(searchResult)) 133 .collect(Collectors.toList()); 134 } 135 136 137 result.put("items", jsonResults); 138 139 return result; 140 } 141 142 /** 143 * Transform a content into a json map 144 * @param searchResult the search result containing tho content 145 * @return a json map 146 */ 147 protected Map<String, String> contentToJson(QuerySearchresult searchResult) 148 { 149 Content content = searchResult.getContent(); 150 Map<String, String> result = _queryHelper.getDataForContent(content); 151 152 result.put("feed_id", searchResult.getQueryId()); 153 result.put("category_name", searchResult.getQueryName()); 154 155 String sortField = searchResult.getSort().sortField(); 156 157 String isoDate = _queryHelper.getContentFormattedDate(content, sortField); 158 result.put("date", isoDate); 159 160 return result; 161 } 162 163 /** 164 * Compare two dates (can be {@link ZonedDateTime} and/or {@link LocalDate} and/or {@link Date} 165 * @param o1 a {@link ZonedDateTime} and/or {@link LocalDate} and/or {@link Date} 166 * @param o2 a {@link ZonedDateTime} and/or {@link LocalDate} and/or {@link Date} 167 * @return -1 if o1 is before o2, +1 if o1 is after o2, 0 if same date (not equals) 168 * @throws ClassCastException one of the object is not a {@link ZonedDateTime} or {@link LocalDate} or {@link Date} 169 */ 170 protected int compareDates(Object o1, Object o2) throws ClassCastException 171 { 172 if (o1 == null && o2 == null) 173 { 174 return 0; 175 } 176 else if (o1 == null) 177 { 178 return -1; 179 } 180 else if (o2 == null) 181 { 182 return 1; 183 } 184 else if (o1 instanceof Date && o2 instanceof Date) 185 { 186 return ((Date) o1).compareTo((Date) o2); 187 } 188 else if (o1 instanceof LocalDate && o2 instanceof LocalDate) 189 { 190 return ((LocalDate) o1).compareTo((LocalDate) o2); 191 } 192 else if (o1 instanceof ZonedDateTime && o2 instanceof ZonedDateTime) 193 { 194 return ((ZonedDateTime) o1).compareTo((ZonedDateTime) o2); 195 } 196 197 Instant i1 = _queryHelper.toInstant(o1, o2); 198 Instant i2 = _queryHelper.toInstant(o2, o1); 199 if (i1 != null && i2 != null) 200 { 201 return i1.compareTo(i2); 202 } 203 204 // One of them is not null and not a LocalDate or ZonedDateTime 205 throw new ClassCastException(); 206 } 207 208 /** 209 * Content fetched from a query 210 */ 211 protected class QuerySearchresult 212 { 213 private Query _query; 214 private ContentSearchSort _sort; 215 private Content _content; 216 217 /** 218 * New QuerySearchresult 219 * @param query the query that found this content 220 * @param sort the sort used 221 * @param content the content itself 222 */ 223 public QuerySearchresult(Query query, ContentSearchSort sort, Content content) 224 { 225 _query = query; 226 _sort = sort; 227 _content = content; 228 } 229 230 /** 231 * The query ID 232 * @return The query ID 233 */ 234 public String getQueryId() 235 { 236 return _query.getId(); 237 } 238 /** 239 * The query name 240 * @return The query name 241 */ 242 public String getQueryName() 243 { 244 return _query.getTitle(); 245 } 246 247 248 /** 249 * The sort 250 * @return The sort 251 */ 252 public ContentSearchSort getSort() 253 { 254 return _sort; 255 } 256 257 /** 258 * The content 259 * @return The content 260 */ 261 public Content getContent() 262 { 263 return _content; 264 } 265 266 } 267 268}