View Javadoc
1   package fr.ifremer.quadrige3.core.service.data.survey;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Server Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import com.google.common.collect.Lists;
27  import fr.ifremer.quadrige3.core.config.QuadrigeConfiguration;
28  import fr.ifremer.quadrige3.core.dao.data.survey.CampaignDao;
29  import fr.ifremer.quadrige3.core.dao.technical.Assert;
30  import fr.ifremer.quadrige3.core.exception.BadUpdateDtException;
31  import fr.ifremer.quadrige3.core.exception.DataLockedException;
32  import fr.ifremer.quadrige3.core.exception.Exceptions;
33  import fr.ifremer.quadrige3.core.vo.data.survey.CampaignVO;
34  import org.apache.commons.collections4.CollectionUtils;
35  import org.springframework.context.annotation.Lazy;
36  import org.springframework.stereotype.Service;
37  
38  import javax.annotation.Resource;
39  import java.util.List;
40  
41  /**
42   * <p>
43   * Campaign service impl class.
44   * </p>
45   * 
46   */
47  @Service("campaignService")
48  @Lazy
49  public class CampaignServiceImpl implements CampaignService {
50  
51  	@Resource
52  	private CampaignDao campaignDao;
53  
54  	@Resource
55  	protected QuadrigeConfiguration config;
56  
57  	/** {@inheritDoc} */
58  	@Override
59  	public List<CampaignVO> save(List<CampaignVO> campaigns) {
60  		List<CampaignVO> result = Lists.newArrayList();
61  
62  		if (CollectionUtils.isNotEmpty(campaigns)) {
63  			for (CampaignVO source : campaigns) {
64  				CampaignVO target = save(source);
65  				result.add(target);
66  			}
67  		}
68  
69  		return result;
70  	}
71  
72  	/** {@inheritDoc} */
73  	@Override
74  	public CampaignVO save(CampaignVO source) {
75  		Assert.notNull(source);
76  		Assert.notNull(source.getQuserId());
77  		Assert.notNull(source.getRecDepId());
78  		Assert.notNull(source.getCampaignStartDt());
79  		Assert.notNull(source.getCampaignNm());
80  
81  		// Save campaign
82  		try {
83  			return campaignDao.save(source);
84  		} catch (RuntimeException e) {
85  			Exceptions.rethrowInnerCauseIfExists(e, BadUpdateDtException.class);
86  			Exceptions.rethrowInnerCauseIfExists(e, DataLockedException.class);
87  			throw e;
88  		}
89  	}
90  
91  	/** {@inheritDoc} */
92  	@Override
93  	public CampaignVO getById(int campaignId) {
94  		return (CampaignVO) campaignDao.get(CampaignDao.TRANSFORM_CAMPAIGNVO, campaignId);
95  	}
96  
97  
98  	/** {@inheritDoc}
99  	 * @param campaignId*/
100 	@Override
101 	public boolean isCampaignUsedBySurvey(int campaignId) {
102 		return campaignDao.countSurveyUsage(campaignId) > 0;
103 	}
104 
105 	@Override
106 	public void delete(List<Integer> campaignIds) {
107 		if (CollectionUtils.isEmpty(campaignIds)) return;
108 		campaignIds.forEach(campaignDao::remove);
109 	}
110 }