Hallo Leute,
Ich habe eine Frage zu folgendem Code.
Wie man sieht ist er recht groß geworden weil er ständig die selbe Logik verwendet.
Wie kann ich ihn am besten reduzieren bzw. kann ich eine Methode bauen um gewisse Vorgänge zu wiederholen?
Danke für eure Mühen!
Ich habe eine Frage zu folgendem Code.
Java:
public boolean synchronize(ManualSynchronizationForm form) {
List<String> modules = Arrays.asList(form.getCheckedEntities());
Collection<String> organizations = Arrays.asList(form.getCheckedOrganizations());
String fromDate = form.getFromDate();
String toDate = form.getToDate();
String customerIds = form.getCustomerId();
String branchIds = form.getBranchId();
String salesfloorIds = form.getSalesfloorId();
String customerNo = null;
String branchNo = null;
String salesfloorNo = null;
boolean result = true;
if (!StringUtils.isEmpty(customerIds) && !StringUtils.isEmpty(branchIds) && !StringUtils.isEmpty(salesfloorIds)) {
List<Customer> filteredCustomer = null;
CacheDS cache = null;
try {
cache = CacheService.getInstance().getCacheDS();
} catch (CacheServiceException e1) {
log.fatal(e1);
}
String[] custIds = customerIds.split(",");
for (final String customerId : custIds) {
Integer custId = StringUtils.isNumeric(customerId) ? Integer.valueOf(customerId) : null;
filteredCustomer = cache.getCustomers();
filteredCustomer = Customers.getCustomersForId(filteredCustomer, custId.intValue());
StringBuilder custNo = new StringBuilder();
for (Customer customer : filteredCustomer) {
custNo.append(customer.getNo());
custNo.append(",");
}
customerNo = custNo.substring(0, custNo.lastIndexOf(","));
}
List<Branch> filteredBranchs = null;
String[] branIds = branchIds.split(",");
for (final String branchId : branIds) {
Integer branId = StringUtils.isNumeric(branchId) ? Integer.valueOf(branchId) : null;
filteredBranchs = cache.getBranches();
filteredBranchs = Branches.getBranchesForId(filteredBranchs, branId.intValue());
StringBuilder branNo = new StringBuilder();
for (Branch branch : filteredBranchs) {
branNo.append(branch.getNo());
branNo.append(",");
}
customerNo = branNo.substring(0, branNo.lastIndexOf(","));
}
List<Salesfloor> filteredSalesfloors = null;
String[] sfloorIds = salesfloorIds.split(",");
for (final String salesfloorId : sfloorIds) {
Integer sfloorId = StringUtils.isNumeric(salesfloorId) ? Integer.valueOf(salesfloorId) : null;
filteredSalesfloors = cache.getSalesfloors();
filteredSalesfloors = Salesfloors.getSalesfloorsForId(filteredSalesfloors, sfloorId.intValue());
StringBuilder sfloorNo = new StringBuilder();
for (Salesfloor salesfloor : filteredSalesfloors) {
sfloorNo.append(salesfloor.getNo());
sfloorNo.append(",");
}
customerNo = sfloorNo.substring(0, sfloorNo.lastIndexOf(","));
}
}
if (!StringUtils.isEmpty(customerIds) && StringUtils.isEmpty(branchIds) && StringUtils.isEmpty(salesfloorIds)) {
final Set<Salesfloor> setSF = new HashSet<Salesfloor>();
final Set<Branch> setB = new HashSet<Branch>();
String[] custIds = customerIds.split(",");
CacheDS cache = null;
try {
cache = CacheService.getInstance().getCacheDS();
} catch (CacheServiceException e1) {
log.fatal(e1);
}
List<Salesfloor> filteredSalesfloors = null;
List<Branch> filteredBranches = null;
for (final String customerId : custIds) {
Integer custId = StringUtils.isNumeric(customerId) ? Integer.valueOf(customerId) : null;
if (filteredSalesfloors == null) {
filteredSalesfloors = new ArrayList<>();
}
if (filteredBranches == null) {
filteredBranches = new ArrayList<>();
}
if (custId != null) {
filteredSalesfloors = Salesfloors.getSalesfloorsForCustomerId(cache.getSalesfloors(),
custId.intValue());
filteredBranches = Branches.getBranchesForCustomerId(cache.getBranches(), custId.intValue());
}
if (filteredSalesfloors != null && !filteredSalesfloors.isEmpty()) {
setSF.addAll(filteredSalesfloors);
}
if (filteredBranches != null && !filteredBranches.isEmpty()) {
setB.addAll(filteredBranches);
}
if (filteredSalesfloors == null) {
log.fatal("Customer " + custId + " has no salesfloors");
return false;
}
}
StringBuilder branNo = new StringBuilder();
for (Branch branch : filteredBranches) {
branNo.append(branch.getNo());
branNo.append(",");
}
branchNo = branNo.substring(0, branNo.lastIndexOf(","));
StringBuilder sfloorNo = new StringBuilder();
for (Salesfloor salesfloor : filteredSalesfloors) {
sfloorNo.append(salesfloor.getNo());
sfloorNo.append(",");
}
salesfloorNo = sfloorNo.substring(0, sfloorNo.lastIndexOf(","));
List<Customer> filteredCustomer = null;
for (final String customerId : custIds) {
Integer custId = StringUtils.isNumeric(customerId) ? Integer.valueOf(customerId) : null;
filteredCustomer = cache.getCustomers();
filteredCustomer = Customers.getCustomersForId(filteredCustomer, custId.intValue());
StringBuilder custNo = new StringBuilder();
for (Customer customer : filteredCustomer) {
custNo.append(customer.getNo());
custNo.append(",");
}
customerNo = custNo.substring(0, custNo.lastIndexOf(","));
}
} else if (!StringUtils.isEmpty(customerIds) && !StringUtils.isEmpty(branchIds)
&& StringUtils.isEmpty(salesfloorIds)) {
final Set<Salesfloor> setSF = new HashSet<Salesfloor>();
String[] branIds = branchIds.split(",");
CacheDS cache = null;
try {
cache = CacheService.getInstance().getCacheDS();
} catch (CacheServiceException e1) {
log.fatal(e1);
}
// for Sf
List<Salesfloor> filteredSalesfloors = null;
for (final String branchId : branIds) {
Integer branId = StringUtils.isNumeric(branchId) ? Integer.valueOf(branchId) : null;
if (filteredSalesfloors == null) {
filteredSalesfloors = new ArrayList<>();
}
if (branIds != null) {
filteredSalesfloors = Salesfloors.getSalesfloorsForBranchId(cache.getSalesfloors(),
branId.intValue());
}
if (filteredSalesfloors != null && !filteredSalesfloors.isEmpty()) {
setSF.addAll(filteredSalesfloors);
}
if (filteredSalesfloors == null) {
log.fatal("Branch " + branId + " has no salesfloors");
return false;
}
}
List<Customer> filteredCustomer = null;
String[] custIds = customerIds.split(",");
for (final String customerId : custIds) {
Integer custId = StringUtils.isNumeric(customerId) ? Integer.valueOf(customerId) : null;
filteredCustomer = cache.getCustomers();
filteredCustomer = Customers.getCustomersForId(filteredCustomer, custId.intValue());
StringBuilder custNo = new StringBuilder();
for (Customer customer : filteredCustomer) {
custNo.append(customer.getNo());
custNo.append(",");
}
customerNo = custNo.substring(0, custNo.lastIndexOf(","));
List<Branch> filteredBranchs = null;
for (final String branchId : branIds) {
Integer branId = StringUtils.isNumeric(branchId) ? Integer.valueOf(branchId) : null;
filteredBranchs = cache.getBranches();
filteredBranchs = Branches.getBranchesForId(filteredBranchs, branId.intValue());
StringBuilder branNo = new StringBuilder();
for (Branch branch : filteredBranchs) {
branNo.append(branch.getNo());
branNo.append(",");
}
customerNo = branNo.substring(0, branNo.lastIndexOf(","));
}
StringBuilder sfloorNo = new StringBuilder();
for (Salesfloor salesfloor : filteredSalesfloors) {
sfloorNo.append(salesfloor.getNo());
sfloorNo.append(",");
}
salesfloorNo = sfloorNo.substring(0, sfloorNo.lastIndexOf(","));
}
}
Wie man sieht ist er recht groß geworden weil er ständig die selbe Logik verwendet.
Wie kann ich ihn am besten reduzieren bzw. kann ich eine Methode bauen um gewisse Vorgänge zu wiederholen?
Danke für eure Mühen!