001/* 002 * Copyright 2011 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.survey.actions; 017 018import java.util.Map; 019 020import javax.jcr.RepositoryException; 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.environment.Redirector; 026import org.apache.cocoon.environment.SourceResolver; 027 028import org.ametys.core.observation.AbstractNotifierAction; 029import org.ametys.plugins.repository.AmetysObjectResolver; 030import org.ametys.plugins.repository.ModifiableTraversableAmetysObject; 031import org.ametys.plugins.repository.jcr.JCRAmetysObject; 032import org.ametys.web.repository.site.SiteManager; 033 034/** 035 * Abstract action for manipulate survey 036 * 037 */ 038public abstract class AbstractSurveyAction extends AbstractNotifierAction 039{ 040 /** Ametys object resolver. */ 041 protected AmetysObjectResolver _resolver; 042 043 /** The site manager */ 044 protected SiteManager _siteManager; 045 046 @Override 047 public void service(ServiceManager serviceManager) throws ServiceException 048 { 049 super.service(serviceManager); 050 _siteManager = (SiteManager) serviceManager.lookup(SiteManager.ROLE); 051 _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE); 052 } 053 054 @Override 055 public abstract Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception; 056 057 /** 058 * Get the root node for surveys 059 * @param siteName the site name 060 * @param lang the language 061 * @return the root node 062 * @throws RepositoryException if an error occurs while manipulating the repository 063 */ 064 protected ModifiableTraversableAmetysObject getSurveyRootNode (String siteName, String lang) throws RepositoryException 065 { 066 ModifiableTraversableAmetysObject pluginsNode = _siteManager.getSite(siteName).getRootPlugins(); 067 068 ModifiableTraversableAmetysObject surveyNode = null; 069 if (!pluginsNode.hasChild("survey")) 070 { 071 surveyNode = ((ModifiableTraversableAmetysObject) pluginsNode.createChild("survey", "ametys:unstructured")).createChild("ametys:surveys", "ametys:unstructured"); 072 } 073 else 074 { 075 surveyNode = pluginsNode.getChild("survey/ametys:surveys"); 076 } 077 078 if (!surveyNode.hasChild(lang)) 079 { 080 surveyNode.createChild(lang, "ametys:unstructured"); 081 ((JCRAmetysObject) pluginsNode).getNode().getSession().save(); 082 } 083 084 return pluginsNode.getChild("survey/ametys:surveys/" + lang); 085 } 086}