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.cms.clientsideelement; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025 026import org.ametys.cms.content.references.OutgoingReferences; 027import org.ametys.cms.repository.Content; 028import org.ametys.cms.transformation.ConsistencyChecker; 029import org.ametys.cms.transformation.ConsistencyChecker.CHECK; 030import org.ametys.core.ui.Callable; 031import org.ametys.core.ui.StaticClientSideElement; 032import org.ametys.plugins.repository.AmetysObjectResolver; 033import org.ametys.runtime.i18n.I18nizableText; 034 035/** 036 * This element creates a toggle button representing the consistency state. 037 */ 038public class ContentConsistencyTestClientSideElement extends StaticClientSideElement 039{ 040 /** Repository content */ 041 protected AmetysObjectResolver _resolver; 042 /** The consistency checker */ 043 protected ConsistencyChecker _consistencyChecker; 044 045 @Override 046 public void service(ServiceManager smanager) throws ServiceException 047 { 048 super.service(smanager); 049 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 050 _consistencyChecker = (ConsistencyChecker) smanager.lookup(ConsistencyChecker.ROLE); 051 } 052 053 /** 054 * Check the consistency of contents 055 * @param contentsId The ids of contents to test 056 * @param shortTest true to make a short test, that means that long tests will return UNKNOWN immediately 057 * @return the consistency test results 058 */ 059 @Callable 060 public Map<String, Object> checkConsistency(List<String> contentsId, boolean shortTest) 061 { 062 Map<String, Object> results = new HashMap<>(); 063 064 results.put("fully-ok-contents", new ArrayList<Map<String, Object>>()); 065 results.put("mainly-ok-contents", new ArrayList<Map<String, Object>>()); 066 results.put("not-ok-contents", new ArrayList<Map<String, Object>>()); 067 068 for (String contentId : contentsId) 069 { 070 Content content = _resolver.resolveById(contentId); 071 072 Map<String, OutgoingReferences> referencesByPath = content.getOutgoingReferences(); 073 074 if (referencesByPath.isEmpty()) 075 { 076 Map<String, Object> contentParams = getContentDefaultParameters(content); 077 contentParams.put("description", _getOkDescription(content)); 078 079 @SuppressWarnings("unchecked") 080 List<Map<String, Object>> okContents = (List<Map<String, Object>>) results.get("fully-ok-contents"); 081 okContents.add(contentParams); 082 } 083 else 084 { 085 int failure = 0; 086 int unknown = 0; 087 088 for (OutgoingReferences references : referencesByPath.values()) 089 { 090 for (String referenceType : references.keySet()) 091 { 092 for (String referenceValue : references.get(referenceType)) 093 { 094 CHECK check = CHECK.SERVER_ERROR; 095 try 096 { 097 check = _consistencyChecker.checkConsistency(referenceType, referenceValue, shortTest); 098 } 099 catch (Exception e) 100 { 101 // Ignore, consider it a failure. 102 } 103 104 switch (check) 105 { 106 case SUCCESS: 107 break; 108 case UNKNOWN: 109 unknown++; 110 break; 111 case NOT_FOUND: 112 case UNAUTHORIZED: 113 case SERVER_ERROR: 114 default: 115 failure++; 116 break; 117 } 118 } 119 } 120 } 121 122 if (failure > 0) 123 { 124 Map<String, Object> contentParams = getContentDefaultParameters(content); 125 contentParams.put("description", _getNotOkDescription(content)); 126 127 @SuppressWarnings("unchecked") 128 List<Map<String, Object>> notOkContents = (List<Map<String, Object>>) results.get("not-ok-contents"); 129 notOkContents.add(contentParams); 130 } 131 else if (unknown > 0) 132 { 133 Map<String, Object> contentParams = getContentDefaultParameters(content); 134 contentParams.put("description", _getMainlyOkDescription(content)); 135 136 @SuppressWarnings("unchecked") 137 List<Map<String, Object>> mainlyOkContents = (List<Map<String, Object>>) results.get("mainly-ok-contents"); 138 mainlyOkContents.add(contentParams); 139 } 140 else 141 { 142 Map<String, Object> contentParams = getContentDefaultParameters(content); 143 contentParams.put("description", _getOkDescription(content)); 144 145 @SuppressWarnings("unchecked") 146 List<Map<String, Object>> okContents = (List<Map<String, Object>>) results.get("fully-ok-contents"); 147 okContents.add(contentParams); 148 } 149 } 150 } 151 152 return results; 153 } 154 155 /** 156 * Get the default content's parameters 157 * @param content The content 158 * @return The default parameters 159 */ 160 protected Map<String, Object> getContentDefaultParameters(Content content) 161 { 162 Map<String, Object> contentParams = new HashMap<>(); 163 contentParams.put("id", content.getId()); 164 contentParams.put("title", content.getTitle()); 165 166 return contentParams; 167 } 168 169 /** 170 * Get content i18n description when the consistency check has succeed 171 * @param content The content 172 * @return The {@link I18nizableText} description 173 */ 174 protected I18nizableText _getOkDescription(Content content) 175 { 176 List<String> i18nParameters = new ArrayList<>(); 177 i18nParameters.add(content.getTitle()); 178 179 I18nizableText ed = (I18nizableText) this._script.getParameters().get("consistency-ok-content-description"); 180 return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters); 181 } 182 183 /** 184 * Get content i18n description when the consistency check has failed for at least one link 185 * @param content The content 186 * @return The {@link I18nizableText} description 187 */ 188 protected I18nizableText _getNotOkDescription(Content content) 189 { 190 List<String> i18nParameters = new ArrayList<>(); 191 i18nParameters.add(content.getTitle()); 192 193 I18nizableText ed = (I18nizableText) this._script.getParameters().get("consistency-error-content-description"); 194 return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters); 195 } 196 197 /** 198 * Get content i18n description when the consistency check returned at least one unknown link 199 * @param content The content 200 * @return The {@link I18nizableText} description 201 */ 202 protected I18nizableText _getMainlyOkDescription(Content content) 203 { 204 List<String> i18nParameters = new ArrayList<>(); 205 i18nParameters.add(content.getTitle()); 206 207 I18nizableText ed = (I18nizableText) this._script.getParameters().get("consistency-okbut-content-description"); 208 return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters); 209 } 210}