<commit_before>/*
 * 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 javax.persistence.NoResultException;
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.app.ApplicationLayer;
import nl.b3p.viewer.config.app.ConfiguredAttribute;
import nl.b3p.viewer.config.services.AttributeDescriptor;
import nl.b3p.viewer.config.services.Layer;
import nl.b3p.viewer.config.services.SimpleFeatureType;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.stripesstuff.stripersist.Stripersist;

/**
 *
 * @author <NAME>
 */
@UrlBinding("/action/appLayer")
@StrictBinding
public class AppLayerActionBean implements ActionBean {
    
    private ActionBeanContext context;
    
    @Validate
    private ApplicationLayer appLayer;
    
    //<editor-fold defaultstate="collapsed" desc="getters en setters">
    public ActionBeanContext getContext() {
        return context;
    }
    
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }
    
    public ApplicationLayer getAppLayer() {
        return appLayer;
    }

    public void setAppLayer(ApplicationLayer appLayer) {
        this.appLayer = appLayer;
    }
    //</editor-fold>

    public Resolution attributes() throws JSONException {
        JSONObject json = new JSONObject();

        json.put("success", Boolean.FALSE);
        String error = null;

        if(appLayer == null) {
            error = "Invalid parameters";
        } else {
            // TODO check if user has rights to appLayer
            
            Layer l = null;
            try {
                l = (Layer)Stripersist.getEntityManager().createQuery("from Layer where service = :service and name = :n order by virtual desc")
                        .setParameter("service", appLayer.getService())
                        .setParameter("n", appLayer.getLayerName())
                        .setMaxResults(1)
                        .getSingleResult();
            } catch(NoResultException nre) {
            }
            Map<String,AttributeDescriptor> ftAttributes = new HashMap<String,AttributeDescriptor>();
            if(l != null) {
                SimpleFeatureType ft = l.getFeatureType();
                if(ft != null) {
                    for(AttributeDescriptor ad: ft.getAttributes()) {
                        ftAttributes.put(ad.getName(), ad);
                    }
                }
            }
            
            JSONArray attributes = new JSONArray();
            for(ConfiguredAttribute ca: appLayer.getAttributes()) {
                JSONObject j = ca.toJSONObject();
                
                AttributeDescriptor ad = ftAttributes.get(ca.getAttributeName());
                if(ad != null) {
                    j.put("alias", ad.getAlias());
                    j.put("type", ad.getType());
                }
                attributes.put(j);
            }        
                       
            json.put("attributes", attributes);
            json.put("success", Boolean.TRUE);
        }
        
        if(error != null) {
            json.put("error", error);
        }      
        
        return new StreamingResolution("application/json", new StringReader(json.toString()));    
    }
    
    public Resolution store() throws JSONException {
        JSONObject json = new JSONObject();
        json.put("features", new JSONArray());
        json.put("total", 0);
                
        return new StreamingResolution("application/json", new StringReader(json.toString()));    
    }
}<commit_msg>store() method for attribute list<commit_after>/*
 * Copyright (C) 2012 B3Part<NAME>.
 *
 * 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 javax.persistence.NoResultException;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.After;
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.controller.LifecycleStage;
import net.sourceforge.stripes.validation.Validate;
import nl.b3p.viewer.config.app.ApplicationLayer;
import nl.b3p.viewer.config.app.ConfiguredAttribute;
import nl.b3p.viewer.config.services.AttributeDescriptor;
import nl.b3p.viewer.config.services.Layer;
import nl.b3p.viewer.config.services.SimpleFeatureType;
import org.geotools.data.FeatureSource;
import org.geotools.data.store.DataFeatureCollection;
import org.geotools.data.wfs.WFSDataStore;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.opengis.feature.simple.SimpleFeature;
import org.stripesstuff.stripersist.Stripersist;

/**
 *
 * @author <NAME>
 */
@UrlBinding("/action/appLayer")
@StrictBinding
public class AppLayerActionBean implements ActionBean {
    private static final int MAX_FEATURES = 30;
    
    private ActionBeanContext context;
    
    @Validate
    private ApplicationLayer appLayer;
    
    private Layer layer = null;
    
    private int limit;
    private int page;
    private int start;
    
    private Map<String,AttributeDescriptor> featureTypeAttributes = new HashMap<String,AttributeDescriptor>();
    
    //<editor-fold defaultstate="collapsed" desc="getters en setters">
    public ActionBeanContext getContext() {
        return context;
    }
    
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }
    
    public ApplicationLayer getAppLayer() {
        return appLayer;
    }

    public void setAppLayer(ApplicationLayer appLayer) {
        this.appLayer = appLayer;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }
    //</editor-fold>

    @After(stages=LifecycleStage.BindingAndValidation)
    public void loadLayer() {
        // TODO check if user has rights to appLayer

        try {
            layer = (Layer)Stripersist.getEntityManager().createQuery("from Layer where service = :service and name = :n order by virtual desc")
                    .setParameter("service", appLayer.getService())
                    .setParameter("n", appLayer.getLayerName())
                    .setMaxResults(1)
                    .getSingleResult();
            
            if(layer != null) {
                SimpleFeatureType ft = layer.getFeatureType();
                if(ft != null) {
                    for(AttributeDescriptor ad: ft.getAttributes()) {
                        featureTypeAttributes.put(ad.getName(), ad);
                    }
                }
            }            
        } catch(NoResultException nre) {
        }
    }
    
    public Resolution attributes() throws JSONException {
        JSONObject json = new JSONObject();

        json.put("success", Boolean.FALSE);
        String error = null;

        if(appLayer == null) {
            error = "Invalid parameters";
        } else {


            
            JSONArray attributes = new JSONArray();
            for(ConfiguredAttribute ca: appLayer.getAttributes()) {
                JSONObject j = ca.toJSONObject();
                
                AttributeDescriptor ad = featureTypeAttributes.get(ca.getAttributeName());
                if(ad != null) {
                    j.put("alias", ad.getAlias());
                    j.put("type", ad.getType());
                }
                attributes.put(j);
            }        
                       
            json.put("attributes", attributes);
            json.put("success", Boolean.TRUE);
        }
        
        if(error != null) {
            json.put("error", error);
        }      
        
        return new StreamingResolution("application/json", new StringReader(json.toString()));    
    }
    
    public Resolution store() throws JSONException, Exception {
        JSONObject json = new JSONObject();
        JSONArray features = new JSONArray();
        
        int total = 0;
        
        if(layer != null && layer.getFeatureType() != null) {
            FeatureSource fs = layer.getFeatureType().openGeoToolsFeatureSource();
            ((WFSDataStore)fs.getDataStore()).setMaxFeatures(MAX_FEATURES);
            
            FeatureCollection fc = fs.getFeatures();
            
            if(!fc.isEmpty()) {
                if(fc instanceof DataFeatureCollection) {
                    total = ((DataFeatureCollection)fc).getCount();
                } else {
                    total = fc.size(); /* Deze methode swallowt exceptions */
                }           
                total = Math.min(MAX_FEATURES, total);

                FeatureIterator<SimpleFeature> it = fc.features();
                int processed = 0;
                while(it.hasNext()) {
                    SimpleFeature f = it.next();
                    processed++;
                    if(start > 0) {
                        start--;
                        continue;
                    }
                    
                    JSONObject j = new JSONObject();
                    for(ConfiguredAttribute ca: appLayer.getAttributes()) {

                        Object value = f.getAttribute(ca.getAttributeName());

                        //AttributeDescriptor ad = featureTypeAttributes.get(ca.getAttributeName());
                        //if(ad != null && ad.getAlias() != null) {
                        //    j.put(ad.getAlias(), value);
                        //} else {
                            j.put(ca.getAttributeName(), value);
                        //}
                    }                     
                    features.put(j);
                    
                    if(processed == MAX_FEATURES) {
                        break;
                    }
                }
            }            
        }
                
        json.put("total", total);
        json.put("features", features);

        return new StreamingResolution("application/json", new StringReader(json.toString()));    
    }
}
{'fraction_non_alphanumeric': 0.09774924839522223, 'fraction_numerical': 0.002600146258227025, 'mean_word_length': 2.0056166056166056, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5213180', 'n_tokens_mistral': 3213, 'n_tokens_neox': 3109, 'n_words': 772}