/*
* Copyright (C) 2016 TIBCO Jaspersoft Corporation. All rights reserved.
* http://community.jaspersoft.com/project/mobile-sdk-android
*
* Unless you have purchased a commercial license agreement from TIBCO Jaspersoft,
* the following license terms apply:
*
* This program is part of TIBCO Jaspersoft Mobile SDK for Android.
*
* TIBCO Jaspersoft Mobile SDK 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, either version 3 of the License, or
* (at your option) any later version.
*
* TIBCO Jaspersoft Mobile SDK 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 TIBCO Jaspersoft Mobile SDK for Android. If not, see
* <http://www.gnu.org/licenses/lgpl>.
*/
package com.jaspersoft.android.sdk.service.report.schedule;
import com.jaspersoft.android.sdk.network.entity.schedule.JobFormEntity;
import com.jaspersoft.android.sdk.service.data.schedule.JobForm;
import com.jaspersoft.android.sdk.service.data.schedule.JobOutputFormat;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.TestOnly;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
/**
* @author Tom Koptel
* @since 2.3
*/
class JobFormMapper {
private final JobTriggerMapper triggerMapper;
private final JobSourceMapper sourceMapper;
private final JobRepoDestinationMapper repoDestinationMapper;
private final JobMailNotificationMapper mailNotificationMapper;
private final JobAlertMapper alertMapper;
JobFormMapper(
JobTriggerMapper triggerMapper,
JobSourceMapper jobSourceMapper,
JobRepoDestinationMapper jobRepoDestinationMapper,
JobMailNotificationMapper mailNotificationMapper,
JobAlertMapper alertMapper
) {
this.triggerMapper = triggerMapper;
this.sourceMapper = jobSourceMapper;
this.repoDestinationMapper = jobRepoDestinationMapper;
this.mailNotificationMapper = mailNotificationMapper;
this.alertMapper = alertMapper;
}
private static class InstanceHolder {
private final static JobFormMapper INSTANCE = new JobFormMapper(
JobTriggerMapper.INSTANCE,
JobSourceMapper.INSTANCE,
JobRepoDestinationMapper.INSTANCE,
JobMailNotificationMapper.INSTANCE,
JobAlertMapper.INSTANCE);
}
public static JobFormMapper getInstance() {
return InstanceHolder.INSTANCE;
}
@NotNull
public JobFormEntity toFormEntity(@NotNull JobForm form) {
JobFormEntity entity = new JobFormEntity();
mapFormToCommonEntityFields(form, entity);
mapFormFormatsOnEntity(form, entity);
repoDestinationMapper.mapFormOnEntity(form, entity);
sourceMapper.mapFormOnEntity(form, entity);
triggerMapper.mapFormOnEntity(form, entity);
mailNotificationMapper.mapFormOnEntity(form, entity);
alertMapper.mapFormOnEntity(form, entity);
return entity;
}
@TestOnly
void mapFormToCommonEntityFields(JobForm form, JobFormEntity entity) {
entity.setLabel(form.getLabel());
entity.setDescription(form.getDescription());
entity.setBaseOutputFilename(form.getBaseOutputFilename());
Integer version = form.getVersion();
if (version != null) {
entity.setVersion(version);
}
}
@TestOnly
void mapFormFormatsOnEntity(JobForm form, JobFormEntity entity) {
Set<JobOutputFormat> outputFormats = form.getOutputFormats();
Collection<String> formats = new ArrayList<>(outputFormats.size());
for (JobOutputFormat outputFormat : outputFormats) {
formats.add(outputFormat.toString());
}
entity.addOutputFormats(formats);
}
@NotNull
public JobForm toDataForm(@NotNull JobFormEntity entity) {
JobForm.Builder form = new JobForm.Builder();
mapEntityCommonFieldsToForm(form, entity);
mapEntityFormatsToForm(form, entity);
repoDestinationMapper.mapEntityOnForm(form, entity);
sourceMapper.mapEntityOnForm(form, entity);
triggerMapper.mapEntityOnForm(form, entity);
mailNotificationMapper.mapEntityOnForm(form, entity);
alertMapper.mapEntityOnForm(form, entity);
return form.build();
}
@TestOnly
void mapEntityCommonFieldsToForm(JobForm.Builder form, JobFormEntity entity) {
form.withVersion(entity.getVersion());
form.withLabel(entity.getLabel());
form.withDescription(entity.getDescription());
form.withBaseOutputFilename(entity.getBaseOutputFilename());
}
@TestOnly
void mapEntityFormatsToForm(JobForm.Builder form, JobFormEntity entity) {
Collection<String> formats = entity.getOutputFormats();
List<JobOutputFormat> formatList = new ArrayList<>();
for (String format : formats) {
JobOutputFormat out = JobOutputFormat.valueOf(format);
formatList.add(out);
}
form.withOutputFormats(formatList);
}
}