Data services refactored
authorelgekko <vasary@elgekko.net>
Wed, 3 May 2023 20:20:07 +0000 (22:20 +0200)
committerelgekko <vasary@elgekko.net>
Wed, 3 May 2023 20:20:07 +0000 (22:20 +0200)
lis-services/src/main/java/hu/user/lis/services/data/AssociateServiceImplImpl.java [moved from lis-services/src/main/java/hu/user/lis/services/data/AssociateServiceImpl.java with 58% similarity]
lis-services/src/main/java/hu/user/lis/services/data/DataService.java [new file with mode: 0644]
lis-services/src/main/java/hu/user/lis/services/data/DataServiceImpl.java [new file with mode: 0644]
lis-services/src/main/java/hu/user/lis/services/data/InvoiceServiceImpl.java
lis-services/src/main/java/hu/user/lis/services/data/PartnerService.java
lis-services/src/main/java/hu/user/lis/services/data/PartnerServiceImpl.java
lis-services/src/main/java/hu/user/lis/services/data/ProjectAssociateServiceImpl.java
lis-services/src/main/java/hu/user/lis/services/data/ProjectService.java
lis-services/src/main/java/hu/user/lis/services/data/ProjectServiceImpl.java
lis-services/src/main/java/hu/user/lis/services/data/SupplierServiceImpl.java
lis-services/src/main/java/hu/user/lis/services/data/TreasuryServiceImpl.java

similarity index 58%
rename from lis-services/src/main/java/hu/user/lis/services/data/AssociateServiceImpl.java
rename to lis-services/src/main/java/hu/user/lis/services/data/AssociateServiceImplImpl.java
index 5127581085d2b76457bef6ee43ed93e1addc5f96..3b4237b6e83823f48650c97f0184d12d1d495d31 100644 (file)
@@ -1,6 +1,5 @@
 package hu.user.lis.services.data;
 
-import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import hu.user.lis.db.Associate;
 import lombok.extern.log4j.Log4j2;
@@ -9,13 +8,14 @@ import org.apache.commons.lang3.RandomUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import java.lang.reflect.Field;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.util.ArrayList;
 import java.util.List;
 
 @Service
 @Log4j2
-public class AssociateServiceImpl implements AssociateService {
+public class AssociateServiceImplImpl extends DataServiceImpl<Associate> implements AssociateService {
     @Autowired
     ObjectMapper mapper;
     @Autowired
@@ -60,45 +60,19 @@ public class AssociateServiceImpl implements AssociateService {
             String name = dataGeneratorService.faker().name().fullName();
             String login = "user" + (i + 1);
             String password = "password";
-            Associate entity = Associate.builder().active(true).id(id).name(name).login(login).password(password).build();
-            result.add(entity);
-        }
-        return result;
-    }
+            Double monthlyCost = BigDecimal.valueOf(RandomUtils.nextDouble(300000, 1500000))
+                    .setScale(2, RoundingMode.CEILING).doubleValue();
 
-    @Override
-    public Associate copy(Associate sourceEntity) {
-        Associate result = null;
-        try {
-            String json = toString(sourceEntity);
-            result = mapper.readValue(json, Associate.class);
-        } catch (JsonProcessingException e) {
-            log.catching(e);
-        }
-        return result;
-    }
 
-    @Override
-    public String toString(Associate sourceEntity) {
-        String result = null;
-        try {
-            result = mapper.writeValueAsString(sourceEntity);
-        } catch (JsonProcessingException e) {
-            log.catching(e);
-        }
-        return result;
-    }
-
-    @Override
-    public Associate copy(Associate sourceEntity, String property, Object value) {
-        Associate result = copy(sourceEntity);
-        Field field = null;
-        try {
-            field = result.getClass().getDeclaredField(property);
-            field.setAccessible(true);
-            field.set(result, value);
-        } catch (Exception e) {
-            log.catching(e);
+            Associate entity = Associate.builder()
+                    .active(true)
+                    .id(id)
+                    .name(name)
+                    .login(login)
+                    .password(password)
+                    .monthlyCost(monthlyCost)
+                    .build();
+            result.add(entity);
         }
         return result;
     }
diff --git a/lis-services/src/main/java/hu/user/lis/services/data/DataService.java b/lis-services/src/main/java/hu/user/lis/services/data/DataService.java
new file mode 100644 (file)
index 0000000..57ab0b1
--- /dev/null
@@ -0,0 +1,9 @@
+package hu.user.lis.services.data;
+
+public interface DataService<T> {
+    T copy(T sourceEntity);
+
+    T copy(T sourceEntity, String property, Object value);
+
+    String toString(T sourceEntity);
+}
diff --git a/lis-services/src/main/java/hu/user/lis/services/data/DataServiceImpl.java b/lis-services/src/main/java/hu/user/lis/services/data/DataServiceImpl.java
new file mode 100644 (file)
index 0000000..08c1d5d
--- /dev/null
@@ -0,0 +1,52 @@
+package hu.user.lis.services.data;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import lombok.extern.log4j.Log4j2;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import java.lang.reflect.Field;
+
+@Log4j2
+public class DataServiceImpl<T> implements DataService<T> {
+    @Autowired
+    ObjectMapper mapper;
+
+    @Override
+    public T copy(T sourceEntity) {
+        T result = null;
+        try {
+            String json = toString(sourceEntity);
+            result = (T) mapper.readValue(json, sourceEntity.getClass());
+        } catch (JsonProcessingException e) {
+            log.catching(e);
+        }
+        return result;
+    }
+
+    @Override
+    public T copy(T sourceEntity, String property, Object value) {
+        T result = copy(sourceEntity);
+        Field field = null;
+        try {
+            field = result.getClass().getDeclaredField(property);
+            field.setAccessible(true);
+            field.set(result, value);
+        } catch (Exception e) {
+            log.catching(e);
+        }
+        return result;
+    }
+
+    @Override
+    public String toString(T sourceEntity) {
+        String result = null;
+        try {
+            result = mapper.writeValueAsString(sourceEntity);
+        } catch (JsonProcessingException e) {
+            log.catching(e);
+        }
+        return result;
+    }
+
+}
index 1b85825d86faabc92411d7cb3c07da8b87539954..9854630f805750b1c0bbc1cafc0f7f6a27775cae 100644 (file)
@@ -1,6 +1,5 @@
 package hu.user.lis.services.data;
 
-import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import hu.user.lis.db.Currency;
 import hu.user.lis.db.Invoice;
@@ -11,7 +10,6 @@ import org.apache.commons.lang3.RandomUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import java.lang.reflect.Field;
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
@@ -23,7 +21,7 @@ import java.util.concurrent.TimeUnit;
 
 @Service
 @Log4j2
-public class InvoiceServiceImpl implements InvoiceService {
+public class InvoiceServiceImpl extends DataServiceImpl<Invoice> implements InvoiceService {
     int GENERATE_COUNT = 1000;
     @Autowired
     DataGeneratorService dataGeneratorService;
@@ -770,41 +768,4 @@ public class InvoiceServiceImpl implements InvoiceService {
         return result;
     }
 
-    @Override
-    public Invoice copy(Invoice sourceEntity) {
-        Invoice result = null;
-        try {
-            String json = toString(sourceEntity);
-            result = mapper.readValue(json, Invoice.class);
-        } catch (JsonProcessingException e) {
-            log.catching(e);
-        }
-        return result;
-    }
-
-    @Override
-    public String toString(Invoice sourceEntity) {
-        String result = null;
-        try {
-            result = mapper.writeValueAsString(sourceEntity);
-        } catch (JsonProcessingException e) {
-            log.catching(e);
-        }
-        return result;
-    }
-
-    @Override
-    public Invoice copy(Invoice sourceEntity, String property, Object value) {
-        Invoice result = copy(sourceEntity);
-        Field field = null;
-        try {
-            field = result.getClass().getDeclaredField(property);
-            field.setAccessible(true);
-            field.set(result, value);
-        } catch (Exception e) {
-            log.catching(e);
-        }
-        return result;
-    }
-
 }
index 8743cc299cd5630a20dfdeb0a3740da98dc25d80..84301235b0864b2bb70cd1f43a97afa55eabe8e4 100644 (file)
@@ -12,6 +12,8 @@ public interface PartnerService {
 
     void add(Partner entity);
 
+    Partner getById(String id);
+
     Partner copy(Partner sourceEntity) throws JsonProcessingException;
 
     void replace(Partner targetEntity, Partner replacementEntity);
index 457650c5e9c6a2a4b12bb8eede25ebfe08f3a335..b30d22403c618c6b94560b2f2aaa7af7606c5c9f 100644 (file)
@@ -1,6 +1,5 @@
 package hu.user.lis.services.data;
 
-import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.github.javafaker.Address;
 import hu.user.lis.db.Partner;
@@ -10,13 +9,12 @@ import org.apache.commons.lang3.RandomUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import java.lang.reflect.Field;
 import java.util.ArrayList;
 import java.util.List;
 
 @Service
 @Log4j2
-public class PartnerServiceImpl implements PartnerService {
+public class PartnerServiceImpl extends DataServiceImpl<Partner> implements PartnerService {
     @Autowired
     ObjectMapper mapper;
     @Autowired
@@ -54,6 +52,11 @@ public class PartnerServiceImpl implements PartnerService {
         return getAll().get(RandomUtils.nextInt(0, entities.size()));
     }
 
+    @Override
+    public Partner getById(String id) {
+        return getAll().stream().filter(p -> p.getId().equals(id)).findFirst().get();
+    }
+
     private List<Partner> generate() {
         List<Partner> result = new ArrayList<>();
         int count = RandomUtils.nextInt(5, 10);
@@ -182,41 +185,4 @@ public class PartnerServiceImpl implements PartnerService {
         return result;
     }
 
-    @Override
-    public Partner copy(Partner sourceEntity) {
-        Partner result = null;
-        try {
-            String json = toString(sourceEntity);
-            result = mapper.readValue(json, Partner.class);
-        } catch (JsonProcessingException e) {
-            log.catching(e);
-        }
-        return result;
-    }
-
-    @Override
-    public String toString(Partner sourceEntity) {
-        String result = null;
-        try {
-            result = mapper.writeValueAsString(sourceEntity);
-        } catch (JsonProcessingException e) {
-            log.catching(e);
-        }
-        return result;
-    }
-
-    @Override
-    public Partner copy(Partner sourceEntity, String property, Object value) {
-        Partner result = copy(sourceEntity);
-        Field field = null;
-        try {
-            field = result.getClass().getDeclaredField(property);
-            field.setAccessible(true);
-            field.set(result, value);
-        } catch (Exception e) {
-            log.catching(e);
-        }
-        return result;
-    }
-
 }
index 460f1ecd8a0d6ae1d6cf5efa7eae5178a526284c..c67689354da5817388207d395b5510510d612952 100644 (file)
@@ -1,6 +1,5 @@
 package hu.user.lis.services.data;
 
-import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import hu.user.lis.db.Associate;
 import hu.user.lis.db.Project;
@@ -17,7 +16,7 @@ import java.util.stream.Collectors;
 
 @Service
 @Log4j2
-public class ProjectAssociateServiceImpl implements ProjectAssociateService {
+public class ProjectAssociateServiceImpl extends DataServiceImpl<ProjectAssociate> implements ProjectAssociateService {
     @Autowired
     ObjectMapper mapper;
     @Autowired
@@ -75,15 +74,4 @@ public class ProjectAssociateServiceImpl implements ProjectAssociateService {
         return result;
     }
 
-    @Override
-    public String toString(ProjectAssociate sourceEntity) {
-        String result = null;
-        try {
-            result = mapper.writeValueAsString(sourceEntity);
-        } catch (JsonProcessingException e) {
-            log.catching(e);
-        }
-        return result;
-    }
-
 }
index d5f1e649b1d9f59f8198dc41ba33f48d47b284c3..b1f1a7dbc842c89cc3ba9f5368dd967c305a792f 100644 (file)
@@ -15,8 +15,6 @@ public interface ProjectService {
 
     void replace(Project targetEntity, Project replacementEntity);
 
-    boolean isInvalid(Project entity);
-
     String toString(Project sourceEntity);
 
     Project copy(Project sourceEntity, String property, Object value);
index 1c073c8cd61697b09660db5dbd89d0575191c22a..a83b16f158e741e5a060bee58f3054f7421acb2b 100644 (file)
@@ -1,27 +1,23 @@
 package hu.user.lis.services.data;
 
-import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import hu.user.lis.db.Partner;
 import hu.user.lis.db.Project;
 import lombok.extern.log4j.Log4j2;
 import org.apache.commons.lang3.RandomStringUtils;
-import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import java.lang.reflect.Field;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
-import java.util.Objects;
 import java.util.concurrent.TimeUnit;
 
 @Service
 @Log4j2
-public class ProjectServiceImpl implements ProjectService {
+public class ProjectServiceImpl extends DataServiceImpl<Project> implements ProjectService {
     @Autowired
     ObjectMapper mapper;
     @Autowired
@@ -181,54 +177,4 @@ public class ProjectServiceImpl implements ProjectService {
         return result;
     }
 
-    @Override
-    public boolean isInvalid(Project entity) {
-        if (StringUtils.isBlank(entity.getName())) {
-            return true;
-        }
-        if (StringUtils.isBlank(entity.getContactName())) {
-            return true;
-        }
-        if (Objects.isNull(entity.getPartner())) {
-            return true;
-        }
-        return false;
-    }
-
-    @Override
-    public Project copy(Project sourceEntity) {
-        Project result = null;
-        try {
-            String json = toString(sourceEntity);
-            result = mapper.readValue(json, Project.class);
-        } catch (JsonProcessingException e) {
-            log.catching(e);
-        }
-        return result;
-    }
-
-    @Override
-    public String toString(Project sourceEntity) {
-        String result = null;
-        try {
-            result = mapper.writeValueAsString(sourceEntity);
-        } catch (JsonProcessingException e) {
-            log.catching(e);
-        }
-        return result;
-    }
-
-    @Override
-    public Project copy(Project sourceEntity, String property, Object value) {
-        Project result = copy(sourceEntity);
-        Field field = null;
-        try {
-            field = result.getClass().getDeclaredField(property);
-            field.setAccessible(true);
-            field.set(result, value);
-        } catch (Exception e) {
-            log.catching(e);
-        }
-        return result;
-    }
 }
index 3913e1ca77b3912c1deeabf667145e1088565c0e..5e9f7752ec25c84ad01a8cdf2b83e493d7bd2c66 100644 (file)
@@ -9,10 +9,10 @@ import java.util.ArrayList;
 import java.util.List;
 
 @Service
-public class SupplierServiceImpl implements SupplierService {
-    private List<Supplier> suppliers;
+public class SupplierServiceImpl extends DataServiceImpl<Supplier> implements SupplierService {
     @Autowired
     DataGeneratorService dataGeneratorService;
+    private List<Supplier> suppliers;
 
     @Override
     public List<Supplier> getAll() {
index 4e72d5a8fc1185a212fddef1da046447f1fa16b3..8cd9bef9109460c7e9b7f11965b22f7781d36279 100644 (file)
@@ -1,6 +1,5 @@
 package hu.user.lis.services.data;
 
-import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import hu.user.lis.db.Currency;
 import hu.user.lis.db.Treasury;
@@ -10,7 +9,6 @@ import org.apache.commons.lang3.RandomUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import java.lang.reflect.Field;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
@@ -21,7 +19,7 @@ import java.util.concurrent.TimeUnit;
 
 @Service
 @Log4j2
-public class TreasuryServiceImpl implements TreasuryService {
+public class TreasuryServiceImpl extends DataServiceImpl<Treasury> implements TreasuryService {
     int GENERATE_COUNT = 500;
     @Autowired
     DataGeneratorService dataGeneratorService;
@@ -85,48 +83,10 @@ public class TreasuryServiceImpl implements TreasuryService {
         int count = RandomUtils.nextInt(2, 5);
         List<Treasury> result = new ArrayList<>();
         for (int i = 0; i < count; i++) {
-            int index = RandomUtils.nextInt(0, GENERATE_COUNT);
+            int index = RandomUtils.nextInt(0, entities.size());
             result.add(entities.get(index));
             entities.remove(index);
         }
         return result;
     }
-
-    @Override
-    public Treasury copy(Treasury sourceEntity) {
-        Treasury result = null;
-        try {
-            String json = toString(sourceEntity);
-            result = mapper.readValue(json, Treasury.class);
-        } catch (JsonProcessingException e) {
-            log.catching(e);
-        }
-        return result;
-    }
-
-    @Override
-    public String toString(Treasury sourceEntity) {
-        String result = null;
-        try {
-            result = mapper.writeValueAsString(sourceEntity);
-        } catch (JsonProcessingException e) {
-            log.catching(e);
-        }
-        return result;
-    }
-
-    @Override
-    public Treasury copy(Treasury sourceEntity, String property, Object value) {
-        Treasury result = copy(sourceEntity);
-        Field field = null;
-        try {
-            field = result.getClass().getDeclaredField(property);
-            field.setAccessible(true);
-            field.set(result, value);
-        } catch (Exception e) {
-            log.catching(e);
-        }
-        return result;
-    }
-
 }