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;
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
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;
}
--- /dev/null
+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);
+}
--- /dev/null
+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;
+ }
+
+}
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;
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;
@Service
@Log4j2
-public class InvoiceServiceImpl implements InvoiceService {
+public class InvoiceServiceImpl extends DataServiceImpl<Invoice> implements InvoiceService {
int GENERATE_COUNT = 1000;
@Autowired
DataGeneratorService dataGeneratorService;
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;
- }
-
}
void add(Partner entity);
+ Partner getById(String id);
+
Partner copy(Partner sourceEntity) throws JsonProcessingException;
void replace(Partner targetEntity, Partner replacementEntity);
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;
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
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);
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;
- }
-
}
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;
@Service
@Log4j2
-public class ProjectAssociateServiceImpl implements ProjectAssociateService {
+public class ProjectAssociateServiceImpl extends DataServiceImpl<ProjectAssociate> implements ProjectAssociateService {
@Autowired
ObjectMapper mapper;
@Autowired
return result;
}
- @Override
- public String toString(ProjectAssociate sourceEntity) {
- String result = null;
- try {
- result = mapper.writeValueAsString(sourceEntity);
- } catch (JsonProcessingException e) {
- log.catching(e);
- }
- return result;
- }
-
}
void replace(Project targetEntity, Project replacementEntity);
- boolean isInvalid(Project entity);
-
String toString(Project sourceEntity);
Project copy(Project sourceEntity, String property, Object value);
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
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;
- }
}
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() {
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;
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;
@Service
@Log4j2
-public class TreasuryServiceImpl implements TreasuryService {
+public class TreasuryServiceImpl extends DataServiceImpl<Treasury> implements TreasuryService {
int GENERATE_COUNT = 500;
@Autowired
DataGeneratorService dataGeneratorService;
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;
- }
-
}