All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bonitasoft.engine.business.application.converter.NodeToApplicationMenuConverter Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2019 Bonitasoft S.A.
 * Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation
 * version 2.1 of the License.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA 02110-1301, USA.
 **/
package org.bonitasoft.engine.business.application.converter;

import org.bonitasoft.engine.api.ImportError;
import org.bonitasoft.engine.business.application.ApplicationService;
import org.bonitasoft.engine.business.application.importer.ApplicationMenuImportResult;
import org.bonitasoft.engine.business.application.model.SApplicationMenu;
import org.bonitasoft.engine.business.application.model.SApplicationPage;
import org.bonitasoft.engine.business.application.model.SApplicationWithIcon;
import org.bonitasoft.engine.business.application.xml.ApplicationMenuNode;
import org.bonitasoft.engine.commons.exceptions.SObjectNotFoundException;
import org.bonitasoft.engine.persistence.SBonitaReadException;
import org.springframework.stereotype.Component;

/**
 * @author Emmanuel Duchastenier
 */
@Component
public class NodeToApplicationMenuConverter {

    private final ApplicationService applicationService;

    public NodeToApplicationMenuConverter(final ApplicationService applicationService) {
        this.applicationService = applicationService;
    }

    /**
     * Convert an {@link ApplicationMenuNode} to
     * {@link SApplicationMenu}
     *
     * @param applicationMenuNode the XML node to convert
     * @param application the application where the menu will be attached to
     * @param parentMenu the parent menu. Null if no parent
     * @return the application where the menu will be attached to
     * @throws SBonitaReadException
     */
    public ApplicationMenuImportResult toSApplicationMenu(ApplicationMenuNode applicationMenuNode,
            SApplicationWithIcon application, SApplicationMenu parentMenu)
            throws SBonitaReadException {
        Long appPageId = null;
        ImportError error = null;
        if (applicationMenuNode.getApplicationPage() != null) {
            try {
                SApplicationPage applicationPage = applicationService.getApplicationPage(application.getToken(),
                        applicationMenuNode.getApplicationPage());
                appPageId = applicationPage.getId();
            } catch (SObjectNotFoundException e) {
                error = new ImportError(applicationMenuNode.getApplicationPage(), ImportError.Type.APPLICATION_PAGE);
            }
        }
        int index = getIndex(parentMenu);
        SApplicationMenu applicationMenu = buildApplicationMenu(applicationMenuNode, application, parentMenu, appPageId,
                index);
        return new ApplicationMenuImportResult(error, applicationMenu);
    }

    private int getIndex(final SApplicationMenu parentMenu) throws SBonitaReadException {
        int index;
        if (parentMenu == null) {
            index = applicationService.getNextAvailableIndex(null);
        } else {
            index = applicationService.getNextAvailableIndex(parentMenu.getId());
        }
        return index;
    }

    private SApplicationMenu buildApplicationMenu(final ApplicationMenuNode applicationMenuNode,
            final SApplicationWithIcon application,
            final SApplicationMenu parentMenu, final Long appPageId, final int index) {
        SApplicationMenu.SApplicationMenuBuilder builder = SApplicationMenu.builder()
                .displayName(applicationMenuNode.getDisplayName()).applicationId(application.getId())
                .applicationPageId(appPageId).index(index);
        if (parentMenu != null) {
            builder.parentId(parentMenu.getId());
        }
        return builder.build();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy