diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java b/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java new file mode 100755 index 0000000..cb0f8ff --- /dev/null +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java @@ -0,0 +1,37 @@ +package com.plannaplan.entities; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; + +@Entity +public class Assignment { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + @ManyToOne + @JoinColumn(name = "group_id") + private Groups group; + @ManyToOne + @JoinColumn(name = "commision_id") + private Commision commision; + + public Assignment(Groups group, Commision commision) { + this.commision = commision; + this.group = group; + } + + public Assignment() { + } + + public Long getId() { + return this.id; + } + + public Groups getGroup() { + return this.group; + } +} \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/Commision.java b/buisnesslogic/src/main/java/com/plannaplan/entities/Commision.java new file mode 100755 index 0000000..dd4b698 --- /dev/null +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/Commision.java @@ -0,0 +1,47 @@ +package com.plannaplan.entities; + +import java.sql.Timestamp; +import java.util.List; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToMany; +import javax.persistence.OneToOne; + +@Entity +public class Commision { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + @OneToOne + @JoinColumn(name = "owner_id") + private User commisionOwner; + private Timestamp commisionDate; + + @OneToMany(mappedBy = "commision") + private List assignments; + + public Commision(User user) { + this.commisionDate = new Timestamp(System.currentTimeMillis()); + this.commisionOwner = user; + } + + public Commision() { + } + + public Long getId() { + return this.id; + } + + public Timestamp getCommisionDate() { + return commisionDate; + } + + public User getCommisionOwner() { + return commisionOwner; + } + +} diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/User.java b/buisnesslogic/src/main/java/com/plannaplan/entities/User.java index 1ed5cfb..f21db21 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/User.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/User.java @@ -77,4 +77,7 @@ public class User { this.name = name; } + public Long getId() { + return this.id; + } } \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/models/FileData.java b/buisnesslogic/src/main/java/com/plannaplan/models/FileData.java index 5461ef3..314e3f6 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/models/FileData.java +++ b/buisnesslogic/src/main/java/com/plannaplan/models/FileData.java @@ -4,22 +4,16 @@ import java.util.HashMap; import java.util.Iterator; import org.apache.poi.ss.usermodel.Row; -//dictionary is deprecated use hashmap instead public class FileData { private HashMap keys; private Iterator rows; - //why variable is called keys2, could it be more specific??? - //why use setters in constructor here instead of assignments??? - // thought that setters are used to change values in runtime public FileData(HashMap keys, Iterator rows) { this.keys = keys; this.rows = rows; } - - public Iterator getRows() { return rows; } diff --git a/buisnesslogic/src/main/java/com/plannaplan/repositories/AssignmentRepository.java b/buisnesslogic/src/main/java/com/plannaplan/repositories/AssignmentRepository.java new file mode 100755 index 0000000..929525b --- /dev/null +++ b/buisnesslogic/src/main/java/com/plannaplan/repositories/AssignmentRepository.java @@ -0,0 +1,18 @@ +package com.plannaplan.repositories; + +import java.util.List; + +import com.plannaplan.entities.Assignment; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +@Repository +public interface AssignmentRepository extends JpaRepository { + + @Query("FROM Assignment WHERE commision_id = ?1") + List getByCommision(@Param("commision_id") Long id); + +} \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/repositories/CommisionRepository.java b/buisnesslogic/src/main/java/com/plannaplan/repositories/CommisionRepository.java new file mode 100755 index 0000000..4f86813 --- /dev/null +++ b/buisnesslogic/src/main/java/com/plannaplan/repositories/CommisionRepository.java @@ -0,0 +1,20 @@ +package com.plannaplan.repositories; + +import java.util.List; + +import com.plannaplan.entities.Commision; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +@Repository +public interface CommisionRepository extends JpaRepository { + @Query("FROM Commision WHERE owner_id = ?1") + List getUsers(@Param("owner_id") Long id); + + @Query("FROM Commision WHERE owner_id = ?1 order by commisionDate desc") + List getNewestCommision(@Param("owner_id") Long id); + +} diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/AssignmentService.java b/buisnesslogic/src/main/java/com/plannaplan/services/AssignmentService.java new file mode 100755 index 0000000..734d554 --- /dev/null +++ b/buisnesslogic/src/main/java/com/plannaplan/services/AssignmentService.java @@ -0,0 +1,28 @@ +package com.plannaplan.services; + +import java.util.List; + +import com.plannaplan.entities.Assignment; +import com.plannaplan.entities.Commision; +import com.plannaplan.repositories.AssignmentRepository; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class AssignmentService { + @Autowired + private AssignmentRepository repo; + + public AssignmentService() { + super(); + } + + public void save(Assignment assignment) { + this.repo.save(assignment); + } + + public List getCommisionAssignments(Commision com) { + return this.repo.getByCommision(com.getId()); + } +} \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java b/buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java new file mode 100755 index 0000000..77700be --- /dev/null +++ b/buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java @@ -0,0 +1,35 @@ +package com.plannaplan.services; + +import java.util.List; +import java.util.Optional; + +import com.plannaplan.entities.Commision; +import com.plannaplan.entities.User; +import com.plannaplan.repositories.CommisionRepository; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class CommisionService { + @Autowired + private CommisionRepository repo; + + public CommisionService() { + } + + public Commision save(Commision commision) { + this.repo.save(commision); + return commision; + + } + + public List getUsersCommisions(User user) { + Long id = user.getId(); + return this.repo.getUsers(id); + } + + public Optional getNewestCommision(User user) { + return Optional.of(this.repo.getNewestCommision(user.getId()).get(0)); + } +} diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java b/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java index ff3c4d1..a01de30 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java @@ -37,4 +37,8 @@ public class GroupService { public int getGroupsAmmount() { return (int) this.repo.count(); } + + public Optional getGroupById(Long id) { + return this.repo.findById(id); + } } \ No newline at end of file diff --git a/restservice/src/main/java/com/plannaplan/App.java b/restservice/src/main/java/com/plannaplan/App.java index da176fa..b85cbb9 100755 --- a/restservice/src/main/java/com/plannaplan/App.java +++ b/restservice/src/main/java/com/plannaplan/App.java @@ -13,33 +13,48 @@ import org.springframework.context.event.EventListener; @SpringBootApplication public class App { - public final static String API_VERSION = "v1"; + public final static String API_VERSION = "v1"; - @Autowired - UserService userService; + @Autowired + UserService userService; - public static void main(String[] args) { - Logo logo = new Logo("beta"); - System.out.println(logo.getLogo()); - System.out.println( - "|=============================================================================================|"); - SpringApplication.run(App.class, args); - } + public static void main(String[] args) { + Logo logo = new Logo("beta"); + System.out.println(logo.getLogo()); + System.out.println( + "|=============================================================================================|"); + SpringApplication.run(App.class, args); + } - @EventListener(ApplicationReadyEvent.class) - public void importData() { - User testUser = new User(); - testUser.setEmail("notexisting@mail.comain"); - testUser.setName("Tom"); - testUser.setSurname("Riddle"); - testUser.setRole(UserRoles.TEST_USER); - this.userService.save(testUser); + @EventListener(ApplicationReadyEvent.class) + public void importData() { + User filip = new User(); + filip.setEmail("filizy@st.amu.edu.pl"); + filip.setName("Filip"); + filip.setSurname("Izydorczyk"); + filip.setRole(UserRoles.STUDENT); + this.userService.save(filip); - User filip = new User(); - filip.setEmail("filizy@st.amu.edu.pl"); - filip.setName("Filip"); - filip.setSurname("Izydorczyk"); - filip.setRole(UserRoles.STUDENT); - this.userService.save(filip); - } + User hub = new User(); + hub.setEmail("hubwrz1@st.amu.edu.pl"); + hub.setName("Hubert"); + hub.setSurname("Wrzesiński"); + hub.setRole(UserRoles.STUDENT); + this.userService.save(hub); + + User mac = new User(); + mac.setEmail("macglo2@st.amu.edu.pl"); + mac.setName("Maciej"); + mac.setSurname("Głowacki"); + mac.setRole(UserRoles.STUDENT); + this.userService.save(mac); + + User mar = new User(); + mar.setEmail("marwoz16@st.amu.edu.pl"); + mar.setName("Marcin"); + mar.setSurname("Woźniak"); + mar.setRole(UserRoles.STUDENT); + this.userService.save(mar); + + } } diff --git a/restservice/src/main/java/com/plannaplan/controllers/AssignmentsController.java b/restservice/src/main/java/com/plannaplan/controllers/AssignmentsController.java new file mode 100755 index 0000000..dcb26b9 --- /dev/null +++ b/restservice/src/main/java/com/plannaplan/controllers/AssignmentsController.java @@ -0,0 +1,66 @@ +package com.plannaplan.controllers; + +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.ArrayList; +import java.util.Dictionary; +import java.util.Hashtable; +import java.util.List; +import java.util.Optional; + +import com.plannaplan.App; +import com.plannaplan.entities.Assignment; +import com.plannaplan.entities.Commision; +import com.plannaplan.entities.Groups; +import com.plannaplan.entities.User; +import com.plannaplan.services.AssignmentService; +import com.plannaplan.services.CommisionService; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestMapping; + +@RestController +@CrossOrigin +@RequestMapping("/api/" + App.API_VERSION + "/assignments") +public class AssignmentsController extends TokenBasedController { + + @Autowired + private CommisionService commisionService; + + @Autowired + private AssignmentService assignmentService; + + @GetMapping("/getCurrentAssignments") + public ResponseEntity> getCurrentAssignments() throws Exception { + User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException()); + Optional com = this.commisionService.getNewestCommision(user); + if (com.isPresent()) { + List respone = this.assignmentService.getCommisionAssignments(com.get()); + + List finalResponse = new ArrayList<>(); + for (Assignment a : respone) { + Dictionary elem = new Hashtable<>(); + Dictionary groupInfo = new Hashtable<>(); + elem.put("id", a.getId()); + Groups g = a.getGroup(); + groupInfo.put("id", g.getId()); + groupInfo.put("day", g.getDay().label); + groupInfo.put("time", g.getTimeString()); + groupInfo.put("lecturer", g.getLecturer().toString()); + groupInfo.put("room", g.getRoom()); + groupInfo.put("capacity", g.getCapacity()); + groupInfo.put("type", g.getType()); + elem.put("group", groupInfo); + finalResponse.add(elem); + } + + return new ResponseEntity<>(finalResponse, HttpStatus.OK); + } + + return new ResponseEntity<>(null, HttpStatus.OK); + } +} diff --git a/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java b/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java new file mode 100755 index 0000000..f1be365 --- /dev/null +++ b/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java @@ -0,0 +1,66 @@ +package com.plannaplan.controllers; + +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +import com.plannaplan.App; +import com.plannaplan.entities.Assignment; +import com.plannaplan.entities.Commision; +import com.plannaplan.entities.Groups; +import com.plannaplan.entities.User; +import com.plannaplan.exceptions.UserNotFoundException; +import com.plannaplan.services.AssignmentService; +import com.plannaplan.services.CommisionService; +import com.plannaplan.services.GroupService; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; + +@RestController +@CrossOrigin +@RequestMapping("/api/" + App.API_VERSION + "/commisions") +public class CommisionController extends TokenBasedController { + + @Autowired + private CommisionService commisionService; + + @Autowired + private GroupService groupServcicxe; + + @Autowired + private AssignmentService assignmentService; + + public CommisionController() { + } + + @PostMapping("/add") + public ResponseEntity addCommision(@RequestBody List groups) throws UserNotFoundException { + + User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException()); + Commision com = new Commision(user); + this.commisionService.save(com); + + groups.stream().forEach((groupId) -> { + Groups group = this.groupServcicxe.getGroupById(groupId).orElseThrow(() -> new NullPointerException()); + Assignment a = new Assignment(group, com); + this.assignmentService.save(a); + }); + + return new ResponseEntity<>("Succes", HttpStatus.OK); + } + + @GetMapping("/getAllCommisions") + public ResponseEntity> getAlCommisions() throws UserNotFoundException { + User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException()); + List result = this.commisionService.getUsersCommisions(user); + return new ResponseEntity<>(result, HttpStatus.OK); + } + +} diff --git a/restservice/src/main/java/com/plannaplan/controllers/TokenBasedController.java b/restservice/src/main/java/com/plannaplan/controllers/TokenBasedController.java new file mode 100755 index 0000000..504ce33 --- /dev/null +++ b/restservice/src/main/java/com/plannaplan/controllers/TokenBasedController.java @@ -0,0 +1,30 @@ +package com.plannaplan.controllers; + +import java.util.Optional; + +import com.plannaplan.entities.User; +import com.plannaplan.exceptions.UserNotFoundException; +import com.plannaplan.services.UserService; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.authentication.AnonymousAuthenticationToken; + +public abstract class TokenBasedController { + @Autowired + protected UserService userService; + + public TokenBasedController() { + } + + protected Optional getCurrentUser() throws UserNotFoundException { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + + if (!(authentication instanceof AnonymousAuthenticationToken)) { + return Optional.of(this.userService.getUserByEmail(authentication.getName())); + } else { + return Optional.empty(); + } + } +} \ No newline at end of file diff --git a/restservice/src/main/java/com/plannaplan/security/AuthenticationProvider.java b/restservice/src/main/java/com/plannaplan/security/AuthenticationProvider.java index adbe57a..110098d 100755 --- a/restservice/src/main/java/com/plannaplan/security/AuthenticationProvider.java +++ b/restservice/src/main/java/com/plannaplan/security/AuthenticationProvider.java @@ -51,7 +51,7 @@ public class AuthenticationProvider extends AbstractUserDetailsAuthenticationPro @Override public String getUsername() { - return user.getName() + " " + user.getSurname(); + return user.getEmail(); } @Override