From 110369db3b16a7db64f5e4ceae2453a322dd8d99 Mon Sep 17 00:00:00 2001 From: deepankar Date: Wed, 9 Oct 2019 13:42:07 +0530 Subject: [PATCH 1/3] committing initial code --- .../controller/BoxTrackingController.java | 66 +++++++++++++ .../easynotes/controller/IndexController.java | 2 +- .../easynotes/controller/NoteController.java | 62 ------------ .../java/com/example/easynotes/model/Box.java | 39 ++++++++ .../com/example/easynotes/model/BoxEvent.java | 98 +++++++++++++++++++ .../com/example/easynotes/model/BoxState.java | 42 ++++++++ .../com/example/easynotes/model/Location.java | 8 ++ .../com/example/easynotes/model/Note.java | 81 --------------- ...epository.java => BoxEventRepository.java} | 6 +- .../easynotes/repository/BoxRepository.java | 14 +++ .../repository/BoxStateRepository.java | 16 +++ src/main/resources/application.properties | 4 +- 12 files changed, 289 insertions(+), 149 deletions(-) create mode 100644 src/main/java/com/example/easynotes/controller/BoxTrackingController.java delete mode 100644 src/main/java/com/example/easynotes/controller/NoteController.java create mode 100644 src/main/java/com/example/easynotes/model/Box.java create mode 100644 src/main/java/com/example/easynotes/model/BoxEvent.java create mode 100644 src/main/java/com/example/easynotes/model/BoxState.java create mode 100644 src/main/java/com/example/easynotes/model/Location.java delete mode 100644 src/main/java/com/example/easynotes/model/Note.java rename src/main/java/com/example/easynotes/repository/{NoteRepository.java => BoxEventRepository.java} (54%) create mode 100644 src/main/java/com/example/easynotes/repository/BoxRepository.java create mode 100644 src/main/java/com/example/easynotes/repository/BoxStateRepository.java diff --git a/src/main/java/com/example/easynotes/controller/BoxTrackingController.java b/src/main/java/com/example/easynotes/controller/BoxTrackingController.java new file mode 100644 index 0000000..827c462 --- /dev/null +++ b/src/main/java/com/example/easynotes/controller/BoxTrackingController.java @@ -0,0 +1,66 @@ +package com.example.easynotes.controller; + +import com.example.easynotes.exception.ResourceNotFoundException; +import com.example.easynotes.model.BoxEvent; +import com.example.easynotes.repository.BoxEventRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; +import java.util.List; + +/** + * Created by DeepankarShukla. + */ +@RestController +@RequestMapping("/api") +public class BoxTrackingController { + + @Autowired + BoxEventRepository boxEventRepository; + + @Autowired + BoxEventRepository boxRepository; + + @Autowired + BoxEventRepository boxStateRepository; + + @GetMapping("/boxes") + public List getAllNotes() { + return boxEventRepository.findAll(); + } + + @PostMapping("/boxes") + public BoxEvent createNote(@Valid @RequestBody BoxEvent boxEvent) { + return boxEventRepository.save(boxEvent); + } + + @GetMapping("/notes/{id}") + public BoxEvent getNoteById(@PathVariable(value = "id") Long boxId) { + return boxEventRepository.findById(boxId) + .orElseThrow(() -> new ResourceNotFoundException("Note", "id", boxId)); + } + + @PutMapping("/notes/{id}") + public BoxEvent updateNote(@PathVariable(value = "id") Long boxId, + @Valid @RequestBody BoxEvent boxDetails) { + + BoxEvent boxEvent = boxEventRepository.findById(boxId) + .orElseThrow(() -> new ResourceNotFoundException("Note", "id", boxId)); + + + return boxEventRepository.save(boxEvent); + + } + + @DeleteMapping("/notes/{id}") + public ResponseEntity deleteNote(@PathVariable(value = "id") Long boxId) { + BoxEvent boxEvent = boxEventRepository.findById(boxId) + .orElseThrow(() -> new ResourceNotFoundException("Note", "id", boxId)); + + boxEventRepository.delete(boxEvent); + + return ResponseEntity.ok().build(); + } +} diff --git a/src/main/java/com/example/easynotes/controller/IndexController.java b/src/main/java/com/example/easynotes/controller/IndexController.java index f9c5ea4..9a5172c 100644 --- a/src/main/java/com/example/easynotes/controller/IndexController.java +++ b/src/main/java/com/example/easynotes/controller/IndexController.java @@ -11,6 +11,6 @@ public class IndexController { @GetMapping public String sayHello() { - return "Hello and Welcome to the EasyNotes application. You can create a new Note by making a POST request to /api/notes endpoint."; + return "Hello and Welcome to the Warehouse application. You can create a new Note by making a POST request to /api/notes endpoint."; } } diff --git a/src/main/java/com/example/easynotes/controller/NoteController.java b/src/main/java/com/example/easynotes/controller/NoteController.java deleted file mode 100644 index 52f2ef0..0000000 --- a/src/main/java/com/example/easynotes/controller/NoteController.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.example.easynotes.controller; - -import com.example.easynotes.exception.ResourceNotFoundException; -import com.example.easynotes.model.Note; -import com.example.easynotes.repository.NoteRepository; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; - -import javax.validation.Valid; -import java.util.List; - -/** - * Created by rajeevkumarsingh on 27/06/17. - */ -@RestController -@RequestMapping("/api") -public class NoteController { - - @Autowired - NoteRepository noteRepository; - - @GetMapping("/notes") - public List getAllNotes() { - return noteRepository.findAll(); - } - - @PostMapping("/notes") - public Note createNote(@Valid @RequestBody Note note) { - return noteRepository.save(note); - } - - @GetMapping("/notes/{id}") - public Note getNoteById(@PathVariable(value = "id") Long noteId) { - return noteRepository.findById(noteId) - .orElseThrow(() -> new ResourceNotFoundException("Note", "id", noteId)); - } - - @PutMapping("/notes/{id}") - public Note updateNote(@PathVariable(value = "id") Long noteId, - @Valid @RequestBody Note noteDetails) { - - Note note = noteRepository.findById(noteId) - .orElseThrow(() -> new ResourceNotFoundException("Note", "id", noteId)); - - note.setTitle(noteDetails.getTitle()); - note.setContent(noteDetails.getContent()); - - Note updatedNote = noteRepository.save(note); - return updatedNote; - } - - @DeleteMapping("/notes/{id}") - public ResponseEntity deleteNote(@PathVariable(value = "id") Long noteId) { - Note note = noteRepository.findById(noteId) - .orElseThrow(() -> new ResourceNotFoundException("Note", "id", noteId)); - - noteRepository.delete(note); - - return ResponseEntity.ok().build(); - } -} diff --git a/src/main/java/com/example/easynotes/model/Box.java b/src/main/java/com/example/easynotes/model/Box.java new file mode 100644 index 0000000..c810c4b --- /dev/null +++ b/src/main/java/com/example/easynotes/model/Box.java @@ -0,0 +1,39 @@ +package com.example.easynotes.model; + +import javax.persistence.Entity; +import javax.persistence.EntityListeners; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +@Entity +@Table(name = "BoxState") +@EntityListeners(AuditingEntityListener.class) +public class Box { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private Location location; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Location getLocation() { + return location; + } + + public void setLocation(Location location) { + this.location = location; + } + +} diff --git a/src/main/java/com/example/easynotes/model/BoxEvent.java b/src/main/java/com/example/easynotes/model/BoxEvent.java new file mode 100644 index 0000000..7c97b89 --- /dev/null +++ b/src/main/java/com/example/easynotes/model/BoxEvent.java @@ -0,0 +1,98 @@ +package com.example.easynotes.model; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +import javax.persistence.*; +import javax.validation.constraints.NotBlank; +import java.util.Date; + +/** + * Created by DeepankarShukla. + */ +@Entity +@Table(name = "BoxEvent") +@EntityListeners(AuditingEntityListener.class) +@JsonIgnoreProperties(value = {"location","fromBox","toBox","packageType"}, + allowGetters = true) +public class BoxEvent { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(nullable = false, updatable = false) + @Temporal(TemporalType.TIMESTAMP) + @CreatedDate + private Date timestamp; + + @NotBlank + private Location location; + + public String getPackageType() { + return packageType; + } + + public void setPackageType(String packageType) { + this.packageType = packageType; + } + + private String fromBox; + + private String toBox; + + private String packageType; + + private String state; + + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Date getTimestamp() { + return timestamp; + } + + public void setTimestamp(Date timestamp) { + this.timestamp = timestamp; + } + + public Location getLocation() { + return location; + } + + public void setLocation(Location location) { + this.location = location; + } + + public String getFromBox() { + return fromBox; + } + + public void setFromBox(String fromBox) { + this.fromBox = fromBox; + } + + public String getToBox() { + return toBox; + } + + public void setToBox(String toBox) { + this.toBox = toBox; + } + + +} diff --git a/src/main/java/com/example/easynotes/model/BoxState.java b/src/main/java/com/example/easynotes/model/BoxState.java new file mode 100644 index 0000000..153055e --- /dev/null +++ b/src/main/java/com/example/easynotes/model/BoxState.java @@ -0,0 +1,42 @@ +package com.example.easynotes.model; + +import javax.persistence.Entity; +import javax.persistence.EntityListeners; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + + +@Entity +@Table(name = "BoxState") +@EntityListeners(AuditingEntityListener.class) +public class BoxState { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + + private String insertionState; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getInsertionState() { + return insertionState; + } + + public void setInsertionState(String insertionState) { + this.insertionState = insertionState; + } + + +} diff --git a/src/main/java/com/example/easynotes/model/Location.java b/src/main/java/com/example/easynotes/model/Location.java new file mode 100644 index 0000000..ca5c8bc --- /dev/null +++ b/src/main/java/com/example/easynotes/model/Location.java @@ -0,0 +1,8 @@ +package com.example.easynotes.model; + +enum Location { +Location1, +Location2, +Location3, +Location4; +} \ No newline at end of file diff --git a/src/main/java/com/example/easynotes/model/Note.java b/src/main/java/com/example/easynotes/model/Note.java deleted file mode 100644 index 4f8b6b1..0000000 --- a/src/main/java/com/example/easynotes/model/Note.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.example.easynotes.model; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import org.springframework.data.annotation.CreatedDate; -import org.springframework.data.annotation.LastModifiedDate; -import org.springframework.data.jpa.domain.support.AuditingEntityListener; - -import javax.persistence.*; -import javax.validation.constraints.NotBlank; -import java.util.Date; - -/** - * Created by rajeevkumarsingh on 27/06/17. - */ -@Entity -@Table(name = "notes") -@EntityListeners(AuditingEntityListener.class) -@JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, - allowGetters = true) -public class Note { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - @NotBlank - private String title; - - @NotBlank - private String content; - - @Column(nullable = false, updatable = false) - @Temporal(TemporalType.TIMESTAMP) - @CreatedDate - private Date createdAt; - - @Column(nullable = false) - @Temporal(TemporalType.TIMESTAMP) - @LastModifiedDate - private Date updatedAt; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public String getContent() { - return content; - } - - public void setContent(String content) { - this.content = content; - } - - public Date getCreatedAt() { - return createdAt; - } - - public void setCreatedAt(Date createdAt) { - this.createdAt = createdAt; - } - - public Date getUpdatedAt() { - return updatedAt; - } - - public void setUpdatedAt(Date updatedAt) { - this.updatedAt = updatedAt; - } - -} diff --git a/src/main/java/com/example/easynotes/repository/NoteRepository.java b/src/main/java/com/example/easynotes/repository/BoxEventRepository.java similarity index 54% rename from src/main/java/com/example/easynotes/repository/NoteRepository.java rename to src/main/java/com/example/easynotes/repository/BoxEventRepository.java index 4b7b8b3..e5df887 100644 --- a/src/main/java/com/example/easynotes/repository/NoteRepository.java +++ b/src/main/java/com/example/easynotes/repository/BoxEventRepository.java @@ -1,14 +1,14 @@ package com.example.easynotes.repository; -import com.example.easynotes.model.Note; +import com.example.easynotes.model.BoxEvent; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; /** - * Created by rajeevkumarsingh on 27/06/17. + * Created by DeepankarShukla. */ @Repository -public interface NoteRepository extends JpaRepository { +public interface BoxEventRepository extends JpaRepository { } diff --git a/src/main/java/com/example/easynotes/repository/BoxRepository.java b/src/main/java/com/example/easynotes/repository/BoxRepository.java new file mode 100644 index 0000000..16f2b5c --- /dev/null +++ b/src/main/java/com/example/easynotes/repository/BoxRepository.java @@ -0,0 +1,14 @@ +package com.example.easynotes.repository; + +import com.example.easynotes.model.Box; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +/** + * Created by DeepankarShukla. + */ + +@Repository +public interface BoxRepository extends JpaRepository { + +} diff --git a/src/main/java/com/example/easynotes/repository/BoxStateRepository.java b/src/main/java/com/example/easynotes/repository/BoxStateRepository.java new file mode 100644 index 0000000..51b7771 --- /dev/null +++ b/src/main/java/com/example/easynotes/repository/BoxStateRepository.java @@ -0,0 +1,16 @@ +package com.example.easynotes.repository; + + +import com.example.easynotes.model.BoxState; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +/** + * Created by DeepankarShukla. + */ + +@Repository +public interface BoxStateRepository extends JpaRepository { + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index b0f06a8..9095f8b 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,7 +1,7 @@ ## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties) -spring.datasource.url = jdbc:mysql://localhost:3306/notes_app?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false +spring.datasource.url = jdbc:mysql://localhost:3306/warehouseapplication?useConfigs=maxPerformance&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useFastDateParsing=false&createDatabaseIfNotExist=true&useSSL=false spring.datasource.username = root -spring.datasource.password = callicoder +spring.datasource.password = 1234 ## Hibernate Properties From 163cb5bb42a2e35105f351a821b5afb3c1ef2a1c Mon Sep 17 00:00:00 2001 From: deepankar Date: Wed, 9 Oct 2019 14:05:41 +0530 Subject: [PATCH 2/3] committing initial code --- .../controller/BoxTrackingController.java | 51 +++++++++++++++---- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/example/easynotes/controller/BoxTrackingController.java b/src/main/java/com/example/easynotes/controller/BoxTrackingController.java index 827c462..b77ea07 100644 --- a/src/main/java/com/example/easynotes/controller/BoxTrackingController.java +++ b/src/main/java/com/example/easynotes/controller/BoxTrackingController.java @@ -1,8 +1,13 @@ package com.example.easynotes.controller; import com.example.easynotes.exception.ResourceNotFoundException; +import com.example.easynotes.model.Box; import com.example.easynotes.model.BoxEvent; +import com.example.easynotes.model.BoxState; import com.example.easynotes.repository.BoxEventRepository; +import com.example.easynotes.repository.BoxRepository; +import com.example.easynotes.repository.BoxStateRepository; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -21,29 +26,55 @@ public class BoxTrackingController { BoxEventRepository boxEventRepository; @Autowired - BoxEventRepository boxRepository; + BoxRepository boxRepository; @Autowired - BoxEventRepository boxStateRepository; + BoxStateRepository boxStateRepository; @GetMapping("/boxes") - public List getAllNotes() { + public List getAllBoxEvents() { return boxEventRepository.findAll(); } + + @GetMapping("/boxStates") + public List getAllBoxStates() { + return boxStateRepository.findAll(); + } @PostMapping("/boxes") - public BoxEvent createNote(@Valid @RequestBody BoxEvent boxEvent) { + public BoxEvent createBoxEvent(@Valid @RequestBody BoxEvent boxEvent) { return boxEventRepository.save(boxEvent); } + + @PostMapping("/boxState") + public BoxState createBoxState(@Valid @RequestBody BoxState boxState) { + return boxStateRepository.save(boxState); + } + + @GetMapping("/box") + public List getAllBoxes() { + return boxRepository.findAll(); + } + + @PostMapping("/box") + public Box createBox(@Valid @RequestBody Box box) { + return boxRepository.save(box); + } - @GetMapping("/notes/{id}") - public BoxEvent getNoteById(@PathVariable(value = "id") Long boxId) { + @GetMapping("/box/{id}") + public Box getBoxById(@PathVariable(value = "id") Long boxId) { + return boxRepository.findById(boxId) + .orElseThrow(() -> new ResourceNotFoundException("Note", "id", boxId)); + } + + @GetMapping("/boxes/{id}") + public BoxEvent getBoxEventById(@PathVariable(value = "id") Long boxId) { return boxEventRepository.findById(boxId) .orElseThrow(() -> new ResourceNotFoundException("Note", "id", boxId)); } - @PutMapping("/notes/{id}") - public BoxEvent updateNote(@PathVariable(value = "id") Long boxId, + @PutMapping("/boxes/{id}") + public BoxEvent updateBox(@PathVariable(value = "id") Long boxId, @Valid @RequestBody BoxEvent boxDetails) { BoxEvent boxEvent = boxEventRepository.findById(boxId) @@ -54,8 +85,8 @@ public BoxEvent updateNote(@PathVariable(value = "id") Long boxId, } - @DeleteMapping("/notes/{id}") - public ResponseEntity deleteNote(@PathVariable(value = "id") Long boxId) { + @DeleteMapping("/boxes/{id}") + public ResponseEntity deleteBox(@PathVariable(value = "id") Long boxId) { BoxEvent boxEvent = boxEventRepository.findById(boxId) .orElseThrow(() -> new ResourceNotFoundException("Note", "id", boxId)); From 7d92c4c1204911fedfeabc6257a7e5b27bd566f7 Mon Sep 17 00:00:00 2001 From: deepankar Date: Thu, 10 Oct 2019 05:03:20 +0530 Subject: [PATCH 3/3] final code commit --- pom.xml | 2 +- .../controller/BoxTrackingController.java | 343 ++++++++++++++---- .../java/com/example/easynotes/model/Box.java | 2 +- .../com/example/easynotes/model/BoxEvent.java | 48 ++- .../com/example/easynotes/model/Location.java | 2 +- .../repository/BoxEventRepository.java | 18 + .../repository/BoxStateRepository.java | 7 + .../examples/box/initialData/InitialData.java | 32 ++ .../examples/box/initialData/ResultData.java | 33 ++ src/main/resources/application.properties | 2 +- 10 files changed, 398 insertions(+), 91 deletions(-) create mode 100644 src/main/java/com/examples/box/initialData/InitialData.java create mode 100644 src/main/java/com/examples/box/initialData/ResultData.java diff --git a/pom.xml b/pom.xml index 9c59df4..9be4f31 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ jar easy-notes - Rest API for a Simple Note Taking Application + Rest API for a WareHouse Box Tracking org.springframework.boot diff --git a/src/main/java/com/example/easynotes/controller/BoxTrackingController.java b/src/main/java/com/example/easynotes/controller/BoxTrackingController.java index b77ea07..f7489b4 100644 --- a/src/main/java/com/example/easynotes/controller/BoxTrackingController.java +++ b/src/main/java/com/example/easynotes/controller/BoxTrackingController.java @@ -4,94 +4,293 @@ import com.example.easynotes.model.Box; import com.example.easynotes.model.BoxEvent; import com.example.easynotes.model.BoxState; +import com.example.easynotes.model.Location; import com.example.easynotes.repository.BoxEventRepository; import com.example.easynotes.repository.BoxRepository; import com.example.easynotes.repository.BoxStateRepository; +import com.examples.box.initialData.InitialData; +import com.examples.box.initialData.ResultData; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; + import org.springframework.http.ResponseEntity; +import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; /** * Created by DeepankarShukla. */ +@CrossOrigin @RestController @RequestMapping("/api") public class BoxTrackingController { - @Autowired - BoxEventRepository boxEventRepository; - - @Autowired - BoxRepository boxRepository; - - @Autowired - BoxStateRepository boxStateRepository; - - @GetMapping("/boxes") - public List getAllBoxEvents() { - return boxEventRepository.findAll(); - } - - @GetMapping("/boxStates") - public List getAllBoxStates() { - return boxStateRepository.findAll(); - } - - @PostMapping("/boxes") - public BoxEvent createBoxEvent(@Valid @RequestBody BoxEvent boxEvent) { - return boxEventRepository.save(boxEvent); - } - - @PostMapping("/boxState") - public BoxState createBoxState(@Valid @RequestBody BoxState boxState) { - return boxStateRepository.save(boxState); - } - - @GetMapping("/box") - public List getAllBoxes() { - return boxRepository.findAll(); - } - - @PostMapping("/box") - public Box createBox(@Valid @RequestBody Box box) { - return boxRepository.save(box); - } - - @GetMapping("/box/{id}") - public Box getBoxById(@PathVariable(value = "id") Long boxId) { - return boxRepository.findById(boxId) - .orElseThrow(() -> new ResourceNotFoundException("Note", "id", boxId)); - } - - @GetMapping("/boxes/{id}") - public BoxEvent getBoxEventById(@PathVariable(value = "id") Long boxId) { - return boxEventRepository.findById(boxId) - .orElseThrow(() -> new ResourceNotFoundException("Note", "id", boxId)); - } - - @PutMapping("/boxes/{id}") - public BoxEvent updateBox(@PathVariable(value = "id") Long boxId, - @Valid @RequestBody BoxEvent boxDetails) { - - BoxEvent boxEvent = boxEventRepository.findById(boxId) - .orElseThrow(() -> new ResourceNotFoundException("Note", "id", boxId)); - - - return boxEventRepository.save(boxEvent); - - } - - @DeleteMapping("/boxes/{id}") - public ResponseEntity deleteBox(@PathVariable(value = "id") Long boxId) { - BoxEvent boxEvent = boxEventRepository.findById(boxId) - .orElseThrow(() -> new ResourceNotFoundException("Note", "id", boxId)); - - boxEventRepository.delete(boxEvent); - - return ResponseEntity.ok().build(); - } + private static final Logger LOG = LoggerFactory.getLogger(BoxTrackingController.class); + + @Autowired + BoxEventRepository boxEventRepository; + + @Autowired + BoxRepository boxRepository; + + @Autowired + BoxStateRepository boxStateRepository; + + @GetMapping("/boxes") + public List getAllBoxEvents() { + return boxEventRepository.findAll(); + } + + @GetMapping("/boxStates") + public List getAllBoxStates() { + return boxStateRepository.findAll(); + } + + @PostMapping("/updateBoxes") + public BoxEvent createBoxEvent(@Valid @RequestBody BoxEvent boxEvent) { + if (!StringUtils.isEmpty(boxEvent.getToBox())) { + List boxStates = boxStateRepository.findBoxState(boxEvent.getFromBox()); + + if (!org.springframework.util.CollectionUtils.isEmpty(boxStates)) { + for (BoxState state : boxStates) { + + String insertionState = state.getInsertionState(); + if (boxEvent.getPackageType().equalsIgnoreCase("P")) { + if (insertionState.endsWith(boxEvent.getFromBox())) { + insertionState = insertionState + "<" + boxEvent.getToBox(); + state.setInsertionState(insertionState); + + } else { + return createErrorData("Box is alredy inside another box"); + } + + } else { + if (state.getInsertionState().contains(boxEvent.getToBox())) { + List lastElement = Arrays.asList(insertionState.split("<")); + if (lastElement.get(lastElement.size() - 1).equalsIgnoreCase(boxEvent.getToBox())) { + insertionState = insertionState.replaceAll("<" + boxEvent.getToBox(), ""); + } else { + return createErrorData( + "Box Can not be removed as this box is not inside mentioned box"); + } + updateRequestBoxLocations(boxEvent); + state.setInsertionState(insertionState); + } else { + return createErrorData("Box Can not be removed"); + } + + } + updateHierarchialBoxLocation(boxEvent, state); + boxStateRepository.save(state); + + } + } else if (boxEvent.getPackageType().equalsIgnoreCase("p")) { + BoxState state = new BoxState(); + state.setInsertionState(boxEvent.getFromBox() + "<" + boxEvent.getToBox()); + updateRequestBoxLocations(boxEvent); + boxStateRepository.save(state); + } else { + return createErrorData("Box Can not be removed"); + } + + } else { + Box frombox = boxRepository.findById(Long.parseLong(boxEvent.getFromBox())) + .orElseThrow(() -> new ResourceNotFoundException("Note", "id", boxEvent.getFromBox())); + frombox.setLocation(boxEvent.getLocation()); + boxRepository.save(frombox); + } + + return boxEventRepository.save(boxEvent); + } + + private BoxEvent createErrorData(String message) { + BoxEvent errorEvent = new BoxEvent(); + errorEvent.setStatus("fail"); + errorEvent.setErrorMessage(message); + return errorEvent; + } + + private void updateRequestBoxLocations(BoxEvent boxEvent) { + Box frombox = boxRepository.findById(Long.parseLong(boxEvent.getFromBox())) + .orElseThrow(() -> new ResourceNotFoundException("Note", "id", boxEvent.getFromBox())); + Box toBox = boxRepository.findById(Long.parseLong(boxEvent.getToBox())) + .orElseThrow(() -> new ResourceNotFoundException("Note", "id", boxEvent.getToBox())); + frombox.setLocation(boxEvent.getLocation()); + toBox.setLocation(boxEvent.getLocation()); + boxRepository.save(frombox); + boxRepository.save(toBox); + } + + private void updateHierarchialBoxLocation(BoxEvent boxEvent, BoxState state) { + List locationUpdateionList = Arrays.asList(state.getInsertionState().split("<")); + for (String originalBox : locationUpdateionList) { + Box originalBoxModel = boxRepository.findById(Long.parseLong(originalBox)) + .orElseThrow(() -> new ResourceNotFoundException("Note", "id", originalBox)); + originalBoxModel.setLocation(boxEvent.getLocation()); + boxRepository.save(originalBoxModel); + } + } + + @PostMapping("/boxState") + public BoxState createBoxState(@Valid @RequestBody BoxState boxState) { + return boxStateRepository.save(boxState); + } + + @GetMapping("/box") + public List getAllBoxes() { + return boxRepository.findAll(); + } + + @GetMapping("/search") + public ResultData search(@RequestParam(value = "boxId", required = false) String boxId, + @RequestParam(value = "timestamp", required = false) String timestamp) { + String pattern = "yyyy-MM-dd HH:mm:ss"; + SimpleDateFormat inputFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); + SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); + List boxEvents = new ArrayList<>(); + List uniqueBoxId = new ArrayList<>(); + if (!StringUtils.isEmpty(boxId)) { + List boxStates = boxStateRepository.findBoxState(boxId); + if (CollectionUtils.isEmpty(boxStates)) { + uniqueBoxId.add(boxId); + } + for (BoxState boxState : boxStates) { + uniqueBoxId.addAll(Arrays.asList(boxState.getInsertionState().split("<"))); + } + } + if (timestamp != null && !StringUtils.isEmpty(boxId)) { + try { + boxEvents = boxEventRepository.findBoxEventsByTimestampAndBoxId( + uniqueBoxId.stream().distinct().collect(Collectors.toList()), inputFormatter.parse(timestamp)); + } catch (ParseException e) { + LOG.info(e.getMessage()); + } + } else if (timestamp != null) { + try { + boxEvents = boxEventRepository.findBoxEventsByTimestamp(inputFormatter.parse(timestamp)); + } catch (ParseException e) { + LOG.info(e.getMessage()); + } + } else if (!StringUtils.isEmpty(boxId)) { + boxEvents = boxEventRepository.findBoxEvents(uniqueBoxId.stream().distinct().collect(Collectors.toList())); + } else { + boxEvents = boxEventRepository.findAll(); + } + return createResultData(boxId, simpleDateFormat, boxEvents); + + } + + private ResultData createResultData(String boxId, SimpleDateFormat simpleDateFormat, List boxEvents) { + ResultData results = new ResultData(); + List resultSummary = new ArrayList<>(); + List boxLocation = new ArrayList<>(); + List emptyBoxlIst = new ArrayList<>(); + for (BoxEvent event : boxEvents) { + String dateString = simpleDateFormat.format(event.getTimestamp()); + + if (StringUtils.isEmpty(event.getToBox())) { + resultSummary.add( + "Box " + event.getFromBox() + " is at " + event.getLocation().toString() + " at " + dateString); + } else if (event.getPackageType().toLowerCase().equalsIgnoreCase("P")) { + + if (!CollectionUtils.isEmpty(boxLocation) + && boxLocation.get(boxLocation.size() - 1).equalsIgnoreCase(event.getFromBox())) { + boxLocation.add(event.getToBox()); + } else { + boxLocation.add(event.getFromBox()); + boxLocation.add(event.getToBox()); + } + resultSummary.add("Box " + event.getFromBox() + " has been put inside Box " + event.getToBox() + " at " + + event.getLocation().toString() + " at " + dateString); + } else { + boxLocation.remove(event.getToBox()); + resultSummary.add("Box " + event.getFromBox() + " has been removed from " + event.getToBox() + " at " + + event.getLocation().toString() + " at " + dateString); + emptyBoxlIst.add(event.getToBox()); + } + } + List boxState = new ArrayList<>(); + + for (int index = 0; index < boxLocation.size() - 1; index++) { + + boxState.add("Box " + boxLocation.get(index) + " is inside Box " + boxLocation.get(index + 1)); + } + for (int index = 0; index < emptyBoxlIst.size(); index++) { + boxState.add("Box " + emptyBoxlIst.get(index) + " is empty"); + } + if (!StringUtils.isEmpty(boxId)) { + Box queriedBox = boxRepository.findById(Long.parseLong(boxId)) + .orElseThrow(() -> new ResourceNotFoundException("Note", "id", boxId)); + results.setCurrentLocation(queriedBox.getLocation()); + } + results.setBoxState(boxState); + results.setActions(resultSummary); + return results; + } + + @PostMapping("/box") + public Box createBox(@Valid @RequestBody Box box) { + return boxRepository.save(box); + } + + @GetMapping("/box/{id}") + public Box getBoxById(@PathVariable(value = "id") Long boxId) { + return boxRepository.findById(boxId).orElseThrow(() -> new ResourceNotFoundException("Note", "id", boxId)); + } + + @GetMapping("/boxes/{id}") + public BoxEvent getBoxEventById(@PathVariable(value = "id") Long boxId) { + return boxEventRepository.findById(boxId).orElseThrow(() -> new ResourceNotFoundException("Note", "id", boxId)); + } + + @PutMapping("/boxes/{id}") + public BoxEvent updateBox(@PathVariable(value = "id") Long boxId, @Valid @RequestBody BoxEvent boxDetails) { + + BoxEvent boxEvent = boxEventRepository.findById(boxId) + .orElseThrow(() -> new ResourceNotFoundException("Note", "id", boxId)); + + return boxEventRepository.save(boxEvent); + + } + + @DeleteMapping("/boxes/{id}") + public ResponseEntity deleteBox(@PathVariable(value = "id") Long boxId) { + BoxEvent boxEvent = boxEventRepository.findById(boxId) + .orElseThrow(() -> new ResourceNotFoundException("Note", "id", boxId)); + + boxEventRepository.delete(boxEvent); + + return ResponseEntity.ok().build(); + } + + @DeleteMapping("/boxesState/{id}") + public ResponseEntity deleteBoxState(@PathVariable(value = "id") Long boxId) { + BoxState boxState = boxStateRepository.findById(boxId) + .orElseThrow(() -> new ResourceNotFoundException("Note", "id", boxId)); + + boxStateRepository.delete(boxState); + + return ResponseEntity.ok().build(); + } + + @GetMapping("/initialData") + public InitialData getInitialData() { + InitialData initialData = new InitialData(); + initialData.setBoxes(boxRepository.findAll()); + initialData.setLocation(Arrays.asList(Location.values())); + return initialData; + } + } diff --git a/src/main/java/com/example/easynotes/model/Box.java b/src/main/java/com/example/easynotes/model/Box.java index c810c4b..914dc8c 100644 --- a/src/main/java/com/example/easynotes/model/Box.java +++ b/src/main/java/com/example/easynotes/model/Box.java @@ -10,7 +10,7 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener; @Entity -@Table(name = "BoxState") +@Table(name = "Box") @EntityListeners(AuditingEntityListener.class) public class Box { diff --git a/src/main/java/com/example/easynotes/model/BoxEvent.java b/src/main/java/com/example/easynotes/model/BoxEvent.java index 7c97b89..d4eb620 100644 --- a/src/main/java/com/example/easynotes/model/BoxEvent.java +++ b/src/main/java/com/example/easynotes/model/BoxEvent.java @@ -1,6 +1,8 @@ package com.example.easynotes.model; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import org.hibernate.annotations.CreationTimestamp; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; @@ -14,28 +16,17 @@ @Entity @Table(name = "BoxEvent") @EntityListeners(AuditingEntityListener.class) -@JsonIgnoreProperties(value = {"location","fromBox","toBox","packageType"}, - allowGetters = true) public class BoxEvent { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - @Column(nullable = false, updatable = false) - @Temporal(TemporalType.TIMESTAMP) - @CreatedDate + @Column(nullable = false, updatable = false) + @CreationTimestamp private Date timestamp; - @NotBlank - private Location location; - - public String getPackageType() { - return packageType; - } - - public void setPackageType(String packageType) { - this.packageType = packageType; - } + + private Location location; private String fromBox; @@ -44,8 +35,35 @@ public void setPackageType(String packageType) { private String packageType; private String state; + + private String status; + + private String errorMessage; + + public String getStatus() { + return status; + } + public void setStatus(String status) { + this.status = status; + } + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + + public String getPackageType() { + return packageType; + } + + public void setPackageType(String packageType) { + this.packageType = packageType; + } public String getState() { return state; } diff --git a/src/main/java/com/example/easynotes/model/Location.java b/src/main/java/com/example/easynotes/model/Location.java index ca5c8bc..0f20ff9 100644 --- a/src/main/java/com/example/easynotes/model/Location.java +++ b/src/main/java/com/example/easynotes/model/Location.java @@ -1,6 +1,6 @@ package com.example.easynotes.model; -enum Location { +public enum Location { Location1, Location2, Location3, diff --git a/src/main/java/com/example/easynotes/repository/BoxEventRepository.java b/src/main/java/com/example/easynotes/repository/BoxEventRepository.java index e5df887..e7a7e45 100644 --- a/src/main/java/com/example/easynotes/repository/BoxEventRepository.java +++ b/src/main/java/com/example/easynotes/repository/BoxEventRepository.java @@ -1,7 +1,15 @@ package com.example.easynotes.repository; import com.example.easynotes.model.BoxEvent; +import com.example.easynotes.model.BoxState; + +import java.util.Collection; +import java.util.Date; +import java.util.List; + 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; /** @@ -11,4 +19,14 @@ @Repository public interface BoxEventRepository extends JpaRepository { + @Query("SELECT bv FROM BoxEvent as bv WHERE bv.fromBox in :inParameter or bv.toBox in :inParameter") + List findBoxEvents(@Param("inParameter")Collection inParameter); + + @Query("SELECT bv FROM BoxEvent as bv WHERE bv.timestamp < :timestamp") + List findBoxEventsByTimestamp(@Param("timestamp")Date timestamp); + + @Query("SELECT bv FROM BoxEvent as bv WHERE (bv.fromBox in :inParameter or bv.toBox in :inParameter) and bv.timestamp < :timestamp ") + List findBoxEventsByTimestampAndBoxId(@Param("inParameter")Collection inParameter,@Param("timestamp")Date timestamp); + } + \ No newline at end of file diff --git a/src/main/java/com/example/easynotes/repository/BoxStateRepository.java b/src/main/java/com/example/easynotes/repository/BoxStateRepository.java index 51b7771..b2bf5aa 100644 --- a/src/main/java/com/example/easynotes/repository/BoxStateRepository.java +++ b/src/main/java/com/example/easynotes/repository/BoxStateRepository.java @@ -3,7 +3,11 @@ import com.example.easynotes.model.BoxState; +import java.util.List; + 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; /** @@ -12,5 +16,8 @@ @Repository public interface BoxStateRepository extends JpaRepository { + + @Query("SELECT bs FROM BoxState as bs WHERE bs.insertionState like %:boxId%") + List findBoxState(@Param("boxId")String boxId); } diff --git a/src/main/java/com/examples/box/initialData/InitialData.java b/src/main/java/com/examples/box/initialData/InitialData.java new file mode 100644 index 0000000..6f3e771 --- /dev/null +++ b/src/main/java/com/examples/box/initialData/InitialData.java @@ -0,0 +1,32 @@ +package com.examples.box.initialData; + +import java.util.List; + +import com.example.easynotes.model.Box; +import com.example.easynotes.model.Location; + +public class InitialData { + + private List boxes; + + private List location; + + public List getBoxes() { + return boxes; + } + + public void setBoxes(List boxes) { + this.boxes = boxes; + } + + public List getLocation() { + return location; + } + + public void setLocation(List location) { + this.location = location; + } + + + +} diff --git a/src/main/java/com/examples/box/initialData/ResultData.java b/src/main/java/com/examples/box/initialData/ResultData.java new file mode 100644 index 0000000..6842ee2 --- /dev/null +++ b/src/main/java/com/examples/box/initialData/ResultData.java @@ -0,0 +1,33 @@ +package com.examples.box.initialData; + +import java.util.List; + +import com.example.easynotes.model.Location; + +public class ResultData { + + List actions; + List boxState; + Location currentLocation; + public List getActions() { + return actions; + } + public void setActions(List actions) { + this.actions = actions; + } + public List getBoxState() { + return boxState; + } + public void setBoxState(List boxState) { + this.boxState = boxState; + } + public Location getCurrentLocation() { + return currentLocation; + } + public void setCurrentLocation(Location currentLocation) { + this.currentLocation = currentLocation; + } + + + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 9095f8b..3d6a35a 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,5 +1,5 @@ ## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties) -spring.datasource.url = jdbc:mysql://localhost:3306/warehouseapplication?useConfigs=maxPerformance&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useFastDateParsing=false&createDatabaseIfNotExist=true&useSSL=false +spring.datasource.url = jdbc:mysql://localhost:3306/warehouse?useConfigs=maxPerformance&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useFastDateParsing=false&createDatabaseIfNotExist=true&useSSL=false spring.datasource.username = root spring.datasource.password = 1234