Skip to content

Commit

Permalink
Done application Opject and add attribute description entity
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunhThanhDe committed Apr 18, 2023
1 parent 63c20fc commit 15ef3a3
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 31 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<name>tms</name>
<description>project Terminal Management System</description>
<properties>
<java.version>8</java.version>
<java.version>11</java.version>
<mysql.version>5.1.30</mysql.version>
</properties>
<dependencies>
Expand Down
54 changes: 51 additions & 3 deletions src/main/java/com/vnptt/tms/api/ApplicationApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@CrossOrigin
@RestController
@RequestMapping("TMS/api")
Expand All @@ -17,17 +20,50 @@ public class ApplicationApi {
private IApplicationService applicationService;

@GetMapping(value = "/application")
public ApplicationOutput showApplication(@RequestParam(value = "page", required = false) Integer page,
@RequestParam(value = "limit", required = false) Integer limit) {
public ApplicationOutput showApplicationes(@RequestParam(value = "page", required = false) Integer page,
@RequestParam(value = "limit", required = false) Integer limit,
@RequestParam(value = "packagename", required = false) String packagename) {
ApplicationOutput result = new ApplicationOutput();
if (page != null && limit != null){
result.setPage(page);
Pageable pageable = PageRequest.of(page -1, limit );
result.setListResult((applicationService.findAll(pageable)));
result.setTotalPage((int) Math.ceil((double) applicationService.totalItem()/ limit));
} else if(packagename != null){
result.setListResult(applicationService.findByPackagename(packagename));
} else {
result.setListResult(applicationService.findAll());
}
if (result.getListResult().size() >= 1){
result.setMessage("Request Success");
result.setTotalElement(result.getListResult().size());
} else {
result.setMessage("no matching element found");
}
return result;
}


@GetMapping("/device/{deviceId}/application")
public ApplicationOutput getAllApplicationByDeviceEntityId(@PathVariable(value = "deviceId") Long deviceId,
@RequestParam(value = "page", required = false) Integer page,
@RequestParam(value = "limit", required = false) Integer limit) {
ApplicationOutput result = new ApplicationOutput();
if (page != null && limit != null){
result.setPage(page);
Pageable pageable = PageRequest.of(page -1, limit );
result.setListResult((applicationService.findAll(pageable)));
result.setTotalPage((int) Math.ceil((double) applicationService.totalItem()/ limit));
} else {
result.setListResult(applicationService.findAllOnDevice(deviceId));
}

if (result.getListResult().size() >= 1){
result.setMessage("Request Success");
result.setTotalElement(result.getListResult().size());
} else {
result.setMessage("no matching element found");
}
return result;
}

Expand All @@ -36,14 +72,26 @@ public ApplicationDTO createApplication(@RequestBody ApplicationDTO model) {
return applicationService.save(model);
}

@PostMapping(value = "/device/{deviceId}/application")
public ApplicationDTO addApplicationToDevice(@PathVariable(value = "deviceId") Long deviceId, @RequestBody ApplicationDTO model) {
return applicationService.addAppToDevice(deviceId, model);
}

@PutMapping(value = "/application/{id}")
public ApplicationDTO updateApplication(@RequestBody ApplicationDTO model, @PathVariable("id") Long id) {
model.setId(id);
return applicationService.save(model);
}

@DeleteMapping(value = "/application")
public void updateApplication(@RequestBody Long[] ids) {
public void deleteApplication(@RequestBody Long[] ids) {
applicationService.delete(ids);
}

@DeleteMapping(value = "/device/{deviceId}/application/{applicationId}")
public ResponseEntity<HttpStatus> removeApplicationOnDevice(@PathVariable(value = "deviceId") Long deviceId,
@PathVariable(value = "applicationId") Long applicationId){
applicationService.removeAppOnDevice(deviceId, applicationId);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
1 change: 1 addition & 0 deletions src/main/java/com/vnptt/tms/api/DeviceApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class DeviceApi {
@Autowired
private IDeviceService deviceService;


@GetMapping(value = "/device")
public DeviceOutput showDevice(@RequestParam(value = "page", required = false) Integer page,
@RequestParam(value = "limit", required = false) Integer limit) {
Expand Down
26 changes: 22 additions & 4 deletions src/main/java/com/vnptt/tms/api/output/AbstractOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
public class AbstractOutput<T> {
private int page;
private int totalPage;
private List<T> T = new ArrayList<>();
private Integer totalElement;
private String message;
private List<T> listResult = new ArrayList<>();

public int getPage() {
return page;
Expand All @@ -26,11 +28,27 @@ public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public Integer getTotalElement() {
return totalElement;
}

public void setTotalElement(Integer totalElement) {
this.totalElement = totalElement;
}

public List<T> getListResult() {
return T;
return listResult;
}

public void setListResult(List<T> t) {
T = t;
public void setListResult(List<T> listResult) {
this.listResult = listResult;
}
}
1 change: 0 additions & 1 deletion src/main/java/com/vnptt/tms/dto/PolicyDTO.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.vnptt.tms.dto;

import java.sql.Timestamp;

public class PolicyDTO extends AbstractDTO<PolicyDTO>{

Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/vnptt/tms/entity/ApkEntity.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.vnptt.tms.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -21,7 +18,11 @@ public class ApkEntity extends BaseEntity{
@Column(name = "packagesize", nullable = false)
private Long packagesize;

@ManyToMany(mappedBy = "apkEntitiesPolicy")
@ManyToMany(fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
},mappedBy = "apkEntitiesPolicy")
private List<PolicyEntity> policyEntities = new ArrayList<>();

public String getPackagename() {
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/com/vnptt/tms/entity/ApplicationEntity.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package com.vnptt.tms.entity;

import javax.persistence.OneToMany;
import javax.persistence.ManyToMany;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Table;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -18,7 +12,11 @@ public class ApplicationEntity extends BaseEntity{
@Column(name = "version", nullable = false)
private int version;

@ManyToMany()
@ManyToMany(fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
})
@JoinTable(name = "device_application",
joinColumns = @JoinColumn(name = "application_id"),
inverseJoinColumns = @JoinColumn(name = "device_id"))
Expand Down
32 changes: 25 additions & 7 deletions src/main/java/com/vnptt/tms/entity/DeviceEntity.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.vnptt.tms.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.OneToMany;
import javax.persistence.*;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -33,10 +29,18 @@ public class DeviceEntity extends BaseEntity{
@Column(name = "desciption", length = 2000)
private String desciption;

@ManyToMany(mappedBy = "deviceEntitiesApplication")
@ManyToMany(fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
},mappedBy = "deviceEntitiesApplication")
private List<ApplicationEntity> applicationEntities = new ArrayList<>();

@OneToMany(mappedBy = "deviceEntityDetail")
@OneToMany(fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
},mappedBy = "deviceEntityDetail")
private List<DevicePolicyDetailEntity> devicePolicyDetailEntities = new ArrayList<>();

@OneToMany(mappedBy = "deviceEntityHistory")
Expand Down Expand Up @@ -145,4 +149,18 @@ public List<HistoryPerformanceEntity> getHistoryPerformanceEntities() {
public void setHistoryPerformanceEntities(List<HistoryPerformanceEntity> historyPerformanceEntities) {
this.historyPerformanceEntities = historyPerformanceEntities;
}

public void addApplication(ApplicationEntity applicationEntity) {
this.applicationEntities.add(applicationEntity);
applicationEntity.getDeviceEntitiesApplication().add(this);
}

public void removeApplication(long applicationId) {
ApplicationEntity applicationEntity = this.applicationEntities.stream().filter(app -> app.getId() == applicationId).findFirst().orElse(null);
if (applicationEntity != null) {
this.applicationEntities.remove(applicationEntity);
applicationEntity.getDeviceEntitiesApplication().remove(this);
}
}

}
7 changes: 6 additions & 1 deletion src/main/java/com/vnptt/tms/entity/PolicyEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ public class PolicyEntity extends BaseEntity{
@JoinColumn(name = "commandId", nullable = false)
private CommandEntity commandEntity;

@ManyToMany
@ManyToMany(fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
})
@JoinTable(name = "apkPolicy",
joinColumns = @JoinColumn(name = "policy_id"),
inverseJoinColumns = @JoinColumn(name = "apk_id"))
Expand All @@ -26,6 +30,7 @@ public class PolicyEntity extends BaseEntity{
@OneToMany(mappedBy = "policyEntityDetail")
private List<DevicePolicyDetailEntity> devicePolicyDetailEntities = new ArrayList<>();


public String getPolicyname() {
return policyname;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
import com.vnptt.tms.entity.ApplicationEntity;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;


public interface ApplicationRepository extends JpaRepository<ApplicationEntity, Long> {

ApplicationEntity findOneById(Long id);

List<ApplicationEntity> findApplicationEntitiesByDeviceEntitiesApplicationId(Long deviceEntitiesApplicationId);

List<ApplicationEntity> findByPackagenameContaining(String packagename);
}
15 changes: 15 additions & 0 deletions src/main/java/com/vnptt/tms/service/IApplicationService.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
package com.vnptt.tms.service;

import com.vnptt.tms.dto.ApplicationDTO;
import com.vnptt.tms.entity.ApplicationEntity;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;

import java.util.List;

public interface IApplicationService {
ApplicationDTO save(ApplicationDTO applicationDTO);

ApplicationDTO findOne(Long id);

int totalItem();

void delete(Long[] ids);

List<ApplicationDTO> findAll(Pageable pageable);

List<ApplicationDTO> findAll();

List<ApplicationDTO> findAllOnDevice(Long deviceId);

ApplicationDTO addAppToDevice(Long deviceId, ApplicationDTO model);

void removeAppOnDevice(Long deviceId, Long applicationId);

List<ApplicationDTO> findByPackagename(String packagename);
}
Loading

0 comments on commit 15ef3a3

Please sign in to comment.