/*
 *  Copyright (C) 2016 Stichting Akvo (Akvo Foundation)
 *
 *  This file is part of Akvo FLOW.
 *
 *  Akvo FLOW is free software: you can redistribute it and modify it under the terms of
 *  the GNU Affero General Public License (AGPL) as published by the Free Software Foundation,
 *  either version 3 of the License or any later version.
 *
 *  Akvo FLOW is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU Affero General Public License included below for more details.
 *
 *  The full license text can also be seen at <http://www.gnu.org/licenses/agpl.html>.
 */

package com.gallatinsystems.survey.dao;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.io.IOUtils;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ArrayNode;
import org.waterforpeople.mapping.domain.CaddisflyResource;

/**
 * Dao for listing Caddisfly resources Note: this class doesn't need to
 * implement baseDAO as it consists of only a single method.
 */
public class CaddisflyResourceDao {
    private static ObjectMapper mapper = new ObjectMapper();

    private static final Logger log = Logger.getLogger(CascadeResourceDao.class
            .getName());

    /**
     * lists caddisfly resources. Source is the json file caddisfly-tests.json
     * stored in WEB-INF/resources
     */
    public List<CaddisflyResource> listResources() {
        List<CaddisflyResource> result = new ArrayList<CaddisflyResource>();

        try {
            InputStream stream = getClass().getClassLoader()
                    .getResourceAsStream(
                            "resources/caddisfly/caddisfly-tests.json");
            String jsonTxt = IOUtils.toString(stream);

            // create a list of caddisflyResource objects
            JsonNode rootNode = mapper.readValue(jsonTxt, JsonNode.class);
            ArrayNode testsNode = (ArrayNode) rootNode.get("tests");

            for (int i = 0; i < testsNode.size(); i++) {
                CaddisflyResource cr = JsonToCaddisflyResource(testsNode.get(i));
                result.add(cr);
            }
        } catch (Exception e) {
            log.log(Level.SEVERE,
                    "Error parsing Caddisfly resource: " + e.getMessage(), e);
        }
        return result;
    }

    private CaddisflyResource JsonToCaddisflyResource(JsonNode rootNode) {
        CaddisflyResource cr = null;
        try {
            cr = mapper.readValue(rootNode, CaddisflyResource.class);
        } catch (IOException e) {
            log.log(Level.SEVERE,
                    "Error parsing Caddisfly resource: " + e.getMessage(), e);
        }
        return cr;
    }
}
@@ -743,36 +743,8 @@
 o;%0A%0A
-import java.io.IOException;%0A
 impo
@@ -784,25 +784,27 @@
 va.util.
-ArrayList
+Collections
 ;%0Aimport
@@ -890,46 +890,8 @@
 r;%0A%0A
-import org.apache.commons.io.IOUtils;%0A
 impo
@@ -1002,21 +1002,25 @@
 son.
-node.ArrayNod
+type.TypeReferenc
 e;%0Ai
@@ -1154,19 +1154,16 @@
  need to
-%0A *
  impleme
@@ -1178,16 +1178,19 @@
 AO as it
+%0A *
  consist
@@ -1510,23 +1510,16 @@
 sts.json
-%0A     *
  stored 
@@ -1520,16 +1520,23 @@
 tored in
+%0A     *
  WEB-INF
@@ -1653,41 +1653,11 @@
  = n
-ew ArrayList%3CCaddisflyResource%3E()
+ull
 ;%0A%0A 
@@ -1848,63 +1848,8 @@
 n%22);
-%0A            String jsonTxt = IOUtils.toString(stream);
 %0A%0A  
@@ -1951,86 +1951,60 @@
 read
-Value(jsonTxt, JsonNode.class);%0A            ArrayNode testsNode = (ArrayNode) 
+Tree(stream);%0A            result = mapper.readValue(
 root
@@ -2020,19 +2020,19 @@
 %22tests%22)
-;%0A%0A
+,%0A 
         
@@ -2039,167 +2039,72 @@
     
-for (int i = 0; i %3C testsNode.size(); i++) %7B%0A                CaddisflyResource cr = JsonToCaddisflyResource(testsNode.get(i));%0A                result.add(cr);%0A
+       new TypeReference%3CList%3CCaddisflyResource%3E%3E() %7B%0A          
     
@@ -2105,27 +2105,28 @@
             
-  %7D
+%7D);%0A
 %0A        %7D c
@@ -2273,413 +2273,140 @@
   %7D%0A
-        return result;
 %0A    
-%7D%0A%0A
     
-private CaddisflyResource JsonToCaddisflyResource(JsonNode rootNode) %7B%0A        CaddisflyResource cr = null;%0A        try %7B%0A            cr = mapper.readValue(rootNode, CaddisflyResource.class);%0A        %7D catch (IOException e) %7B%0A            log.log(Level.SEVERE,%0A                    %22Error parsing Caddisfly resource: %22 + e.getMessage(), e);%0A        %7D%0A        return cr;
+if (result != null) %7B%0A            return result;%0A        %7D else %7B%0A            return Collections.emptyList();%0A        %7D
 %0A    %7D%0A%7D
@@ -2405,8 +2405,9 @@
 %0A    %7D%0A%7D
+%0A

Refactor caddifly test definitions listing