/*
 * Copyright (c) 2005-2010 Grameen Foundation USA
 *  All rights reserved.
 *
 *  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.
 *
 *  See also http://www.apache.org/licenses/LICENSE-2.0.html for an
 *  explanation of the license and how it is applied.
 */

package org.mifos.platform.questionnaire.domain.ppi;

import org.mifos.framework.exceptions.SystemException;
import org.mifos.framework.util.ConfigurationLocator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

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

import static java.util.Arrays.asList;
import static org.mifos.platform.questionnaire.QuestionnaireConstants.*; //NOPMD
import static org.mifos.platform.util.CollectionUtils.isNotEmpty;

public final class PPISurveyLocatorImpl implements PPISurveyLocator, ResourceLoaderAware {
    @Autowired
    private ConfigurationLocator configurationLocator;
    private ResourceLoader resourceLoader;
    private final String ppiXmlFolder;

    public PPISurveyLocatorImpl(String ppiXmlFolder) {
        this.ppiXmlFolder = ppiXmlFolder;
    }

    @SuppressWarnings("PMD")
    @Override
    public List<String> getAllPPISurveyFiles() {
        try {
            String path = resolvePath();
            Resource resource = this.resourceLoader.getResource(path);
            List<String> surveyFiles = getPPISurveyFiles(resource.getFile());
            if (surveyFiles.isEmpty()) {
                throw new RuntimeException("No PPI files found at " + path);
            }
            return surveyFiles;
        } catch (IOException e) {
            throw new SystemException(FETCH_PPI_XMLS_FAILED, e);
        }
    }

    @Override
    public String getPPIUploadFileForCountry(String country) {
        try {
            String fileName = getPPIXmlFileName(country);
            Resource resource = this.resourceLoader.getResource(resolvePath());
            return getPPIFilePath(fileName, resource.getFile());
        } catch (IOException e) {
            throw new SystemException(FETCH_PPI_COUNTRY_XML_FAILED, e);
        }
    }

    @SuppressWarnings("PMD.NullAssignment")
    private String getPPIFilePath(String fileName, File ppiFolder) {
        List<File> ppiFiles = new ArrayList<File>();
        if (ppiFolder.isDirectory()) {
            ppiFiles.addAll(asList(ppiFolder.listFiles(new PPICompleteFileNameFilter(fileName))));
        }
        return isNotEmpty(ppiFiles)? ppiFiles.get(0).getAbsolutePath(): null;
    }

    private List<String> getPPISurveyFiles(File ppiFolder) {
        List<String> ppiFiles = new ArrayList<String>();
        if (ppiFolder.isDirectory()) {
            ppiFiles.addAll(asList(ppiFolder.list(new PPIPartialFileNameFilter())));
        }
        return ppiFiles;
    }

    private String getPPIXmlFileName(String country) {
        return String.format("%s%s%s", PPI_SURVEY_FILE_PREFIX, country, PPI_SURVEY_FILE_EXT);
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @edu.umd.cs.findbugs.annotations.SuppressWarnings
    private String resolvePath() {
        return this.configurationLocator.resolvePath(ppiXmlFolder);
    }

    private static class PPIPartialFileNameFilter implements FilenameFilter {
        @Override
        public boolean accept(File file, String fileName) {
            return fileName.startsWith(PPI_SURVEY_FILE_PREFIX) && fileName.endsWith(PPI_SURVEY_FILE_EXT);
        }
    }

    private static class PPICompleteFileNameFilter implements FilenameFilter {
        private final String ppiFileName;

        public PPICompleteFileNameFilter(String ppiFileName) {
            this.ppiFileName = ppiFileName;
        }

        @Override
        public boolean accept(File file, String fileName) {
            return org.apache.commons.lang.StringUtils.equalsIgnoreCase(fileName, ppiFileName);
        }
    }
}

@@ -1818,37 +1818,8 @@
  %7D%0A%0A
-    @SuppressWarnings(%22PMD%22)%0A
     
@@ -1820,32 +1820,32 @@
 %0A%0A    @Override%0A
+
     public List%3C
@@ -1895,49 +1895,8 @@
 y %7B%0A
-            String path = resolvePath();%0A
     
@@ -1955,20 +1955,29 @@
 esource(
-p
+resolveP
 ath
+()
 );%0A     
@@ -1987,34 +1987,14 @@
     
-List%3CString%3E surveyFiles =
+return
  get
@@ -2033,172 +2033,8 @@
 ));%0A
-            if (surveyFiles.isEmpty()) %7B%0A                throw new RuntimeException(%22No PPI files found at %22 + path);%0A            %7D%0A            return surveyFiles;%0A
     

Revert "Temporary commit to trace the issue while loading QG XMLs files"