001/* 002 * Copyright 2010 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.workflow; 017 018import java.util.HashMap; 019import java.util.List; 020import java.util.Map; 021import java.util.concurrent.Future; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025 026import org.ametys.cms.repository.Content; 027import org.ametys.cms.repository.ModifiableContent; 028import org.ametys.cms.repository.ModifiableWorkflowAwareContent; 029import org.ametys.core.observation.Event; 030import org.ametys.core.user.CurrentUserProvider; 031import org.ametys.plugins.repository.RepositoryConstants; 032import org.ametys.plugins.repository.UnknownAmetysObjectException; 033import org.ametys.plugins.repository.collection.AmetysObjectCollection; 034import org.ametys.runtime.i18n.I18nizableText; 035import org.ametys.web.ObservationConstants; 036import org.ametys.web.repository.ModifiableSiteAwareAmetysObject; 037import org.ametys.web.repository.page.ModifiablePage; 038import org.ametys.web.repository.page.ModifiableZone; 039import org.ametys.web.repository.page.ModifiableZoneItem; 040import org.ametys.web.repository.page.Page.PageType; 041import org.ametys.web.repository.page.SitemapElement; 042import org.ametys.web.repository.page.ZoneItem; 043import org.ametys.web.repository.page.ZoneItem.ZoneType; 044import org.ametys.web.repository.site.Site; 045import org.ametys.web.repository.site.SiteManager; 046 047import com.opensymphony.workflow.WorkflowException; 048 049/** 050 * OSWorkflow function for creating a content. 051 */ 052public class CreateContentFunction extends org.ametys.cms.workflow.CreateContentFunction 053{ 054 /** Constant for storing the site to use into the transient variables map. */ 055 public static final String SITE_KEY = Site.class.getName(); 056 /** Constant for knowing on which page the content will be affected. */ 057 public static final String PAGE_KEY = CreateContentFunction.class.getName() + "$pageId"; 058 /** Constant for knowing on which zone of the page to affect the content. */ 059 public static final String PAGE_ZONE_KEY = CreateContentFunction.class.getName() + "$zoneName"; 060 061 /** The sites manager */ 062 protected SiteManager _siteManager; 063 /** Current user provider */ 064 protected CurrentUserProvider _currentUserProvider; 065 066 @Override 067 public void service(ServiceManager manager) throws ServiceException 068 { 069 super.service(manager); 070 _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE); 071 _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE); 072 } 073 074 @Override 075 protected List<Future> _notifyContentAdded(Content content, Map transientVars) throws WorkflowException 076 { 077 List<Future> futures = super._notifyContentAdded(content, transientVars); 078 079 String pageId = (String) transientVars.get(PAGE_KEY); 080 if (pageId != null) 081 { 082 SitemapElement sitemapElement = _resolver.resolveById(pageId); 083 084 Map<String, Object> eventParams = new HashMap<>(); 085 eventParams.put(ObservationConstants.ARGS_SITEMAP_ELEMENT, sitemapElement); 086 eventParams.put(ObservationConstants.ARGS_ZONE_ITEM_ID, getResultsMap(transientVars).get("zoneitem-id")); 087 eventParams.put(ObservationConstants.ARGS_ZONE_TYPE, ZoneType.CONTENT); 088 eventParams.put(ObservationConstants.ARGS_ZONE_ITEM_CONTENT, content); 089 _observationManager.notify(new Event(ObservationConstants.EVENT_ZONEITEM_ADDED, _currentUserProvider.getUser(), eventParams)); 090 } 091 092 return futures; 093 } 094 095 096 @Override 097 protected String _getObjectType(Map transientVars, Map args) 098 { 099 return RepositoryConstants.NAMESPACE_PREFIX + ":defaultWebContent"; 100 } 101 102 @Override 103 protected AmetysObjectCollection< ? , ModifiableWorkflowAwareContent> _getContentRoot(Map transientVars) throws WorkflowException 104 { 105 String siteName = _getNonNullVar(transientVars, SITE_KEY, "Missing sitename"); 106 107 Site site = null; 108 try 109 { 110 site = _siteManager.getSite(siteName); 111 } 112 catch (UnknownAmetysObjectException e) 113 { 114 throw new WorkflowException("Site does not exist", e); 115 } 116 117 // FIXME use constant or site getter 118 return site.getChild(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":contents"); 119 } 120 121 @Override 122 protected void _populateContent(Map transientVars, ModifiableContent content) throws WorkflowException 123 { 124 super._populateContent(transientVars, content); 125 126 String siteName = _getNonNullVar(transientVars, SITE_KEY, "Missing sitename"); 127 128 // FIXME API what if not modifiable 129 ((ModifiableSiteAwareAmetysObject) content).setSiteName(siteName); 130 } 131 132 @Override 133 protected void _populateAdditionalData(Map transientVars, ModifiableContent content) throws WorkflowException 134 { 135 super._populateAdditionalData(transientVars, content); 136 137 // FIXME API test if not modifiable 138 ModifiablePage page = null; 139 ModifiableZone zone = null; 140 try 141 { 142 String zoneName = (String) transientVars.get(PAGE_ZONE_KEY); 143 if (zoneName != null) 144 { 145 String pageId = (String) transientVars.get(PAGE_KEY); 146 if (pageId != null) 147 { 148 page = _resolver.resolveById(pageId); 149 150 if (page.getType() != PageType.CONTAINER) 151 { 152 throw new WorkflowException("Page is not of type Container"); 153 } 154 155 if (page.hasZone(zoneName)) 156 { 157 zone = page.getZone(zoneName); 158 } 159 else 160 { 161 zone = page.createZone(zoneName); 162 } 163 164 ModifiableZoneItem zoneItem = zone.addZoneItem(); 165 zoneItem.setType(ZoneItem.ZoneType.CONTENT); 166 zoneItem.setContent(content); 167 zoneItem.setViewName("main"); 168 page.saveChanges(); 169 170 getResultsMap(transientVars).put("zoneitem-id", zoneItem.getId()); 171 } 172 } 173 } 174 catch (UnknownAmetysObjectException e) 175 { 176 throw new WorkflowException("Page does not exist", e); 177 } 178 } 179 180 @Override 181 public I18nizableText getLabel() 182 { 183 return new I18nizableText("plugin.web", "PLUGINS_WEB_CREATE_CONTENT_FUNCTION_LABEL"); 184 } 185}