001/*
002 *  Copyright 2019 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.plugins.odfsync.apogee.ws;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023
024import org.ametys.core.ui.Callable;
025import org.ametys.odf.program.Program;
026import org.ametys.plugins.odfsync.apogee.ws.ApogeeExportReport.ExportStatus;
027import org.ametys.plugins.odfsync.apogee.ws.structure.ApogeeExportStructure;
028import org.ametys.plugins.repository.AmetysObjectResolver;
029
030/**
031 * Client side element for Apogee export button
032 */
033public class ApogeeButtonClientSideElement extends ApogeeClientSideElement
034{
035    /** The Ametys object resolver */
036    protected AmetysObjectResolver _resolver;
037    
038    /** The Apogee structure component */
039    protected ApogeeStructureComponent _apogeeStructureComponent;
040    
041    @Override
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        super.service(manager);
045        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
046        _apogeeStructureComponent = (ApogeeStructureComponent) manager.lookup(ApogeeStructureComponent.ROLE);
047    }
048    
049    /**
050     * Export a program in Apogee
051     * @param programId the program id to export
052     * @return the export results
053     */
054    @Callable
055    public Map<String, Object> exportProgram(String programId)
056    {
057        Map<String, Object> results = new HashMap<>();
058
059        ApogeeExportReport apogeeExportReport = _apogeeStructureComponent.getApogeeExportReport(programId);
060        ExportStatus exportStatus = apogeeExportReport.getExportStatus();
061        if (exportStatus == ExportStatus.OK)
062        {
063            Program program = _resolver.resolveById(programId);
064            ApogeeExportStructure structure = apogeeExportReport.getApogeeExportStructure();
065            structure.createProgram(program, apogeeExportReport);
066        }
067        
068        results.put("status", apogeeExportReport.getExportStatus().name());
069        return results;
070    }
071}