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