public List<Product> getNotLinkedProducts(String clientId, Integer providerId, String words) { List<LinkedProductEntry> entries = linkedEntryRepository.find(new LinkedEntryCondition(providerId)); Set<String> linkedNames = new HashSet<String>(); for (LinkedProductEntry entry : entries) linkedNames.add(entry.name); List<Product> notLinkedProducts = new ArrayList<>(); ProductSource source = new ProviderSourceStub(); for (Product product : source.get(providerId)) if (!linkedNames.contains(product.name) && contain(product.name, words)) notLinkedProducts.add(product); return notLinkedProducts; }
public void autoLink(String clientId, Integer providerId) { for (Product product : getNotLinkedProducts(clientId, providerId, null)) { ProductConditions conditions = new ProductConditions(); conditions.words = Arrays.asList(product.name.split(" ")); List<Product> sameNamedProducts = Services.getProductDAO(clientId).find(conditions); if (sameNamedProducts.size() == 1) { Product sameNamedProduct = sameNamedProducts.get(0); ProductEntry productEntry = productEntryRepository.find(clientId, sameNamedProduct.id); if (productEntry == null) continue; LinkedProductEntry entry = new LinkedProductEntry(); entry.name = product.name; entry.productProvider = productProviderRepository.find(providerId); entry.productEntry = productEntry; linkedEntryRepository.create(entry); } } }