001/* 002 * Copyright 2013 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; 017 018import org.apache.avalon.framework.logger.LogEnabled; 019import org.apache.avalon.framework.logger.Logger; 020import org.apache.avalon.framework.service.ServiceException; 021import org.apache.avalon.framework.service.ServiceManager; 022import org.apache.avalon.framework.service.Serviceable; 023import org.apache.avalon.framework.thread.ThreadSafe; 024 025import org.ametys.cms.repository.Content; 026import org.ametys.plugins.repository.AmetysObject; 027import org.ametys.plugins.repository.AmetysObjectIterable; 028import org.ametys.plugins.repository.AmetysObjectResolver; 029import org.ametys.plugins.repository.ModifiableTraversableAmetysObject; 030import org.ametys.plugins.repository.UnknownAmetysObjectException; 031import org.ametys.plugins.survey.repository.Survey; 032import org.ametys.plugins.survey.repository.SurveyPage; 033import org.ametys.plugins.survey.repository.SurveyQuestion; 034import org.ametys.plugins.survey.repository.SurveyRule; 035import org.ametys.plugins.survey.repository.SurveyRule.RuleType; 036import org.ametys.web.repository.page.Page; 037import org.ametys.web.repository.site.Site; 038import org.ametys.web.repository.sitemap.Sitemap; 039import org.ametys.web.site.CopyUpdater; 040 041/** 042 * Updates copied surveys, updating the rules. 043 */ 044public class SurveyCopyUpdater implements CopyUpdater, ThreadSafe, Serviceable, LogEnabled 045{ 046 private AmetysObjectResolver _resolver; 047 private Logger _logger; 048 049 @Override 050 public void service(ServiceManager manager) throws ServiceException 051 { 052 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 053 } 054 055 @Override 056 public void enableLogging(Logger logger) 057 { 058 _logger = logger; 059 } 060 061 @Override 062 public void updateSite(Site initialSite, Site createdSite) 063 { 064 ModifiableTraversableAmetysObject pluginsNode = createdSite.getRootPlugins(); 065 066 for (Sitemap sitemap : createdSite.getSitemaps()) 067 { 068 try 069 { 070 ModifiableTraversableAmetysObject surveysNode = pluginsNode.getChild("survey/ametys:surveys/" + sitemap.getSitemapName()); 071 072 AmetysObjectIterable<Survey> surveys = surveysNode.getChildren(); 073 for (Survey survey : surveys) 074 { 075 for (SurveyPage cPage : survey.getPages()) 076 { 077 _updateRulesAfterCopy(survey, cPage); 078 } 079 } 080 } 081 catch (UnknownAmetysObjectException e) 082 { 083 // Nothing to do 084 } 085 086 } 087 } 088 089 private void _updateRulesAfterCopy (Survey cSurvey, SurveyPage cPage) 090 { 091 if (cPage.hasRule()) 092 { 093 SurveyRule rule = cPage.getRule(); 094 String pageId = rule.getPage(); 095 if (pageId != null) 096 { 097 try 098 { 099 // Find symmetric page in created survey 100 AmetysObject originalPage = _resolver.resolveById(pageId); 101 AmetysObject symmetricPage = cSurvey.getChild(originalPage.getName()); 102 cPage.setRule(rule.getType(), symmetricPage.getId()); 103 } 104 catch (UnknownAmetysObjectException e) 105 { 106 // Delete the rule 107 _logger.warn("Symmetric page has not found during copy. The rule is deleted."); 108 cPage.deleteRule(); 109 } 110 } 111 } 112 113 for (SurveyQuestion cQuestion : cPage.getQuestions()) 114 { 115 _updateRulesAfterCopy (cSurvey, cQuestion); 116 } 117 } 118 119 private void _updateRulesAfterCopy (Survey cSurvey, SurveyQuestion cQuestion) 120 { 121 for (SurveyRule cRule : cQuestion.getRules()) 122 { 123 String pageId = cRule.getPage(); 124 if (pageId != null) 125 { 126 String option = cRule.getOption(); 127 try 128 { 129 // Find symmetric page in created survey 130 AmetysObject originalPage = _resolver.resolveById(pageId); 131 AmetysObject symmetricPage = cSurvey.getChild(originalPage.getName()); 132 133 RuleType type = cRule.getType(); 134 cQuestion.deleteRule(option); 135 cQuestion.addRules(option, type, symmetricPage.getId()); 136 } 137 catch (UnknownAmetysObjectException e) 138 { 139 // Delete rule 140 _logger.warn("Symmetric page has not found during copy. The rule is deleted."); 141 cQuestion.deleteRule(option); 142 } 143 } 144 } 145 } 146 147 @Override 148 public void updateContent(Site initialSite, Site createdSite, Content initialContent, Content createdContent) 149 { 150 // Nothing to do 151 } 152 153 @Override 154 public void updatePage(Site initialSite, Site createdSite, Page page) 155 { 156 // Nothing to do 157 158 } 159}