View Javadoc
1   package fr.ifremer.quadrige2.synchro.server.rest.administration.program;
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 com.google.common.collect.Lists;
28  import fr.ifremer.quadrige2.core.service.administration.program.ProgramService;
29  import fr.ifremer.quadrige2.core.vo.administration.program.ProgramVO;
30  import fr.ifremer.quadrige2.synchro.server.security.SecurityContextHelper;
31  import fr.ifremer.quadrige2.synchro.server.service.ServiceLocator;
32  import fr.ifremer.quadrige2.synchro.server.service.synchro.ReferentialSynchroService;
33  import org.springframework.security.access.AccessDeniedException;
34  import org.springframework.web.bind.annotation.*;
35  
36  import java.util.List;
37  
38  @RestController
39  @RequestMapping("/program")
40  public class ProgramRestController {
41  
42      @RequestMapping(value="/save/list/", method = RequestMethod.POST)
43      public @ResponseBody List<ProgramVO> save(@RequestBody List<ProgramVO> programs) throws AccessDeniedException {
44  
45          ProgramService service = ServiceLocator.instance().getProgramService();
46  
47          // Check user permission
48          List<String> hasWritePermission = Lists.transform(programs, ProgramVO::getProgCd);
49          boolean hasWriteAccess = service.hasWritePermission(SecurityContextHelper.getPrincipalUserId(), hasWritePermission);
50          if (!hasWriteAccess) {
51              throw new AccessDeniedException("No write permission on all program to save");
52          }
53  
54          List<ProgramVO> savedPrograms = service.save(programs);
55  
56          // clean cache on referential last update date
57          ServiceLocator.instance().getCacheService()
58                  .clearCache(ReferentialSynchroService.REFERENTIAL_UPDATE_DATE_CACHE);
59          
60          return savedPrograms;
61      }
62  
63      @RequestMapping(value="/save", method = RequestMethod.POST)
64      public @ResponseBody ProgramVO save(@RequestBody ProgramVO program) throws AccessDeniedException {
65  
66          ProgramService service = ServiceLocator.instance().getProgramService();
67  
68          // Check user permission
69          boolean hasWriteAccess = program.getProgCd() == null || service.hasWritePermission(SecurityContextHelper.getPrincipalUserId(), ImmutableList.of(program.getProgCd()));
70          if (!hasWriteAccess) {
71              throw new AccessDeniedException(String.format("No write permission on program [%s] ", program.getProgCd()));
72          }
73  
74          ProgramVO result = service.save(program);
75  
76          // clean cache on referential last update date
77          ServiceLocator.instance().getCacheService()
78                  .clearCache(ReferentialSynchroService.REFERENTIAL_UPDATE_DATE_CACHE);
79  
80          return result;
81      }
82  
83      @RequestMapping(value="/get/{progCd}", method = RequestMethod.GET)
84      public @ResponseBody ProgramVO get(@PathVariable(value="progCd") String progCd) {
85  
86          ProgramService service = ServiceLocator.instance().getProgramService();
87          return service.getByCode(progCd);
88      }
89  }