View Javadoc
1   package com.rest;
2   
3   /*
4    * #%L
5    * Gateway
6    * %%
7    * Copyright (C) 2015 Powered by Sergey
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  
24  import com.dto.GroupRequestDto;
25  import com.dto.InvitationRequestDto;
26  import com.dto.ProjectRequestDto;
27  import com.entity.*;
28  import com.repository.*;
29  import com.utils.SecurityContextReader;
30  import org.modelmapper.ModelMapper;
31  import org.springframework.beans.factory.annotation.Autowired;
32  import org.springframework.context.i18n.LocaleContextHolder;
33  import org.springframework.http.HttpStatus;
34  import org.springframework.http.ResponseEntity;
35  import org.springframework.stereotype.Component;
36  import org.springframework.transaction.annotation.Transactional;
37  import org.springframework.web.bind.annotation.RequestBody;
38  import org.springframework.web.bind.annotation.RequestMapping;
39  import org.springframework.web.bind.annotation.RequestMethod;
40  import org.springframework.web.bind.annotation.RestController;
41  
42  import javax.validation.Valid;
43  
44  /**
45   * Created by aautushk on 9/13/2015.
46   */
47  @Component
48  @RestController
49  @RequestMapping("/gateway")
50  public class ProjectRest {
51      private ModelMapper modelMapper = new ModelMapper(); //read more at http://modelmapper.org/user-manual/
52  
53      @Autowired
54      public IProjectRepository projectRepository;
55  
56      @Autowired
57      public IProjectUserRepository projectUserRepository;
58  
59      @Autowired
60      public IAuthorityRepository authorityRepository;
61  
62      @Autowired
63      public ITranslationRepository translationRepository;
64  
65      @Autowired
66      public IGroupRepository groupRepository;
67  
68      @Autowired
69      public IProjectGroupRepository projectGroupRepository;
70  
71      @Autowired
72      public IInvitationRepository invitationRepository;
73  
74      @Autowired
75      public IInvitationGroupRepository invitationGroupRepository;
76  
77      public SecurityContextReader securityContextReader = new SecurityContextReader();
78  
79      @RequestMapping(value = "/createProject", method = RequestMethod.POST)
80      @Transactional
81      public ResponseEntity createProject(@Valid @RequestBody ProjectRequestDto projectRequestDto) {
82          ProjectEntity project = new ProjectEntity();
83          projectRepository.save(project);//project is created
84  
85          TranslationEntity translationEntity = new TranslationEntity();
86          translationEntity.setParentGuid(project.getProjectGuid());
87          translationEntity.setParentEntity("Project");
88          translationEntity.setField("description");
89          translationEntity.setLanguage(LocaleContextHolder.getLocale().getDisplayLanguage());
90          translationEntity.setContent(projectRequestDto.getDescription());
91  
92          translationRepository.save(translationEntity);//project description translation is created
93  
94          ProjectUserEntity projectUserEntity = new ProjectUserEntity();
95          projectUserEntity.setUsername(securityContextReader.getUsername());
96  
97          projectUserRepository.save(projectUserEntity);//project is assigned to its author
98  
99          AuthorityEntity authorityEntity = new AuthorityEntity();
100         authorityEntity.setUsername(securityContextReader.getUsername());
101         authorityEntity.setAuthority(project.getProjectGuid() + "_admin");
102 
103         authorityRepository.save(authorityEntity);//admin role is assigned to the project author
104 
105         for(GroupRequestDto groupRequestDto : projectRequestDto.getGroups()){
106             GroupEntity groupEntity = new GroupEntity();
107             groupEntity.setGroupName(groupRequestDto.getGroupName());
108             if(groupRequestDto.getGroupGuid() != ""){
109                 groupEntity.setGroupGuid(groupRequestDto.getGroupGuid());
110             }
111 
112             groupRepository.save(groupEntity);//group is created
113 
114             ProjectGroupEntity projectGroupEntity = new ProjectGroupEntity();
115             projectGroupEntity.setProjectGuid(project.getProjectGuid());
116             projectGroupEntity.setGroupGuid(groupEntity.getGroupGuid());
117             projectGroupRepository.save(projectGroupEntity);//group is assigned to the project
118         }
119 
120         for(InvitationRequestDto invitation : projectRequestDto.getInvitations()){
121             InvitationEntity invitationEntity = new InvitationEntity();
122             invitationEntity.setRecipientEmail(invitation.getRecipientEmail());
123             invitationEntity.setProjectGuid(project.getProjectGuid());
124             invitationEntity.setAuthority(invitation.getAuthority());
125             invitationEntity.setIsInvitationAccepted(false);
126 
127             invitationRepository.save(invitationEntity);//invitation is created
128 
129             for(GroupRequestDto groupRequestDto : invitation.getGroups()){
130                 InvitationGroupEntity invitationGroupEntity = new InvitationGroupEntity();
131                 invitationGroupEntity.setInvitationGuid(invitationEntity.getInvitationGuid());
132                 invitationGroupEntity.setGroupGuid(groupRequestDto.getGroupGuid());
133 
134                 invitationGroupRepository.save(invitationGroupEntity);//group is assigned to the invitation
135             }
136         }
137 
138         return new ResponseEntity(HttpStatus.OK);
139     }
140 
141 //    private ProjectResponseDto convertToResponseDto(ProjectEntity project) {
142 //        ProjectResponseDto projectResponseDto = modelMapper.map(project, ProjectResponseDto.class);
143 //        TranslationEntity translationEntity = translationRepository.findByParentGuidAndFieldAndLanguage(projectResponseDto.getProjectGuid(), "description", LocaleContextHolder.getLocale().getDisplayLanguage());
144 //
145 //        if(translationEntity != null){
146 //            projectResponseDto.setDescription(translationEntity.getContent());
147 //        }else{
148 //            projectResponseDto.setDescription("");
149 //        }
150 //
151 //        return projectResponseDto;
152 //    }
153 //
154 //    @RequestMapping(value = "/getProjects", method = RequestMethod.GET)
155 //    public Page<ProjectResponseDto> getProjects(Pageable pageable) {
156 //        int totalElements = 0;
157 //        List<ProjectResponseDto> projectsResponseDto = new ArrayList<ProjectResponseDto>();
158 //        Page<ProjectEntity> projects = projectRepository.findAll(pageable);
159 //
160 //        if(projects != null){
161 //            totalElements = projects.getNumberOfElements();
162 //            for(ProjectEntity project : projects){
163 //                ProjectResponseDto projectResponseDto = this.convertToResponseDto(project);
164 //                projectsResponseDto.add(projectResponseDto);
165 //            }
166 //        }
167 //
168 //        return new PageImpl<>(projectsResponseDto, pageable, totalElements);
169 //    }
170 }