001/* 002 * Copyright 2015 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.syndication; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.parameters.Parameters; 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.cocoon.acting.ServiceableAction; 027import org.apache.cocoon.environment.ObjectModelHelper; 028import org.apache.cocoon.environment.Redirector; 029import org.apache.cocoon.environment.Request; 030import org.apache.cocoon.environment.SourceResolver; 031import org.apache.commons.lang.StringUtils; 032 033import org.ametys.core.cocoon.ActionResultGenerator; 034import org.ametys.core.user.CurrentUserProvider; 035import org.ametys.core.user.UserIdentity; 036import org.ametys.core.userpref.UserPreferencesManager; 037import org.ametys.core.util.URIUtils; 038import org.ametys.plugins.repository.AmetysObjectResolver; 039import org.ametys.runtime.i18n.I18nizableText; 040import org.ametys.web.repository.page.Page; 041import org.ametys.web.repository.page.ZoneItem; 042import org.ametys.web.userpref.FOUserPreferencesConstants; 043 044 045/** 046 * Set user preference 047 */ 048public class SetRssFeedUserPreferencesAction extends ServiceableAction 049{ 050 /** The user preferences manager. */ 051 protected UserPreferencesManager _userPrefManager; 052 053 /** The Ametys resolver */ 054 protected AmetysObjectResolver _resolver; 055 056 /** The current user provider */ 057 protected CurrentUserProvider _currentUserProvider; 058 059 @Override 060 public void service(ServiceManager serviceManager) throws ServiceException 061 { 062 super.service(serviceManager); 063 _userPrefManager = (UserPreferencesManager) serviceManager.lookup(UserPreferencesManager.ROLE + ".FO"); 064 _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE); 065 _currentUserProvider = (CurrentUserProvider) serviceManager.lookup(CurrentUserProvider.ROLE); 066 } 067 068 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 069 { 070 Request request = ObjectModelHelper.getRequest(objectModel); 071 Map<String, Object> result = new HashMap<>(); 072 List<I18nizableText> errors = new ArrayList<>(); 073 074 UserIdentity user = _currentUserProvider.getUser(); 075 076 String zoneItemId = request.getParameter("zone-item-id"); 077 ZoneItem zoneItem = _resolver.resolveById(zoneItemId); 078 079 Page page = zoneItem.getZone().getPage(); 080 String language = page.getSitemapName(); 081 String siteName = page.getSiteName(); 082 083 String storageContext = siteName + "/" + language + "/" + zoneItem.getId(); 084 Map<String, String> contextVars = _getContextVars(siteName, language); 085 086 Map<String, String> values = new HashMap<>(); 087 String ids = _getIds(request); 088 089 int nbMaxCheck = Integer.parseInt(request.getParameter("nbMaxCheck")); 090 if (StringUtils.split(ids, ",").length <= nbMaxCheck || nbMaxCheck == 0) 091 { 092 values.put(FeedsGenerator.USER_PREF_RSS_ID_KEY, ids); 093 values.put(FeedsGenerator.USER_PREF_RSS_URL_KEY, _getUrls(request)); 094 095 _userPrefManager.setUserPreferences(user, storageContext, contextVars, values); 096 } 097 else 098 { 099 errors.add(new I18nizableText("plugin.syndication", "SYNDICATION_SERVICE_CONFIG_RSS_ERROR_TO_MANY_CHECK")); 100 } 101 102 if (errors.size() > 0) 103 { 104 result.put("error", errors); 105 } 106 107 request.setAttribute(ActionResultGenerator.MAP_REQUEST_ATTR, result); 108 109 return EMPTY_MAP; 110 } 111 112 private String _getUrls(Request request) 113 { 114 int nbFeedUser = Integer.parseInt(request.getParameter("nbMaxUser")); 115 116 int nbFeed = Integer.parseInt(request.getParameter("nbFeedService")); 117 int position = nbFeed + 1; 118 119 List<String> urls = new ArrayList<>(); 120 for (int i = 0; i < nbFeedUser; i++) 121 { 122 String url = request.getParameter("feed-url-" + (position + i)); 123 if (!StringUtils.isWhitespace(url)) 124 { 125 urls.add(URIUtils.encodePath(url)); 126 } 127 } 128 129 return StringUtils.join(urls, ","); 130 } 131 132 private String _getIds(Request request) 133 { 134 int nbFeed = Integer.parseInt(request.getParameter("nbFeedService")); 135 int nbFeedUser = Integer.parseInt(request.getParameter("nbMaxUser")); 136 137 List<String> ids = new ArrayList<>(); 138 for (int i = 1; i <= (nbFeed + nbFeedUser); i++) 139 { 140 String checked = request.getParameter("feed-check-" + i); 141 if (!StringUtils.isEmpty(checked)) 142 { 143 if (checked.equals("true") || checked.equals("on")) 144 { 145 String url = request.getParameter("feed-url-" + i); 146 if (!StringUtils.isWhitespace(url)) 147 { 148 ids.add(request.getParameter("feed-id-" + i)); 149 } 150 } 151 } 152 } 153 154 return StringUtils.join(ids, ","); 155 } 156 157 private Map<String, String> _getContextVars(String siteName, String language) 158 { 159 Map<String, String> contextVars = new HashMap<>(); 160 161 contextVars.put(FOUserPreferencesConstants.CONTEXT_VAR_SITENAME, siteName); 162 contextVars.put(FOUserPreferencesConstants.CONTEXT_VAR_LANGUAGE, language); 163 164 return contextVars; 165 } 166 167}