package ua.avk.rest.service;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import ua.avk.rest.entity.Contact;
import ua.avk.rest.dao.IContactDao;
import ua.avk.rest.service.util.Filter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Alex Kononenko
 * @version 1.0.0
 *
 * The contact.
 */
@Service
class ContactSrv implements IContactSrv {

    @Autowired
    IContactDao contactDao;

    /**
     * For unit test
     *
     * @param contactDao object
     */
    public void setContactDao(IContactDao contactDao) {
        this.contactDao = contactDao;
    }

    /**
     * Get json object of the contacts by limit from database
     *
     * @param regExp
     * @param startId
     * @param count
     * @return json object
     */
    public JSONObject getJsonContactsByFilter(String regExp, long startId, long count){
        // get all contacts from the database
        List<Contact> contacts = contactDao.getContactByLimit(startId, count);

        // set filter
        contacts = this.toFilter(contacts, regExp);
        /*
        Filter filter = new Filter(regExp);
        for (Iterator<Contact> iter = contacts.listIterator(); iter.hasNext(); ) {
            Contact c = iter.next();
            if(c.getName()!=null && c.getName()!="") {
                if (filter.getFilter(c.getName())) {
                    iter.remove();
                    //System.out.println("REMOVE: "+c.getName());
                }
            }
        }
        */

        // create json obj
        JSONObject responseContactDetailsJson = this.toJson(contacts); //new JSONObject();
        return responseContactDetailsJson;
    }

    /**
     * Created json object from list of the contacts
     *
     * @param contacts list of the contacts
     * @return JSON Object
     */
    public JSONObject toJson(List<Contact> contacts){
        JSONObject responseContactDetailsJson = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < contacts.size(); i++) {
            JSONObject contactDetailsJson = new JSONObject();
            contactDetailsJson.put("id", contacts.get(i).getId());
            contactDetailsJson.put("name", contacts.get(i).getName());

            jsonArray.add(contactDetailsJson);
        }
        responseContactDetailsJson.put("contact", jsonArray);
        return responseContactDetailsJson;
    }

    /**
     * Filtered the list of the contacts
     *
     * @param contacts list of the contacts
     * @param regExp regular expression
     * @return filtered list
     */
    public List<Contact> toFilter(List<Contact> contacts, String regExp){
        Filter filter = new Filter(regExp);
        for (Iterator<Contact> iter = contacts.listIterator(); iter.hasNext();) {
            Contact c = iter.next();
            if(c.getName()!=null && c.getName()!="") {
                if (filter.getFilter(c.getName())) {
                    iter.remove();
                    //System.out.println("REMOVE: "+c.getName());
                }
            }
        }
        return contacts;
    }

}

@@ -164,58 +164,8 @@
 d;%0D%0A
-import org.springframework.stereotype.Component;%0D%0A
 impo
@@ -231,22 +231,23 @@
 est.
-entity.
+dao.I
 Contact
+Dao
 ;%0D%0Ai
@@ -260,39 +260,38 @@
 ua.avk.rest.
-dao.I
+entity.
 Contact
-Dao
 ;%0D%0Aimport ua
@@ -328,37 +328,8 @@
 %0D%0A%0D%0A
-import java.util.ArrayList;%0D%0A
 impo
@@ -431,20 +431,43 @@
 .0.0%0D%0A *
-%0D%0A *
+          %3Cp/%3E%0D%0A *         
  The con
@@ -1035,16 +1035,17 @@
 g count)
+ 
 %7B%0D%0A     
@@ -1248,620 +1248,46 @@
 );%0D%0A
-        /*%0D%0A        Filter filter = new Filter(regExp);%0D%0A        for (Iterator%3CContact%3E iter = contacts.listIterator(); iter.hasNext(); ) %7B%0D%0A            Contact c = iter.next();%0D%0A            if(c.getName()!=null && c.getName()!=%22%22) %7B%0D%0A                if (filter.getFilter(c.getName())) %7B%0D%0A                    iter.remove();%0D%0A                    //System.out.println(%22REMOVE: %22+c.getName());%0D%0A                %7D%0D%0A            %7D%0D%0A        %7D%0D%0A        */%0D%0A%0D%0A        // create json obj%0D%0A        JSONObject responseContactDetailsJson = this.toJson(contacts); //new JSONObject();%0D%0A        return responseContactDetailsJson
+%0D%0A        return this.toJson(contacts)
 ;%0D%0A 
@@ -1499,16 +1499,17 @@
 ontacts)
+ 
 %7B%0D%0A     
@@ -2176,16 +2176,18 @@
  regExp 
+  
 regular 
@@ -2309,16 +2309,17 @@
  regExp)
+ 
 %7B%0D%0A     
@@ -2436,16 +2436,17 @@
 sNext();
+ 
 ) %7B%0D%0A   
@@ -2494,16 +2494,17 @@
       if
+ 
 (c.getNa
@@ -2507,18 +2507,20 @@
 etName()
+ 
 !=
+ 
 null && 
@@ -2534,37 +2534,18 @@
 me()
+ 
 !=
+ 
 %22%22
-) %7B%0D%0A                if (
+ && 
 filt
@@ -2590,20 +2590,16 @@
         
-    
 iter.rem
@@ -2610,94 +2610,8 @@
 );%0D%0A
-                    //System.out.println(%22REMOVE: %22+c.getName());%0D%0A                %7D%0D%0A
     

Update ContactSrv.java