/*
 * Copyright (c) 2016 NECTEC
 *   National Electronics and Computer Technology Center, Thailand
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package th.or.nectec.tanrabad.survey.service;

import com.bluelinelabs.logansquare.LoganSquare;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import th.or.nectec.tanrabad.domain.place.PlaceSubTypeRepository;
import th.or.nectec.tanrabad.entity.Place;
import th.or.nectec.tanrabad.survey.TanrabadApp;
import th.or.nectec.tanrabad.survey.repository.BrokerPlaceSubTypeRepository;
import th.or.nectec.tanrabad.survey.service.json.JsonPlace;

public class PlaceRestService extends AbsUploadRestService<Place> {

    public static final String PATH = "/place";
    private PlaceSubTypeRepository placeSubTypeRepository;

    public PlaceRestService() {
        this(BASE_API, new ServiceLastUpdatePreference(TanrabadApp.getInstance(), PATH),
                BrokerPlaceSubTypeRepository.getInstance());
    }

    public PlaceRestService(String apiBaseUrl,
                            ServiceLastUpdate serviceLastUpdate,
                            PlaceSubTypeRepository placeSubTypeRepository) {
        super(apiBaseUrl, serviceLastUpdate);
        this.placeSubTypeRepository = placeSubTypeRepository;
    }

    @Override
    public String getDefaultParams() {
        return new QueryStringBuilder("geostd=4326", getApiFilterParam()).build();
    }

    @Override
    protected String getPath() {
        return PATH;
    }

    @Override
    protected List<Place> jsonToEntityList(String responseBody) throws IOException {
        ArrayList<Place> places = new ArrayList<>();
        List<JsonPlace> jsonPlaces = LoganSquare.parseList(responseBody, JsonPlace.class);
        for (JsonPlace eachJsonPlace : jsonPlaces)
            places.add(eachJsonPlace.getEntity(placeSubTypeRepository));
        return places;
    }

    @Override
    protected String entityToJsonString(Place data) {
        try {
            return LoganSquare.serialize(JsonPlace.parse(data));
        } catch (IOException io) {
            throw new RestServiceException(io);
        }
    }

    @Override
    protected String getId(Place data) {
        return data.getId().toString();
    }
}

@@ -754,89 +754,8 @@
 re;%0A
-%0Aimport java.io.IOException;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 impo
@@ -1046,16 +1046,96 @@
 Place;%0A%0A
+import java.io.IOException;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 public c
@@ -2307,16 +2307,18 @@
 nPlaces)
+ %7B
 %0A       
@@ -2326,19 +2326,22 @@
     
-places.add(
+Place place = 
 each
@@ -2383,18 +2383,175 @@
 ository)
-)
 ;
+%0A            if (eachJsonPlace.active) %7B%0A                places.add(place);%0A            %7D else %7B%0A                addDeleteData(place);%0A            %7D%0A        %7D
 %0A       

implement place active=false ใน PlaceRestService