/*
 * Copyright (C) 2012 B3Partners B.V.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package nl.b3p.viewer.stripes;

import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.action.StrictBinding;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.validation.Validate;
import nl.b3p.viewer.config.services.ArcGISService;
import nl.b3p.viewer.config.services.ArcIMSService;
import nl.b3p.viewer.config.services.GeoService;
import nl.b3p.viewer.config.services.WMSService;
import nl.b3p.web.stripes.ErrorMessageResolution;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Matthijs Laan
 */
@UrlBinding("/service/info")
@StrictBinding
public class ServiceActionBean implements ActionBean {
    
    private ActionBeanContext context;
    
    @Validate
    private String protocol;
    @Validate
    private String url;
    @Validate
    private String serviceName;
    
    //<editor-fold defaultstate="collapsed" desc="getters en setters">
    public ActionBeanContext getContext() {
        return context;
    }
    
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }
    
    public String getProtocol() {
        return protocol;
    }
    
    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }
    
    public String getServiceName() {
        return serviceName;
    }
    
    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }
    
    public String getUrl() {
        return url;
    }
    
    public void setUrl(String url) {
        this.url = url;
    }
    //</editor-fold>
    
    public Resolution info() throws JSONException {
        JSONObject json = new JSONObject();

        json.put("success", Boolean.FALSE);
        String error = null;
        GeoService service = null;
        
        if(protocol == null || url == null) {
            error = "Invalid parameters";
        } else {
            
            Map params = new HashMap();
            params.put(GeoService.PARAM_PERSIST_FEATURESOURCE, Boolean.FALSE);
            
            try {
                if(protocol.equals(WMSService.PROTOCOL)) {
                    //params.put(WMSService.PARAM_OVERRIDE_URL, overrideUrl);
                    service = new WMSService().loadFromUrl(url, params);
                } else if(protocol.equals(ArcGISService.PROTOCOL)) {
                    service = new ArcGISService().loadFromUrl(url, params);
                } else if(protocol.equals(ArcIMSService.PROTOCOL)) {
                    params.put(ArcIMSService.PARAM_SERVICENAME, serviceName);
                    service = new ArcIMSService().loadFromUrl(url, params);
                } else {
                    error = "Invalid protocol";
                }            
            } catch(Exception e) {
                
                error = "Fout bij laden service " + e.toString();
                if(e.getCause() != null) {
                    error += "; oorzaak: " + e.getCause().toString();
                }
            }
        }
        
        if(service != null) {
            json.put("success", Boolean.TRUE);
            json.put("service", service.toJSONObject(false));
        } else {
            json.put("success", Boolean.FALSE);
            json.put("error", error);
        }      
        
        return new StreamingResolution("application/json", new StringReader(json.toString()));        
    }
}

@@ -4118,12 +4118,11 @@
 ect(
-fals
+tru
 e));

include layer tree in JSON