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 (String dataPath : referencesByPath.keySet())
093                {
094                    OutgoingReferences references = referencesByPath.get(dataPath);
095                    for (String referenceType : references.keySet())
096                    {
097                        for (String referenceValue : references.get(referenceType))
098                        {
099                            CHECK check = CHECK.SERVER_ERROR;
100                            try
101                            {
102                                check = _consistencyChecker.checkConsistency(referenceType, referenceValue, content.getId(), dataPath, shortTest);
103                            }
104                            catch (Exception e)
105                            {
106                                // Ignore, consider it a failure.
107                            }
108                            
109                            switch (check)
110                            {
111                                case SUCCESS:
112                                    break;
113                                case UNKNOWN:
114                                    unknown++;
115                                    break;
116                                case NOT_FOUND:
117                                case UNAUTHORIZED:
118                                case SERVER_ERROR:
119                                default:
120                                    failure++;
121                                    break;
122                            }
123                        }
124                    }
125                }
126                
127                if (failure > 0)
128                {
129                    Map<String, Object> contentParams = getContentDefaultParameters(content);
130                    contentParams.put("description", _getNotOkDescription(content));
131                    
132                    @SuppressWarnings("unchecked")
133                    List<Map<String, Object>> notOkContents = (List<Map<String, Object>>) results.get("not-ok-contents");
134                    notOkContents.add(contentParams);
135                }
136                else if (unknown > 0)
137                {
138                    Map<String, Object> contentParams = getContentDefaultParameters(content);
139                    contentParams.put("description", _getMainlyOkDescription(content));
140                    
141                    @SuppressWarnings("unchecked")
142                    List<Map<String, Object>> mainlyOkContents = (List<Map<String, Object>>) results.get("mainly-ok-contents");
143                    mainlyOkContents.add(contentParams);
144                }
145                else 
146                {
147                    Map<String, Object> contentParams = getContentDefaultParameters(content);
148                    contentParams.put("description", _getOkDescription(content));
149                    
150                    @SuppressWarnings("unchecked")
151                    List<Map<String, Object>> okContents = (List<Map<String, Object>>) results.get("fully-ok-contents");
152                    okContents.add(contentParams);
153                }
154            }
155        }
156
157        return results;
158    }
159    
160    /**
161     * Get the default content's parameters
162     * @param content The content
163     * @return The default parameters
164     */
165    protected Map<String, Object> getContentDefaultParameters(Content content)
166    {
167        Map<String, Object> contentParams = new HashMap<>();
168        contentParams.put("id", content.getId());
169        contentParams.put("title", _contentHelper.getTitle(content));
170        
171        return contentParams;
172    }
173    
174    /**
175     * Get content i18n description when the consistency check has succeed
176     * @param content The content
177     * @return The {@link I18nizableText} description
178     */
179    protected I18nizableText _getOkDescription(Content content)
180    {
181        List<String> i18nParameters = new ArrayList<>();
182        i18nParameters.add(_contentHelper.getTitle(content));
183        
184        I18nizableText ed = (I18nizableText) this._script.getParameters().get("consistency-ok-content-description");
185        return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
186    }
187    
188    /**
189     * Get content i18n description when the consistency check has failed for at least one link
190     * @param content The content
191     * @return The {@link I18nizableText} description
192     */
193    protected I18nizableText _getNotOkDescription(Content content)
194    {
195        List<String> i18nParameters = new ArrayList<>();
196        i18nParameters.add(_contentHelper.getTitle(content));
197        
198        I18nizableText ed = (I18nizableText) this._script.getParameters().get("consistency-error-content-description");
199        return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
200    }
201    
202    /**
203     * Get content i18n description when the consistency check returned at least one unknown link
204     * @param content The content
205     * @return The {@link I18nizableText} description
206     */
207    protected I18nizableText _getMainlyOkDescription(Content content)
208    {
209        List<String> i18nParameters = new ArrayList<>();
210        i18nParameters.add(_contentHelper.getTitle(content));
211        
212        I18nizableText ed = (I18nizableText) this._script.getParameters().get("consistency-okbut-content-description");
213        return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
214    }
215}