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

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

There is a newer version: 10.2.0
Show 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.api.ImportStatus;
import org.bonitasoft.engine.api.impl.validator.ApplicationImportValidator;
import org.bonitasoft.engine.business.application.ApplicationService;
import org.bonitasoft.engine.business.application.importer.ImportResult;
import org.bonitasoft.engine.business.application.model.SApplication;
import org.bonitasoft.engine.business.application.model.SApplicationState;
import org.bonitasoft.engine.business.application.xml.ApplicationNode;
import org.bonitasoft.engine.exception.ImportException;
import org.bonitasoft.engine.page.PageService;
import org.bonitasoft.engine.page.SPage;
import org.bonitasoft.engine.persistence.SBonitaReadException;
import org.bonitasoft.engine.profile.ProfileService;
import org.bonitasoft.engine.profile.exception.profile.SProfileNotFoundException;
import org.bonitasoft.engine.profile.model.SProfile;

/**
 * @author Elias Ricken de Medeiros
 */
public class NodeToApplicationConverter {

    private final ProfileService profileService;
    private final PageService pageService;
    private final ApplicationImportValidator validator;

    public NodeToApplicationConverter(final ProfileService profileService, final PageService pageService,
            final ApplicationImportValidator validator) {
        this.profileService = profileService;
        this.pageService = pageService;
        this.validator = validator;
    }

    public ImportResult toSApplication(final ApplicationNode applicationNode, final long createdBy)
            throws SBonitaReadException, ImportException {
        String token = applicationNode.getToken();
        validator.validate(token);
        final ImportStatus importStatus = new ImportStatus(token);
        Long layoutId = getLayoutId(getLayoutName(applicationNode), token, importStatus);
        Long themeId = getThemeId(getThemeName(applicationNode), token, importStatus);
        final long currentDate = System.currentTimeMillis();
        final SApplication.SApplicationBuilder builder = SApplication.builder().token(token)
                .displayName(applicationNode.getDisplayName()).version(applicationNode.getVersion())
                .creationDate(currentDate).lastUpdateDate(currentDate)
                .createdBy(createdBy).state(SApplicationState.ACTIVATED.name()).layoutId(layoutId).themeId(themeId)
                .iconPath(applicationNode.getIconPath()).description(applicationNode.getDescription())
                .state(applicationNode.getState());

        final ImportError importError = setProfile(applicationNode, builder);
        if (importError != null) {
            importStatus.addError(importError);
        }

        final SApplication application = builder.build();
        return new ImportResult(application, importStatus);
    }

    private Long getLayoutId(final String layoutName, final String applicationToken, final ImportStatus importStatus)
            throws SBonitaReadException, ImportException {
        SPage layout = pageService.getPageByName(layoutName);
        if (layout == null) {
            return handleMissingLayout(layoutName, applicationToken, importStatus);
        }
        return layout.getId();
    }

    private Long getThemeId(final String themeName, final String applicationToken, final ImportStatus importStatus)
            throws SBonitaReadException, ImportException {
        SPage theme = pageService.getPageByName(themeName);
        if (theme == null) {
            return handleMissingTheme(themeName, applicationToken, importStatus);
        }
        return theme.getId();
    }

    protected Long handleMissingLayout(final String layoutName, final String applicationToken,
            final ImportStatus importStatus) throws ImportException, SBonitaReadException {
        throw new ImportException(
                String.format("Unable to import application with token '%s' because the layout '%s' was not found.",
                        applicationToken, layoutName));
    }

    protected Long handleMissingTheme(final String themeName, final String applicationToken,
            final ImportStatus importStatus) throws ImportException, SBonitaReadException {
        throw new ImportException(
                String.format("Unable to import application with token '%s' because the theme '%s' was not found.",
                        applicationToken, themeName));
    }

    protected String getLayoutName(final ApplicationNode applicationNode) {
        return applicationNode.getLayout() != null ? applicationNode.getLayout()
                : ApplicationService.DEFAULT_LAYOUT_NAME;
    }

    protected String getThemeName(final ApplicationNode applicationNode) {
        return applicationNode.getTheme() != null ? applicationNode.getTheme() : ApplicationService.DEFAULT_THEME_NAME;
    }

    private ImportError setProfile(final ApplicationNode applicationNode,
            final SApplication.SApplicationBuilder builder) {
        ImportError importError = null;
        if (applicationNode.getProfile() != null) {
            try {
                final SProfile profile = profileService.getProfileByName(applicationNode.getProfile());
                builder.profileId(profile.getId());
            } catch (final SProfileNotFoundException | SBonitaReadException e) {
                importError = new ImportError(applicationNode.getProfile(), ImportError.Type.PROFILE);
            }
        }
        return importError;
    }

    protected PageService getPageService() {
        return pageService;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy