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