001/* 002 * Copyright 2012 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.linkdirectory.theme; 017 018import java.util.ArrayList; 019import java.util.LinkedHashMap; 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.lang3.StringUtils; 032 033import org.ametys.core.cocoon.JSonReader; 034import org.ametys.plugins.linkdirectory.DirectoryHelper; 035import org.ametys.plugins.linkdirectory.repository.DefaultTheme; 036import org.ametys.plugins.repository.AmetysObjectIterable; 037import org.ametys.plugins.repository.AmetysObjectResolver; 038import org.ametys.plugins.repository.TraversableAmetysObject; 039import org.ametys.web.repository.site.Site; 040import org.ametys.web.repository.site.SiteManager; 041 042/** 043 * Get the themes as JSON result 044 */ 045public class GetThemesAction extends ServiceableAction 046{ 047 private SiteManager _siteManager; 048 private AmetysObjectResolver _resolver; 049 private ThemeDAO _themeDAO; 050 private DirectoryHelper _directoryHelper; 051 052 @Override 053 public void service(ServiceManager smanager) throws ServiceException 054 { 055 super.service(smanager); 056 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 057 _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE); 058 _themeDAO = (ThemeDAO) smanager.lookup(ThemeDAO.ROLE); 059 _directoryHelper = (DirectoryHelper) smanager.lookup(DirectoryHelper.ROLE); 060 } 061 062 @Override 063 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 064 { 065 Request request = ObjectModelHelper.getRequest(objectModel); 066 067 String siteName = parameters.getParameter("siteName", (String) request.getAttribute("site")); 068 if (StringUtils.isEmpty(siteName)) 069 { 070 siteName = request.getParameter("siteName"); 071 } 072 073 Map<String, Object> result = new LinkedHashMap<>(); 074 075 if (StringUtils.isNotEmpty(siteName)) 076 { 077 result.put("siteName", siteName); 078 } 079 080 List<Object> jsonThemes = new ArrayList<>(); 081 082 String ids = request.getParameter("ids"); 083 if (StringUtils.isNotEmpty(ids)) 084 { 085 String[] themeIds = ids.split(","); 086 for (String themeId : themeIds) 087 { 088 if (_resolver.hasAmetysObjectForId(themeId)) 089 { 090 jsonThemes.add(_themeDAO.getTheme(themeId)); 091 } 092 } 093 } 094 else 095 { 096 String language = parameters.getParameter("language", (String) request.getAttribute("renderingLanguage")); 097 if (StringUtils.isEmpty(language)) 098 { 099 language = request.getParameter("language"); 100 } 101 102 Site site = _siteManager.getSite(siteName); 103 TraversableAmetysObject themesNode = _directoryHelper.getThemesNode(site, language); 104 AmetysObjectIterable<DefaultTheme> themes = themesNode.getChildren(); 105 106 for (DefaultTheme theme : themes) 107 { 108 jsonThemes.add(_themeDAO.getTheme(theme.getId())); 109 } 110 } 111 112 result.put("themes", jsonThemes); 113 114 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 115 116 return EMPTY_MAP; 117 } 118}