001/* 
002 * JKNIV, whinstone one contract to access your database.
003 * 
004 * Copyright (C) 2017, the original author or authors.
005 *
006 * This library is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 2.1 of the License.
010 * 
011 * This library is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 * 
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with this library; if not, write to the Free Software Foundation, Inc., 
018 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
019 */
020package net.sf.jkniv.whinstone.couchdb.commands;
021
022import java.io.IOException;
023import java.util.Collections;
024import java.util.List;
025
026import org.apache.http.client.methods.CloseableHttpResponse;
027import org.apache.http.client.methods.HttpGet;
028import org.apache.http.client.methods.HttpRequestBase;
029import org.apache.http.impl.client.CloseableHttpClient;
030import org.apache.http.impl.client.HttpClients;
031import org.apache.http.util.EntityUtils;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035import net.sf.jkniv.sqlegance.RepositoryException;
036import net.sf.jkniv.whinstone.Queryable;
037import net.sf.jkniv.whinstone.couchdb.CouchDbResult;
038import net.sf.jkniv.whinstone.couchdb.HttpBuilder;
039
040public class AllDocsCommand extends AbstractCommand implements CouchCommand 
041{
042    private static final Logger LOG = LoggerFactory.getLogger(FindCommand.class);
043    private static final Logger LOGSQL = net.sf.jkniv.whinstone.couchdb.LoggerFactory.getLogger();
044    private HttpBuilder httpBuilder;
045    private Queryable queryable;
046
047    public AllDocsCommand(HttpBuilder httpBuilder, Queryable queryable)
048    {
049        super();
050        //stmt.rows();
051        this.httpBuilder = httpBuilder;
052        //this.body = stmt.getBody();
053        this.queryable = queryable;
054        this.method = HttpMethod.GET;
055        this.url = httpBuilder.getUrlForAllDocs(queryable);
056    }
057
058    
059    @Override
060    public <T> T execute()
061    {
062        String json = null;
063        CloseableHttpResponse response = null;
064        CouchDbResult answer = null;
065        List<?> list = Collections.emptyList();
066        HttpRequestBase http = null;
067        try
068        {
069            CloseableHttpClient httpclient = HttpClients.createDefault();
070            if (method == HttpMethod.GET)
071            {
072                http = new HttpGet(url);
073                this.httpBuilder.setHeader(http);
074            }
075            printRequest(http);
076            response = httpclient.execute(http);
077            json = EntityUtils.toString(response.getEntity());
078            printResponse(response, json);
079
080            int statusCode = response.getStatusLine().getStatusCode();
081            if (statusCode == HTTP_OK)
082            {
083                JsonMapper.setCurrentQuery(queryable);
084                answer = JsonMapper.MAPPER.readerFor(CouchDbResultImpl.class).readValue(json);
085                list = answer.getRows();
086                queryable.setTotal(answer.getTotalRows());
087            }
088            else if (isNotFound(statusCode))
089            {
090                // 204 No Content, 304 Not Modified, 205 Reset Content
091                LOG.warn(errorFormat(http, response.getStatusLine(), json));
092            }
093            else
094            {
095                LOG.error(errorFormat(http, response.getStatusLine(), json));
096                throw new RepositoryException(response.getStatusLine().toString());
097            }
098            //commandHandler.postCommit();
099        }
100        catch (Exception e) // ClientProtocolException | JsonParseException | JsonMappingException | IOException
101        {
102            //commandHandler.postException();
103            handlerException.handle(e);
104        }
105        finally
106        {
107            if (response != null)
108            {
109                try
110                {
111                    response.close();
112                }
113                catch (IOException e)
114                {
115                    handlerException.handle(e);
116                }
117            }
118        }
119        return (T)list;
120    }
121
122    @Override
123    public String getBody()
124    {
125        return this.body;
126    }
127    
128}