View Javadoc
1   package fr.ifremer.quadrige2.core.service.data.survey;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 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.base.Preconditions;
27  import com.google.common.collect.Lists;
28  import fr.ifremer.quadrige2.core.config.Quadrige2Configuration;
29  import fr.ifremer.quadrige2.core.dao.data.survey.CampaignDao;
30  import fr.ifremer.quadrige2.core.exception.BadUpdateDtException;
31  import fr.ifremer.quadrige2.core.exception.DataLockedException;
32  import fr.ifremer.quadrige2.core.exception.Exceptions;
33  import fr.ifremer.quadrige2.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 Quadrige2Configuration 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  		Preconditions.checkNotNull(source);
76  		Preconditions.checkNotNull(source.getQuserId());
77  		Preconditions.checkNotNull(source.getRecDepId());
78  		Preconditions.checkNotNull(source.getCampaignStartDt());
79  		Preconditions.checkNotNull(source.getCampaignNm());
80  
81  		// Save campaign
82  		try {
83  			return campaignDao.save(source);
84  		} catch (RuntimeException e) {
85  			if (Exceptions.hasCause(e, BadUpdateDtException.class)) {
86  				throw new BadUpdateDtException(Exceptions.getCause(e, BadUpdateDtException.class).getMessage(), e);
87  			}
88  			if (Exceptions.hasCause(e, DataLockedException.class)) {
89  				throw new DataLockedException(Exceptions.getCause(e, DataLockedException.class).getMessage(), e);
90  			}
91  			throw e;
92  		}
93  	}
94  
95  	/** {@inheritDoc} */
96  	@Override
97  	public CampaignVO getById(int campaignId) {
98  		return (CampaignVO) campaignDao.get(CampaignDao.TRANSFORM_CAMPAIGNVO, campaignId);
99  	}
100 
101 
102 	/** {@inheritDoc}
103 	 * @param campaignId*/
104 	@Override
105 	public boolean isCampaignUsedBySurvey(int campaignId) {
106 		return campaignDao.countSurveyUsage(campaignId) > 0;
107 	}
108 
109 	@Override
110 	public void delete(List<Integer> campaignIds) {
111 		if (CollectionUtils.isEmpty(campaignIds)) return;
112 		campaignIds.forEach(campaignDao::remove);
113 	}
114 }