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