View Javadoc
1   package fr.ifremer.quadrige3.synchro.server.rest.data.survey;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Synchro server
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.ImmutableList;
27  import fr.ifremer.quadrige3.core.exception.DeleteForbiddenException;
28  import fr.ifremer.quadrige3.core.service.data.survey.CampaignService;
29  import fr.ifremer.quadrige3.core.vo.data.survey.CampaignVO;
30  import fr.ifremer.quadrige3.synchro.server.service.ServiceLocator;
31  import fr.ifremer.quadrige3.synchro.server.service.synchro.ReferentialSynchroService;
32  import org.apache.commons.collections4.CollectionUtils;
33  import org.springframework.http.HttpStatus;
34  import org.springframework.web.bind.annotation.*;
35  
36  import java.util.List;
37  import java.util.stream.Collectors;
38  
39  @RestController
40  @RequestMapping("/campaign")
41  public class CampaignRestController {
42  
43      @RequestMapping(value = "/save/list/", method = RequestMethod.POST)
44      public @ResponseBody
45      List<CampaignVO> save(@RequestBody List<CampaignVO> campaigns) {
46  
47          CampaignService service = ServiceLocator.instance().getCampaignService();
48  
49          List<CampaignVO> savedCampaigns = service.save(campaigns);
50  
51          // clean cache on referential last update date
52          ServiceLocator.instance().getCacheService()
53                  .clearCache(ReferentialSynchroService.REFERENTIAL_UPDATE_DATE_CACHE);
54  
55          return savedCampaigns;
56      }
57  
58      @RequestMapping(value = "/save", method = RequestMethod.POST)
59      public @ResponseBody
60      CampaignVO save(@RequestBody CampaignVO campaign) {
61  
62          CampaignService service = ServiceLocator.instance().getCampaignService();
63  
64          CampaignVO savedCampaign = service.save(campaign);
65  
66          // clean cache on referential last update date
67          ServiceLocator.instance().getCacheService()
68                  .clearCache(ReferentialSynchroService.REFERENTIAL_UPDATE_DATE_CACHE);
69  
70          return savedCampaign;
71      }
72  
73      @RequestMapping(value = "/get/{campaignId}", method = RequestMethod.GET)
74      public @ResponseBody
75      CampaignVO get(@PathVariable(value = "campaignId") Integer campaignId) {
76  
77          CampaignService service = ServiceLocator.instance().getCampaignService();
78          return service.getById(campaignId);
79      }
80  
81      @ResponseStatus(value = HttpStatus.OK, reason = "Delete campaign successfully processed")
82      @RequestMapping(value = "/delete/{campaignId}", method = RequestMethod.POST)
83      public void delete(@PathVariable(value = "campaignId") Integer campaignId) {
84  
85          if (campaignId == null) return;
86  
87          List<Integer> campaignIds = ImmutableList.of(campaignId);
88  
89          // Delete the campaigns by their ids
90          deleteCampaigns(campaignIds);
91  
92      }
93  
94      @ResponseStatus(value = HttpStatus.OK, reason = "Delete campaigns successfully processed")
95      @RequestMapping(value = "/delete/list/", method = RequestMethod.POST)
96      public void delete(@RequestBody List<Integer> campaignIds) {
97  
98          if (CollectionUtils.isEmpty(campaignIds)) return;
99  
100         deleteCampaigns(campaignIds);
101     }
102 
103     private void deleteCampaigns(List<Integer> campaignIds) {
104         if (campaignIds == null) return;
105 
106         CampaignService service = ServiceLocator.instance().getCampaignService();
107         // Check campaigns are deletable
108         List<String> forbiddenIds = campaignIds.stream()
109                 .filter(campaignId -> campaignId != null && service.isCampaignUsedBySurvey(campaignId))
110                 .map(Object::toString)
111                 .collect(Collectors.toList());
112 
113         // throw exception if one is not deletable
114         if (!forbiddenIds.isEmpty())
115             throw new DeleteForbiddenException("A campaign is used by survey", forbiddenIds);
116 
117         // If ok, delete them
118         service.delete(campaignIds);
119     }
120 
121 }