<commit_before>﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web;
using ServiceStack.Common.Web;
using ServiceStack.ServiceHost;
using Terradue.OpenSearch;
using Terradue.OpenSearch.Engine;
using Terradue.OpenSearch.Result;
using Terradue.OpenSearch.Schema;
using Terradue.Portal;
using Terradue.Portal.OpenSearch;
using Terradue.ServiceModel.Syndication;
using Terradue.Tep.WebServer;
using Terradue.WebService.Model;

namespace Terradue.Tep.WebServer.Services {

     [Api("Tep Terradue webserver")]
    [Restrict(EndpointAttributes.InSecure | EndpointAttributes.InternalNetworkAccess | EndpointAttributes.Json,
              EndpointAttributes.Secure | EndpointAttributes.External | EndpointAttributes.Json)]
    public class ThematicAppServiceTep : ServiceStack.ServiceInterface.Service {

        private static readonly log4net.ILog log = log4net.LogManager.GetLogger
            (System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// Get the specified request.
        /// </summary>
        /// <param name="request">Request.</param>
        /// /apps/{identifier}/search GET
        public object Get (ThematicAppSearchRequestTep request)
        {
            IfyWebContext context = TepWebContext.GetWebContext (PagePrivileges.EverybodyView);
            context.Open ();
            context.LogInfo (this, string.Format ("/apps/search GET"));

            OpenSearchEngine ose = MasterCatalogue.OpenSearchEngine;
            Type responseType = OpenSearchFactory.ResolveTypeFromRequest(HttpContext.Current.Request, ose);
            List<Terradue.OpenSearch.IOpenSearchable> osentities = new List<Terradue.OpenSearch.IOpenSearchable>();

            //first we get the communities the user can see
            var communities = new EntityList<ThematicCommunity>(context);
            if (context.UserId == 0) communities.SetFilter("Kind", (int)DomainKind.Public + "");
            else {
                communities.SetFilter("Kind", (int)DomainKind.Public + "," + (int)DomainKind.Private);
                communities.AddSort("Kind", SortDirection.Ascending);
            }
            communities.Load();

            //foreach community we get the apps link
            foreach (var community in communities.Items) {
                if (community.Kind == DomainKind.Public || community.IsUserJoined(context.UserId) || community.IsUserPending(context.UserId)) {
                    if (!string.IsNullOrEmpty(community.AppsLink)) 
                        osentities.Add(new SmartGenericOpenSearchable(new OpenSearchUrl(community.AppsLink), ose));
                }
            }

            //get thematic apps without any domain
            var apps = new EntityList<ThematicApplication>(context);
            apps.SetFilter("DomainId", SpecialSearchValue.Null);
            apps.SetFilter("Kind", ThematicApplication.KINDRESOURCESETAPPS + "");
            apps.Load();
            foreach (var app in apps) {
                app.LoadItems();
                foreach (var item in app.Items) {
                    osentities.Add(new SmartGenericOpenSearchable(new OpenSearchUrl(item.Location), ose));
                }
            }

            MultiGenericOpenSearchable multiOSE = new MultiGenericOpenSearchable(osentities, ose);
            var result = ose.Query(multiOSE, Request.QueryString, responseType);

            context.Close ();
            return new HttpResult (result.SerializeToString (), result.ContentType);
        }

        public object Get (ThematicAppByCommunitySearchRequestTep request)
        {
            IfyWebContext context = TepWebContext.GetWebContext (PagePrivileges.EverybodyView);
            context.Open ();
            context.LogInfo (this, string.Format ("/community/{{domain}}/apps/search GET domain='{0}'",request.Domain));

            var domain = Domain.FromIdentifier (context, request.Domain);
            var apps = new EntityList<DataPackage> (context);
            apps.SetFilter ("Kind", ThematicApplication.KINDRESOURCESETAPPS.ToString ());
            apps.SetFilter ("DomainId", domain.Id.ToString());
            apps.Load ();

            // the opensearch cache system uses the query parameters
            // we add to the parameters the filters added to the load in order to avoir wrong cache
            // we use 't2-' in order to not interfer with possibly used query parameters
            var qs = new NameValueCollection(Request.QueryString);
            foreach (var filter in apps.FilterValues) qs.Add("t2-" + filter.Key.FieldName, filter.Value.ToString());

            var result = GetAppsResultCollection (apps, qs);

            context.Close ();
            return new HttpResult (result.SerializeToString (), result.ContentType);
        }
<commit_msg>username in thematic apps<commit_after>﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web;
using ServiceStack.Common.Web;
using ServiceStack.ServiceHost;
using Terradue.OpenSearch;
using Terradue.OpenSearch.Engine;
using Terradue.OpenSearch.Result;
using Terradue.OpenSearch.Schema;
using Terradue.Portal;
using Terradue.Portal.OpenSearch;
using Terradue.ServiceModel.Syndication;
using Terradue.Tep.WebServer;
using Terradue.WebService.Model;

namespace Terradue.Tep.WebServer.Services {

     [Api("Tep Terradue webserver")]
    [Restrict(EndpointAttributes.InSecure | EndpointAttributes.InternalNetworkAccess | EndpointAttributes.Json,
              EndpointAttributes.Secure | EndpointAttributes.External | EndpointAttributes.Json)]
    public class ThematicAppServiceTep : ServiceStack.ServiceInterface.Service {

        private static readonly log4net.ILog log = log4net.LogManager.GetLogger
            (System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// Get the specified request.
        /// </summary>
        /// <param name="request">Request.</param>
        /// /apps/{identifier}/search GET
        public object Get (ThematicAppSearchRequestTep request)
        {
            IfyWebContext context = TepWebContext.GetWebContext (PagePrivileges.EverybodyView);
            context.Open ();
            context.LogInfo (this, string.Format ("/apps/search GET"));

            OpenSearchEngine ose = MasterCatalogue.OpenSearchEngine;
            Type responseType = OpenSearchFactory.ResolveTypeFromRequest(HttpContext.Current.Request, ose);
            List<Terradue.OpenSearch.IOpenSearchable> osentities = new List<Terradue.OpenSearch.IOpenSearchable>();

            //first we get the communities the user can see
            var communities = new EntityList<ThematicCommunity>(context);
            if (context.UserId == 0) communities.SetFilter("Kind", (int)DomainKind.Public + "");
            else {
                communities.SetFilter("Kind", (int)DomainKind.Public + "," + (int)DomainKind.Private);
                communities.AddSort("Kind", SortDirection.Ascending);
            }
            communities.Load();

            //foreach community we get the apps link
            foreach (var community in communities.Items) {
                if (community.Kind == DomainKind.Public || community.IsUserJoined(context.UserId) || community.IsUserPending(context.UserId)) {
                    if (!string.IsNullOrEmpty(community.AppsLink)) 
                        osentities.Add(new SmartGenericOpenSearchable(new OpenSearchUrl(community.AppsLink), ose));
                }
            }

            //get thematic apps without any domain
            var apps = new EntityList<ThematicApplication>(context);
            apps.SetFilter("DomainId", SpecialSearchValue.Null);
            apps.SetFilter("Kind", ThematicApplication.KINDRESOURCESETAPPS + "");
            apps.Load();
            foreach (var app in apps) {
                app.LoadItems();
                foreach (var item in app.Items) {
                    osentities.Add(new SmartGenericOpenSearchable(new OpenSearchUrl(item.Location), ose));
                }
            }

            MultiGenericOpenSearchable multiOSE = new MultiGenericOpenSearchable(osentities, ose);
            var result = ose.Query(multiOSE, Request.QueryString, responseType);

            context.Close ();

            string sresult = result.SerializeToString();
            sresult.Replace("${USERNAME}", context.Username);

            return new HttpResult (sresult, result.ContentType);
        }

        public object Get (ThematicAppByCommunitySearchRequestTep request)
        {
            IfyWebContext context = TepWebContext.GetWebContext (PagePrivileges.EverybodyView);
            context.Open ();
            context.LogInfo (this, string.Format ("/community/{{domain}}/apps/search GET domain='{0}'",request.Domain));

            var domain = Domain.FromIdentifier (context, request.Domain);
            var apps = new EntityList<DataPackage> (context);
            apps.SetFilter ("Kind", ThematicApplication.KINDRESOURCESETAPPS.ToString ());
            apps.SetFilter ("DomainId", domain.Id.ToString());
            apps.Load ();

            // the opensearch cache system uses the query parameters
            // we add to the parameters the filters added to the load in order to avoir wrong cache
            // we use 't2-' in order to not interfer with possibly used query parameters
            var qs = new NameValueCollection(Request.QueryString);
            foreach (var filter in apps.FilterValues) qs.Add("t2-" + filter.Key.FieldName, filter.Value.ToString());

            var result = GetAppsResultCollection (apps, qs);

            context.Close ();
            return new HttpResult (result.SerializeToString (), result.ContentType);
        }

{'fraction_non_alphanumeric': 0.09866340169370473, 'fraction_numerical': 0.001224364860728497, 'mean_word_length': 2.645221271848271, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1374130', 'n_tokens_mistral': 2512, 'n_tokens_neox': 2456, 'n_words': 560}