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.web.repository.page.actions; 017 018import java.util.HashMap; 019import java.util.List; 020import java.util.Map; 021 022import org.apache.avalon.framework.parameters.Parameters; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.cocoon.acting.ServiceableAction; 026import org.apache.cocoon.environment.ObjectModelHelper; 027import org.apache.cocoon.environment.Redirector; 028import org.apache.cocoon.environment.Request; 029import org.apache.cocoon.environment.SourceResolver; 030import org.apache.commons.lang3.StringUtils; 031 032import org.ametys.cms.FilterNameHelper; 033import org.ametys.core.cocoon.JSonReader; 034import org.ametys.plugins.repository.AmetysObjectResolver; 035import org.ametys.plugins.repository.ModifiableTraversableAmetysObject; 036import org.ametys.web.repository.page.ModifiablePage; 037import org.ametys.web.repository.page.Page; 038import org.ametys.web.repository.page.Page.PageType; 039import org.ametys.web.repository.page.PageDAO; 040import org.ametys.web.repository.page.PagesContainer; 041import org.ametys.web.repository.site.Site; 042import org.ametys.web.skin.SkinTemplate; 043import org.ametys.web.skin.SkinsManager; 044 045/** 046 * Get available services for a page or a not-yet existing page 047 * 048 */ 049public class GetAvailableServices extends ServiceableAction 050{ 051 private PageDAO _pageDAO; 052 private AmetysObjectResolver _resolver; 053 private SkinsManager _skinsManager; 054 055 @Override 056 public void service(ServiceManager smanager) throws ServiceException 057 { 058 super.service(smanager); 059 _pageDAO = (PageDAO) smanager.lookup(PageDAO.ROLE); 060 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 061 _skinsManager = (SkinsManager) smanager.lookup(SkinsManager.ROLE); 062 } 063 064 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 065 { 066 Request request = ObjectModelHelper.getRequest(objectModel); 067 Map<String, Object> services = new HashMap<>(); 068 069 @SuppressWarnings("unchecked") 070 Map<String, Object> jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT); 071 072 String pageId = (String) jsParameters.get("pageId"); 073 String parentId = (String) jsParameters.get("parentId"); 074 String zoneName = (String) jsParameters.get("zone"); 075 076 if (StringUtils.isNotEmpty(pageId)) 077 { 078 // Get available template for a page 079 List<Map<String, Object>> ctypes = _pageDAO.getAvailableServices(pageId, zoneName); 080 services.put("services", ctypes); 081 } 082 else if (StringUtils.isNotEmpty(parentId)) 083 { 084 String pageTitle = (String) jsParameters.get("title"); 085 String template = (String) jsParameters.get("template"); 086 087 // Get available content types for a not yet existing page 088 PagesContainer parent = _resolver.resolveById(parentId); 089 090 // Create page temporarily 091 Page page = _createPage(parent, pageTitle, template); 092 093 List<Map<String, Object>> ctypes = _pageDAO.getAvailableServices(page.getId(), zoneName); 094 services.put("services", ctypes); 095 096 // Cancel page creation 097 page.getSitemap().revertChanges(); 098 } 099 100 request.setAttribute(JSonReader.OBJECT_TO_READ, services); 101 102 return EMPTY_MAP; 103 } 104 105 private Page _createPage (PagesContainer parent, String pageTitle, String template) 106 { 107 Site site = parent.getSite(); 108 String originalPageName = FilterNameHelper.filterName(pageTitle); 109 110 String pageName = originalPageName; 111 int index = 2; 112 while (parent.hasChild(pageName)) 113 { 114 pageName = originalPageName + "-" + index++; 115 } 116 117 ModifiablePage page = ((ModifiableTraversableAmetysObject) parent).createChild(pageName, "ametys:defaultPage"); 118 119 page.setTitle(pageTitle); 120 page.setType(PageType.NODE); 121 page.setSiteName(site.getName()); 122 page.setSitemapName(page.getSitemap().getName()); 123 124 125 String skinId = page.getSite().getSkinId(); 126 SkinTemplate tpl = _skinsManager.getSkin(skinId).getTemplate(template); 127 if (tpl == null) 128 { 129 throw new IllegalStateException("Template '" + template + "' does not exist on skin '" + skinId + "'"); 130 } 131 132 // Set temporary the template to get available services 133 page.setType(PageType.CONTAINER); 134 page.setTemplate(template); 135 136 return page; 137 } 138 139}