001/*
002 *  Copyright 2022 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 */
016
017package org.ametys.plugins.odfsync.export;
018
019import java.util.ArrayList;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023import java.util.Set;
024
025import org.apache.avalon.framework.activity.Initializable;
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028
029import org.ametys.cms.clientsideelement.AbstractContentClientSideElement;
030import org.ametys.cms.contenttype.ContentType;
031import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
032import org.ametys.cms.repository.Content;
033import org.ametys.core.ui.Callable;
034import org.ametys.odf.ProgramItem;
035import org.ametys.odf.course.Course;
036import org.ametys.odf.courselist.CourseList;
037import org.ametys.odf.orgunit.OrgUnit;
038import org.ametys.odf.program.Container;
039import org.ametys.odf.program.Program;
040import org.ametys.odf.program.SubProgram;
041import org.ametys.plugins.odfsync.export.ExportReport.ExportStatus;
042import org.ametys.plugins.repository.AmetysObjectResolver;
043import org.ametys.runtime.config.Config;
044import org.ametys.runtime.i18n.I18nizableText;
045
046/**
047 * Client side element for connector export buttons
048 */
049public abstract class AbstractExportClientSideElement extends AbstractContentClientSideElement implements Initializable
050{
051    /** The avalon service manager */
052    protected ServiceManager _manager;
053    
054    /** The Ametys object resolver */
055    protected AmetysObjectResolver _resolver;
056    
057    /** The content type extension point */
058    protected ContentTypeExtensionPoint _ctExtPoint;
059    
060    /** The structure component (depending of the current implementation */
061    protected AbstractStructureComponent _structureComponent;
062    
063    private boolean _isActive;
064    
065    public void initialize() throws Exception
066    {
067        _isActive = Config.getInstance().getValue(getActivateParam(), true, false);
068    }
069    
070    @Override
071    public void service(ServiceManager smanager) throws ServiceException
072    {
073        super.service(smanager);
074        _manager = smanager;
075        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
076        _ctExtPoint = (ContentTypeExtensionPoint) smanager.lookup(ContentTypeExtensionPoint.ROLE);
077        _structureComponent = (AbstractStructureComponent) _manager.lookup(getStructureComponentRole());
078    }
079    
080    @Override
081    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
082    {
083        if (_isActive)
084        {
085            return super.getScripts(ignoreRights, contextParameters);
086        }
087        return new ArrayList<>();
088    }
089
090    /**
091     * Get the name of the configuration parameter to check to associated buttons.
092     * @return The name of the configuration parameter
093     */
094    protected abstract String getActivateParam();
095    
096    /**
097     * Get the role of the structure component, depending of the implementation.
098     * @return The role of the structure component
099     */
100    protected abstract String getStructureComponentRole();
101    
102    /**
103     * Get the report informations of the export of the program id
104     * @param programId the program id to export
105     * @return the export results
106     */
107    @Callable
108    public Map<String, Object> getExportReportInfo(String programId)
109    {
110        Map<String, Object> results = new HashMap<>();
111        
112        Program program = _resolver.resolveById(programId);
113        if (hasRight(program))
114        {
115            ExportReport report = _structureComponent.getExportReport(program);
116            ExportStatus exportStatus = report.getStatus();
117            
118            results.put("status", exportStatus.name());
119            if (exportStatus == ExportStatus.CONTENT_DATA_INVALID)
120            {
121                Map<String, Object> programReport = new HashMap<>();
122                Map<String, Object> subProgramReport = new HashMap<>();
123                Map<String, Object> containerReport = new HashMap<>();
124                Map<String, Object> courseListReport = new HashMap<>();
125                Map<String, Object> courseReport = new HashMap<>();
126                Map<String, Object> orgUnitReport = new HashMap<>();
127                Map<String, Object> otherContentReport = new HashMap<>();
128                
129                Map<Content, Set<I18nizableText>> invalidDataByContent = report.getInvalidDataPathByContent();
130                for (Content content : invalidDataByContent.keySet())
131                {
132                    if (content instanceof Program)
133                    {
134                        programReport.put(content.getId(), _getContentParams(content, invalidDataByContent.get(content)));
135                    }
136                    else if (content instanceof SubProgram)
137                    {
138                        subProgramReport.put(content.getId(), _getContentParams(content, invalidDataByContent.get(content)));
139                    }
140                    else if (content instanceof Container)
141                    {
142                        containerReport.put(content.getId(), _getContentParams(content, invalidDataByContent.get(content)));
143                    }
144                    else if (content instanceof CourseList)
145                    {
146                        courseListReport.put(content.getId(), _getContentParams(content, invalidDataByContent.get(content)));
147                    }
148                    else if (content instanceof Course)
149                    {
150                        courseReport.put(content.getId(), _getContentParams(content, invalidDataByContent.get(content)));
151                    }
152                    else if (content instanceof OrgUnit)
153                    {
154                        orgUnitReport.put(content.getId(), _getContentParams(content, invalidDataByContent.get(content)));
155                    }
156                    else
157                    {
158                        otherContentReport.put(content.getId(), _getContentParams(content, invalidDataByContent.get(content)));
159                    }
160                }
161                
162                results.put("program", programReport);
163                results.put("subprogram", subProgramReport);
164                results.put("container", containerReport);
165                results.put("courselist", courseListReport);
166                results.put("course", courseReport);
167                results.put("orgunit", orgUnitReport);
168                results.put("other", otherContentReport);
169            }
170        }
171        else 
172        {
173            results.put("status", ExportStatus.ERROR.name());
174        }
175        
176        return results;
177    }
178    
179    /**
180     * Export a program to external connector
181     * @param programId the program id to export
182     * @return the export results
183     */
184    @Callable
185    public Map<String, Object> exportProgram(String programId)
186    {
187        Map<String, Object> results = new HashMap<>();
188        
189        Program program = _resolver.resolveById(programId);
190        if (hasRight(program))
191        {
192            ExportReport exportReport = _structureComponent.getExportReport(program);
193            if (exportReport.getStatus() == ExportStatus.OK)
194            {
195                exportReport.getExportStructure().createProgram(program, exportReport);
196            }
197            
198            results.put("nbExported", exportReport.getNbExported());
199            results.put("nbNotExported", exportReport.getNbNotExported());
200            results.put("nbPartlyExported", exportReport.getNbPartlyExported());
201            results.put("problemsEncountered", exportReport.getProblemsEncountered());
202            results.put("status", exportReport.getStatus().name());
203        }
204        else 
205        {
206            results.put("status", ExportStatus.ERROR.name());
207        }
208
209        results.put("contentId", program.getId());
210        results.put("contentTitle", program.getTitle());
211        
212        return results;
213    }
214    
215    /** 
216     * Get content params
217     * @param content The content
218     * @param invalidMessages The modelItems
219     * @return parameters of the content
220     */
221    protected Map<String, Object> _getContentParams(Content content, Set<I18nizableText> invalidMessages)
222    {
223        Map<String, Object> params = new HashMap<>();
224        params.put("contentTitle", content.getTitle());
225        if (content instanceof ProgramItem)
226        {
227            params.put("contentCode", ((ProgramItem) content).getCode());
228        }
229        else if (content instanceof OrgUnit)
230        {
231            params.put("contentCode", ((OrgUnit) content).getUAICode());
232        }
233        else
234        {
235            String contentTypeId = content.getTypes()[0];
236            ContentType contentType = _ctExtPoint.getExtension(contentTypeId);
237            params.put("contentTypeLabel", contentType.getLabel());
238            params.put("contentTypeId", contentTypeId);
239            params.put("isTableRef", true);
240        }
241        
242        params.put("attributes", invalidMessages);
243        
244        return params;
245    }
246}