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